]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawhatch.cpp
Initial import
[architektonas] / src / actions / rs_actiondrawhatch.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawhatch.cpp 2381 2005-05-16 17:05:44Z 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_actiondrawhatch.h"
28
29 #include "rs_snapper.h"
30 #include "rs_graphicview.h"
31 #include "rs_information.h"
32
33 RS_ActionDrawHatch::RS_ActionDrawHatch(RS_EntityContainer& container,
34         RS_GraphicView& graphicView):
35         RS_PreviewActionInterface("Draw Hatch", container, graphicView)
36 {
37     hatch = NULL;
38 }
39
40 QAction * RS_ActionDrawHatch::createGUIAction(RS2::ActionType /*type*/, QObject* /*parent*/)
41 {
42         QAction * action = new QAction(tr("&Hatch"), 0);
43 //      QAction* action = new QAction(tr("Hatch"), tr("&Hatch"),
44 //                                                                      QKeySequence(), NULL);
45         action->setStatusTip(tr("Draw Hatches and Solid Fills"));
46         return action;
47 }
48
49 void RS_ActionDrawHatch::init(int status)
50 {
51     RS_ActionInterface::init(status);
52
53     clearPreview();
54
55     RS_Hatch tmp(container, data);
56     tmp.setLayerToActive();
57     tmp.setPenToActive();
58     if (RS_DIALOGFACTORY->requestHatchDialog(&tmp)) {
59         data = tmp.getData();
60         trigger();
61         finish();
62         graphicView->redraw();
63     } else {
64         finish();
65     }
66 }
67
68
69
70 void RS_ActionDrawHatch::trigger() {
71
72     RS_DEBUG->print("RS_ActionDrawHatch::trigger()");
73
74     //if (pos.valid) {
75     //deletePreview();
76     deleteSnapper();
77         RS_Entity* e;
78
79         // deselect unhatchable entities:
80     for (e=container->firstEntity(RS2::ResolveNone); e!=NULL;
81             e=container->nextEntity(RS2::ResolveNone)) {
82         if (e->isSelected() &&
83                     (e->rtti()==RS2::EntityHatch ||
84                          e->rtti()==RS2::EntityEllipse || e->rtti()==RS2::EntityPoint ||
85                          e->rtti()==RS2::EntityText ||
86                          RS_Information::isDimension(e->rtti()))) {
87                         e->setSelected(false);
88         }
89     }
90     for (e=container->firstEntity(RS2::ResolveAll); e!=NULL;
91             e=container->nextEntity(RS2::ResolveAll)) {
92         if (e->isSelected() &&
93                     (e->rtti()==RS2::EntityHatch ||
94                          e->rtti()==RS2::EntityEllipse || e->rtti()==RS2::EntityPoint ||
95                          e->rtti()==RS2::EntityText ||
96                          RS_Information::isDimension(e->rtti()))) {
97                         e->setSelected(false);
98         }
99     }
100
101         // look for selected contours:
102     bool haveContour = false;
103     for (e=container->firstEntity(RS2::ResolveAll); e!=NULL;
104             e=container->nextEntity(RS2::ResolveAll)) {
105         if (e->isSelected()) {
106             haveContour = true;
107         }
108     }
109
110     if (!haveContour) {
111         std::cerr << "no contour selected\n";
112         return;
113     }
114
115     hatch = new RS_Hatch(container, data);
116     hatch->setLayerToActive();
117     hatch->setPenToActive();
118     RS_EntityContainer* loop = new RS_EntityContainer(hatch);
119     loop->setPen(RS_Pen(RS2::FlagInvalid));
120
121     // add selected contour:
122     for (RS_Entity* e=container->firstEntity(RS2::ResolveAll); e!=NULL;
123             e=container->nextEntity(RS2::ResolveAll)) {
124
125         if (e->isSelected()) {
126             e->setSelected(false);
127                         // entity is part of a complex entity (spline, polyline, ..):
128                         if (e->getParent()!=NULL &&
129                             (e->getParent()->rtti()==RS2::EntitySpline ||
130                                  e->getParent()->rtti()==RS2::EntityPolyline)) {
131                                 e->getParent()->setSelected(false);
132                         }
133             RS_Entity* cp = e->clone();
134             cp->setPen(RS_Pen(RS2::FlagInvalid));
135             cp->reparent(loop);
136             loop->addEntity(cp);
137         }
138     }
139
140     hatch->addEntity(loop);
141         if (hatch->validate()) {
142                 container->addEntity(hatch);
143
144                 if (document) {
145                         document->startUndoCycle();
146                         document->addUndoable(hatch);
147                         document->endUndoCycle();
148                 }
149                 hatch->update();
150
151                 graphicView->drawEntity(hatch);
152
153                 RS_DIALOGFACTORY->commandMessage(tr("Hatch created successfully."));
154         }
155         else {
156                 delete hatch;
157                 hatch = NULL;
158                 RS_DIALOGFACTORY->commandMessage(tr("Invalid hatch area. Please check that "
159                 "the entities chosen form one or more closed contours."));
160         }
161     //}
162 }
163
164
165
166 void RS_ActionDrawHatch::mouseMoveEvent(QMouseEvent*) {
167     RS_DEBUG->print("RS_ActionDrawHatch::mouseMoveEvent begin");
168
169     /*if (getStatus()==SetPos) {
170         Vector mouse = snapPoint(e);
171         pos = mouse;
172
173
174         deletePreview();
175         if (hatch!=NULL && !hatch->isVisible()) {
176             hatch->setVisible(true);
177         }
178         offset = Vector(graphicView->toGuiDX(pos.x),
179                            -graphicView->toGuiDY(pos.y));
180         drawPreview();
181 }*/
182
183     RS_DEBUG->print("RS_ActionDrawHatch::mouseMoveEvent end");
184 }
185
186
187
188 void RS_ActionDrawHatch::mouseReleaseEvent(QMouseEvent* e) {
189     if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
190         Vector mouse = snapPoint(e);
191
192         switch (getStatus()) {
193         case ShowDialog:
194             break;
195
196         default:
197             break;
198         }
199     } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
200         //deletePreview();
201         deleteSnapper();
202         init(getStatus()-1);
203     }
204 }
205
206
207
208 void RS_ActionDrawHatch::updateMouseButtonHints() {
209     RS_DIALOGFACTORY->updateMouseWidget("", "");
210 }
211
212
213
214 void RS_ActionDrawHatch::updateMouseCursor() {
215     graphicView->setMouseCursor(RS2::CadCursor);
216 }
217
218
219
220 void RS_ActionDrawHatch::updateToolBar() {
221     RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
222 }
223
224
225 // EOF