]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlineparallel.cpp
Last checkin before major refactor...
[architektonas] / src / actions / actiondrawlineparallel.cpp
1 // 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 "actiondrawlineparallel.h"
16
17 #include "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 ActionDrawLineParallel::ActionDrawLineParallel(RS_EntityContainer & container,
26         GraphicView & graphicView): ActionInterface("Draw Parallels",
27         container, graphicView)
28 {
29         parallel = NULL;
30         entity = NULL;
31         distance = 1.0;
32         number = 1;
33         coord = Vector(false);
34 }
35
36 ActionDrawLineParallel::~ActionDrawLineParallel()
37 {
38 }
39
40 /*virtual*/ RS2::ActionType ActionDrawLineParallel::rtti()
41 {
42         return RS2::ActionDrawLineParallel;
43 }
44
45 void ActionDrawLineParallel::trigger()
46 {
47         ActionInterface::trigger();
48
49         RS_Creation creation(container, graphicView);
50         RS_Entity * e = creation.createParallel(coord, distance, number, entity);
51
52         if (!e)
53                 RS_DEBUG->print("ActionDrawLineParallel::trigger: No parallels added\n");
54 }
55
56 void ActionDrawLineParallel::mouseMoveEvent(QMouseEvent * e)
57 {
58         RS_DEBUG->print("ActionDrawLineParallel::mouseMoveEvent begin");
59         coord = Vector(graphicView->toGraphX(e->x()), graphicView->toGraphY(e->y()));
60         entity = catchEntity(e, RS2::ResolveAll);
61
62         switch (getStatus())
63         {
64         case SetEntity:
65         {
66                 deletePreview();
67                 clearPreview();
68 //              RS_Creation creation(preview, NULL, false);
69 //              creation.createParallel(coord, distance, number, entity);
70                 drawPreview();
71         }
72         break;
73
74         default:
75                 break;
76         }
77
78         RS_DEBUG->print("ActionDrawLineParallel::mouseMoveEvent end");
79 }
80
81 void ActionDrawLineParallel::mouseReleaseEvent(QMouseEvent * e)
82 {
83         if (e->button() == Qt::RightButton)
84                 init(getStatus() - 1);
85         else
86                 trigger();
87 }
88
89 void 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 ActionDrawLineParallel::showOptions()
114 {
115         ActionInterface::showOptions();
116
117         if (RS_DIALOGFACTORY != NULL)
118                 RS_DIALOGFACTORY->requestOptions(this, true);
119
120         updateMouseButtonHints();
121 }
122
123 void ActionDrawLineParallel::hideOptions()
124 {
125         ActionInterface::hideOptions();
126
127         if (RS_DIALOGFACTORY != NULL)
128                 RS_DIALOGFACTORY->requestOptions(this, false);
129 }
130
131 void 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 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 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 ActionDrawLineParallel::updateMouseCursor()
227 {
228         graphicView->setMouseCursor(RS2::CadCursor);
229 }
230
231 void ActionDrawLineParallel::updateToolBar()
232 {
233         if (RS_DIALOGFACTORY != NULL)
234                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
235 }
236
237 double ActionDrawLineParallel::getDistance()
238 {
239         return distance;
240 }
241
242 void ActionDrawLineParallel::setDistance(double d)
243 {
244         distance = d;
245 }
246
247 int ActionDrawLineParallel::getNumber()
248 {
249         return number;
250 }
251
252 void ActionDrawLineParallel::setNumber(int n)
253 {
254         number = n;
255 }