]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimleader.cpp
c1d2aa9ba084c4ba3b5dbb6303e4492dccf66562
[architektonas] / src / actions / rs_actiondimleader.cpp
1 // rs_actiondimleader.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/03/2010  Added this text. :-)
13 //
14
15 #include "rs_actiondimleader.h"
16
17 #include "rs_dialogfactory.h"
18 #include "rs_graphicview.h"
19 #include "rs_preview.h"
20
21 RS_ActionDimLeader::RS_ActionDimLeader(RS_EntityContainer & container, RS_GraphicView & graphicView):
22         RS_PreviewActionInterface("Draw leaders", container, graphicView)
23 {
24         reset();
25 }
26
27 RS_ActionDimLeader::~RS_ActionDimLeader()
28 {
29 }
30
31 /*virtual*/ RS2::ActionType RS_ActionDimLeader::rtti()
32 {
33         return RS2::ActionDimLeader;
34 }
35
36 void RS_ActionDimLeader::reset()
37 {
38         //data = RS_LineData(Vector(false), Vector(false));
39         //start = Vector(false);
40         //history.clear();
41         points.clear();
42 }
43
44 void RS_ActionDimLeader::init(int status)
45 {
46         RS_PreviewActionInterface::init(status);
47
48         reset();
49 }
50
51 void RS_ActionDimLeader::trigger()
52 {
53         RS_PreviewActionInterface::trigger();
54
55         if (points.count() > 0)
56         {
57                 RS_Leader * leader = new RS_Leader(container, RS_LeaderData(true));
58                 leader->setLayerToActive();
59                 leader->setPenToActive();
60
61 //              for(Vector * v=points.first(); v!=NULL; v=points.next())
62 //                      leader->addVertex(*v);
63                 for (int i = 0; i < points.size(); i++)
64                         leader->addVertex(*(points[i]));
65
66                 container->addEntity(leader);
67
68                 // upd. undo list:
69                 if (document != NULL)
70                 {
71                         document->startUndoCycle();
72                         document->addUndoable(leader);
73                         document->endUndoCycle();
74                 }
75
76                 deletePreview();
77                 clearPreview();
78                 deleteSnapper();
79                 Vector rz = graphicView->getRelativeZero();
80                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
81                 graphicView->drawEntity(leader);
82                 graphicView->moveRelativeZero(rz);
83                 //drawSnapper();
84
85                 RS_DEBUG->print("RS_ActionDimLeader::trigger(): leader added: %d", leader->getId());
86         }
87 }
88
89 void RS_ActionDimLeader::mouseMoveEvent(QMouseEvent * e)
90 {
91         RS_DEBUG->print("RS_ActionDimLeader::mouseMoveEvent begin");
92
93         Vector mouse = snapPoint(e);
94
95         if (getStatus() == SetEndpoint && points.last() != NULL)
96         {
97                 deletePreview();
98                 clearPreview();
99
100                 // fill in lines that were already set:
101                 Vector last(false);
102
103 //              for(Vector * v=points.first(); v!=NULL; v=points.next())
104                 for (int i = 0; i < points.size(); i++)
105                 {
106                         Vector * v = points[i];
107
108                         if (last.valid)
109                                 preview->addEntity(new RS_Line(preview, RS_LineData(last, *v)));
110
111                         last = *v;
112                 }
113
114                 Vector p = *points.last();
115                 preview->addEntity(new RS_Line(preview, RS_LineData(p, mouse)));
116                 drawPreview();
117         }
118
119         RS_DEBUG->print("RS_ActionDimLeader::mouseMoveEvent end");
120 }
121
122 void RS_ActionDimLeader::mouseReleaseEvent(QMouseEvent * e)
123 {
124         if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
125         {
126                 Vector ce(snapPoint(e));
127                 coordinateEvent(&ce);
128         }
129         else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
130         {
131                 if (getStatus() == SetEndpoint)
132                 {
133                         trigger();
134                         reset();
135                         setStatus(SetStartpoint);
136                 }
137                 else
138                 {
139                         deletePreview();
140                         deleteSnapper();
141                         init(getStatus() - 1);
142                 }
143         }
144 }
145
146 void RS_ActionDimLeader::keyPressEvent(QKeyEvent * e)
147 {
148         if (getStatus() == SetEndpoint && e->key() == Qt::Key_Enter)
149         {
150                 trigger();
151                 reset();
152                 setStatus(SetStartpoint);
153         }
154 }
155
156 void RS_ActionDimLeader::coordinateEvent(Vector * e)
157 {
158         if (e == NULL)
159                 return;
160
161         Vector mouse = *e;
162
163         switch (getStatus())
164         {
165         case SetStartpoint:
166                 //data.startpoint = mouse;
167                 points.clear();
168                 points.append(new Vector(mouse));
169                 //start = data.startpoint;
170                 setStatus(SetEndpoint);
171                 graphicView->moveRelativeZero(mouse);
172                 break;
173
174         case SetEndpoint:
175                 //data.endpoint = mouse;
176                 points.append(new Vector(mouse));
177                 //trigger();
178                 //data.startpoint = data.endpoint;
179                 graphicView->moveRelativeZero(mouse);
180                 break;
181
182         default:
183                 break;
184         }
185 }
186
187 void RS_ActionDimLeader::commandEvent(RS_CommandEvent * e)
188 {
189         QString c = e->getCommand().toLower();
190
191         if (checkCommand("help", c))
192         {
193                 if (RS_DIALOGFACTORY != NULL)
194                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
195                                 + getAvailableCommands().join(", "));
196
197                 return;
198         }
199
200         // enter to finish
201         if (c == "")
202         {
203                 trigger();
204                 reset();
205                 setStatus(SetStartpoint);
206                 //finish();
207         }
208 }
209
210 QStringList RS_ActionDimLeader::getAvailableCommands()
211 {
212         QStringList cmd;
213
214         return cmd;
215 }
216
217 void RS_ActionDimLeader::updateMouseButtonHints()
218 {
219         if (RS_DIALOGFACTORY != NULL)
220         {
221                 switch (getStatus())
222                 {
223                 case SetStartpoint:
224                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"), tr("Cancel"));
225                         break;
226
227                 case SetEndpoint:
228                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Finish"));
229                         break;
230
231                 default:
232                         RS_DIALOGFACTORY->updateMouseWidget("", "");
233                         break;
234                 }
235         }
236 }
237
238 void RS_ActionDimLeader::showOptions()
239 {
240         RS_ActionInterface::showOptions();
241
242         //RS_DIALOGFACTORY->requestOptions(this, true);
243 }
244
245 void RS_ActionDimLeader::hideOptions()
246 {
247         RS_ActionInterface::hideOptions();
248
249         //RS_DIALOGFACTORY->requestOptions(this, false);
250 }
251
252 void RS_ActionDimLeader::updateMouseCursor()
253 {
254         graphicView->setMouseCursor(RS2::CadCursor);
255 }
256
257 void RS_ActionDimLeader::updateToolBar()
258 {
259         if (RS_DIALOGFACTORY != NULL)
260         {
261                 if (!isFinished())
262                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
263                 else
264                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarDim);
265         }
266 }
267