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