]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinerectangle.cpp
9882256c29a1b8670352fc70917c6880d05d9784
[architektonas] / src / actions / actiondrawlinerectangle.cpp
1 // 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 "actiondrawlinerectangle.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 ActionDrawLineRectangle::ActionDrawLineRectangle(
23         RS_EntityContainer & container, GraphicView & graphicView):
24         ActionInterface("Draw rectangles", container, graphicView)
25 {
26         reset();
27 }
28
29 ActionDrawLineRectangle::~ActionDrawLineRectangle()
30 {
31 }
32
33 void ActionDrawLineRectangle::reset()
34 {
35         for(int i=0; i<4; i++)
36                 data[i] = RS_LineData(Vector(false), Vector(false));
37 }
38
39 void ActionDrawLineRectangle::init(int status)
40 {
41         ActionInterface::init(status);
42         reset();
43 }
44
45 void ActionDrawLineRectangle::trigger()
46 {
47         ActionInterface::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, data[i]);
56                 line[i]->setLayerToActive();
57                 line[i]->setPenToActive();
58                 container->addEntity(line[i]);
59         }
60
61         // upd. undo list:
62         if (document)
63         {
64                 document->startUndoCycle();
65
66                 for(int i=0; i<4; i++)
67                         document->addUndoable(line[i]);
68
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("ActionDrawLineRectangle::trigger(): line added: %d",
80                         line[i]->getId());
81         }
82         graphicView->moveRelativeZero(corner2);
83 }
84
85 void ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent * e)
86 {
87         RS_DEBUG->print("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
102                 drawPreview();
103         }
104
105         RS_DEBUG->print("ActionDrawLineRectangle::mouseMoveEvent end");
106 }
107
108 void 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 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 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 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 ActionDrawLineRectangle::getAvailableCommands()
171 {
172         QStringList cmd;
173         return cmd;
174 }
175
176 void 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 ActionDrawLineRectangle::updateMouseCursor()
200 {
201         graphicView->setMouseCursor(RS2::CadCursor);
202 }
203
204 void 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