]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinepolygon2.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawlinepolygon2.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawlinepolygon2.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_actiondrawlinepolygon2.h"
28
29 #include "rs_creation.h"
30 #include "rs_snapper.h"
31
32
33
34 RS_ActionDrawLinePolygon2::RS_ActionDrawLinePolygon2(
35     RS_EntityContainer& container,
36     RS_GraphicView& graphicView)
37         :RS_PreviewActionInterface("Draw Polygons", container, graphicView) {
38
39     corner1 = Vector(false);
40     corner2 = Vector(false);
41
42     number = 3;
43 }
44
45 QAction* RS_ActionDrawLinePolygon2::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
46 {
47         QAction * action = new QAction(tr("Polygo&n (Cor,Cor)"), 0);
48 //      QAction* action = new QAction(tr("Polygon"), tr("Polygo&n (Cor,Cor)"),
49 //                                                                      QKeySequence(), NULL);
50         action->setStatusTip(tr("Draw polygon with two corners"));
51         return action;
52 }
53
54
55 void RS_ActionDrawLinePolygon2::trigger() {
56     RS_PreviewActionInterface::trigger();
57
58     deleteSnapper();
59     deletePreview();
60     clearPreview();
61
62     RS_Creation creation(container, graphicView);
63     bool ok = creation.createPolygon2(corner1, corner2, number);
64
65     if (!ok) {
66         RS_DEBUG->print("RS_ActionDrawLinePolygon2::trigger:"
67                         " No polygon added\n");
68     }
69 }
70
71
72
73 void RS_ActionDrawLinePolygon2::mouseMoveEvent(QMouseEvent* e) {
74     RS_DEBUG->print("RS_ActionDrawLinePolygon2::mouseMoveEvent begin");
75
76     Vector mouse = snapPoint(e);
77
78     switch (getStatus()) {
79     case SetCorner1:
80         break;
81
82     case SetCorner2:
83         if (corner1.valid) {
84             corner2 = mouse;
85             deletePreview();
86             clearPreview();
87
88             RS_Creation creation(preview, NULL, false);
89             creation.createPolygon2(corner1, corner2, number);
90
91             drawPreview();
92         }
93         break;
94
95     default:
96         break;
97     }
98 }
99
100
101
102 void RS_ActionDrawLinePolygon2::mouseReleaseEvent(QMouseEvent* e) {
103     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
104         RS_CoordinateEvent ce(snapPoint(e));
105         coordinateEvent(&ce);
106     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
107         deletePreview();
108         clearPreview();
109         deleteSnapper();
110         init(getStatus()-1);
111     }
112 }
113
114
115
116 void RS_ActionDrawLinePolygon2::coordinateEvent(RS_CoordinateEvent* e) {
117     if (e==NULL) {
118         return;
119     }
120
121     Vector mouse = e->getCoordinate();
122
123     switch (getStatus()) {
124     case SetCorner1:
125         corner1 = mouse;
126         setStatus(SetCorner2);
127         graphicView->moveRelativeZero(mouse);
128         break;
129
130     case SetCorner2:
131         corner2 = mouse;
132         trigger();
133         break;
134
135     default:
136         break;
137     }
138 }
139
140
141
142 void RS_ActionDrawLinePolygon2::updateMouseButtonHints() {
143     if (RS_DIALOGFACTORY!=NULL) {
144         switch (getStatus()) {
145         case SetCorner1:
146             RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
147                                                 tr("Cancel"));
148             break;
149
150         case SetCorner2:
151             RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
152                                                 tr("Back"));
153             break;
154
155         case SetNumber:
156             RS_DIALOGFACTORY->updateMouseWidget(tr("Number:"), tr("Back"));
157             break;
158
159         default:
160             RS_DIALOGFACTORY->updateMouseWidget("", "");
161             break;
162         }
163     }
164 }
165
166
167
168 void RS_ActionDrawLinePolygon2::showOptions() {
169     RS_ActionInterface::showOptions();
170
171     if (RS_DIALOGFACTORY!=NULL) {
172         RS_DIALOGFACTORY->requestOptions(this, true);
173     }
174 }
175
176
177
178 void RS_ActionDrawLinePolygon2::hideOptions() {
179     RS_ActionInterface::hideOptions();
180
181     if (RS_DIALOGFACTORY!=NULL) {
182         RS_DIALOGFACTORY->requestOptions(this, false);
183     }
184 }
185
186
187
188 void RS_ActionDrawLinePolygon2::commandEvent(RS_CommandEvent* e) {
189     QString c = e->getCommand().toLower();
190
191     if (checkCommand("help", c)) {
192         if (RS_DIALOGFACTORY!=NULL) {
193             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
194                                              + getAvailableCommands().join(", "));
195         }
196         return;
197     }
198
199     switch (getStatus()) {
200     case SetCorner1:
201     case SetCorner2:
202         if (checkCommand("number", c)) {
203             deleteSnapper();
204             deletePreview();
205             clearPreview();
206             lastStatus = (Status)getStatus();
207             setStatus(SetNumber);
208         }
209         break;
210
211     case SetNumber: {
212             bool ok;
213             int n = c.toInt(&ok);
214             if (ok==true) {
215                 if (n>0 && n<10000) {
216                     number = n;
217                 } else {
218                     if (RS_DIALOGFACTORY!=NULL) {
219                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
220                                                             "Try 1..9999"));
221                     }
222                 }
223             } else {
224                 if (RS_DIALOGFACTORY!=NULL) {
225                     RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression."));
226                 }
227             }
228             if (RS_DIALOGFACTORY!=NULL) {
229                 RS_DIALOGFACTORY->requestOptions(this, true, true);
230             }
231             setStatus(lastStatus);
232         }
233         break;
234
235     default:
236         break;
237     }
238 }
239
240
241
242 QStringList RS_ActionDrawLinePolygon2::getAvailableCommands() {
243     QStringList cmd;
244
245     switch (getStatus()) {
246     case SetCorner1:
247     case SetCorner2:
248         cmd += command("number");
249         break;
250     default:
251         break;
252     }
253
254     return cmd;
255 }
256
257
258
259 void RS_ActionDrawLinePolygon2::updateMouseCursor() {
260     graphicView->setMouseCursor(RS2::CadCursor);
261 }
262
263
264
265 void RS_ActionDrawLinePolygon2::updateToolBar() {
266     if (RS_DIALOGFACTORY!=NULL) {
267         if (!isFinished()) {
268             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
269         } else {
270             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
271         }
272     }
273 }
274
275
276 // EOF