]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinefree.cpp
1f6683df8ef537117a1d01a72b24845449f35e24
[architektonas] / src / actions / actiondrawlinefree.cpp
1 // actiondrawlinefree.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/04/2010  Added this text. :-)
15 //
16
17 #include "actiondrawlinefree.h"
18
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_polyline.h"
22
23 ActionDrawLineFree::ActionDrawLineFree(RS_EntityContainer & container,
24         GraphicView & graphicView): ActionInterface("Draw freehand lines",
25         container, graphicView)
26 {
27         vertex = Vector(false);
28         polyline = NULL;
29 }
30
31 ActionDrawLineFree::~ActionDrawLineFree()
32 {
33         if (polyline)
34                 delete polyline;
35 }
36
37 void ActionDrawLineFree::trigger()
38 {
39         if (polyline)
40         {
41                 container->addEntity(polyline);
42                 deleteSnapper();
43
44                 if (document)
45                 {
46                         document->startUndoCycle();
47                         document->addUndoable(polyline);
48                         document->endUndoCycle();
49                 }
50
51                 RS_DEBUG->print("ActionDrawLineFree::trigger():"
52                         " polyline added: %d", polyline->getId());
53                 polyline = NULL;
54         }
55 }
56
57 void ActionDrawLineFree::mouseMoveEvent(QMouseEvent * e)
58 {
59         if (vertex.valid && polyline)
60         {
61                 Vector v = snapPoint(e);
62                 RS_Entity * ent = polyline->addVertex(v);
63                 ent->setLayerToActive();
64                 ent->setPenToActive();
65
66                 deleteSnapper();
67                 graphicView->drawEntity(ent);
68                 drawSnapper();
69
70                 vertex = v;
71
72                 RS_DEBUG->print("ActionDrawLineFree::mouseMoveEvent():"
73                         " line added: %d", ent->getId());
74         }
75 }
76
77 void ActionDrawLineFree::mousePressEvent(QMouseEvent * e)
78 {
79         if (e->button() == Qt::LeftButton)
80         {
81                 vertex = snapPoint(e);
82                 polyline = new RS_Polyline(container, RS_PolylineData(vertex, vertex, 0));
83                 polyline->setLayerToActive();
84                 polyline->setPenToActive();
85         }
86 }
87
88 void ActionDrawLineFree::mouseReleaseEvent(QMouseEvent * e)
89 {
90         if (e->button() == Qt::LeftButton)
91         {
92                 vertex = Vector(false);
93                 trigger();
94         }
95         else if (e->button() == Qt::RightButton)
96         {
97                 if (polyline)
98                 {
99                         delete polyline;
100                         polyline = NULL;
101                 }
102
103                 deleteSnapper();
104                 init(getStatus() - 1);
105         }
106 }
107
108 void ActionDrawLineFree::updateMouseButtonHints()
109 {
110         switch (getStatus())
111         {
112         case 0:
113                 RS_DIALOGFACTORY->updateMouseWidget(tr("Click and drag to draw a line"), tr("Cancel"));
114                 break;
115
116         default:
117                 RS_DIALOGFACTORY->updateMouseWidget("", "");
118                 break;
119         }
120 }
121
122 void ActionDrawLineFree::updateMouseCursor()
123 {
124         graphicView->setMouseCursor(RS2::CadCursor);
125 }
126
127 void ActionDrawLineFree::updateToolBar()
128 {
129         if (!isFinished())
130                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
131         else
132                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
133 }