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