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