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