]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinerectangle.cpp
Major refactoring of actions: Moved implementation from header files
[architektonas] / src / actions / rs_actiondrawlinerectangle.cpp
1 // rs_actiondrawlinerectangle.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_actiondrawlinerectangle.h"
16
17 #include "rs_dialogfactory.h"
18 #include "rs_graphicview.h"
19 #include "rs_preview.h"
20
21 RS_ActionDrawLineRectangle::RS_ActionDrawLineRectangle(
22         RS_EntityContainer & container, RS_GraphicView & graphicView):
23         RS_PreviewActionInterface("Draw rectangles", container, graphicView)
24 {
25         reset();
26 }
27
28 RS_ActionDrawLineRectangle::~RS_ActionDrawLineRectangle()
29 {
30 }
31
32 void RS_ActionDrawLineRectangle::reset()
33 {
34         for (int i = 0; i < 4; ++i)
35                 data[i] = RS_LineData(Vector(false), Vector(false));
36 }
37
38 void RS_ActionDrawLineRectangle::init(int status)
39 {
40         RS_PreviewActionInterface::init(status);
41         reset();
42 }
43
44 void RS_ActionDrawLineRectangle::trigger()
45 {
46         RS_PreviewActionInterface::trigger();
47
48         RS_Line * line[4];
49         preparePreview();
50
51         // create and add rectangle:
52         for (int i = 0; i < 4; ++i)
53         {
54                 line[i] = new RS_Line(container,
55                                 data[i]);
56                 line[i]->setLayerToActive();
57                 line[i]->setPenToActive();
58                 container->addEntity(line[i]);
59         }
60
61         // upd. undo list:
62         if (document != NULL)
63         {
64                 document->startUndoCycle();
65
66                 for (int i = 0; i < 4; ++i)
67                         document->addUndoable(line[i]);
68                 document->endUndoCycle();
69         }
70
71         // upd. view
72         deleteSnapper();
73         graphicView->moveRelativeZero(Vector(0.0, 0.0));
74
75         for (int i = 0; i < 4; ++i)
76         {
77                 graphicView->drawEntity(line[i]);
78                 RS_DEBUG->print("RS_ActionDrawLineRectangle::trigger():"
79                         " line added: %d",
80                         line[i]->getId());
81         }
82         graphicView->moveRelativeZero(corner2);
83 }
84
85 void RS_ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent * e)
86 {
87         RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent begin");
88
89         Vector mouse = snapPoint(e);
90
91         if (getStatus() == SetCorner2 && corner1.valid)
92         {
93                 corner2 = mouse;
94                 deletePreview();
95                 clearPreview();
96
97                 preparePreview();
98
99                 for (int i = 0; i < 4; ++i)
100                         preview->addEntity(new RS_Line(preview, data[i]));
101                 drawPreview();
102         }
103
104         RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent end");
105 }
106
107 void RS_ActionDrawLineRectangle::mouseReleaseEvent(QMouseEvent * e)
108 {
109         if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
110         {
111                 Vector ce(snapPoint(e));
112                 coordinateEvent(&ce);
113         }
114         else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
115         {
116                 deletePreview();
117                 deleteSnapper();
118                 init(getStatus() - 1);
119         }
120 }
121
122 void RS_ActionDrawLineRectangle::preparePreview()
123 {
124         data[0] = RS_LineData(corner1, Vector(corner2.x, corner1.y));
125         data[1] = RS_LineData(Vector(corner2.x, corner1.y), corner2);
126         data[2] = RS_LineData(corner2, Vector(corner1.x, corner2.y));
127         data[3] = RS_LineData(Vector(corner1.x, corner2.y), corner1);
128 }
129
130 void RS_ActionDrawLineRectangle::coordinateEvent(Vector * e)
131 {
132         if (e == NULL)
133                 return;
134
135         Vector mouse = *e;
136
137         switch (getStatus())
138         {
139         case SetCorner1:
140                 corner1 = mouse;
141                 graphicView->moveRelativeZero(mouse);
142                 setStatus(SetCorner2);
143                 break;
144
145         case SetCorner2:
146                 corner2 = mouse;
147                 trigger();
148                 setStatus(SetCorner1);
149                 break;
150
151         default:
152                 break;
153         }
154 }
155
156 void RS_ActionDrawLineRectangle::commandEvent(RS_CommandEvent * e)
157 {
158         QString c = e->getCommand().toLower();
159
160         if (checkCommand("help", c))
161         {
162                 if (RS_DIALOGFACTORY != NULL)
163                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
164                                 + getAvailableCommands().join(", "));
165                 return;
166         }
167 }
168
169 QStringList RS_ActionDrawLineRectangle::getAvailableCommands()
170 {
171         QStringList cmd;
172         return cmd;
173 }
174
175 void RS_ActionDrawLineRectangle::updateMouseButtonHints()
176 {
177         if (RS_DIALOGFACTORY != NULL)
178         {
179                 switch (getStatus())
180                 {
181                 case SetCorner1:
182                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
183                                 tr("Cancel"));
184                         break;
185
186                 case SetCorner2:
187                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
188                                 tr("Back"));
189                         break;
190
191                 default:
192                         RS_DIALOGFACTORY->updateMouseWidget("", "");
193                         break;
194                 }
195         }
196 }
197
198 void RS_ActionDrawLineRectangle::updateMouseCursor()
199 {
200         graphicView->setMouseCursor(RS2::CadCursor);
201 }
202
203 void RS_ActionDrawLineRectangle::updateToolBar()
204 {
205         if (RS_DIALOGFACTORY != NULL)
206         {
207                 if (!isFinished())
208                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
209                 else
210                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
211         }
212 }
213
214 // EOF