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