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