]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinerectangle.cpp
Bugfixes related to removing Snapper class.
[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 "commandevent.h"
20 #include "debug.h"
21 #include "dialogfactory.h"
22 #include "graphicview.h"
23 #include "preview.h"
24
25 ActionDrawLineRectangle::ActionDrawLineRectangle(
26         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] = 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         Line * line[4];
53         preparePreview();
54
55         // create and add rectangle:
56         for(int i=0; i<4; i++)
57         {
58                 line[i] = new 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                 DEBUG->print("ActionDrawLineRectangle::trigger(): line added: %d", line[i]->getId());
83         }
84
85         graphicView->moveRelativeZero(corner2);
86         graphicView->redraw();
87 }
88
89 void ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent * e)
90 {
91         DEBUG->print("ActionDrawLineRectangle::mouseMoveEvent begin");
92
93         Vector mouse = snapPoint(e);
94
95         if (getStatus() == SetCorner2 && corner1.valid)
96         {
97                 corner2 = mouse;
98 //              deletePreview();
99 //              clearPreview();
100 //              preparePreview();
101
102 //              for(int i=0; i<4; i++)
103 //                      preview->addEntity(new Line(preview, data[i]));
104
105 //              drawPreview();
106                 preparePreview();
107                 graphicView->preview.clear();
108
109                 for(int i=0; i<4; i++)
110                         graphicView->preview.addEntity(new Line(&(graphicView->preview), data[i]));
111         }
112
113         graphicView->redraw();
114         DEBUG->print("ActionDrawLineRectangle::mouseMoveEvent end");
115 }
116
117 void ActionDrawLineRectangle::mouseReleaseEvent(QMouseEvent * e)
118 {
119         if (e->button() == Qt::LeftButton)
120         {
121 //              Vector ce(snapPoint(e));
122                 Vector ce(graphicView->SnapPoint(e));
123                 coordinateEvent(&ce);
124         }
125         else if (e->button() == Qt::RightButton)
126         {
127 //              deletePreview();
128 //              deleteSnapper();
129                 init(getStatus() - 1);
130         }
131
132         graphicView->preview.clear();                           // Remove entities from container
133         graphicView->redraw();
134 }
135
136 void ActionDrawLineRectangle::preparePreview()
137 {
138         data[0] = LineData(corner1, Vector(corner2.x, corner1.y));
139         data[1] = LineData(Vector(corner2.x, corner1.y), corner2);
140         data[2] = LineData(corner2, Vector(corner1.x, corner2.y));
141         data[3] = LineData(Vector(corner1.x, corner2.y), corner1);
142 }
143
144 void ActionDrawLineRectangle::coordinateEvent(Vector * e)
145 {
146         if (!e)
147                 return;
148
149         Vector mouse = *e;
150
151         switch (getStatus())
152         {
153         case SetCorner1:
154                 corner1 = mouse;
155                 graphicView->moveRelativeZero(mouse);
156                 setStatus(SetCorner2);
157                 break;
158
159         case SetCorner2:
160                 corner2 = mouse;
161                 trigger();
162                 setStatus(SetCorner1);
163                 break;
164
165         default:
166                 break;
167         }
168 }
169
170 void ActionDrawLineRectangle::commandEvent(CommandEvent * e)
171 {
172         QString c = e->getCommand().toLower();
173
174         if (checkCommand("help", c))
175         {
176                 if (DIALOGFACTORY)
177                         DIALOGFACTORY->commandMessage(msgAvailableCommands()
178                                 + getAvailableCommands().join(", "));
179                 return;
180         }
181 }
182
183 QStringList ActionDrawLineRectangle::getAvailableCommands()
184 {
185         QStringList cmd;
186         return cmd;
187 }
188
189 void ActionDrawLineRectangle::updateMouseButtonHints()
190 {
191         if (DIALOGFACTORY)
192         {
193                 switch (getStatus())
194                 {
195                 case SetCorner1:
196                         DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"), tr("Cancel"));
197                         break;
198
199                 case SetCorner2:
200                         DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"), tr("Back"));
201                         break;
202
203                 default:
204                         DIALOGFACTORY->updateMouseWidget("", "");
205                         break;
206                 }
207         }
208 }
209
210 void ActionDrawLineRectangle::updateMouseCursor()
211 {
212         graphicView->setMouseCursor(RS2::CadCursor);
213 }
214
215 void ActionDrawLineRectangle::updateToolBar()
216 {
217         if (DIALOGFACTORY)
218         {
219                 if (!isFinished())
220                         DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
221                 else
222                         DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
223         }
224 }