]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawtext.cpp
Last checkin before major refactor...
[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 // (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 "actiondrawtext.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
23
24 ActionDrawText::ActionDrawText(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw Text", container, graphicView)
25 {
26         pos = Vector(false);
27         textChanged = true;
28 }
29
30 ActionDrawText::~ActionDrawText()
31 {
32 }
33
34 /*virtual*/ RS2::ActionType ActionDrawText::rtti()
35 {
36         return RS2::ActionDrawText;
37 }
38
39 void ActionDrawText::init(int status)
40 {
41         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 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 ActionDrawText::trigger()
91 {
92         RS_DEBUG->print("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 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         preview->addEntity(text);
127         textChanged = false;*/
128 }
129
130 void ActionDrawText::mouseMoveEvent(QMouseEvent * e)
131 {
132         RS_DEBUG->print("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("ActionDrawText::mouseMoveEvent end");
155 }
156
157 void ActionDrawText::mouseReleaseEvent(QMouseEvent * e)
158 {
159         if (e->button() == Qt::LeftButton)
160         {
161                 Vector 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 ActionDrawText::coordinateEvent(Vector * e)
180 {
181         if (e == NULL)
182                 return;
183
184         Vector mouse = *e;
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 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                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
209                                 + getAvailableCommands().join(", "));
210                 return;
211         }
212
213         switch (getStatus())
214         {
215         case SetPos:
216
217                 if (checkCommand("text", c))
218                 {
219                         deleteSnapper();
220                         deletePreview();
221                         clearPreview();
222                         graphicView->disableCoordinateInput();
223                         setStatus(SetText);
224                 }
225
226                 break;
227
228         case SetText:
229                 setText(e->getCommand());
230
231                 if (RS_DIALOGFACTORY)
232                         RS_DIALOGFACTORY->requestOptions(this, true, true);
233
234                 graphicView->enableCoordinateInput();
235                 setStatus(SetPos);
236                 break;
237
238         default:
239                 break;
240         }
241 }
242
243 QStringList ActionDrawText::getAvailableCommands()
244 {
245         QStringList cmd;
246
247         if (getStatus() == SetPos)
248                 cmd += command("text");
249
250         return cmd;
251 }
252
253 void ActionDrawText::showOptions()
254 {
255         ActionInterface::showOptions();
256
257         if (RS_DIALOGFACTORY)
258                 RS_DIALOGFACTORY->requestOptions(this, true, true);
259 }
260
261 void ActionDrawText::hideOptions()
262 {
263         ActionInterface::hideOptions();
264
265         if (RS_DIALOGFACTORY)
266                 RS_DIALOGFACTORY->requestOptions(this, false);
267 }
268
269 void ActionDrawText::updateMouseButtonHints()
270 {
271         if (!RS_DIALOGFACTORY)
272                 return;
273
274         switch (getStatus())
275         {
276         case SetPos:
277                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify insertion point"), tr("Cancel"));
278                 break;
279
280         case SetText:
281                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter text:"), tr("Back"));
282                 break;
283
284         default:
285                 RS_DIALOGFACTORY->updateMouseWidget("", "");
286                 break;
287         }
288 }
289
290 void ActionDrawText::updateMouseCursor()
291 {
292         graphicView->setMouseCursor(RS2::CadCursor);
293 }
294
295 void ActionDrawText::updateToolBar()
296 {
297         if (!RS_DIALOGFACTORY)
298                 return;
299
300         switch (getStatus())
301         {
302         case SetPos:
303                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
304                 break;
305
306         default:
307                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
308                 break;
309         }
310 }
311
312 void ActionDrawText::setText(const QString & t)
313 {
314         data.text = t;
315         textChanged = true;
316 }
317
318 QString ActionDrawText::getText()
319 {
320         return data.text;
321 }
322
323 void ActionDrawText::setAngle(double a)
324 {
325         data.angle = a;
326         textChanged = true;
327 }
328
329 double ActionDrawText::getAngle()
330 {
331         return data.angle;
332 }