~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/actions/rs_actiondrawlinerectangle.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_actiondrawlinerectangle.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_ActionDrawLineRectangle::RS_ActionDrawLineRectangle(
 
37
    RS_EntityContainer& container,
 
38
    RS_GraphicView& graphicView)
 
39
        :RS_PreviewActionInterface("Draw rectangles",
 
40
                           container, graphicView) {
 
41
    reset();
 
42
}
 
43
 
 
44
 
 
45
 
 
46
RS_ActionDrawLineRectangle::~RS_ActionDrawLineRectangle() {}
 
47
 
 
48
QAction* RS_ActionDrawLineRectangle::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/) {
 
49
        //  tr("&Rectangle"),
 
50
        QAction* action = new QAction(tr("Rectangle"), NULL);
 
51
        action->setIcon(QIcon(":/extui/linesrect.png"));
 
52
    //action->zetStatusTip(tr("Draw rectangles"));
 
53
        return action;
 
54
}
 
55
 
 
56
 
 
57
void RS_ActionDrawLineRectangle::reset() {
 
58
    for (int i=0; i<4; ++i) {
 
59
        data[i] = RS_LineData(RS_Vector(false),
 
60
                              RS_Vector(false));
 
61
    }
 
62
}
 
63
 
 
64
 
 
65
 
 
66
void RS_ActionDrawLineRectangle::init(int status) {
 
67
    RS_PreviewActionInterface::init(status);
 
68
 
 
69
    reset();
 
70
}
 
71
 
 
72
 
 
73
 
 
74
void RS_ActionDrawLineRectangle::trigger() {
 
75
    RS_PreviewActionInterface::trigger();
 
76
 
 
77
    RS_Line* line[4];
 
78
    preparePreview();
 
79
 
 
80
    // create and add rectangle:
 
81
    for (int i=0; i<4; ++i) {
 
82
        line[i] = new RS_Line(container,
 
83
                              data[i]);
 
84
        line[i]->setLayerToActive();
 
85
        line[i]->setPenToActive();
 
86
        container->addEntity(line[i]);
 
87
    }
 
88
 
 
89
    // upd. undo list:
 
90
    if (document!=NULL) {
 
91
        document->startUndoCycle();
 
92
        for (int i=0; i<4; ++i) {
 
93
            document->addUndoable(line[i]);
 
94
        }
 
95
        document->endUndoCycle();
 
96
    }
 
97
 
 
98
    // upd. view
 
99
        graphicView->redraw(RS2::RedrawDrawing);
 
100
    graphicView->moveRelativeZero(corner2);
 
101
}
 
102
 
 
103
 
 
104
 
 
105
void RS_ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent* e) {
 
106
    RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent begin");
 
107
 
 
108
    RS_Vector mouse = snapPoint(e);
 
109
    if (getStatus()==SetCorner2 && corner1.valid) {
 
110
        corner2 = mouse;
 
111
        deletePreview();
 
112
 
 
113
        preparePreview();
 
114
 
 
115
        for (int i=0; i<4; ++i) {
 
116
            preview->addEntity(new RS_Line(preview, data[i]));
 
117
        }
 
118
        drawPreview();
 
119
    }
 
120
 
 
121
    RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent end");
 
122
}
 
123
 
 
124
 
 
125
void RS_ActionDrawLineRectangle::mouseReleaseEvent(QMouseEvent* e) {
 
126
    if (e->button()==Qt::LeftButton) {
 
127
        RS_CoordinateEvent ce(snapPoint(e));
 
128
        coordinateEvent(&ce);
 
129
    } else if (e->button()==Qt::RightButton) {
 
130
        deletePreview();
 
131
        init(getStatus()-1);
 
132
    }
 
133
}
 
134
 
 
135
 
 
136
 
 
137
void RS_ActionDrawLineRectangle::preparePreview() {
 
138
    data[0] = RS_LineData(corner1, RS_Vector(corner2.x, corner1.y));
 
139
    data[1] = RS_LineData(RS_Vector(corner2.x, corner1.y), corner2);
 
140
    data[2] = RS_LineData(corner2, RS_Vector(corner1.x, corner2.y));
 
141
    data[3] = RS_LineData(RS_Vector(corner1.x, corner2.y), corner1);
 
142
}
 
143
 
 
144
 
 
145
void RS_ActionDrawLineRectangle::coordinateEvent(RS_CoordinateEvent* e) {
 
146
    if (e==NULL) {
 
147
        return;
 
148
    }
 
149
 
 
150
    RS_Vector mouse = e->getCoordinate();
 
151
 
 
152
    switch (getStatus()) {
 
153
    case SetCorner1:
 
154
        corner1 = mouse;
 
155
        graphicView->moveRelativeZero(mouse);
 
156
        setStatus(SetCorner2);
 
157
        break;
 
158
 
 
159
    case SetCorner2:
 
160
        corner2 = mouse;
 
161
        trigger();
 
162
        setStatus(SetCorner1);
 
163
        break;
 
164
 
 
165
    default:
 
166
        break;
 
167
    }
 
168
}
 
169
 
 
170
 
 
171
 
 
172
void RS_ActionDrawLineRectangle::commandEvent(RS_CommandEvent* e) {
 
173
    QString c = e->getCommand().toLower();
 
174
 
 
175
    if (checkCommand("help", c)) {
 
176
        if (RS_DIALOGFACTORY!=NULL) {
 
177
            RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
 
178
                                             + getAvailableCommands().join(", "));
 
179
        }
 
180
        return;
 
181
    }
 
182
}
 
183
 
 
184
 
 
185
 
 
186
QStringList RS_ActionDrawLineRectangle::getAvailableCommands() {
 
187
    QStringList cmd;
 
188
    return cmd;
 
189
}
 
190
 
 
191
 
 
192
void RS_ActionDrawLineRectangle::updateMouseButtonHints() {
 
193
    if (RS_DIALOGFACTORY!=NULL) {
 
194
        switch (getStatus()) {
 
195
        case SetCorner1:
 
196
            RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
 
197
                                                tr("Cancel"));
 
198
            break;
 
199
        case SetCorner2:
 
200
            RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
 
201
                                                tr("Back"));
 
202
            break;
 
203
        default:
 
204
            RS_DIALOGFACTORY->updateMouseWidget("", "");
 
205
            break;
 
206
        }
 
207
    }
 
208
}
 
209
 
 
210
 
 
211
void RS_ActionDrawLineRectangle::updateMouseCursor() {
 
212
    graphicView->setMouseCursor(RS2::CadCursor);
 
213
}
 
214
 
 
215
 
 
216
//void RS_ActionDrawLineRectangle::updateToolBar() {
 
217
//    if (RS_DIALOGFACTORY!=NULL) {
 
218
//        if (isFinished()) {
 
219
//            RS_DIALOGFACTORY->resetToolBar();
 
220
//        }
 
221
//    }
 
222
//}
 
223
 
 
224
// EOF