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