]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawhatch.cpp
Major refactoring of actions: Moved implementation from header files
[architektonas] / src / actions / rs_actiondrawhatch.cpp
1 // rs_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 "rs_actiondrawhatch.h"
16
17 #include "rs_dialogfactory.h"
18 #include "rs_graphicview.h"
19 #include "rs_information.h"
20
21 RS_ActionDrawHatch::RS_ActionDrawHatch(RS_EntityContainer & container, RS_GraphicView & graphicView):
22         RS_PreviewActionInterface("Draw Hatch", container, graphicView)
23 {
24         hatch = NULL;
25 }
26
27 RS_ActionDrawHatch::~RS_ActionDrawHatch()
28 {
29 }
30
31 void RS_ActionDrawHatch::init(int status)
32 {
33         RS_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 RS_ActionDrawHatch::trigger()
52 {
53         RS_DEBUG->print("RS_ActionDrawHatch::trigger()");
54
55         //if (pos.valid) {
56         //deletePreview();
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                 if (e->isSelected()
64                     && (e->rtti() == RS2::EntityHatch
65                         || e->rtti() == RS2::EntityEllipse || e->rtti() == RS2::EntityPoint
66                         || e->rtti() == RS2::EntityText
67                         || RS_Information::isDimension(e->rtti())))
68                         e->setSelected(false);
69
70
71         for (e = container->firstEntity(RS2::ResolveAll); e != NULL;
72              e = container->nextEntity(RS2::ResolveAll))
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         // look for selected contours:
81         bool haveContour = false;
82
83         for (e = container->firstEntity(RS2::ResolveAll); e != NULL;
84              e = container->nextEntity(RS2::ResolveAll))
85                 if (e->isSelected())
86                         haveContour = true;
87
88         if (!haveContour)
89         {
90                 std::cerr << "no contour selected\n";
91                 return;
92         }
93
94         hatch = new RS_Hatch(container, data);
95         hatch->setLayerToActive();
96         hatch->setPenToActive();
97         RS_EntityContainer * loop = new RS_EntityContainer(hatch);
98         loop->setPen(RS_Pen(RS2::FlagInvalid));
99
100         // add selected contour:
101         for (RS_Entity * e = container->firstEntity(RS2::ResolveAll); e != NULL;
102              e = container->nextEntity(RS2::ResolveAll))
103
104                 if (e->isSelected())
105                 {
106                         e->setSelected(false);
107
108                         // entity is part of a complex entity (spline, polyline, ..):
109                         if (e->getParent() != NULL
110                             && (e->getParent()->rtti() == RS2::EntitySpline
111                                 || e->getParent()->rtti() == RS2::EntityPolyline))
112                                 e->getParent()->setSelected(false);
113                         RS_Entity * cp = e->clone();
114                         cp->setPen(RS_Pen(RS2::FlagInvalid));
115                         cp->reparent(loop);
116                         loop->addEntity(cp);
117                 }
118
119         hatch->addEntity(loop);
120
121         if (hatch->validate())
122         {
123                 container->addEntity(hatch);
124
125                 if (document)
126                 {
127                         document->startUndoCycle();
128                         document->addUndoable(hatch);
129                         document->endUndoCycle();
130                 }
131                 hatch->update();
132
133                 graphicView->drawEntity(hatch);
134
135                 RS_DIALOGFACTORY->commandMessage(tr("Hatch created successfully."));
136         }
137         else
138         {
139                 delete hatch;
140                 hatch = NULL;
141                 RS_DIALOGFACTORY->commandMessage(tr("Invalid hatch area. Please check that "
142                                 "the entities chosen form one or more closed contours."));
143         }
144         //}
145 }
146
147 void RS_ActionDrawHatch::mouseMoveEvent(QMouseEvent *)
148 {
149         RS_DEBUG->print("RS_ActionDrawHatch::mouseMoveEvent begin");
150
151         /*if (getStatus()==SetPos) {
152             Vector mouse = snapPoint(e);
153             pos = mouse;
154
155
156             deletePreview();
157             if (hatch!=NULL && !hatch->isVisible()) {
158                 hatch->setVisible(true);
159             }
160             offset = Vector(graphicView->toGuiDX(pos.x),
161                                -graphicView->toGuiDY(pos.y));
162             drawPreview();
163            }*/
164
165         RS_DEBUG->print("RS_ActionDrawHatch::mouseMoveEvent end");
166 }
167
168 void RS_ActionDrawHatch::mouseReleaseEvent(QMouseEvent * e)
169 {
170 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
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 (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
185         else if (e->button() == Qt::RightButton)
186         {
187                 //deletePreview();
188                 deleteSnapper();
189                 init(getStatus() - 1);
190         }
191 }
192
193 void RS_ActionDrawHatch::updateMouseButtonHints()
194 {
195         RS_DIALOGFACTORY->updateMouseWidget("", "");
196 }
197
198 void RS_ActionDrawHatch::updateMouseCursor()
199 {
200         graphicView->setMouseCursor(RS2::CadCursor);
201 }
202
203 void RS_ActionDrawHatch::updateToolBar()
204 {
205         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
206 }
207