]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondefault.cpp
Initial import
[architektonas] / src / actions / rs_actiondefault.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondefault.cpp 1161 2004-12-09 23:10:09Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use 
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actiondefault.h"
28
29 #include "rs.h"
30 #include "rs_modification.h"
31 #include "rs_snapper.h"
32 #include "rs_selection.h"
33
34
35
36 /**
37  * Constructor.
38  */
39 RS_ActionDefault::RS_ActionDefault(RS_EntityContainer& container,
40                                    RS_GraphicView& graphicView)
41         : RS_PreviewActionInterface("Default",
42                             container, graphicView) {
43
44         RS_DEBUG->print("RS_ActionDefault::RS_ActionDefault");
45         RS_DEBUG->print("RS_ActionDefault::RS_ActionDefault: OK");
46 }
47
48
49
50 RS_ActionDefault::~RS_ActionDefault() {
51 }
52
53
54
55 QAction* RS_ActionDefault::createGUIAction(RS2::ActionType /*type*/, 
56         QObject* /*parent*/) {
57
58         return NULL;
59 }
60
61
62 void RS_ActionDefault::init(int status) {
63         RS_DEBUG->print("RS_ActionDefault::init");
64     
65         RS_PreviewActionInterface::init(status);
66     v1 = v2 = Vector(false);
67     snapMode = RS2::SnapFree;
68     snapRes = RS2::RestrictNothing;
69     restrBak = RS2::RestrictNothing;
70         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
71         
72         RS_DEBUG->print("RS_ActionDefault::init: OK");
73 }
74
75
76
77 void RS_ActionDefault::trigger() {
78     RS_PreviewActionInterface::trigger();
79
80 }
81
82 void RS_ActionDefault::keyPressEvent(QKeyEvent* e) {
83         if (e->key()==Qt::Key_Shift) {
84                 restrBak = snapRes;
85                 setSnapRestriction(RS2::RestrictOrthogonal);
86         }
87 }
88
89 void RS_ActionDefault::keyReleaseEvent(QKeyEvent* e) {
90         if (e->key()==Qt::Key_Shift) {
91                 setSnapRestriction(restrBak);
92         }
93 }
94
95
96 void RS_ActionDefault::mouseMoveEvent(QMouseEvent* e) {
97
98     Vector mouse = graphicView->toGraph(Vector(e->x(), e->y()));
99     Vector relMouse = mouse - graphicView->getRelativeZero();
100
101     RS_DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse);
102
103     switch (getStatus()) {
104     case Dragging:
105         //v2 = graphicView->toGraph(e->x(), e->y());
106         v2 = mouse;
107
108         if (graphicView->toGuiDX(v1.distanceTo(v2))>10) {
109             // look for reference points to drag:
110             double dist;
111             Vector ref = container->getNearestSelectedRef(v1, &dist);
112             if (ref.valid==true && graphicView->toGuiDX(dist)<8) {
113                                 RS_DEBUG->print("RS_ActionDefault::mouseMoveEvent: "
114                                         "moving reference point");
115                 setStatus(MovingRef);
116                 v1 = ref;
117                         graphicView->moveRelativeZero(v1);
118             } 
119                         else {
120                 // test for an entity to drag:
121                 RS_Entity* en = catchEntity(v1);
122                 if (en!=NULL && en->isSelected()) {
123                                         RS_DEBUG->print("RS_ActionDefault::mouseMoveEvent: "
124                                                 "moving entity");
125                     setStatus(Moving);
126                     v1 = en->getNearestRef(v1);
127                                 graphicView->moveRelativeZero(v1);
128                 }
129
130                 // no entity found. start area selection:
131                 else {
132                     setStatus(SetCorner2);
133                 }
134             }
135         }
136         break;
137                 
138     case MovingRef:
139         v2 = snapPoint(e);
140
141         deletePreview();
142         clearPreview();
143         preview->addSelectionFrom(*container);
144                 preview->moveRef(v1, v2-v1);
145         drawPreview();
146         break;
147
148     case Moving:
149         v2 = snapPoint(e);
150
151         deletePreview();
152         clearPreview();
153         preview->addSelectionFrom(*container);
154         preview->move(v2-v1);
155         drawPreview();
156         break;
157
158     case SetCorner2:
159         if (v1.valid) {
160             //v2 = snapPoint(e);
161             v2 = mouse;
162
163             //deleteSnapper();
164             deletePreview();
165             clearPreview();
166
167             preview->addEntity(new RS_Line(preview,
168                                            RS_LineData(Vector(v1.x, v1.y),
169                                                        Vector(v2.x, v1.y))));
170             preview->addEntity(new RS_Line(preview,
171                                            RS_LineData(Vector(v2.x, v1.y),
172                                                        Vector(v2.x, v2.y))));
173             preview->addEntity(new RS_Line(preview,
174                                            RS_LineData(Vector(v2.x, v2.y),
175                                                        Vector(v1.x, v2.y))));
176             preview->addEntity(new RS_Line(preview,
177                                            RS_LineData(Vector(v1.x, v2.y),
178                                                        Vector(v1.x, v1.y))));
179             drawPreview();
180         }
181
182     default:
183         break;
184     }
185 }
186
187
188
189 void RS_ActionDefault::mousePressEvent(QMouseEvent* e) {
190     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
191         switch (getStatus()) {
192         case Neutral:
193             v1 = graphicView->toGraph(e->x(), e->y());
194             setStatus(Dragging);
195             break;
196
197         case Moving: {
198                         v2 = snapPoint(e);
199                 deleteSnapper();
200                 deletePreview();
201                 clearPreview();
202                 RS_Modification m(*container, graphicView);
203                 RS_MoveData data;
204                 data.number = 0;
205                 data.useCurrentLayer = false;
206                 data.useCurrentAttributes = false;
207                 data.offset = v2-v1;
208                 m.move(data);
209                 setStatus(Neutral);
210                 RS_DIALOGFACTORY->updateSelectionWidget(
211                         container->countSelected());
212                                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
213             }
214             break;
215                         
216         case MovingRef: {
217                         v2 = snapPoint(e);
218                 deleteSnapper();
219                 deletePreview();
220                 clearPreview();
221                 RS_Modification m(*container, graphicView);
222                 RS_MoveRefData data;
223                                 data.ref = v1;
224                                 data.offset = v2-v1;
225                 m.moveRef(data);
226                                 //container->moveSelectedRef(v1, v2-v2);
227                 setStatus(Neutral);
228                 RS_DIALOGFACTORY->updateSelectionWidget(
229                         container->countSelected());
230                                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
231             }
232             break;
233
234         default:
235             break;
236         }
237     }
238 }
239
240
241
242 void RS_ActionDefault::mouseReleaseEvent(QMouseEvent* e) {
243     RS_DEBUG->print("RS_ActionDefault::mouseReleaseEvent()");
244
245     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
246         v2 = graphicView->toGraph(e->x(), e->y());
247         switch (getStatus()) {
248         case Dragging: {
249                 // select single entity:
250                 RS_Entity* en = catchEntity(e);
251
252                 if (en!=NULL) {
253                     deleteSnapper();
254                     deletePreview();
255                     clearPreview();
256
257                     RS_Selection s(*container, graphicView);
258                     s.selectSingle(en);
259
260                     RS_DIALOGFACTORY->updateSelectionWidget(
261                         container->countSelected());
262
263                     //deleteSnapper();
264 #if QT_VERSION>=0x030000
265                     e->accept();
266 #endif
267
268                     setStatus(Neutral);
269                 } else {
270                     setStatus(SetCorner2);
271                 }
272             }
273             break;
274
275         case SetCorner2: {
276                 //v2 = snapPoint(e);
277                 v2 = graphicView->toGraph(e->x(), e->y());
278
279                 // select window:
280                 //if (graphicView->toGuiDX(v1.distanceTo(v2))>20) {
281                 deleteSnapper();
282                 deletePreview();
283                 clearPreview();
284
285                 bool cross = (v2.y>v1.y);
286                 RS_Selection s(*container, graphicView);
287                 s.selectWindow(v1, v2, true, cross);
288
289                 RS_DIALOGFACTORY->updateSelectionWidget(
290                     container->countSelected());
291
292                 setStatus(Neutral);
293 #if QT_VERSION>=0x030000
294
295                 e->accept();
296 #endif
297                 //}
298             }
299             break;
300
301
302         default:
303             break;
304
305         }
306     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
307         switch (getStatus()) {
308                 case SetCorner2:
309                 case Moving:
310                 case MovingRef:
311             deletePreview();
312             clearPreview();
313             deleteSnapper();
314             setStatus(Neutral);
315                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
316 #if QT_VERSION>=0x030000
317
318             e->accept();
319 #endif
320                         break;
321
322                 default:
323             deleteSnapper();
324             RS_DIALOGFACTORY->requestPreviousMenu();
325 #if QT_VERSION>=0x030000
326
327             e->accept();
328 #endif
329                         break;
330                 }
331     }
332 }
333
334
335
336 void RS_ActionDefault::commandEvent(RS_CommandEvent* e) {
337     QString c = e->getCommand().toLower();
338
339     // if the current action can't deal with the command,
340     //   it might be intended to launch a new command
341     //if (!e.isAccepted()) {
342     // command for new action:
343     //RS2::ActionType type = RS_COMMANDS->cmdToAction(c);
344     //if (type!=RS2::ActionNone) {
345     //graphicView->setCurrentAction(type);
346     //return true;
347     //}
348     //}
349 }
350
351
352
353 QStringList RS_ActionDefault::getAvailableCommands() {
354     QStringList cmd;
355
356     //cmd += "line";
357     //cmd += "rectangle";
358
359     return cmd;
360 }
361
362
363 void RS_ActionDefault::updateMouseButtonHints() {
364     switch (getStatus()) {
365     case Neutral:
366         RS_DIALOGFACTORY->updateMouseWidget("", "");
367         break;
368     case SetCorner2:
369         RS_DIALOGFACTORY->updateMouseWidget(tr("Choose second edge"),
370                                             tr("Back"));
371         break;
372     default:
373         RS_DIALOGFACTORY->updateMouseWidget("", "");
374         break;
375     }
376 }
377
378
379
380 void RS_ActionDefault::updateMouseCursor() {
381         switch (getStatus()) {
382         case Neutral:
383         graphicView->setMouseCursor(RS2::ArrowCursor);
384                 break;
385         case Moving:
386         case MovingRef:
387         graphicView->setMouseCursor(RS2::SelectCursor);
388                 break;
389         default:
390                 break;
391         }
392 }
393
394
395
396 void RS_ActionDefault::updateToolBar() {
397     //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
398         switch (getStatus()) {
399         case Neutral:
400                 // would switch back to main in edit / measure / .. modes
401                 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
402                 break;
403         case Moving:
404         case MovingRef:
405                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
406                 break;
407         default:
408                 break;
409         }
410 }
411
412 // EOF