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