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