]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinerectangle.cpp
e8ef45c98d1de585a7ba6a0cb6c38e566e6188b5
[architektonas] / src / actions / rs_actiondrawlinerectangle.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawlinerectangle.cpp 1134 2004-07-13 23:26:13Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actiondrawlinerectangle.h"
28 #include "rs_snapper.h"
29
30
31
32 RS_ActionDrawLineRectangle::RS_ActionDrawLineRectangle(
33     RS_EntityContainer& container,
34     RS_GraphicView& graphicView)
35         :RS_PreviewActionInterface("Draw rectangles",
36                            container, graphicView) {
37     reset();
38 }
39
40
41
42 RS_ActionDrawLineRectangle::~RS_ActionDrawLineRectangle() {}
43
44 QAction* RS_ActionDrawLineRectangle::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
45 {
46         QAction * action = new QAction(tr("&Rectangle"), 0);
47 //      QAction* action = new QAction(tr("Rectangle"), tr("&Rectangle"),
48 //                                                      QKeySequence(), NULL);
49         action->setStatusTip(tr("Draw rectangles"));
50         return action;
51 }
52
53
54 void RS_ActionDrawLineRectangle::reset() {
55     for (int i=0; i<4; ++i) {
56         data[i] = RS_LineData(Vector(false),
57                               Vector(false));
58     }
59 }
60
61
62
63 void RS_ActionDrawLineRectangle::init(int status) {
64     RS_PreviewActionInterface::init(status);
65
66     reset();
67 }
68
69
70
71 void RS_ActionDrawLineRectangle::trigger() {
72     RS_PreviewActionInterface::trigger();
73
74     RS_Line* line[4];
75     preparePreview();
76
77     // create and add rectangle:
78     for (int i=0; i<4; ++i) {
79         line[i] = new RS_Line(container,
80                               data[i]);
81         line[i]->setLayerToActive();
82         line[i]->setPenToActive();
83         container->addEntity(line[i]);
84     }
85
86     // upd. undo list:
87     if (document!=NULL) {
88         document->startUndoCycle();
89         for (int i=0; i<4; ++i) {
90             document->addUndoable(line[i]);
91         }
92         document->endUndoCycle();
93     }
94
95     // upd. view
96     deleteSnapper();
97     graphicView->moveRelativeZero(Vector(0.0,0.0));
98     for (int i=0; i<4; ++i) {
99         graphicView->drawEntity(line[i]);
100         RS_DEBUG->print("RS_ActionDrawLineRectangle::trigger():"
101                         " line added: %d",
102                         line[i]->getId());
103     }
104     graphicView->moveRelativeZero(corner2);
105 }
106
107
108
109 void RS_ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent* e) {
110     RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent begin");
111
112     Vector mouse = snapPoint(e);
113     if (getStatus()==SetCorner2 && corner1.valid) {
114         corner2 = mouse;
115         deletePreview();
116         clearPreview();
117
118         preparePreview();
119
120         for (int i=0; i<4; ++i) {
121             preview->addEntity(new RS_Line(preview, data[i]));
122         }
123         drawPreview();
124     }
125
126     RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent end");
127 }
128
129
130 void RS_ActionDrawLineRectangle::mouseReleaseEvent(QMouseEvent* e) {
131     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
132         RS_CoordinateEvent ce(snapPoint(e));
133         coordinateEvent(&ce);
134     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
135         deletePreview();
136         deleteSnapper();
137         init(getStatus()-1);
138     }
139 }
140
141
142
143 void RS_ActionDrawLineRectangle::preparePreview() {
144     data[0] = RS_LineData(corner1, Vector(corner2.x, corner1.y));
145     data[1] = RS_LineData(Vector(corner2.x, corner1.y), corner2);
146     data[2] = RS_LineData(corner2, Vector(corner1.x, corner2.y));
147     data[3] = RS_LineData(Vector(corner1.x, corner2.y), corner1);
148 }
149
150
151 void RS_ActionDrawLineRectangle::coordinateEvent(RS_CoordinateEvent* e) {
152     if (e==NULL) {
153         return;
154     }
155
156     Vector mouse = e->getCoordinate();
157
158     switch (getStatus()) {
159     case SetCorner1:
160         corner1 = mouse;
161         graphicView->moveRelativeZero(mouse);
162         setStatus(SetCorner2);
163         break;
164
165     case SetCorner2:
166         corner2 = mouse;
167         trigger();
168         setStatus(SetCorner1);
169         break;
170
171     default:
172         break;
173     }
174 }
175
176
177
178 void RS_ActionDrawLineRectangle::commandEvent(RS_CommandEvent* e) {
179     QString c = e->getCommand().toLower();
180
181     if (checkCommand("help", c)) {
182         if (RS_DIALOGFACTORY!=NULL) {
183             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
184                                              + getAvailableCommands().join(", "));
185         }
186         return;
187     }
188 }
189
190
191
192 QStringList RS_ActionDrawLineRectangle::getAvailableCommands() {
193     QStringList cmd;
194     return cmd;
195 }
196
197
198 void RS_ActionDrawLineRectangle::updateMouseButtonHints() {
199     if (RS_DIALOGFACTORY!=NULL) {
200         switch (getStatus()) {
201         case SetCorner1:
202             RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
203                                                 tr("Cancel"));
204             break;
205         case SetCorner2:
206             RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
207                                                 tr("Back"));
208             break;
209         default:
210             RS_DIALOGFACTORY->updateMouseWidget("", "");
211             break;
212         }
213     }
214 }
215
216
217 void RS_ActionDrawLineRectangle::updateMouseCursor() {
218     graphicView->setMouseCursor(RS2::CadCursor);
219 }
220
221
222 void RS_ActionDrawLineRectangle::updateToolBar() {
223     if (RS_DIALOGFACTORY!=NULL) {
224         if (!isFinished()) {
225             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
226         } else {
227             RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
228         }
229     }
230 }
231
232 // EOF