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 <QAction>
28 #include <QMouseEvent>
29 #include "rs_actionblockscreate.h"
30 
31 #include "rs_creation.h"
32 #include "rs_dialogfactory.h"
33 #include "rs_graphicview.h"
34 #include "rs_graphic.h"
35 #include "rs_insert.h"
36 #include "rs_modification.h"
37 #include "rs_coordinateevent.h"
38 
39 /**
40  * Constructor.
41  */
RS_ActionBlocksCreate(RS_EntityContainer & container,RS_GraphicView & graphicView)42 RS_ActionBlocksCreate::RS_ActionBlocksCreate(RS_EntityContainer& container,
43         RS_GraphicView& graphicView)
44         :RS_PreviewActionInterface("Blocks Create",
45 						   container, graphicView)
46 		,referencePoint(new RS_Vector{})
47 {
48 	actionType=RS2::ActionBlocksCreate;
49 }
50 
51 RS_ActionBlocksCreate::~RS_ActionBlocksCreate() = default;
52 
53 
init(int status)54 void RS_ActionBlocksCreate::init(int status) {
55     RS_PreviewActionInterface::init(status);
56 }
57 
58 
59 
trigger()60 void RS_ActionBlocksCreate::trigger() {
61 	if (graphic) {
62         RS_BlockList* blockList = graphic->getBlockList();
63 		if (blockList) {
64             RS_BlockData d =
65                 RS_DIALOGFACTORY->requestNewBlockDialog(blockList);
66 
67             if (!d.name.isEmpty()) {
68                 RS_Creation creation(container, graphicView);
69 				creation.createBlock(&d, *referencePoint, true);
70 
71                 RS_InsertData id(
72                     d.name,
73 					*referencePoint,
74                     RS_Vector(1.0,1.0),
75                     0.0,
76                     1, 1, RS_Vector(0.0,0.0)
77                 );
78 				creation.createInsert(&id);
79             }
80         }
81     }
82 
83     graphicView->redraw(RS2::RedrawDrawing);
84 
85     setStatus(getStatus()+1); // clear mouse button hints
86     updateMouseButtonHints();
87     graphicView->killSelectActions();
88     finish(false);
89 }
90 
91 
mouseMoveEvent(QMouseEvent * e)92 void RS_ActionBlocksCreate::mouseMoveEvent(QMouseEvent* e) {
93     snapPoint(e);
94 
95     switch (getStatus()) {
96     case SetReferencePoint:
97         //data.insertionPoint = snapPoint(e);
98 
99 		/*if (block) {
100             deletePreview();
101             //preview->addAllFrom(*block);
102             //preview->move(data.insertionPoint);
103 				RS_Creation creation(preview, nullptr, false);
104                 creation.createInsert(data);
105             drawPreview();
106     }*/
107         break;
108 
109     default:
110         break;
111     }
112 }
113 
114 
115 
mouseReleaseEvent(QMouseEvent * e)116 void RS_ActionBlocksCreate::mouseReleaseEvent(QMouseEvent* e) {
117     if (e->button()==Qt::LeftButton) {
118         RS_CoordinateEvent ce(snapPoint(e));
119         coordinateEvent(&ce);
120     } else if (e->button()==Qt::RightButton) {
121         init(getStatus()-1);
122     }
123 }
124 
125 
126 
coordinateEvent(RS_CoordinateEvent * e)127 void RS_ActionBlocksCreate::coordinateEvent(RS_CoordinateEvent* e) {
128 	if (!e) {
129         return;
130     }
131 
132     switch (getStatus()) {
133     case SetReferencePoint:
134 		*referencePoint = e->getCoordinate();
135         trigger();
136         break;
137 
138     default:
139         break;
140 
141     }
142 }
143 
144 
145 
updateMouseButtonHints()146 void RS_ActionBlocksCreate::updateMouseButtonHints() {
147     switch (getStatus()) {
148     case SetReferencePoint:
149         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
150                                             tr("Cancel"));
151         break;
152     default:
153 		RS_DIALOGFACTORY->updateMouseWidget();
154         break;
155     }
156 }
157 
158 
updateMouseCursor()159 void RS_ActionBlocksCreate::updateMouseCursor() {
160     graphicView->setMouseCursor(RS2::CadCursor);
161 }
162 
163 // EOF
164