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