]> Shamusworld >> Repos - architektonas/blob - src/actions/actionmodifytrimamount.cpp
7d2e4613c4ad812168d5250a4fcc66fb76e5958f
[architektonas] / src / actions / actionmodifytrimamount.cpp
1 // actionmodifytrimamount.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  06/04/2010  Added this text. :-)
15 //
16
17 #include "actionmodifytrimamount.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "rs_modification.h"
22
23 ActionModifyTrimAmount::ActionModifyTrimAmount(
24         RS_EntityContainer & container, GraphicView & graphicView):
25         ActionInterface("Trim Entity by a given amount",
26                 container, graphicView)
27 {
28         trimEntity = NULL;
29         trimCoord = Vector(false);
30         distance = 0.0;
31 }
32
33 ActionModifyTrimAmount::~ActionModifyTrimAmount()
34 {
35 }
36
37 /*virtual*/ RS2::ActionType ActionModifyTrimAmount::rtti()
38 {
39         return RS2::ActionModifyTrimAmount;
40 }
41
42 void ActionModifyTrimAmount::init(int status)
43 {
44         ActionInterface::init(status);
45
46 /*      snapMode = RS2::SnapFree;
47         snapRes = RS2::RestrictNothing;*/
48 }
49
50 void ActionModifyTrimAmount::trigger()
51 {
52         RS_DEBUG->print("ActionModifyTrimAmount::trigger()");
53
54         if (trimEntity != NULL && trimEntity->isAtomic())
55         {
56                 RS_Modification m(*container, graphicView);
57                 m.trimAmount(trimCoord, (RS_AtomicEntity *)trimEntity, distance);
58
59                 trimEntity = NULL;
60                 setStatus(ChooseTrimEntity);
61
62                 RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
63         }
64 }
65
66 void ActionModifyTrimAmount::mouseReleaseEvent(QMouseEvent * e)
67 {
68         trimCoord = graphicView->toGraph(e->x(), e->y());
69         trimEntity = catchEntity(e);
70
71         if (e->button() == Qt::LeftButton)
72         {
73                 switch (getStatus())
74                 {
75                 case ChooseTrimEntity:
76
77                         if (trimEntity != NULL && trimEntity->isAtomic())
78                                 trigger();
79                         else
80                         {
81                                 if (trimEntity == NULL)
82                                         RS_DIALOGFACTORY->commandMessage(
83                                                 tr("No entity found. "));
84                                 else if (trimEntity->rtti() == RS2::EntityInsert)
85                                         RS_DIALOGFACTORY->commandMessage(
86                                                 tr("The chosen Entity is in a block. "
87                                                         "Please edit the block."));
88                                 else
89                                         RS_DIALOGFACTORY->commandMessage(
90                                                 tr("The chosen Entity is not an atomic entity "
91                                                         "or cannot be trimmed."));
92                         }
93                         break;
94
95                 default:
96                         break;
97                 }
98         }
99         else if (e->button() == Qt::RightButton)
100         {
101                 deleteSnapper();
102                 init(getStatus() - 1);
103         }
104 }
105
106 void ActionModifyTrimAmount::commandEvent(RS_CommandEvent * e)
107 {
108         QString c = e->getCommand().toLower();
109
110         if (checkCommand("help", c))
111         {
112                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
113                         + getAvailableCommands().join(", "));
114                 return;
115         }
116
117         switch (getStatus())
118         {
119         case ChooseTrimEntity: {
120                 bool ok;
121                 double d = RS_Math::eval(c, &ok);
122
123                 if (ok == true)
124                         distance = d;
125                 else
126                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
127                 RS_DIALOGFACTORY->requestOptions(this, true, true);
128                 setStatus(ChooseTrimEntity);
129         }
130         break;
131
132         default:
133                 break;
134         }
135 }
136
137 QStringList ActionModifyTrimAmount::getAvailableCommands()
138 {
139         QStringList cmd;
140
141         switch (getStatus())
142         {
143         case ChooseTrimEntity:
144                 break;
145
146         default:
147                 break;
148         }
149
150         return cmd;
151 }
152
153 void ActionModifyTrimAmount::showOptions()
154 {
155         ActionInterface::showOptions();
156
157         RS_DIALOGFACTORY->requestOptions(this, true);
158 }
159
160 void ActionModifyTrimAmount::hideOptions()
161 {
162         ActionInterface::hideOptions();
163
164         RS_DIALOGFACTORY->requestOptions(this, false);
165 }
166
167 void ActionModifyTrimAmount::updateMouseButtonHints()
168 {
169         switch (getStatus())
170         {
171         case ChooseTrimEntity:
172                 RS_DIALOGFACTORY->updateMouseWidget(
173                         tr("Select entity to trim or enter distance:"), tr("Back"));
174                 break;
175
176         default:
177                 RS_DIALOGFACTORY->updateMouseWidget("", "");
178                 break;
179         }
180 }
181
182 void ActionModifyTrimAmount::updateMouseCursor()
183 {
184         graphicView->setMouseCursor(RS2::CadCursor);
185 }
186
187 void ActionModifyTrimAmount::updateToolBar()
188 {
189         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarModify);
190 }
191
192 double ActionModifyTrimAmount::getDistance()
193 {
194         return distance;
195 }
196
197 void ActionModifyTrimAmount::setDistance(double d)
198 {
199         distance = d;
200 }