]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlineparallel.cpp
Last checkin before major refactor...
[architektonas] / src / actions / rs_actiondrawlineparallel.cpp
1 // rs_actiondrawlineparallel.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/22/2010  Added this text. :-)
13 //
14
15 #include "rs_actiondrawlineparallel.h"
16
17 #include "rs_actiondrawlineparallelthrough.h"
18 #include "rs_commandevent.h"
19 #include "commands.h"
20 #include "rs_creation.h"
21 #include "rs_dialogfactory.h"
22 #include "graphicview.h"
23 #include "rs_preview.h"
24
25 RS_ActionDrawLineParallel::RS_ActionDrawLineParallel(RS_EntityContainer & container, GraphicView & graphicView): RS_PreviewActionInterface("Draw Parallels",
26                 container, graphicView)
27 {
28         parallel = NULL;
29         entity = NULL;
30         distance = 1.0;
31         number = 1;
32         coord = Vector(false);
33 }
34
35 RS_ActionDrawLineParallel::~RS_ActionDrawLineParallel()
36 {
37 }
38
39 /*virtual*/ RS2::ActionType RS_ActionDrawLineParallel::rtti()
40 {
41         return RS2::ActionDrawLineParallel;
42 }
43
44 void RS_ActionDrawLineParallel::trigger()
45 {
46         RS_PreviewActionInterface::trigger();
47
48         RS_Creation creation(container, graphicView);
49         RS_Entity * e = creation.createParallel(coord, distance, number, entity);
50
51         if (e == NULL)
52                 RS_DEBUG->print("RS_ActionDrawLineParallel::trigger: No parallels added\n");
53 }
54
55 void RS_ActionDrawLineParallel::mouseMoveEvent(QMouseEvent * e)
56 {
57         RS_DEBUG->print("RS_ActionDrawLineParallel::mouseMoveEvent begin");
58         coord = Vector(graphicView->toGraphX(e->x()), graphicView->toGraphY(e->y()));
59         entity = catchEntity(e, RS2::ResolveAll);
60
61         switch (getStatus())
62         {
63         case SetEntity:
64         {
65                 deletePreview();
66                 clearPreview();
67                 RS_Creation creation(preview, NULL, false);
68                 creation.createParallel(coord, distance, number, entity);
69                 drawPreview();
70         }
71         break;
72
73         default:
74                 break;
75         }
76
77         RS_DEBUG->print("RS_ActionDrawLineParallel::mouseMoveEvent end");
78 }
79
80 void RS_ActionDrawLineParallel::mouseReleaseEvent(QMouseEvent * e)
81 {
82 //      if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
83         if (e->button() == Qt::RightButton)
84                 init(getStatus() - 1);
85         else
86                 trigger();
87 }
88
89 void RS_ActionDrawLineParallel::updateMouseButtonHints()
90 {
91         if (RS_DIALOGFACTORY != NULL)
92         {
93                 switch (getStatus())
94                 {
95                 case SetEntity:
96                         RS_DIALOGFACTORY->updateMouseWidget(
97                                 tr("Specify Distance <%1> or select entity or [%2]")
98                                 .arg(distance).arg(RS_COMMANDS->command("through")),
99                                 tr("Cancel"));
100                         break;
101
102                 case SetNumber:
103                         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter number:"), "");
104                         break;
105
106                 default:
107                         RS_DIALOGFACTORY->updateMouseWidget("", "");
108                         break;
109                 }
110         }
111 }
112
113 void RS_ActionDrawLineParallel::showOptions()
114 {
115         RS_ActionInterface::showOptions();
116
117         if (RS_DIALOGFACTORY != NULL)
118                 RS_DIALOGFACTORY->requestOptions(this, true);
119
120         updateMouseButtonHints();
121 }
122
123 void RS_ActionDrawLineParallel::hideOptions()
124 {
125         RS_ActionInterface::hideOptions();
126
127         if (RS_DIALOGFACTORY != NULL)
128                 RS_DIALOGFACTORY->requestOptions(this, false);
129 }
130
131 void RS_ActionDrawLineParallel::commandEvent(RS_CommandEvent * e)
132 {
133         QString c = e->getCommand().toLower();
134
135         if (checkCommand("help", c))
136         {
137                 if (RS_DIALOGFACTORY != NULL)
138                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
139                                 + getAvailableCommands().join(", "));
140
141                 return;
142         }
143
144         switch (getStatus())
145         {
146         case SetEntity:
147                 if (checkCommand("through", c))
148                 {
149                         finish();
150                         graphicView->setCurrentAction(new RS_ActionDrawLineParallelThrough(*container,
151                                         *graphicView));
152                 }
153                 else if (checkCommand("number", c))
154                 {
155                         deleteSnapper();
156                         deletePreview();
157                         clearPreview();
158                         setStatus(SetNumber);
159                 }
160                 else
161                 {
162                         bool ok;
163                         double d = RS_Math::eval(c, &ok);
164
165                         if (ok && d > 1.0e-10)
166                                 distance = d;
167                         else    if (RS_DIALOGFACTORY != NULL)
168                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
169
170
171                         if (RS_DIALOGFACTORY != NULL)
172                                 RS_DIALOGFACTORY->requestOptions(this, true, true);
173
174                         updateMouseButtonHints();
175                         //setStatus(SetEntity);
176                 }
177                 break;
178
179         case SetNumber:
180         {
181                 bool ok;
182                 int n = c.toInt(&ok);
183
184                 if (ok)
185                 {
186                         if (n > 0 && n < 100)
187                                 number = n;
188                         else    if (RS_DIALOGFACTORY != NULL)
189                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. Try 1..99"));
190
191                 }
192                 else    if (RS_DIALOGFACTORY != NULL)
193                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
194
195
196                 if (RS_DIALOGFACTORY != NULL)
197                         RS_DIALOGFACTORY->requestOptions(this, true, true);
198
199                 setStatus(SetEntity);
200         }
201         break;
202
203         default:
204                 break;
205         }
206 }
207
208 QStringList RS_ActionDrawLineParallel::getAvailableCommands()
209 {
210         QStringList cmd;
211
212         switch (getStatus())
213         {
214         case SetEntity:
215                 cmd += command("number");
216                 cmd += command("through");
217                 break;
218
219         default:
220                 break;
221         }
222
223         return cmd;
224 }
225
226 void RS_ActionDrawLineParallel::updateMouseCursor()
227 {
228         graphicView->setMouseCursor(RS2::CadCursor);
229 }
230
231 void RS_ActionDrawLineParallel::updateToolBar()
232 {
233         if (RS_DIALOGFACTORY != NULL)
234                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
235 }
236
237 double RS_ActionDrawLineParallel::getDistance()
238 {
239         return distance;
240 }
241
242 void RS_ActionDrawLineParallel::setDistance(double d)
243 {
244         distance = d;
245 }
246
247 int RS_ActionDrawLineParallel::getNumber()
248 {
249         return number;
250 }
251
252 void RS_ActionDrawLineParallel::setNumber(int n)
253 {
254         number = n;
255 }
256