~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actiondrawtext.cpp

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
9
** This file may be distributed and/or modified under the terms of the
 
10
** GNU General Public License version 2 as published by the Free Software
 
11
** Foundation and appearing in the file gpl-2.0.txt included in the
 
12
** packaging of this file.
 
13
**
 
14
** This program is distributed in the hope that it will be useful,
 
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
** GNU General Public License for more details.
 
18
**
 
19
** You should have received a copy of the GNU General Public License
 
20
** along with this program; if not, write to the Free Software
 
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
#include "rs_actiondrawtext.h"
 
28
 
 
29
#include <QAction>
 
30
#include "rs_dialogfactory.h"
 
31
#include "rs_graphicview.h"
 
32
#include "rs_commandevent.h"
 
33
 
 
34
 
 
35
 
 
36
RS_ActionDrawText::RS_ActionDrawText(RS_EntityContainer& container,
 
37
                                     RS_GraphicView& graphicView)
 
38
        :RS_PreviewActionInterface("Draw Text",
 
39
                           container, graphicView) {
 
40
 
 
41
    //text = NULL;
 
42
    pos = RS_Vector(false);
 
43
    secPos = RS_Vector(false);
 
44
    textChanged = true;
 
45
}
 
46
 
 
47
 
 
48
QAction* RS_ActionDrawText::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
 
49
        // tr("Text")
 
50
    QAction* action = new QAction(tr("&Text"),  NULL);
 
51
        action->setIcon(QIcon(":/extui/menutext.png"));
 
52
    //action->zetStatusTip(tr("Draw Text Entities"));
 
53
    return action;
 
54
}
 
55
 
 
56
 
 
57
void RS_ActionDrawText::init(int status) {
 
58
    RS_ActionInterface::init(status);
 
59
    if (RS_DIALOGFACTORY!=NULL) {
 
60
 
 
61
        switch (status) {
 
62
        case ShowDialog: {
 
63
                reset();
 
64
 
 
65
                RS_Text tmp(NULL, data);
 
66
                if (RS_DIALOGFACTORY->requestTextDialog(&tmp)) {
 
67
                    data = tmp.getData();
 
68
                    setStatus(SetPos);
 
69
                    showOptions();
 
70
                } else {
 
71
                    hideOptions();
 
72
                    setFinished();
 
73
                }
 
74
            }
 
75
            break;
 
76
 
 
77
        case SetPos:
 
78
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
79
            deletePreview();
 
80
            preview->setVisible(true);
 
81
            preparePreview();
 
82
            break;
 
83
 
 
84
        default:
 
85
            break;
 
86
        }
 
87
    }
 
88
}
 
89
 
 
90
 
 
91
 
 
92
void RS_ActionDrawText::reset() {
 
93
    data = RS_TextData(RS_Vector(0.0,0.0), RS_Vector(0.0,0.0),
 
94
                       1.0, 1.0,
 
95
                       RS_TextData::VABaseline,
 
96
                       RS_TextData::HALeft,
 
97
                       RS_TextData::None,
 
98
                       data.text,
 
99
                       "standard",
 
100
                       0.0,
 
101
                       RS2::Update);
 
102
}
 
103
 
 
104
 
 
105
 
 
106
void RS_ActionDrawText::trigger() {
 
107
 
 
108
    RS_DEBUG->print("RS_ActionDrawText::trigger()");
 
109
 
 
110
    if (pos.valid) {
 
111
        deletePreview();
 
112
 
 
113
        RS_Text* text = new RS_Text(container, data);
 
114
        text->update();
 
115
        container->addEntity(text);
 
116
 
 
117
        if (document) {
 
118
            document->startUndoCycle();
 
119
            document->addUndoable(text);
 
120
            document->endUndoCycle();
 
121
        }
 
122
 
 
123
                graphicView->redraw(RS2::RedrawDrawing);
 
124
 
 
125
        textChanged = true;
 
126
        secPos = RS_Vector(false);
 
127
        setStatus(SetPos);
 
128
    }
 
129
}
 
130
 
 
131
 
 
132
void RS_ActionDrawText::preparePreview() {
 
133
    if (data.halign == RS_TextData::HAFit || data.halign == RS_TextData::HAAligned) {
 
134
        if (secPos.valid) {
 
135
            RS_Line* text = new RS_Line(pos, secPos);
 
136
            preview->addEntity(text);
 
137
        }
 
138
    } else {
 
139
        data.insertionPoint = pos;
 
140
        RS_Text* text = new RS_Text(preview, data);
 
141
        text->update();
 
142
        preview->addEntity(text);
 
143
    }
 
144
    textChanged = false;
 
145
}
 
146
 
 
147
 
 
148
void RS_ActionDrawText::mouseMoveEvent(QMouseEvent* e) {
 
149
    RS_DEBUG->print("RS_ActionDrawText::mouseMoveEvent begin");
 
150
 
 
151
    if (getStatus()==SetPos) {
 
152
        RS_Vector mouse = snapPoint(e);
 
153
        RS_Vector mov = mouse-pos;
 
154
        pos = mouse;
 
155
        if (textChanged || pos.valid == false || preview->isEmpty()) {
 
156
            deletePreview();
 
157
            preparePreview();
 
158
        } else {
 
159
            preview->move(mov);
 
160
            preview->setVisible(true);
 
161
        }
 
162
        drawPreview();
 
163
    } else if (getStatus()==SetSecPos) {
 
164
        secPos = snapPoint(e);
 
165
        deletePreview();
 
166
        preparePreview();
 
167
        drawPreview();
 
168
    }
 
169
 
 
170
    RS_DEBUG->print("RS_ActionDrawText::mouseMoveEvent end");
 
171
}
 
172
 
 
173
 
 
174
 
 
175
void RS_ActionDrawText::mouseReleaseEvent(QMouseEvent* e) {
 
176
    if (e->button()==Qt::LeftButton) {
 
177
        RS_CoordinateEvent ce(snapPoint(e));
 
178
        coordinateEvent(&ce);
 
179
    } else if (e->button()==Qt::RightButton) {
 
180
        deletePreview();
 
181
        //init(getStatus()-1);
 
182
        finish(false);
 
183
    }
 
184
}
 
185
 
 
186
 
 
187
 
 
188
void RS_ActionDrawText::coordinateEvent(RS_CoordinateEvent* e) {
 
189
    if (e==NULL) {
 
190
        return;
 
191
    }
 
192
 
 
193
    RS_Vector mouse = e->getCoordinate();
 
194
 
 
195
    switch (getStatus()) {
 
196
    case ShowDialog:
 
197
        break;
 
198
 
 
199
    case SetPos:
 
200
        data.insertionPoint = mouse;
 
201
        if (data.halign == RS_TextData::HAFit || data.halign == RS_TextData::HAAligned)
 
202
            setStatus(SetSecPos);
 
203
        else
 
204
            trigger();
 
205
        break;
 
206
 
 
207
    case SetSecPos:
 
208
        data.secondPoint = mouse;
 
209
        trigger();
 
210
        break;
 
211
 
 
212
    default:
 
213
        break;
 
214
    }
 
215
}
 
216
 
 
217
 
 
218
 
 
219
void RS_ActionDrawText::commandEvent(RS_CommandEvent* e) {
 
220
    QString c = e->getCommand().toLower();
 
221
 
 
222
    if (checkCommand("help", c)) {
 
223
        if (RS_DIALOGFACTORY!=NULL) {
 
224
            RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
 
225
                                             + getAvailableCommands().join(", "));
 
226
        }
 
227
        return;
 
228
    }
 
229
 
 
230
    switch (getStatus()) {
 
231
    case SetPos:
 
232
        if (checkCommand("text", c)) {
 
233
            deletePreview();
 
234
            graphicView->disableCoordinateInput();
 
235
            setStatus(SetText);
 
236
        }
 
237
        break;
 
238
 
 
239
    case SetText: {
 
240
            setText(e->getCommand());
 
241
            if (RS_DIALOGFACTORY!=NULL) {
 
242
                RS_DIALOGFACTORY->requestOptions(this, true, true);
 
243
            }
 
244
            graphicView->enableCoordinateInput();
 
245
            setStatus(SetPos);
 
246
        }
 
247
        break;
 
248
 
 
249
    default:
 
250
        break;
 
251
    }
 
252
}
 
253
 
 
254
 
 
255
 
 
256
QStringList RS_ActionDrawText::getAvailableCommands() {
 
257
    QStringList cmd;
 
258
    if (getStatus()==SetPos) {
 
259
        cmd += command("text");
 
260
    }
 
261
    return cmd;
 
262
}
 
263
 
 
264
 
 
265
 
 
266
void RS_ActionDrawText::showOptions() {
 
267
    RS_ActionInterface::showOptions();
 
268
 
 
269
    if (RS_DIALOGFACTORY!=NULL) {
 
270
        RS_DIALOGFACTORY->requestOptions(this, true, true);
 
271
    }
 
272
}
 
273
 
 
274
 
 
275
 
 
276
void RS_ActionDrawText::hideOptions() {
 
277
    RS_ActionInterface::hideOptions();
 
278
 
 
279
    if (RS_DIALOGFACTORY!=NULL) {
 
280
        RS_DIALOGFACTORY->requestOptions(this, false);
 
281
    }
 
282
}
 
283
 
 
284
 
 
285
 
 
286
void RS_ActionDrawText::updateMouseButtonHints() {
 
287
    if (RS_DIALOGFACTORY!=NULL) {
 
288
        switch (getStatus()) {
 
289
        case SetPos:
 
290
            RS_DIALOGFACTORY->updateMouseWidget(tr("Specify insertion point"),
 
291
                                                tr("Cancel"));
 
292
            break;
 
293
        case SetSecPos:
 
294
            RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second point"),
 
295
                                                tr("Cancel"));
 
296
            break;
 
297
        case ShowDialog:
 
298
        case SetText:
 
299
            RS_DIALOGFACTORY->updateMouseWidget(tr("Enter text:"),
 
300
                                                tr("Back"));
 
301
            break;
 
302
        default:
 
303
            RS_DIALOGFACTORY->updateMouseWidget("", "");
 
304
            break;
 
305
        }
 
306
    }
 
307
}
 
308
 
 
309
 
 
310
 
 
311
void RS_ActionDrawText::updateMouseCursor() {
 
312
    graphicView->setMouseCursor(RS2::CadCursor);
 
313
}
 
314
 
 
315
//void RS_ActionDrawText::updateToolBar() {
 
316
//    if(isFinished()) {
 
317
//        if (RS_DIALOGFACTORY!=NULL) {
 
318
//            RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
319
//        }
 
320
//    }
 
321
//    /*
 
322
//    //not needed any more with new snap
 
323
//    return;
 
324
//    if (RS_DIALOGFACTORY!=NULL) {
 
325
//        switch (getStatus()) {
 
326
//        case SetPos:
 
327
//            RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
 
328
//            break;
 
329
//        default:
 
330
//            RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
331
//            break;
 
332
//        }
 
333
//    }
 
334
//    */
 
335
//}
 
336
 
 
337
 
 
338
 
 
339
void RS_ActionDrawText::setText(const QString& t) {
 
340
    data.text = t;
 
341
    textChanged = true;
 
342
}
 
343
 
 
344
 
 
345
 
 
346
QString RS_ActionDrawText::getText() {
 
347
    return data.text;
 
348
}
 
349
 
 
350
 
 
351
void RS_ActionDrawText::setAngle(double a) {
 
352
    data.angle = a;
 
353
    textChanged = true;
 
354
}
 
355
 
 
356
double RS_ActionDrawText::getAngle() {
 
357
    return data.angle;
 
358
}
 
359
 
 
360
 
 
361
// EOF