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