]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimleader.cpp
Initial import
[architektonas] / src / actions / rs_actiondimleader.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondimleader.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_actiondimleader.h"
28
29 #include "rs_snapper.h"
30
31 RS_ActionDimLeader::RS_ActionDimLeader(RS_EntityContainer & container,
32         RS_GraphicView & graphicView):
33         RS_PreviewActionInterface("Draw leaders", container, graphicView)
34 {
35         reset();
36 }
37
38 RS_ActionDimLeader::~RS_ActionDimLeader()
39 {
40 }
41
42 /*virtual*/ RS2::ActionType RS_ActionDimLeader::rtti()
43 {
44         return RS2::ActionDimLeader;
45 }
46
47 QAction * RS_ActionDimLeader::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
48 {
49         QAction * action = new QAction(tr("&Leader"), 0);
50 //      QAction* action = new QAction(tr("Leader"), tr("&Leader"),
51 //                                                                      QKeySequence(), NULL);
52         action->setStatusTip(tr("Leader Dimension"));
53
54         return action;
55 }
56
57 void RS_ActionDimLeader::reset()
58 {
59         //data = RS_LineData(Vector(false), Vector(false));
60         //start = Vector(false);
61         //history.clear();
62         points.clear();
63 }
64
65 void RS_ActionDimLeader::init(int status)
66 {
67         RS_PreviewActionInterface::init(status);
68
69         reset();
70 }
71
72 void RS_ActionDimLeader::trigger()
73 {
74         RS_PreviewActionInterface::trigger();
75
76         if (points.count() > 0)
77         {
78                 RS_Leader * leader = new RS_Leader(container, RS_LeaderData(true));
79                 leader->setLayerToActive();
80                 leader->setPenToActive();
81
82 //              for(Vector * v=points.first(); v!=NULL; v=points.next())
83 //                      leader->addVertex(*v);
84                 for(int i=0; i<points.size(); i++)
85                         leader->addVertex(*(points[i]));
86
87                 container->addEntity(leader);
88
89                 // upd. undo list:
90                 if (document != NULL)
91                 {
92                         document->startUndoCycle();
93                         document->addUndoable(leader);
94                         document->endUndoCycle();
95                 }
96
97                 deletePreview();
98                 clearPreview();
99                 deleteSnapper();
100                 Vector rz = graphicView->getRelativeZero();
101                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
102                 graphicView->drawEntity(leader);
103                 graphicView->moveRelativeZero(rz);
104                 //drawSnapper();
105
106                 RS_DEBUG->print("RS_ActionDimLeader::trigger(): leader added: %d", leader->getId());
107         }
108 }
109
110 void RS_ActionDimLeader::mouseMoveEvent(QMouseEvent * e)
111 {
112         RS_DEBUG->print("RS_ActionDimLeader::mouseMoveEvent begin");
113
114         Vector mouse = snapPoint(e);
115
116         if (getStatus()==SetEndpoint && points.last()!=NULL)
117         {
118                 deletePreview();
119                 clearPreview();
120
121                 // fill in lines that were already set:
122                 Vector last(false);
123
124 //              for(Vector * v=points.first(); v!=NULL; v=points.next())
125                 for(int i=0; i<points.size(); i++)
126                 {
127                         Vector * v = points[i];
128
129                         if (last.valid)
130                                 preview->addEntity(new RS_Line(preview, RS_LineData(last, *v)));
131
132                         last = *v;
133                 }
134
135                 Vector p = *points.last();
136                 preview->addEntity(new RS_Line(preview, RS_LineData(p, mouse)));
137                 drawPreview();
138         }
139
140         RS_DEBUG->print("RS_ActionDimLeader::mouseMoveEvent end");
141 }
142
143 void RS_ActionDimLeader::mouseReleaseEvent(QMouseEvent * e)
144 {
145         if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
146         {
147                 RS_CoordinateEvent ce(snapPoint(e));
148                 coordinateEvent(&ce);
149         }
150         else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
151         {
152                 if (getStatus() == SetEndpoint)
153                 {
154                         trigger();
155                         reset();
156                         setStatus(SetStartpoint);
157                 }
158                 else
159                 {
160                         deletePreview();
161                         deleteSnapper();
162                         init(getStatus() - 1);
163                 }
164         }
165 }
166
167 void RS_ActionDimLeader::keyPressEvent(QKeyEvent * e)
168 {
169         if (getStatus() == SetEndpoint && e->key() == Qt::Key_Enter)
170         {
171                 trigger();
172                 reset();
173                 setStatus(SetStartpoint);
174         }
175 }
176
177 void RS_ActionDimLeader::coordinateEvent(RS_CoordinateEvent * e)
178 {
179         if (e == NULL)
180                 return;
181
182         Vector mouse = e->getCoordinate();
183
184         switch (getStatus())
185         {
186         case SetStartpoint:
187                 //data.startpoint = mouse;
188                 points.clear();
189                 points.append(new Vector(mouse));
190                 //start = data.startpoint;
191                 setStatus(SetEndpoint);
192                 graphicView->moveRelativeZero(mouse);
193                 break;
194
195         case SetEndpoint:
196                 //data.endpoint = mouse;
197                 points.append(new Vector(mouse));
198                 //trigger();
199                 //data.startpoint = data.endpoint;
200                 graphicView->moveRelativeZero(mouse);
201                 break;
202
203         default:
204                 break;
205         }
206 }
207
208 void RS_ActionDimLeader::commandEvent(RS_CommandEvent * e)
209 {
210         QString c = e->getCommand().toLower();
211
212         if (checkCommand("help", c))
213         {
214                 if (RS_DIALOGFACTORY != NULL)
215                 {
216                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
217                                 + getAvailableCommands().join(", "));
218                 }
219
220                 return;
221         }
222
223         // enter to finish
224         if (c == "")
225         {
226                 trigger();
227                 reset();
228                 setStatus(SetStartpoint);
229                 //finish();
230         }
231 }
232
233 QStringList RS_ActionDimLeader::getAvailableCommands()
234 {
235         QStringList cmd;
236
237         return cmd;
238 }
239
240 void RS_ActionDimLeader::updateMouseButtonHints()
241 {
242         if (RS_DIALOGFACTORY != NULL)
243         {
244                 switch (getStatus())
245                 {
246                 case SetStartpoint:
247                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"), tr("Cancel"));
248                         break;
249                 case SetEndpoint:
250                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Finish"));
251                         break;
252                 default:
253                         RS_DIALOGFACTORY->updateMouseWidget("", "");
254                         break;
255                 }
256         }
257 }
258
259 void RS_ActionDimLeader::showOptions()
260 {
261         RS_ActionInterface::showOptions();
262
263         //RS_DIALOGFACTORY->requestOptions(this, true);
264 }
265
266 void RS_ActionDimLeader::hideOptions()
267 {
268         RS_ActionInterface::hideOptions();
269
270         //RS_DIALOGFACTORY->requestOptions(this, false);
271 }
272
273 void RS_ActionDimLeader::updateMouseCursor()
274 {
275         graphicView->setMouseCursor(RS2::CadCursor);
276 }
277
278 void RS_ActionDimLeader::updateToolBar()
279 {
280         if (RS_DIALOGFACTORY != NULL)
281         {
282                 if (!isFinished())
283                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
284                 else
285                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarDim);
286         }
287 }