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