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