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