~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actionblocksinsert.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_actionblocksinsert.h"
 
28
 
 
29
#include <QAction>
 
30
#include "rs_graphic.h"
 
31
#include "rs_graphicview.h"
 
32
#include "rs_dialogfactory.h"
 
33
#include "rs_commandevent.h"
 
34
#include "rs_creation.h"
 
35
 
 
36
/**
 
37
 * Constructor.
 
38
 */
 
39
RS_ActionBlocksInsert::RS_ActionBlocksInsert(RS_EntityContainer& container,
 
40
        RS_GraphicView& graphicView)
 
41
        :RS_PreviewActionInterface("Blocks Insert",
 
42
                           container, graphicView) {
 
43
    block = NULL;
 
44
    reset();    // init data Member
 
45
    lastStatus = SetUndefined;
 
46
}
 
47
 
 
48
 
 
49
 
 
50
RS_ActionBlocksInsert::~RS_ActionBlocksInsert() {}
 
51
 
 
52
QAction* RS_ActionBlocksInsert::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
 
53
        // tr("Insert Block")
 
54
    QAction* action = new QAction(tr("&Insert Block"),  NULL);
 
55
    //action->zetStatusTip(tr("Insert Block"));
 
56
        action->setIcon(QIcon(":/ui/blockinsert.png"));
 
57
    return action;
 
58
}
 
59
 
 
60
void RS_ActionBlocksInsert::init(int status) {
 
61
    RS_PreviewActionInterface::init(status);
 
62
 
 
63
    reset();
 
64
 
 
65
    if (graphic!=NULL) {
 
66
        block = graphic->getActiveBlock();
 
67
        if (block!=NULL) {
 
68
            data.name = block->getName();
 
69
        } else {
 
70
            finish(false);
 
71
        }
 
72
    }
 
73
}
 
74
 
 
75
 
 
76
 
 
77
void RS_ActionBlocksInsert::reset() {
 
78
    data = RS_InsertData("",
 
79
                         RS_Vector(0.0,0.0),
 
80
                         RS_Vector(1.0,1.0),
 
81
                         0.0,
 
82
                         1, 1,
 
83
                         RS_Vector(1.0,1.0),
 
84
                         NULL,
 
85
                         RS2::Update);
 
86
}
 
87
 
 
88
 
 
89
 
 
90
void RS_ActionBlocksInsert::trigger() {
 
91
    deletePreview();
 
92
 
 
93
    //RS_Modification m(*container, graphicView);
 
94
    //m.paste(data.insertionPoint);
 
95
    //std::cout << *RS_Clipboard::instance();
 
96
 
 
97
    if (block!=NULL) {
 
98
        RS_Creation creation(container, graphicView);
 
99
                data.updateMode = RS2::Update;
 
100
        creation.createInsert(data);
 
101
    }
 
102
 
 
103
        graphicView->redraw(RS2::RedrawDrawing); 
 
104
 
 
105
    //finish();
 
106
}
 
107
 
 
108
 
 
109
void RS_ActionBlocksInsert::mouseMoveEvent(QMouseEvent* e) {
 
110
    switch (getStatus()) {
 
111
    case SetTargetPoint:
 
112
        data.insertionPoint = snapPoint(e);
 
113
 
 
114
        if (block!=NULL) {
 
115
            deletePreview();
 
116
            //preview->addAllFrom(*block);
 
117
            //preview->move(data.insertionPoint);
 
118
            RS_Creation creation(preview, NULL, false);
 
119
                        // Create insert as preview only
 
120
                        data.updateMode = RS2::PreviewUpdate;
 
121
            creation.createInsert(data);
 
122
            drawPreview();
 
123
        }
 
124
        break;
 
125
 
 
126
    default:
 
127
        break;
 
128
    }
 
129
}
 
130
 
 
131
 
 
132
 
 
133
void RS_ActionBlocksInsert::mouseReleaseEvent(QMouseEvent* e) {
 
134
    if (e->button()==Qt::LeftButton) {
 
135
        RS_CoordinateEvent ce(snapPoint(e));
 
136
        coordinateEvent(&ce);
 
137
    } else if (e->button()==Qt::RightButton) {
 
138
        init(getStatus()-1);
 
139
    }
 
140
}
 
141
 
 
142
 
 
143
 
 
144
void RS_ActionBlocksInsert::coordinateEvent(RS_CoordinateEvent* e) {
 
145
    if (e==NULL) {
 
146
        return;
 
147
    }
 
148
 
 
149
    data.insertionPoint = e->getCoordinate();
 
150
    trigger();
 
151
}
 
152
 
 
153
 
 
154
 
 
155
void RS_ActionBlocksInsert::commandEvent(RS_CommandEvent* e) {
 
156
    QString c = e->getCommand().toLower();
 
157
 
 
158
    if (checkCommand("help", c)) {
 
159
        RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
 
160
                                         + getAvailableCommands().join(", "));
 
161
        return;
 
162
    }
 
163
 
 
164
    switch (getStatus()) {
 
165
    case SetTargetPoint:
 
166
        if (checkCommand("angle", c)) {
 
167
            deletePreview();
 
168
            lastStatus = (Status)getStatus();
 
169
            setStatus(SetAngle);
 
170
        } else if (checkCommand("factor", c)) {
 
171
            deletePreview();
 
172
            lastStatus = (Status)getStatus();
 
173
            setStatus(SetFactor);
 
174
        } else if (checkCommand("columns", c)) {
 
175
            deletePreview();
 
176
            lastStatus = (Status)getStatus();
 
177
            setStatus(SetColumns);
 
178
        } else if (checkCommand("rows", c)) {
 
179
            deletePreview();
 
180
            lastStatus = (Status)getStatus();
 
181
            setStatus(SetRows);
 
182
        } else if (checkCommand("columnspacing", c)) {
 
183
            deletePreview();
 
184
            lastStatus = (Status)getStatus();
 
185
            setStatus(SetColumnSpacing);
 
186
        } else if (checkCommand("rowspacing", c)) {
 
187
            deletePreview();
 
188
            lastStatus = (Status)getStatus();
 
189
            setStatus(SetRowSpacing);
 
190
        }
 
191
        break;
 
192
 
 
193
    case SetAngle: {
 
194
            bool ok;
 
195
            double a = RS_Math::eval(c, &ok);
 
196
            if (ok==true) {
 
197
                data.angle = RS_Math::deg2rad(a);
 
198
            } else {
 
199
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
200
            }
 
201
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
202
            setStatus(lastStatus);
 
203
        }
 
204
        break;
 
205
 
 
206
    case SetFactor: {
 
207
            bool ok;
 
208
            double f = RS_Math::eval(c, &ok);
 
209
            if (ok==true) {
 
210
                setFactor(f);
 
211
            } else {
 
212
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
213
            }
 
214
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
215
            setStatus(lastStatus);
 
216
        }
 
217
        break;
 
218
 
 
219
    case SetColumns: {
 
220
            bool ok;
 
221
            int cols = (int)RS_Math::eval(c, &ok);
 
222
            if (ok==true) {
 
223
                data.cols = cols;
 
224
            } else {
 
225
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
226
            }
 
227
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
228
            setStatus(lastStatus);
 
229
        }
 
230
        break;
 
231
 
 
232
    case SetRows: {
 
233
            bool ok;
 
234
            int rows = (int)RS_Math::eval(c, &ok);
 
235
            if (ok==true) {
 
236
                data.rows = rows;
 
237
            } else {
 
238
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
239
            }
 
240
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
241
            setStatus(lastStatus);
 
242
        }
 
243
        break;
 
244
 
 
245
    case SetColumnSpacing: {
 
246
            bool ok;
 
247
            double cs = (int)RS_Math::eval(c, &ok);
 
248
            if (ok==true) {
 
249
                data.spacing.x = cs;
 
250
            } else {
 
251
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
252
            }
 
253
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
254
            setStatus(lastStatus);
 
255
        }
 
256
        break;
 
257
 
 
258
    case SetRowSpacing: {
 
259
            bool ok;
 
260
            int rs = (int)RS_Math::eval(c, &ok);
 
261
            if (ok==true) {
 
262
                data.spacing.y = rs;
 
263
            } else {
 
264
                RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
 
265
            }
 
266
            RS_DIALOGFACTORY->requestOptions(this, true, true);
 
267
            setStatus(lastStatus);
 
268
        }
 
269
        break;
 
270
 
 
271
    default:
 
272
        break;
 
273
    }
 
274
}
 
275
 
 
276
 
 
277
 
 
278
QStringList RS_ActionBlocksInsert::getAvailableCommands() {
 
279
    QStringList cmd;
 
280
 
 
281
    switch (getStatus()) {
 
282
    case SetTargetPoint:
 
283
        cmd += command("angle");
 
284
        cmd += command("factor");
 
285
        ;
 
286
        cmd += command("columns");
 
287
        cmd += command("rows");
 
288
        cmd += command("columnspacing");
 
289
        cmd += command("rowspacing");
 
290
        break;
 
291
    default:
 
292
        break;
 
293
    }
 
294
 
 
295
    return cmd;
 
296
}
 
297
 
 
298
 
 
299
void RS_ActionBlocksInsert::showOptions() {
 
300
    RS_ActionInterface::showOptions();
 
301
 
 
302
    RS_DIALOGFACTORY->requestOptions(this, true);
 
303
}
 
304
 
 
305
 
 
306
 
 
307
void RS_ActionBlocksInsert::hideOptions() {
 
308
    RS_ActionInterface::hideOptions();
 
309
 
 
310
    RS_DIALOGFACTORY->requestOptions(this, false);
 
311
}
 
312
 
 
313
 
 
314
void RS_ActionBlocksInsert::updateMouseButtonHints() {
 
315
    switch (getStatus()) {
 
316
    case SetTargetPoint:
 
317
        RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
 
318
                                            tr("Cancel"));
 
319
        break;
 
320
    case SetAngle:
 
321
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"),
 
322
                                            "");
 
323
        break;
 
324
    case SetFactor:
 
325
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter factor:"),
 
326
                                            "");
 
327
        break;
 
328
    case SetColumns:
 
329
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter columns:"),
 
330
                                            "");
 
331
        break;
 
332
    case SetRows:
 
333
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter rows:"),
 
334
                                            "");
 
335
        break;
 
336
    case SetColumnSpacing:
 
337
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter column spacing:"),
 
338
                                            "");
 
339
        break;
 
340
    case SetRowSpacing:
 
341
        RS_DIALOGFACTORY->updateMouseWidget(tr("Enter row spacing:"),
 
342
                                            "");
 
343
        break;
 
344
    default:
 
345
        RS_DIALOGFACTORY->updateMouseWidget("", "");
 
346
        break;
 
347
    }
 
348
}
 
349
 
 
350
 
 
351
 
 
352
void RS_ActionBlocksInsert::updateMouseCursor() {
 
353
    graphicView->setMouseCursor(RS2::CadCursor);
 
354
}
 
355
 
 
356
 
 
357
 
 
358
void RS_ActionBlocksInsert::updateToolBar() {
 
359
    //not needed any more
 
360
    return;
 
361
    if (!isFinished()) {
 
362
        RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
 
363
    } else {
 
364
        RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
365
    }
 
366
}
 
367
 
 
368
 
 
369
// EOF