~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actiondefault.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_actiondefault.h"
 
28
 
 
29
#include "rs_dialogfactory.h"
 
30
#include "rs_graphicview.h"
 
31
#include "rs_commandevent.h"
 
32
#include "rs_modification.h"
 
33
#include "rs_selection.h"
 
34
#include "rs_overlaybox.h"
 
35
 
 
36
 
 
37
 
 
38
/**
 
39
 * Constructor.
 
40
 */
 
41
RS_ActionDefault::RS_ActionDefault(RS_EntityContainer& container,
 
42
                                   RS_GraphicView& graphicView)
 
43
    : RS_PreviewActionInterface("Default",
 
44
                                                                container, graphicView),
 
45
        v1(false),
 
46
        v2(false),
 
47
        restrBak(RS2::RestrictNothing)
 
48
{
 
49
 
 
50
    RS_DEBUG->print("RS_ActionDefault::RS_ActionDefault");
 
51
    RS_DEBUG->print("RS_ActionDefault::RS_ActionDefault: OK");
 
52
}
 
53
 
 
54
 
 
55
 
 
56
RS_ActionDefault::~RS_ActionDefault() {
 
57
}
 
58
 
 
59
 
 
60
 
 
61
QAction* RS_ActionDefault::createGUIAction(RS2::ActionType /*type*/,
 
62
                                           QObject* /*parent*/) {
 
63
 
 
64
    return NULL;
 
65
}
 
66
 
 
67
 
 
68
void RS_ActionDefault::init(int status) {
 
69
    RS_DEBUG->print("RS_ActionDefault::init");
 
70
    if(status==Neutral){
 
71
        deletePreview();
 
72
        deleteSnapper();
 
73
    }
 
74
    RS_PreviewActionInterface::init(status);
 
75
    v1 = v2 = RS_Vector(false);
 
76
    //    snapMode.clear();
 
77
    //    snapMode.restriction = RS2::RestrictNothing;
 
78
    //    restrBak = RS2::RestrictNothing;
 
79
    //        RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
80
 
 
81
    RS_DEBUG->print("RS_ActionDefault::init: OK");
 
82
}
 
83
 
 
84
 
 
85
 
 
86
void RS_ActionDefault::trigger() {
 
87
    RS_PreviewActionInterface::trigger();
 
88
 
 
89
}
 
90
 
 
91
void RS_ActionDefault::keyPressEvent(QKeyEvent* e) {
 
92
    //        std::cout<<"RS_ActionDefault::keyPressEvent(): begin"<<std::endl;
 
93
    switch(e->key()){
 
94
    case Qt::Key_Shift:
 
95
        restrBak = snapMode.restriction;
 
96
        setSnapRestriction(RS2::RestrictOrthogonal);
 
97
        e->accept();
 
98
        //cleanup default action, issue#285
 
99
    case Qt::Key_Escape:
 
100
        //        std::cout<<"RS_ActionDefault::keyPressEvent(): Qt::Key_Escape"<<std::endl;
 
101
        deletePreview();
 
102
        deleteSnapper();
 
103
        setStatus(Neutral);
 
104
        e->accept();
 
105
    default:
 
106
        e->ignore();
 
107
    }
 
108
 
 
109
}
 
110
 
 
111
void RS_ActionDefault::keyReleaseEvent(QKeyEvent* e) {
 
112
    if (e->key()==Qt::Key_Shift) {
 
113
        setSnapRestriction(restrBak);
 
114
        e->accept();
 
115
    }
 
116
}
 
117
 
 
118
 
 
119
void RS_ActionDefault::mouseMoveEvent(QMouseEvent* e) {
 
120
 
 
121
    RS_Vector mouse = graphicView->toGraph(RS_Vector(e->x(), e->y()));
 
122
    RS_Vector relMouse = mouse - graphicView->getRelativeZero();
 
123
 
 
124
    RS_DIALOGFACTORY->updateCoordinateWidget(mouse, relMouse);
 
125
 
 
126
    switch (getStatus()) {
 
127
    case Neutral:
 
128
        deleteSnapper();
 
129
        break;
 
130
    case Dragging:
 
131
        //v2 = graphicView->toGraph(e->x(), e->y());
 
132
        v2 = mouse;
 
133
 
 
134
        if (graphicView->toGuiDX(v1.distanceTo(v2))>10) {
 
135
            // look for reference points to drag:
 
136
            double dist;
 
137
            RS_Vector ref = container->getNearestSelectedRef(v1, &dist);
 
138
            if (ref.valid==true && graphicView->toGuiDX(dist)<8) {
 
139
                RS_DEBUG->print("RS_ActionDefault::mouseMoveEvent: "
 
140
                                "moving reference point");
 
141
                setStatus(MovingRef);
 
142
                v1 = ref;
 
143
                graphicView->moveRelativeZero(v1);
 
144
            }
 
145
            else {
 
146
                // test for an entity to drag:
 
147
                RS_Entity* en = catchEntity(v1);
 
148
                if (en!=NULL && en->isSelected()) {
 
149
                    RS_DEBUG->print("RS_ActionDefault::mouseMoveEvent: "
 
150
                                    "moving entity");
 
151
                    setStatus(Moving);
 
152
                    RS_Vector vp= en->getNearestRef(v1);
 
153
                    if(vp.valid) v1=vp;
 
154
 
 
155
                    //graphicView->moveRelativeZero(v1);
 
156
                }
 
157
 
 
158
                // no entity found. start area selection:
 
159
                else {
 
160
                    setStatus(SetCorner2);
 
161
                }
 
162
            }
 
163
        }
 
164
        break;
 
165
 
 
166
    case MovingRef:
 
167
        v2 = snapPoint(e);
 
168
 
 
169
        deletePreview();
 
170
        preview->addSelectionFrom(*container);
 
171
        preview->moveRef(v1, v2-v1);
 
172
        drawPreview();
 
173
        break;
 
174
 
 
175
    case Moving:
 
176
        v2 = snapPoint(e);
 
177
 
 
178
        deletePreview();
 
179
        preview->addSelectionFrom(*container);
 
180
        preview->move(v2-v1);
 
181
        drawPreview();
 
182
        break;
 
183
 
 
184
    case SetCorner2:
 
185
        if (v1.valid) {
 
186
            v2 = mouse;
 
187
 
 
188
            deletePreview();
 
189
 
 
190
            RS_OverlayBox* ob=new RS_OverlayBox(preview, RS_OverlayBoxData(v1, v2));
 
191
            preview->addEntity(ob);
 
192
 
 
193
            drawPreview();
 
194
        }
 
195
 
 
196
    default:
 
197
        break;
 
198
    }
 
199
}
 
200
 
 
201
 
 
202
 
 
203
void RS_ActionDefault::mousePressEvent(QMouseEvent* e) {
 
204
    if (e->button()==Qt::LeftButton) {
 
205
        switch (getStatus()) {
 
206
        case Neutral:
 
207
            v1 = graphicView->toGraph(e->x(), e->y());
 
208
            setStatus(Dragging);
 
209
            break;
 
210
 
 
211
        case Moving: {
 
212
            v2 = snapPoint(e);
 
213
            deletePreview();
 
214
            RS_Modification m(*container, graphicView);
 
215
            RS_MoveData data;
 
216
            data.number = 0;
 
217
            data.useCurrentLayer = false;
 
218
            data.useCurrentAttributes = false;
 
219
            data.offset = v2-v1;
 
220
            m.move(data);
 
221
            setStatus(Neutral);
 
222
            RS_DIALOGFACTORY->updateSelectionWidget(
 
223
                        container->countSelected(),container->totalSelectedLength());
 
224
            RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
225
            deleteSnapper();
 
226
        }
 
227
            break;
 
228
 
 
229
        case MovingRef: {
 
230
            v2 = snapPoint(e);
 
231
            deletePreview();
 
232
            RS_Modification m(*container, graphicView);
 
233
            RS_MoveRefData data;
 
234
            data.ref = v1;
 
235
            data.offset = v2-v1;
 
236
            m.moveRef(data);
 
237
            //container->moveSelectedRef(v1, v2-v2);
 
238
            setStatus(Neutral);
 
239
            RS_DIALOGFACTORY->updateSelectionWidget(
 
240
                        container->countSelected(),container->totalSelectedLength());
 
241
            RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
242
        }
 
243
            break;
 
244
 
 
245
        default:
 
246
            break;
 
247
        }
 
248
    } else if (e->button()==Qt::RightButton) {
 
249
        //cleanup
 
250
        setStatus(Neutral);
 
251
        e->accept();
 
252
    }
 
253
}
 
254
 
 
255
 
 
256
 
 
257
void RS_ActionDefault::mouseReleaseEvent(QMouseEvent* e) {
 
258
    RS_DEBUG->print("RS_ActionDefault::mouseReleaseEvent()");
 
259
 
 
260
    if (e->button()==Qt::LeftButton) {
 
261
        v2 = graphicView->toGraph(e->x(), e->y());
 
262
        switch (getStatus()) {
 
263
        case Dragging: {
 
264
            // select single entity:
 
265
            RS_Entity* en = catchEntity(e);
 
266
 
 
267
            if (en!=NULL) {
 
268
                deletePreview();
 
269
 
 
270
                RS_Selection s(*container, graphicView);
 
271
                s.selectSingle(en);
 
272
 
 
273
                RS_DIALOGFACTORY->updateSelectionWidget(
 
274
                            container->countSelected(),container->totalSelectedLength());
 
275
 
 
276
                e->accept();
 
277
 
 
278
                setStatus(Neutral);
 
279
            } else {
 
280
                setStatus(SetCorner2);
 
281
            }
 
282
        }
 
283
            break;
 
284
 
 
285
        case SetCorner2: {
 
286
            //v2 = snapPoint(e);
 
287
            v2 = graphicView->toGraph(e->x(), e->y());
 
288
 
 
289
            // select window:
 
290
            //if (graphicView->toGuiDX(v1.distanceTo(v2))>20) {
 
291
            deletePreview();
 
292
 
 
293
            bool cross = (v1.x>v2.x);
 
294
            RS_Selection s(*container, graphicView);
 
295
            s.selectWindow(v1, v2, true, cross);
 
296
 
 
297
            RS_DIALOGFACTORY->updateSelectionWidget(
 
298
                        container->countSelected(),container->totalSelectedLength());
 
299
 
 
300
            setStatus(Neutral);
 
301
            e->accept();
 
302
            //}
 
303
        }
 
304
            break;
 
305
 
 
306
 
 
307
        default:
 
308
            break;
 
309
 
 
310
        }
 
311
    } else if (e->button()==Qt::RightButton) {
 
312
        //cleanup
 
313
        setStatus(Neutral);
 
314
        e->accept();
 
315
    }
 
316
}
 
317
 
 
318
void RS_ActionDefault::commandEvent(RS_CommandEvent* e) {
 
319
    QString c = e->getCommand().toLower();
 
320
 
 
321
    // if the current action can't deal with the command,
 
322
    //   it might be intended to launch a new command
 
323
    //if (!e.isAccepted()) {
 
324
    // command for new action:
 
325
    //RS2::ActionType type = RS_COMMANDS->cmdToAction(c);
 
326
    //if (type!=RS2::ActionNone) {
 
327
    //graphicView->setCurrentAction(type);
 
328
    //return true;
 
329
    //}
 
330
    //}
 
331
}
 
332
 
 
333
 
 
334
 
 
335
QStringList RS_ActionDefault::getAvailableCommands() {
 
336
    QStringList cmd;
 
337
 
 
338
    //cmd += "line";
 
339
    //cmd += "rectangle";
 
340
 
 
341
    return cmd;
 
342
}
 
343
 
 
344
 
 
345
void RS_ActionDefault::updateMouseButtonHints() {
 
346
    switch (getStatus()) {
 
347
    case Neutral:
 
348
        RS_DIALOGFACTORY->updateMouseWidget("", "");
 
349
        break;
 
350
    case SetCorner2:
 
351
        RS_DIALOGFACTORY->updateMouseWidget(tr("Choose second edge"),
 
352
                                            tr("Back"));
 
353
        break;
 
354
    default:
 
355
        RS_DIALOGFACTORY->updateMouseWidget("", "");
 
356
        break;
 
357
    }
 
358
}
 
359
 
 
360
void RS_ActionDefault::updateMouseCursor() {
 
361
    switch (getStatus()) {
 
362
    case Neutral:
 
363
        graphicView->setMouseCursor(RS2::ArrowCursor);
 
364
        break;
 
365
    case Moving:
 
366
    case MovingRef:
 
367
        graphicView->setMouseCursor(RS2::SelectCursor);
 
368
        break;
 
369
    default:
 
370
        break;
 
371
    }
 
372
}
 
373
 
 
374
 
 
375
 
 
376
//void RS_ActionDefault::updateToolBar() {
 
377
//    //not needed any more
 
378
//    return;
 
379
//    //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
380
//        switch (getStatus()) {
 
381
//        case Neutral:
 
382
//                // would switch back to main in edit / measure / .. modes
 
383
//                //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
 
384
//                break;
 
385
//        case Moving:
 
386
//        case MovingRef:
 
387
//                RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
 
388
//                break;
 
389
//        default:
 
390
//                break;
 
391
//        }
 
392
//}
 
393
 
 
394
// EOF