]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actionmodifyround.cpp
530ddfab42e48a283af7e8493b4e9eaf52e3d1fc
[architektonas] / src / actions / rs_actionmodifyround.cpp
1 // rs_actionmodifyround.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/04/2010  Added this text. :-)
13 //
14
15 #include "rs_actionmodifyround.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "rs_information.h"
20 #include "rs_preview.h"
21
22 RS_ActionModifyRound::RS_ActionModifyRound(RS_EntityContainer & container, GraphicView & graphicView):
23         RS_PreviewActionInterface("Round Entities", container, graphicView)
24 {
25         entity1 = NULL;
26         entity2 = NULL;
27         coord1 = Vector(false);
28         coord2 = Vector(false);
29 }
30
31 RS_ActionModifyRound::~RS_ActionModifyRound()
32 {
33 }
34
35 /*virtual*/ RS2::ActionType RS_ActionModifyRound::rtti()
36 {
37         return RS2::ActionModifyRound;
38 }
39
40 void RS_ActionModifyRound::init(int status)
41 {
42         RS_ActionInterface::init(status);
43
44         snapMode = RS2::SnapFree;
45         snapRes = RS2::RestrictNothing;
46 }
47
48 void RS_ActionModifyRound::trigger()
49 {
50         RS_DEBUG->print("RS_ActionModifyRound::trigger()");
51
52         if (entity1 != NULL && entity1->isAtomic()
53             && entity2 != NULL && entity2->isAtomic())
54         {
55                 deletePreview();
56
57                 RS_Modification m(*container, graphicView);
58                 m.round(coord2,
59                         coord1,
60                         (RS_AtomicEntity *)entity1,
61                         coord2,
62                         (RS_AtomicEntity *)entity2,
63                         data);
64
65                 //coord = Vector(false);
66                 coord1 = Vector(false);
67                 entity1 = NULL;
68                 coord2 = Vector(false);
69                 entity2 = NULL;
70                 setStatus(SetEntity1);
71
72                 clearPreview();
73
74                 if (RS_DIALOGFACTORY != NULL)
75                         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
76         }
77 }
78
79 void RS_ActionModifyRound::mouseMoveEvent(QMouseEvent * e)
80 {
81         RS_DEBUG->print("RS_ActionModifyRound::mouseMoveEvent begin");
82
83         Vector mouse = graphicView->toGraph(e->x(), e->y());
84         RS_Entity * se = catchEntity(e, RS2::ResolveAll);
85
86         switch (getStatus())
87         {
88         case SetEntity1:
89                 entity1 = se;
90                 coord1 = mouse;
91                 break;
92
93         case SetEntity2:
94                 entity2 = se;
95                 coord2 = mouse;
96
97                 if (entity1 != NULL && entity2 != NULL && entity2->isAtomic()
98                     && RS_Information::isTrimmable(entity1, entity2))
99                 {
100                         deletePreview();
101                         clearPreview();
102                         //preview->addSelectionFrom(*container);
103                         //preview->move(targetPoint-referencePoint);
104                         RS_Entity * tmp1 = entity1->clone();
105                         RS_Entity * tmp2 = entity2->clone();
106                         tmp1->reparent(preview);
107                         tmp2->reparent(preview);
108                         preview->addEntity(tmp1);
109                         preview->addEntity(tmp2);
110
111                         bool trim = data.trim;
112                         data.trim = false;
113                         RS_Modification m(*preview, NULL, false);
114                         m.round(coord2,
115                                 coord1,
116                                 (RS_AtomicEntity *)tmp1,
117                                 coord2,
118                                 (RS_AtomicEntity *)tmp2,
119                                 data);
120                         data.trim = trim;
121
122                         preview->removeEntity(tmp1);
123                         preview->removeEntity(tmp2);
124                         drawPreview();
125                 }
126                 break;
127
128         default:
129                 break;
130         }
131
132         RS_DEBUG->print("RS_ActionModifyRound::mouseMoveEvent end");
133 }
134
135 void RS_ActionModifyRound::mouseReleaseEvent(QMouseEvent * e)
136 {
137         Vector mouse = graphicView->toGraph(e->x(), e->y());
138         RS_Entity * se = catchEntity(e, RS2::ResolveAll);
139
140         if (e->button() == Qt::LeftButton)
141         {
142                 switch (getStatus())
143                 {
144                 case SetEntity1:
145                         entity1 = se;
146                         coord1 = mouse;
147
148                         if (entity1 != NULL && entity1->isAtomic()
149                             && RS_Information::isTrimmable(entity1))
150                                 setStatus(SetEntity2);
151                         break;
152
153                 case SetEntity2:
154                         entity2 = se;
155                         coord2 = mouse;
156
157                         if (entity2 != NULL && entity2->isAtomic()
158                             && RS_Information::isTrimmable(entity1, entity2))
159                                 //setStatus(ChooseRounding);
160                                 trigger();
161                         break;
162
163                 default:
164                         break;
165                 }
166         }
167         else if (e->button() == Qt::RightButton)
168         {
169                 deletePreview();
170                 deleteSnapper();
171                 init(getStatus() - 1);
172         }
173 }
174
175 void RS_ActionModifyRound::commandEvent(RS_CommandEvent * e)
176 {
177         QString c = e->getCommand().toLower();
178
179         if (checkCommand("help", c))
180         {
181                 if (RS_DIALOGFACTORY != NULL)
182                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
183                                 + getAvailableCommands().join(", "));
184                 return;
185         }
186
187         switch (getStatus())
188         {
189         case SetEntity1:
190         case SetEntity2:
191
192                 if (checkCommand("radius", c))
193                 {
194                         deleteSnapper();
195                         deletePreview();
196                         clearPreview();
197                         lastStatus = (Status)getStatus();
198                         setStatus(SetRadius);
199                 }
200                 else if (checkCommand("trim", c))
201                 {
202                         /*deleteSnapper();
203                            deletePreview();
204                            clearPreview();
205                            lastStatus = (Status)getStatus();
206                            setStatus(SetTrim);
207                          */
208                         data.trim = !data.trim;
209
210                         if (RS_DIALOGFACTORY != NULL)
211                                 RS_DIALOGFACTORY->requestOptions(this, true, true);
212                 }
213                 break;
214
215         case SetRadius: {
216                 bool ok;
217                 double r = RS_Math::eval(c, &ok);
218
219                 if (ok == true)
220                         data.radius = r;
221                 else if (RS_DIALOGFACTORY != NULL)
222                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
223
224                 if (RS_DIALOGFACTORY != NULL)
225                         RS_DIALOGFACTORY->requestOptions(this, true, true);
226                 setStatus(lastStatus);
227         }
228         break;
229
230         /*case SetTrim: {
231            if (c==cmdYes.toLower() || c==cmdYes2) {
232            data.trim = true;
233            } else if (c==cmdNo.toLower() || c==cmdNo2) {
234            data.trim = false;
235                 } else {
236                     RS_DIALOGFACTORY->commandMessage(tr("Please enter 'Yes' "
237                "or 'No'"));
238                 }
239                 RS_DIALOGFACTORY->requestOptions(this, true, true);
240                 setStatus(lastStatus);
241             }
242             break;*/
243
244         default:
245                 break;
246         }
247 }
248
249 QStringList RS_ActionModifyRound::getAvailableCommands()
250 {
251         QStringList cmd;
252
253         switch (getStatus())
254         {
255         case SetEntity1:
256         case SetEntity2:
257                 cmd += command("radius");
258                 cmd += command("trim");
259                 break;
260
261         default:
262                 break;
263         }
264         return cmd;
265 }
266
267 void RS_ActionModifyRound::showOptions()
268 {
269         RS_ActionInterface::showOptions();
270
271         if (RS_DIALOGFACTORY != NULL)
272                 RS_DIALOGFACTORY->requestOptions(this, true);
273 }
274
275 void RS_ActionModifyRound::hideOptions()
276 {
277         RS_ActionInterface::hideOptions();
278
279         if (RS_DIALOGFACTORY != NULL)
280                 RS_DIALOGFACTORY->requestOptions(this, false);
281 }
282
283 void RS_ActionModifyRound::updateMouseButtonHints()
284 {
285         if (RS_DIALOGFACTORY != NULL)
286         {
287                 switch (getStatus())
288                 {
289                 case SetEntity1:
290                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first entity"),
291                                 tr("Back"));
292                         break;
293
294                 case SetEntity2:
295                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second entity"),
296                                 tr("Back"));
297                         break;
298
299                 case SetRadius:
300                         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter radius:"),
301                                 tr("Cancel"));
302                         break;
303
304                 /*case SetTrim:
305                     RS_DIALOGFACTORY->updateMouseWidget(tr("Trim on? (yes/no):"),
306                                                         "");
307                     break;*/
308                 default:
309                         RS_DIALOGFACTORY->updateMouseWidget("", "");
310                         break;
311                 }
312         }
313 }
314
315 void RS_ActionModifyRound::updateMouseCursor()
316 {
317         graphicView->setMouseCursor(RS2::CadCursor);
318 }
319
320 void RS_ActionModifyRound::updateToolBar()
321 {
322         if (RS_DIALOGFACTORY != NULL)
323                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarModify);
324 }
325
326 void RS_ActionModifyRound::setRadius(double r)
327 {
328         data.radius = r;
329 }
330
331 double RS_ActionModifyRound::getRadius()
332 {
333         return data.radius;
334 }
335
336 void RS_ActionModifyRound::setTrim(bool t)
337 {
338         data.trim = t;
339 }
340
341 bool RS_ActionModifyRound::isTrimOn()
342 {
343         return data.trim;
344 }