]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawtext.cpp
Last checkin before major refactor...
[architektonas] / src / actions / rs_actiondrawtext.cpp
1 // rs_actiondrawtext.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/28/2010  Added this text. :-)
13 // JLH  05/28/2010  Fixed crashing bug when right-clicking out of a text
14 //                  preview.
15 //
16
17 #include "rs_actiondrawtext.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
23
24 RS_ActionDrawText::RS_ActionDrawText(RS_EntityContainer & container, GraphicView & graphicView): RS_PreviewActionInterface("Draw Text", container, graphicView)
25 {
26         pos = Vector(false);
27         textChanged = true;
28 }
29
30 RS_ActionDrawText::~RS_ActionDrawText()
31 {
32 }
33
34 /*virtual*/ RS2::ActionType RS_ActionDrawText::rtti()
35 {
36         return RS2::ActionDrawText;
37 }
38
39 void RS_ActionDrawText::init(int status)
40 {
41         RS_ActionInterface::init(status);
42
43         if (RS_DIALOGFACTORY == NULL)
44                 return;
45
46         switch (status)
47         {
48         case ShowDialog:
49         {
50                 clearPreview();
51                 reset();
52
53                 RS_Text tmp(NULL, data);
54
55                 if (RS_DIALOGFACTORY->requestTextDialog(&tmp))
56                 {
57                         data = tmp.getData();
58                         preparePreview();
59                         preview->setVisible(false);
60
61                         setStatus(SetPos);
62                         showOptions();
63                 }
64                 else
65                 {
66                         hideOptions();
67                         finish();
68                 }
69
70                 graphicView->redraw();
71         }
72         break;
73
74         case SetPos:
75                 RS_DIALOGFACTORY->requestOptions(this, true, true);
76                 break;
77
78         default:
79                 break;
80         }
81 }
82
83 void RS_ActionDrawText::reset()
84 {
85         data = RS_TextData(Vector(0.0, 0.0), 1.0, 100.0, RS2::VAlignTop,
86                         RS2::HAlignLeft, RS2::LeftToRight, RS2::Exact, 1.0, data.text,
87                         "standard", 0.0, RS2::Update);
88 }
89
90 void RS_ActionDrawText::trigger()
91 {
92         RS_DEBUG->print("RS_ActionDrawText::trigger()");
93
94         if (pos.valid)
95         {
96 //removing *this* causes previews not to be drawn correctly...
97                 deletePreview();
98                 clearPreview();
99                 deleteSnapper();
100
101                 RS_Text * text = new RS_Text(container, data);
102                 text->update();
103                 container->addEntity(text);
104
105                 if (document)
106                 {
107                         document->startUndoCycle();
108                         document->addUndoable(text);
109                         document->endUndoCycle();
110                 }
111
112 //Can't be done...
113 //              graphicView->drawEntity(text);
114
115                 textChanged = true;
116                 setStatus(SetPos);
117         }
118 }
119
120 void RS_ActionDrawText::preparePreview()
121 {
122         clearPreview();
123         data.insertionPoint = Vector(0.0, 0.0);
124         RS_Text * text = new RS_Text(preview, data);
125         text->update();
126         //text->setVisible(false);
127         preview->addEntity(text);
128         textChanged = false;
129 }
130
131 void RS_ActionDrawText::mouseMoveEvent(QMouseEvent * e)
132 {
133         RS_DEBUG->print("RS_ActionDrawText::mouseMoveEvent begin");
134
135         if (getStatus() == SetPos)
136         {
137                 Vector mouse = snapPoint(e);
138                 pos = mouse;
139
140 //without this, nothing is shown...
141 //Which shows that the preview rendering pipeline is seriously fucked up.
142 //Need to fix this shit!
143                 deletePreview();
144
145                 if (textChanged)
146                         preparePreview();
147
148                 if (!preview->isVisible())
149                         preview->setVisible(true);
150
151                 offset = Vector(graphicView->toGuiDX(pos.x), -graphicView->toGuiDY(pos.y));
152                 drawPreview();
153         }
154
155         RS_DEBUG->print("RS_ActionDrawText::mouseMoveEvent end");
156 }
157
158 void RS_ActionDrawText::mouseReleaseEvent(QMouseEvent * e)
159 {
160         if (e->button() == Qt::LeftButton)
161         {
162                 Vector ce(snapPoint(e));
163                 coordinateEvent(&ce);
164         }
165         else if (e->button() == Qt::RightButton)
166         {
167 // The problem is that even though the preview entity has been deleted, the rendering
168 // pipeline still thinks it's valid (because it's not NULL).
169 //              deletePreview();
170 //              deleteSnapper();
171                 //init(getStatus()-1);
172 //Clear the preview entity so something else doesn't try to draw it after it's
173 //been deleted, then redraw the graphicView...
174                 clearPreview();
175                 graphicView->redraw();
176                 finish();
177         }
178 }
179
180 void RS_ActionDrawText::coordinateEvent(Vector * e)
181 {
182         if (e == NULL)
183                 return;
184
185         Vector mouse = *e;
186
187         switch (getStatus())
188         {
189         case ShowDialog:
190                 break;
191
192         case SetPos:
193                 data.insertionPoint = mouse;
194                 trigger();
195                 break;
196
197         default:
198                 break;
199         }
200 }
201
202 void RS_ActionDrawText::commandEvent(RS_CommandEvent * e)
203 {
204         QString c = e->getCommand().toLower();
205
206         if (checkCommand("help", c))
207         {
208                 if (RS_DIALOGFACTORY != NULL)
209                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
210                                 + getAvailableCommands().join(", "));
211                 return;
212         }
213
214         switch (getStatus())
215         {
216         case SetPos:
217
218                 if (checkCommand("text", c))
219                 {
220                         deleteSnapper();
221                         deletePreview();
222                         clearPreview();
223                         graphicView->disableCoordinateInput();
224                         setStatus(SetText);
225                 }
226
227                 break;
228
229         case SetText:
230                 setText(e->getCommand());
231
232                 if (RS_DIALOGFACTORY != NULL)
233                         RS_DIALOGFACTORY->requestOptions(this, true, true);
234
235                 graphicView->enableCoordinateInput();
236                 setStatus(SetPos);
237                 break;
238
239         default:
240                 break;
241         }
242 }
243
244 QStringList RS_ActionDrawText::getAvailableCommands()
245 {
246         QStringList cmd;
247
248         if (getStatus() == SetPos)
249                 cmd += command("text");
250
251         return cmd;
252 }
253
254 void RS_ActionDrawText::showOptions()
255 {
256         RS_ActionInterface::showOptions();
257
258         if (RS_DIALOGFACTORY != NULL)
259                 RS_DIALOGFACTORY->requestOptions(this, true, true);
260 }
261
262 void RS_ActionDrawText::hideOptions()
263 {
264         RS_ActionInterface::hideOptions();
265
266         if (RS_DIALOGFACTORY != NULL)
267                 RS_DIALOGFACTORY->requestOptions(this, false);
268 }
269
270 void RS_ActionDrawText::updateMouseButtonHints()
271 {
272         if (RS_DIALOGFACTORY == NULL)
273                 return;
274
275         switch (getStatus())
276         {
277         case SetPos:
278                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify insertion point"), tr("Cancel"));
279                 break;
280
281         case SetText:
282                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter text:"), tr("Back"));
283                 break;
284
285         default:
286                 RS_DIALOGFACTORY->updateMouseWidget("", "");
287                 break;
288         }
289 }
290
291 void RS_ActionDrawText::updateMouseCursor()
292 {
293         graphicView->setMouseCursor(RS2::CadCursor);
294 }
295
296 void RS_ActionDrawText::updateToolBar()
297 {
298         if (RS_DIALOGFACTORY == NULL)
299                 return;
300
301         switch (getStatus())
302         {
303         case SetPos:
304                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
305                 break;
306
307         default:
308                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
309                 break;
310         }
311 }
312
313 void RS_ActionDrawText::setText(const QString & t)
314 {
315         data.text = t;
316         textChanged = true;
317 }
318
319 QString RS_ActionDrawText::getText()
320 {
321         return data.text;
322 }
323
324 void RS_ActionDrawText::setAngle(double a)
325 {
326         data.angle = a;
327         textChanged = true;
328 }
329
330 double RS_ActionDrawText::getAngle()
331 {
332         return data.angle;
333 }
334