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