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