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 <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_commandevent.h"
34 #include "rs_line.h"
35 #include "rs_coordinateevent.h"
36 #include "rs_polyline.h"
37 #include "rs_preview.h"
38 #include "rs_debug.h"
39
40 struct RS_ActionDrawLineRectangle::Points {
41 /**
42 * 1st corner.
43 */
44 RS_Vector corner1;
45 /**
46 * 2nd corner.
47 */
48 RS_Vector corner2;
49 };
50
RS_ActionDrawLineRectangle(RS_EntityContainer & container,RS_GraphicView & graphicView)51 RS_ActionDrawLineRectangle::RS_ActionDrawLineRectangle(
52 RS_EntityContainer& container,
53 RS_GraphicView& graphicView)
54 :RS_PreviewActionInterface("Draw rectangles",
55 container, graphicView)
56 , pPoints(new Points{})
57 {
58 actionType=RS2::ActionDrawLineRectangle;
59 }
60
61 RS_ActionDrawLineRectangle::~RS_ActionDrawLineRectangle() = default;
62
63
trigger()64 void RS_ActionDrawLineRectangle::trigger() {
65 RS_PreviewActionInterface::trigger();
66
67 RS_Polyline* polyline = new RS_Polyline(container);
68
69 // create and add rectangle:
70 polyline->addVertex(pPoints->corner1);
71 polyline->setLayerToActive();
72 polyline->setPenToActive();
73 polyline->addVertex({pPoints->corner2.x, pPoints->corner1.y});
74 polyline->addVertex(pPoints->corner2);
75 polyline->addVertex({pPoints->corner1.x, pPoints->corner2.y});
76 polyline->setClosed(true);
77 polyline->endPolyline();
78 container->addEntity(polyline);
79
80 // upd. undo list:
81 if (document) {
82 document->startUndoCycle();
83 document->addUndoable(polyline);
84 document->endUndoCycle();
85 }
86
87 // upd. view
88 graphicView->redraw(RS2::RedrawDrawing);
89 graphicView->moveRelativeZero(pPoints->corner2);
90 }
91
92
93
mouseMoveEvent(QMouseEvent * e)94 void RS_ActionDrawLineRectangle::mouseMoveEvent(QMouseEvent* e) {
95 RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent begin");
96
97 RS_Vector mouse = snapPoint(e);
98 if (getStatus()==SetCorner2 && pPoints->corner1.valid) {
99 pPoints->corner2 = mouse;
100 deletePreview();
101 preview->addRectangle(pPoints->corner1, pPoints->corner2);
102 drawPreview();
103 }
104
105 RS_DEBUG->print("RS_ActionDrawLineRectangle::mouseMoveEvent end");
106 }
107
108
mouseReleaseEvent(QMouseEvent * e)109 void RS_ActionDrawLineRectangle::mouseReleaseEvent(QMouseEvent* e) {
110 if (e->button()==Qt::LeftButton) {
111 RS_CoordinateEvent ce(snapPoint(e));
112 coordinateEvent(&ce);
113 } else if (e->button()==Qt::RightButton) {
114 deletePreview();
115 init(getStatus()-1);
116 }
117 }
118
coordinateEvent(RS_CoordinateEvent * e)119 void RS_ActionDrawLineRectangle::coordinateEvent(RS_CoordinateEvent* e) {
120 if (!e) return;
121
122 RS_Vector mouse = e->getCoordinate();
123
124 switch (getStatus()) {
125 case SetCorner1:
126 pPoints->corner1 = mouse;
127 graphicView->moveRelativeZero(mouse);
128 setStatus(SetCorner2);
129 break;
130
131 case SetCorner2:
132 pPoints->corner2 = mouse;
133 trigger();
134 setStatus(SetCorner1);
135 break;
136
137 default:
138 break;
139 }
140 }
141
commandEvent(RS_CommandEvent * e)142 void RS_ActionDrawLineRectangle::commandEvent(RS_CommandEvent* e) {
143 QString const& c = e->getCommand().toLower();
144
145 if (checkCommand("help", c)) {
146 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
147 + getAvailableCommands().join(", "));
148 return;
149 }
150 }
151
updateMouseButtonHints()152 void RS_ActionDrawLineRectangle::updateMouseButtonHints() {
153 switch (getStatus()) {
154 case SetCorner1:
155 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
156 tr("Cancel"));
157 break;
158 case SetCorner2:
159 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
160 tr("Back"));
161 break;
162 default:
163 RS_DIALOGFACTORY->updateMouseWidget();
164 break;
165 }
166 }
167
168
updateMouseCursor()169 void RS_ActionDrawLineRectangle::updateMouseCursor() {
170 graphicView->setMouseCursor(RS2::CadCursor);
171 }
172
173 // EOF
174