]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawtext.cpp
55b9ae64587c2760e3763b2523013e46fc3152c2
[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_dialogfactory.h"
20 #include "rs_graphicview.h"
21 #include "rs_preview.h"
22
23 RS_ActionDrawText::RS_ActionDrawText(RS_EntityContainer & container, 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                 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 RS_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 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                         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 != NULL)
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 RS_ActionDrawText::getAvailableCommands()
244 {
245         QStringList cmd;
246
247         if (getStatus() == SetPos)
248                 cmd += command("text");
249
250         return cmd;
251 }
252
253 void RS_ActionDrawText::showOptions()
254 {
255         RS_ActionInterface::showOptions();
256
257         if (RS_DIALOGFACTORY != NULL)
258                 RS_DIALOGFACTORY->requestOptions(this, true, true);
259 }
260
261 void RS_ActionDrawText::hideOptions()
262 {
263         RS_ActionInterface::hideOptions();
264
265         if (RS_DIALOGFACTORY != NULL)
266                 RS_DIALOGFACTORY->requestOptions(this, false);
267 }
268
269 void RS_ActionDrawText::updateMouseButtonHints()
270 {
271         if (RS_DIALOGFACTORY == NULL)
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 RS_ActionDrawText::updateMouseCursor()
291 {
292         graphicView->setMouseCursor(RS2::CadCursor);
293 }
294
295 void RS_ActionDrawText::updateToolBar()
296 {
297         if (RS_DIALOGFACTORY == NULL)
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 RS_ActionDrawText::setText(const QString & t)
313 {
314         data.text = t;
315         textChanged = true;
316 }
317
318 QString RS_ActionDrawText::getText()
319 {
320         return data.text;
321 }
322
323 void RS_ActionDrawText::setAngle(double a)
324 {
325         data.angle = a;
326         textChanged = true;
327 }
328
329 double RS_ActionDrawText::getAngle()
330 {
331         return data.angle;
332 }
333