]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawhatch.cpp
e2332b5efe2cd08886adc5fdd1f54555de611b60
[architektonas] / src / actions / actiondrawhatch.cpp
1 // actiondrawhatch.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/22/2010  Added this text. :-)
15 //
16
17 #include "actiondrawhatch.h"
18
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_information.h"
22
23 ActionDrawHatch::ActionDrawHatch(RS_EntityContainer & container, GraphicView & graphicView):
24         ActionInterface("Draw Hatch", container, graphicView)
25 {
26         hatch = NULL;
27 }
28
29 ActionDrawHatch::~ActionDrawHatch()
30 {
31 }
32
33 void ActionDrawHatch::init(int status)
34 {
35         ActionInterface::init(status);
36         clearPreview();
37
38         RS_Hatch tmp(container, data);
39         tmp.setLayerToActive();
40         tmp.setPenToActive();
41
42         if (RS_DIALOGFACTORY->requestHatchDialog(&tmp))
43         {
44                 data = tmp.getData();
45                 trigger();
46                 finish();
47                 graphicView->redraw();
48         }
49         else
50                 finish();
51 }
52
53 void ActionDrawHatch::trigger()
54 {
55         RS_DEBUG->print("ActionDrawHatch::trigger()");
56
57         deleteSnapper();
58         RS_Entity * e;
59
60         // deselect unhatchable entities:
61         for(e=container->firstEntity(RS2::ResolveNone); e!=NULL;
62                 e=container->nextEntity(RS2::ResolveNone))
63         {
64                 if (e->isSelected()
65                     && (e->rtti() == RS2::EntityHatch
66                         || e->rtti() == RS2::EntityEllipse || e->rtti() == RS2::EntityPoint
67                         || e->rtti() == RS2::EntityText
68                         || RS_Information::isDimension(e->rtti())))
69                         e->setSelected(false);
70         }
71
72         for(e=container->firstEntity(RS2::ResolveAll); e!=NULL;
73                 e=container->nextEntity(RS2::ResolveAll))
74         {
75                 if (e->isSelected()
76                         && (e->rtti() == RS2::EntityHatch
77                         || e->rtti() == RS2::EntityEllipse || e->rtti() == RS2::EntityPoint
78                         || e->rtti() == RS2::EntityText
79                         || RS_Information::isDimension(e->rtti())))
80                         e->setSelected(false);
81         }
82
83         // look for selected contours:
84         bool haveContour = false;
85
86         for(e=container->firstEntity(RS2::ResolveAll); e!=NULL;
87                 e=container->nextEntity(RS2::ResolveAll))
88                 if (e->isSelected())
89                         haveContour = true;
90
91         if (!haveContour)
92         {
93                 std::cerr << "no contour selected\n";
94                 return;
95         }
96
97         hatch = new RS_Hatch(container, data);
98         hatch->setLayerToActive();
99         hatch->setPenToActive();
100         RS_EntityContainer * loop = new RS_EntityContainer(hatch);
101         loop->setPen(RS_Pen(RS2::FlagInvalid));
102
103         // add selected contour:
104         for(RS_Entity * e=container->firstEntity(RS2::ResolveAll); e!=NULL;
105              e=container->nextEntity(RS2::ResolveAll))
106         {
107                 if (e->isSelected())
108                 {
109                         e->setSelected(false);
110
111                         // entity is part of a complex entity (spline, polyline, ..):
112                         if (e->getParent() != NULL
113                                 && (e->getParent()->rtti() == RS2::EntitySpline
114                                 || e->getParent()->rtti() == RS2::EntityPolyline))
115                                 e->getParent()->setSelected(false);
116
117                         RS_Entity * cp = e->clone();
118                         cp->setPen(RS_Pen(RS2::FlagInvalid));
119                         cp->reparent(loop);
120                         loop->addEntity(cp);
121                 }
122         }
123
124         hatch->addEntity(loop);
125
126         if (hatch->validate())
127         {
128                 container->addEntity(hatch);
129
130                 if (document)
131                 {
132                         document->startUndoCycle();
133                         document->addUndoable(hatch);
134                         document->endUndoCycle();
135                 }
136
137                 hatch->update();
138                 graphicView->drawEntity(hatch);
139                 RS_DIALOGFACTORY->commandMessage(tr("Hatch created successfully."));
140         }
141         else
142         {
143                 delete hatch;
144                 hatch = NULL;
145                 RS_DIALOGFACTORY->commandMessage(tr("Invalid hatch area. Please check that "
146                         "the entities chosen form one or more closed contours."));
147         }
148 }
149
150 void ActionDrawHatch::mouseMoveEvent(QMouseEvent *)
151 {
152         RS_DEBUG->print("ActionDrawHatch::mouseMoveEvent begin");
153
154         /*if (getStatus()==SetPos) {
155             Vector mouse = snapPoint(e);
156             pos = mouse;
157
158
159             deletePreview();
160             if (hatch!=NULL && !hatch->isVisible()) {
161                 hatch->setVisible(true);
162             }
163             offset = Vector(graphicView->toGuiDX(pos.x),
164                                -graphicView->toGuiDY(pos.y));
165             drawPreview();
166            }*/
167
168         RS_DEBUG->print("ActionDrawHatch::mouseMoveEvent end");
169 }
170
171 void ActionDrawHatch::mouseReleaseEvent(QMouseEvent * e)
172 {
173         if (e->button() == Qt::LeftButton)
174         {
175                 Vector mouse = snapPoint(e);
176
177                 switch (getStatus())
178                 {
179                 case ShowDialog:
180                         break;
181
182                 default:
183                         break;
184                 }
185         }
186         else if (e->button() == Qt::RightButton)
187         {
188                 //deletePreview();
189                 deleteSnapper();
190                 init(getStatus() - 1);
191         }
192 }
193
194 void ActionDrawHatch::updateMouseButtonHints()
195 {
196         RS_DIALOGFACTORY->updateMouseWidget("", "");
197 }
198
199 void ActionDrawHatch::updateMouseCursor()
200 {
201         graphicView->setMouseCursor(RS2::CadCursor);
202 }
203
204 void ActionDrawHatch::updateToolBar()
205 {
206         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
207 }