]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actionmodifydeletefree.cpp
2a4c8bb72dc6a2ec4515724c2584267ca47efeb5
[architektonas] / src / actions / rs_actionmodifydeletefree.cpp
1 // rs_actionmodifydeletefree.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  06/04/2010  Added this text. :-)
13 //
14
15 #include "rs_actionmodifydeletefree.h"
16
17 #include "rs_dialogfactory.h"
18 #include "graphicview.h"
19 #include "rs_modification.h"
20 #include "rs_polyline.h"
21 #include "rs_undo.h"
22
23 RS_ActionModifyDeleteFree::RS_ActionModifyDeleteFree(
24         RS_EntityContainer & container, GraphicView & graphicView):
25         RS_ActionInterface("Delete Entities Freehand",
26                 container, graphicView)
27 {
28 }
29
30 RS_ActionModifyDeleteFree::~RS_ActionModifyDeleteFree()
31 {
32 }
33
34 void RS_ActionModifyDeleteFree::init(int status)
35 {
36         RS_ActionInterface::init(status);
37         polyline = NULL;
38         e1 = e2 = NULL;
39         v1 = v2 = Vector(false);
40         setSnapMode(RS2::SnapOnEntity);
41 }
42
43 void RS_ActionModifyDeleteFree::trigger()
44 {
45         if (e1 != NULL && e2 != NULL)
46         {
47                 RS_EntityContainer * parent = e2->getParent();
48
49                 if (parent != NULL)
50                 {
51                         if (parent->rtti() == RS2::EntityPolyline)
52                         {
53                                 if (parent->getId() == polyline->getId())
54                                 {
55                                         // deletes whole polyline on screen:
56                                         graphicView->deleteEntity((RS_Entity *)polyline);
57
58                                         // splits up the polyline in the container:
59                                         RS_Polyline * pl1;
60                                         RS_Polyline * pl2;
61                                         RS_Modification m(*container);
62                                         m.splitPolyline(*polyline,
63                                                 *e1, v1,
64                                                 *e2, v2,
65                                                 &pl1, &pl2);
66
67                                         if (document)
68                                         {
69                                                 document->startUndoCycle();
70                                                 document->addUndoable(polyline);
71                                                 document->addUndoable(pl1);
72                                                 document->addUndoable(pl2);
73                                                 document->endUndoCycle();
74                                         }
75
76                                         // draws the new polylines on the screen:
77                                         graphicView->drawEntity((RS_Entity *)pl1);
78                                         graphicView->drawEntity((RS_Entity *)pl2);
79
80                                         init();
81
82                                         RS_DIALOGFACTORY->updateSelectionWidget(
83                                                 container->countSelected());
84                                 }
85                                 else
86                                         RS_DIALOGFACTORY->commandMessage(tr("Entities not in the same polyline."));
87                         }
88                         else
89                                 RS_DIALOGFACTORY->commandMessage(tr("Parent of second entity is not a polyline"));
90                 }
91                 else
92                         RS_DIALOGFACTORY->commandMessage(tr("Parent of second entity is NULL"));
93         }
94         else
95                 RS_DIALOGFACTORY->commandMessage(tr("One of the chosen entities is NULL"));
96 }
97
98 void RS_ActionModifyDeleteFree::mouseReleaseEvent(QMouseEvent * e)
99 {
100         if (e->button() == Qt::RightButton)
101                 init(getStatus() - 1);
102         else
103         {
104                 switch (getStatus())
105                 {
106                 case 0:
107                         v1 = snapPoint(e);
108                         e1 = getKeyEntity();
109
110                         if (e1 != NULL)
111                         {
112                                 RS_EntityContainer * parent = e1->getParent();
113
114                                 if (parent != NULL)
115                                 {
116                                         if (parent->rtti() == RS2::EntityPolyline)
117                                         {
118                                                 polyline = (RS_Polyline *)parent;
119                                                 setStatus(1);
120                                         }
121                                         else
122                                                 RS_DIALOGFACTORY->commandMessage(
123                                                         tr("Parent of first entity is not a polyline"));
124                                 }
125                                 else
126                                         RS_DIALOGFACTORY->commandMessage(
127                                                 tr("Parent of first entity is NULL"));
128                         }
129                         else
130                                 RS_DIALOGFACTORY->commandMessage(
131                                         tr("First entity is NULL"));
132
133                         break;
134
135                 case 1:
136                         v2 = snapPoint(e);
137                         e2 = getKeyEntity();
138
139                         if (e2 != NULL)
140                                 trigger();
141                         else
142                                 RS_DIALOGFACTORY->commandMessage(tr("Second entity is NULL"));
143
144                         break;
145                 }
146         }
147 }
148
149 void RS_ActionModifyDeleteFree::updateMouseButtonHints()
150 {
151         switch (getStatus())
152         {
153         case 0:
154                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first break point "
155                                 "on a polyline"), tr("Cancel"));
156                 break;
157
158         case 1:
159                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second break point "
160                                 "on the same polyline"),
161                         tr("Back"));
162                 break;
163
164         default:
165                 RS_DIALOGFACTORY->updateMouseWidget("", "");
166                 break;
167         }
168 }
169