]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimaligned.cpp
63ee67b0c700e15b6ea53ad9f38c2877e4e3e605
[architektonas] / src / actions / rs_actiondimaligned.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondimaligned.cpp 1134 2004-07-13 23:26:13Z 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_actiondimaligned.h"
28
29 #include "rs_snapper.h"
30 #include "rs_constructionline.h"
31 #include "rs_dialogfactory.h"
32
33
34
35 RS_ActionDimAligned::RS_ActionDimAligned(RS_EntityContainer& container,
36         RS_GraphicView& graphicView)
37         :RS_ActionDimension("Draw aligned dimensions",
38                     container, graphicView)
39 {
40     reset();
41 }
42
43 RS_ActionDimAligned::~RS_ActionDimAligned()
44 {
45 }
46
47 QAction * RS_ActionDimAligned::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
48 {
49         QAction * action = new QAction(tr("&Aligned"), 0);
50 //        QAction* action = new QAction(tr("Aligned"), tr("&Aligned"),
51 //                             QKeySequence(), NULL);
52         action->setStatusTip(tr("Aligned Dimension"));
53
54         return action;
55 }
56
57
58 void RS_ActionDimAligned::reset()
59 {
60     RS_ActionDimension::reset();
61     edata = RS_DimAlignedData(Vector(false), Vector(false));
62     lastStatus = SetExtPoint1;
63
64         if (RS_DIALOGFACTORY != NULL)
65         RS_DIALOGFACTORY->requestOptions(this, true, true);
66 }
67
68
69
70 void RS_ActionDimAligned::trigger()
71 {
72     RS_ActionDimension::trigger();
73
74     preparePreview();
75     graphicView->moveRelativeZero(data.definitionPoint);
76
77         //data.text = getText();
78     RS_DimAligned* dim =
79         new RS_DimAligned(container, data, edata);
80     dim->setLayerToActive();
81     dim->setPenToActive();
82     dim->update();
83     container->addEntity(dim);
84
85     // upd. undo list:
86     if (document!=NULL) {
87         document->startUndoCycle();
88         document->addUndoable(dim);
89         document->endUndoCycle();
90     }
91
92     deleteSnapper();
93     Vector rz = graphicView->getRelativeZero();
94     graphicView->moveRelativeZero(Vector(0.0,0.0));
95     graphicView->drawEntity(dim);
96     graphicView->moveRelativeZero(rz);
97     drawSnapper();
98
99     RS_DEBUG->print("RS_ActionDimAligned::trigger():"
100                     " dim added: %d", dim->getId());
101 }
102
103
104
105 void RS_ActionDimAligned::preparePreview() {
106     Vector dirV;
107     dirV.setPolar(100.0,
108                   edata.extensionPoint1.angleTo(
109                       edata.extensionPoint2)
110                   +M_PI/2.0);
111     RS_ConstructionLine cl(NULL,
112                            RS_ConstructionLineData(
113                                edata.extensionPoint2,
114                                edata.extensionPoint2+dirV));
115
116     data.definitionPoint =
117         cl.getNearestPointOnEntity(data.definitionPoint);
118 }
119
120
121
122 void RS_ActionDimAligned::mouseMoveEvent(QMouseEvent* e) {
123     RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent begin");
124
125     Vector mouse = snapPoint(e);
126
127     switch (getStatus()) {
128     case SetExtPoint1:
129         break;
130
131     case SetExtPoint2:
132         if (edata.extensionPoint1.valid) {
133             deletePreview();
134             clearPreview();
135             preview->addEntity(
136                 new RS_Line(preview,
137                             RS_LineData(edata.extensionPoint1, mouse))
138             );
139             drawPreview();
140         }
141         break;
142
143     case SetDefPoint:
144         if (edata.extensionPoint1.valid && edata.extensionPoint2.valid) {
145             deletePreview();
146             clearPreview();
147             data.definitionPoint = mouse;
148
149             preparePreview();
150
151                         //data.text = getText();
152             RS_DimAligned* dim = new RS_DimAligned(preview, data, edata);
153             dim->update();
154             preview->addEntity(dim);
155             drawPreview();
156         }
157         break;
158
159         default:
160                 break;
161     }
162
163     RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent end");
164 }
165
166
167
168 void RS_ActionDimAligned::mouseReleaseEvent(QMouseEvent* e) {
169     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
170         RS_CoordinateEvent ce(snapPoint(e));
171         coordinateEvent(&ce);
172     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
173         deletePreview();
174         deleteSnapper();
175         init(getStatus()-1);
176     }
177 }
178
179
180
181 void RS_ActionDimAligned::coordinateEvent(RS_CoordinateEvent* e) {
182     if (e==NULL) {
183         return;
184     }
185
186     Vector pos = e->getCoordinate();
187
188     switch (getStatus()) {
189     case SetExtPoint1:
190         edata.extensionPoint1 = pos;
191         graphicView->moveRelativeZero(pos);
192         setStatus(SetExtPoint2);
193         break;
194
195     case SetExtPoint2:
196         edata.extensionPoint2 = pos;
197         graphicView->moveRelativeZero(pos);
198         setStatus(SetDefPoint);
199         break;
200
201     case SetDefPoint:
202         data.definitionPoint = pos;
203         trigger();
204         reset();
205         setStatus(SetExtPoint1);
206         break;
207
208     default:
209         break;
210     }
211 }
212
213
214
215 void RS_ActionDimAligned::commandEvent(RS_CommandEvent* e) {
216     QString c = e->getCommand().toLower();
217
218     if (checkCommand("help", c)) {
219         if (RS_DIALOGFACTORY!=NULL) {
220             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
221                                              + getAvailableCommands().join(", "));
222         }
223         return;
224     }
225
226     switch (getStatus()) {
227     case SetText: {
228             setText(c);
229             if (RS_DIALOGFACTORY!=NULL) {
230                 RS_DIALOGFACTORY->requestOptions(this, true, true);
231             }
232             setStatus(lastStatus);
233             graphicView->enableCoordinateInput();
234         }
235         break;
236
237     default:
238         if (checkCommand("text", c)) {
239             lastStatus = (Status)getStatus();
240             graphicView->disableCoordinateInput();
241             setStatus(SetText);
242         }
243         break;
244     }
245 }
246
247
248
249 QStringList RS_ActionDimAligned::getAvailableCommands() {
250     QStringList cmd;
251
252     switch (getStatus()) {
253     case SetExtPoint1:
254     case SetExtPoint2:
255     case SetDefPoint:
256         cmd += command("text");
257         break;
258
259     default:
260         break;
261     }
262
263     return cmd;
264 }
265
266
267
268 void RS_ActionDimAligned::updateMouseButtonHints() {
269     if (RS_DIALOGFACTORY!=NULL) {
270         switch (getStatus()) {
271         case SetExtPoint1:
272             RS_DIALOGFACTORY->updateMouseWidget(
273                 tr("Specify first extension line origin"),
274                 tr("Cancel"));
275             break;
276         case SetExtPoint2:
277             RS_DIALOGFACTORY->updateMouseWidget(
278                 tr("Specify second extension line origin"),
279                 tr("Back"));
280             break;
281         case SetDefPoint:
282             RS_DIALOGFACTORY->updateMouseWidget(
283                 tr("Specify dimension line location"),
284                 tr("Back"));
285             break;
286         case SetText:
287             RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
288             break;
289         default:
290             RS_DIALOGFACTORY->updateMouseWidget("", "");
291             break;
292         }
293     }
294 }
295
296
297
298 void RS_ActionDimAligned::hideOptions() {
299     if (RS_DIALOGFACTORY!=NULL) {
300         RS_DIALOGFACTORY->requestOptions(this, false);
301     }
302
303     RS_ActionDimension::hideOptions();
304 }
305
306
307
308 void RS_ActionDimAligned::showOptions() {
309     RS_ActionDimension::showOptions();
310
311     if (RS_DIALOGFACTORY!=NULL) {
312         RS_DIALOGFACTORY->requestOptions(this, true);
313     }
314 }
315
316
317 // EOF