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