StateInstance.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2014
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "StateInstance.h"
24
25#include <cmath>
26
27#include "../State.h"
28
30{
31 const QSizeF StateInstance::StateDefaultSize = QSizeF(800, 600);
32
33 StateInstance::StateInstance(const QString& instanceName, StatePtr parentState) :
34 name(instanceName),
36 position(QPointF(StateDefaultSize.toSize().width() * 0.1 +
37 rand() % StateDefaultSize.toSize().width() * 0.7,
38 StateDefaultSize.toSize().height() * 0.3 +
39 rand() % StateDefaultSize.toSize().height()) *
40 0.7),
42 // size(DEFAULTSIZE)
43 // scale(0.3f)
44 {
45 }
46
47 QString
49 {
50 if (name.length() > 0)
51 {
52 return name;
53 }
54 else if (getStateClass())
55 {
56 return getStateClass()->getStateName();
57 }
58 else
59 {
60 return "Undefined";
61 }
62 }
63
64 void
65 StateInstance::setInstanceName(const QString& value)
66 {
67 if (name == value)
68 {
69 if (getParent())
70 {
71 emit getParent()->substateChanged(shared_from_this(), eUnchanged);
72 }
73 }
74 else
75 {
76 name = value;
77
78 if (getParent())
79 {
80 emit getParent()->substateChanged(shared_from_this(), eChanged);
81 }
82 }
83 }
84
85 const QPointF
87 {
88 QPointF newPosition =
89 position + QPointF(boundingSquareSize * 0.5f, boundingSquareSize * 0.5f);
90
91 return newPosition;
92 }
93
94 QRectF
96 {
97 QRectF result;
98 result.setTopLeft(getTopLeft());
99 result.setWidth(boundingSquareSize);
100 result.setHeight(boundingSquareSize);
101 return result;
102 }
103
104 //void StateInstance::setBounds(const QRectF &newBounds)
105 //{
106 // ARMARX_INFO_S << "new bounds of " << getInstanceName();
107 // bounds = newBounds;
108 // if(parentState)
109 // emit parentState->substateChanged(shared_from_this(), eUpdated);
110 //}
111
112
113 QPointF
114 StateInstance::adjustPosition(QPointF& newPos) const
115 {
116 auto parent = getParent();
117 if (!parent)
118 {
119 return newPos;
120 }
121
122 QRectF rect(QPointF(0, 0), parent->getSize());
123 QPointF oldPos = getTopLeft();
124
125 auto rm = parent->margin.width();
126 // auto statewidth = getBoundingSquareSize();
127 auto right = rect.right();
128 auto left = rect.left();
129 auto lm = parent->margin.left();
130 auto bw = getBounds().width();
131 newPos.setX(qMin(right - bw - rm, qMax(newPos.x(), left + lm)));
132 newPos.setY(qMin(rect.bottom() - getBounds().height() - parent->margin.height(),
133 qMax(newPos.y(), rect.top() + parent->margin.top())));
134
135 return oldPos;
136 }
137
138 void
139 StateInstance::setPosition(QPointF newPosition)
140 {
141 if (std::isnan(newPosition.x()) || std::isnan(newPosition.y()))
142 {
143 ARMARX_WARNING_S << "new Position for " << getInstanceName()
144 << " contains NaN. wont set it";
145 return;
146 }
147
148 adjustPosition(newPosition);
149 if ((position - newPosition).manhattanLength() < 2)
150 {
151 // ARMARX_INFO_S << getInstanceName() << ":UNCHANGED " << newPosition << "\n" << LogSender::CreateBackTrace();
152
153 if (getParent())
154 {
155 emit getParent()->substateChanged(shared_from_this(), eUnchanged);
156 }
157 else
158 {
159 // ARMARX_INFO << " No parent for state " << getInstanceName();
160 }
161 }
162 else
163 {
164 // ARMARX_INFO_S << getInstanceName() << ":CHANGED " << newPosition << "\n" << LogSender::CreateBackTrace();
165 position = newPosition;
166 // ARMARX_INFO_S << getInstanceName() << ": " << position << ": " << newPosition << " Distance: " << (position - newPosition).manhattanLength();
167 if (getParent())
168 {
169 emit getParent()->substateChanged(shared_from_this(), eChanged);
170 }
171 else
172 {
173 // ARMARX_INFO << " No parent for state " << getInstanceName();
174 }
175 }
176 }
177
178 void
179 StateInstance::setCenter(const QPointF& newStateCenter)
180 {
181 if (std::isnan(newStateCenter.x()) || std::isnan(newStateCenter.y()))
182 {
183 ARMARX_WARNING_S << "new Position for " << getInstanceName()
184 << " contains NaN. wont setting it";
185 return;
186 }
187
188 // ARMARX_INFO_S << getInstanceName() << ": " << position << ": " << newStateCenter;
189
190
191 QPointF newPosition =
192 newStateCenter - QPointF(boundingSquareSize * 0.5f, boundingSquareSize * 0.5f);
193 setPosition(newPosition);
194 }
195
196 void
198 {
199 // ARMARX_INFO << "new bb size: " << squareSize;
200 if (boundingSquareSize == squareSize)
201 {
202 if (getParent())
203 {
204 emit getParent()->substateChanged(shared_from_this(), eUnchanged);
205 }
206 }
207 else
208 {
209 boundingSquareSize = squareSize;
210
211 if (getParent())
212 {
213 emit getParent()->substateChanged(shared_from_this(), eChanged);
214 }
215 }
216 }
217
218 void
220 {
221 if (getParent())
222 {
223 emit getParent()->substateChanged(shared_from_this(), eChanged);
224 }
225 }
226
227 //void StateInstance::setScale(float newScale)
228 //{
229 // if(scale == newScale)
230 // return;
231 //// ARMARX_INFO_S << "new Scale: " << newScale;
232 // scale = newScale;
233 // if(parentState)
234 // emit parentState->substateChanged(shared_from_this(), eUpdated);
235 //}
236
237 QSizeF
239 {
240 if (getStateClass())
241 {
242 return getStateClass()->getSize();
243 }
244 else
245 {
247 }
248 }
249
250 float
252 {
253 float xRatio = boundingSquareSize / getClassSize().width();
254 float yRatio = boundingSquareSize / getClassSize().height();
255 return std::min(xRatio, yRatio);
256 }
257
258 bool
260 {
261 // return active;
262 if (getParent() && getParent()->getActiveSubstate() == shared_from_this())
263 {
264 return true;
265 }
266 else
267 {
268 return false;
269 }
270 }
271
272 //void statechartmodel::StateInstance::setActive(bool status)
273 //{
274 // if (active != status)
275 // {
276 // active = status;
277 // emit getParent()->substateChanged(shared_from_this(), status ? eActivated : eDeactivated);
278
279 // if (!status && getStateClass())
280 // {
281 // // this does not work with orthogonal states
282 // getStateClass()->clearActiveSubstates();
283 // }
284
285 // }
286 // else
287 // {
288 // emit getParent()->substateChanged(shared_from_this(), eUnchanged);
289 // }
290 //}
291} // namespace armarx::statechartmodel
StateInstance(const QString &instanceName, StatePtr parentState=StatePtr())
QRectF getBounds() const
getBounds returns the rectangle occupied by this state instance in the parent coordinate system.
const QPointF & getTopLeft() const
getTopLeft returns the topleft point of the stateinstance in the coordinate system of the parent stat...
QPointF adjustPosition(QPointF &newPos) const
QRectF getBoundingSquare() const
getBoundingSquare return the maximum bounding box of this state instance in parent coordinate system
virtual void setInstanceName(const QString &value)
float getScale() const
getScale returns the scale of this state instance, which is the relation between boundingSquareSize a...
void setPosition(QPointF newPosition)
virtual StatePtr getStateClass() const
void setCenter(const QPointF &newStateCenter)
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::shared_ptr< State > StatePtr
Definition State.h:48