~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actiondrawlinefree.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_actiondrawlinefree.h"
 
28
 
 
29
#include <QAction>
 
30
#include "rs_dialogfactory.h"
 
31
#include "rs_graphicview.h"
 
32
 
 
33
 
 
34
 
 
35
RS_ActionDrawLineFree::RS_ActionDrawLineFree(RS_EntityContainer& container,
 
36
        RS_GraphicView& graphicView)
 
37
        :RS_PreviewActionInterface("Draw freehand lines",
 
38
                    container, graphicView) {
 
39
    vertex = RS_Vector(false);
 
40
    polyline = NULL;
 
41
}
 
42
 
 
43
RS_ActionDrawLineFree::~RS_ActionDrawLineFree() {
 
44
    if (polyline!=NULL) {
 
45
        delete polyline;
 
46
    }
 
47
}
 
48
 
 
49
QAction* RS_ActionDrawLineFree::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
 
50
        // tr(""Line: Freehand"),
 
51
    QAction* action = new QAction(tr("&Freehand Line"), NULL);
 
52
        action->setIcon(QIcon(":/extui/linesfree.png"));
 
53
    //action->zetStatusTip(tr("Draw freehand lines"));
 
54
    return action;
 
55
}
 
56
 
 
57
void RS_ActionDrawLineFree::trigger() {
 
58
    if (polyline!=NULL) {
 
59
        deletePreview();
 
60
 
 
61
            polyline->endPolyline();
 
62
            RS_VectorSolutions sol=polyline->getRefPoints();
 
63
            if(sol.getNumber() > 2 ) {
 
64
                container->addEntity(polyline);
 
65
                if (document) {
 
66
                        document->startUndoCycle();
 
67
                        document->addUndoable(polyline);
 
68
                        document->endUndoCycle();
 
69
                }
 
70
                graphicView->redraw(RS2::RedrawDrawing);
 
71
        RS_DEBUG->print("RS_ActionDrawLineFree::trigger():"
 
72
                        " polyline added: %d", polyline->getId());
 
73
            } else {
 
74
            delete polyline;
 
75
            }
 
76
        polyline = NULL;
 
77
    }
 
78
    setStatus(SetStartpoint);
 
79
}
 
80
 
 
81
/*
 
82
 * 11 Aug 2011, Dongxu Li
 
83
 */
 
84
 
 
85
void RS_ActionDrawLineFree::mouseMoveEvent(QMouseEvent* e) {
 
86
    if (getStatus()==Dragging && polyline!=NULL) {
 
87
        RS_Vector v = snapPoint(e);
 
88
        if( (graphicView->toGui(v) - graphicView->toGui(vertex)).squared()< 1. ){
 
89
            //do not add the same mouse position
 
90
            return;
 
91
        }
 
92
        RS_Entity* ent = polyline->addVertex(v);
 
93
        if (polyline->count() > 0){
 
94
            preview->addCloneOf(ent);
 
95
            drawPreview();
 
96
        }
 
97
 
 
98
        vertex = v;
 
99
 
 
100
        RS_DEBUG->print("RS_ActionDrawLineFree::mouseMoveEvent():"
 
101
                        " line added: %d", ent->getId());
 
102
    }
 
103
}
 
104
 
 
105
 
 
106
 
 
107
void RS_ActionDrawLineFree::mousePressEvent(QMouseEvent* e) {
 
108
    if (e->button()==Qt::LeftButton) {
 
109
        switch(getStatus()){
 
110
        case SetStartpoint:
 
111
            setStatus(Dragging);
 
112
        case Dragging:
 
113
            vertex = snapPoint(e);
 
114
            polyline = new RS_Polyline(container,
 
115
                                       RS_PolylineData(vertex, vertex, 0));
 
116
            polyline->setLayerToActive();
 
117
            polyline->setPenToActive();
 
118
            break;
 
119
        default:
 
120
            break;
 
121
 
 
122
        }
 
123
    }
 
124
    //else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton && !vertex.valid) {
 
125
    //}
 
126
}
 
127
 
 
128
 
 
129
 
 
130
void RS_ActionDrawLineFree::mouseReleaseEvent(QMouseEvent* e) {
 
131
    if (e->button()==Qt::LeftButton) {
 
132
        if(getStatus()==Dragging){
 
133
        vertex = RS_Vector(false);
 
134
        trigger();
 
135
        }
 
136
    } else if (e->button()==Qt::RightButton) {
 
137
        if (polyline!=NULL) {
 
138
            delete polyline;
 
139
            polyline = NULL;
 
140
        }
 
141
        init(getStatus()-1);
 
142
    }
 
143
}
 
144
 
 
145
 
 
146
 
 
147
void RS_ActionDrawLineFree::updateMouseButtonHints() {
 
148
    switch (getStatus()) {
 
149
    case SetStartpoint:
 
150
    case Dragging:
 
151
        RS_DIALOGFACTORY->updateMouseWidget(
 
152
            tr("Click and drag to draw a line"), tr("Cancel"));
 
153
        break;
 
154
    default:
 
155
        RS_DIALOGFACTORY->updateMouseWidget("", "");
 
156
        break;
 
157
    }
 
158
}
 
159
 
 
160
 
 
161
 
 
162
void RS_ActionDrawLineFree::updateMouseCursor() {
 
163
    graphicView->setMouseCursor(RS2::CadCursor);
 
164
}
 
165
 
 
166
 
 
167
 
 
168
//void RS_ActionDrawLineFree::updateToolBar() {
 
169
//    if (RS_DIALOGFACTORY!=NULL) {
 
170
//        if (isFinished()) {
 
171
//            RS_DIALOGFACTORY->resetToolBar();
 
172
//        }
 
173
//    }
 
174
//}
 
175
 
 
176
// EOF