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 "../State.h"
26 
27 #include <cmath>
28 
30 {
31  const QSizeF StateInstance::StateDefaultSize = QSizeF(800, 600);
32 
33  StateInstance::StateInstance(const QString& instanceName, StatePtr parentState) :
34  name(instanceName),
35  parentState(parentState),
36  position(QPointF(StateDefaultSize.toSize().width() * 0.1 + rand() % StateDefaultSize.toSize().width() * 0.7, StateDefaultSize.toSize().height() * 0.3 + rand() % StateDefaultSize.toSize().height()) * 0.7),
37  boundingSquareSize(defaultBoundingSquareSize)
38  // size(DEFAULTSIZE)
39  // scale(0.3f)
40  {
41 
42  }
43 
45  {
46  if (name.length() > 0)
47  {
48  return name;
49  }
50  else if (getStateClass())
51  {
52  return getStateClass()->getStateName();
53  }
54  else
55  {
56  return "Undefined";
57  }
58  }
59 
61  {
62  if (name == value)
63  {
64  if (getParent())
65  {
66  emit getParent()->substateChanged(shared_from_this(), eUnchanged);
67  }
68  }
69  else
70  {
71  name = value;
72 
73  if (getParent())
74  {
75  emit getParent()->substateChanged(shared_from_this(), eChanged);
76  }
77  }
78  }
79 
80  const QPointF StateInstance::getCenter() const
81  {
82  QPointF newPosition = position + QPointF(boundingSquareSize * 0.5f, boundingSquareSize * 0.5f);
83 
84  return newPosition;
85  }
86 
88  {
89  QRectF result;
90  result.setTopLeft(getTopLeft());
91  result.setWidth(boundingSquareSize);
92  result.setHeight(boundingSquareSize);
93  return result;
94  }
95 
96 
97 
98  //void StateInstance::setBounds(const QRectF &newBounds)
99  //{
100  // ARMARX_INFO_S << "new bounds of " << getInstanceName();
101  // bounds = newBounds;
102  // if(parentState)
103  // emit parentState->substateChanged(shared_from_this(), eUpdated);
104  //}
105 
106 
107  QPointF StateInstance::adjustPosition(QPointF& newPos) const
108  {
109  auto parent = getParent();
110  if (!parent)
111  {
112  return newPos;
113  }
114 
115  QRectF rect(QPointF(0, 0), parent->getSize());
116  QPointF oldPos = getTopLeft();
117 
118  auto rm = parent->margin.width();
119  // auto statewidth = getBoundingSquareSize();
120  auto right = rect.right();
121  auto left = rect.left();
122  auto lm = parent->margin.left();
123  auto bw = getBounds().width();
124  newPos.setX(qMin(right - bw - rm,
125  qMax(newPos.x(),
126  left + lm)));
127  newPos.setY(qMin(rect.bottom() - getBounds().height() - parent->margin.height(),
128  qMax(newPos.y(),
129  rect.top() + parent->margin.top())));
130 
131  return oldPos;
132  }
133 
134  void StateInstance::setPosition(QPointF newPosition)
135  {
136  if (std::isnan(newPosition.x()) || std::isnan(newPosition.y()))
137  {
138  ARMARX_WARNING_S << "new Position for " << getInstanceName() << " contains NaN. wont set it";
139  return;
140  }
141 
142  adjustPosition(newPosition);
143  if ((position - newPosition).manhattanLength() < 2)
144  {
145  // ARMARX_INFO_S << getInstanceName() << ":UNCHANGED " << newPosition << "\n" << LogSender::CreateBackTrace();
146 
147  if (getParent())
148  {
149  emit getParent()->substateChanged(shared_from_this(), eUnchanged);
150  }
151  else
152  {
153  // ARMARX_INFO << " No parent for state " << getInstanceName();
154  }
155  }
156  else
157  {
158  // ARMARX_INFO_S << getInstanceName() << ":CHANGED " << newPosition << "\n" << LogSender::CreateBackTrace();
159  position = newPosition;
160  // ARMARX_INFO_S << getInstanceName() << ": " << position << ": " << newPosition << " Distance: " << (position - newPosition).manhattanLength();
161  if (getParent())
162  {
163  emit getParent()->substateChanged(shared_from_this(), eChanged);
164  }
165  else
166  {
167  // ARMARX_INFO << " No parent for state " << getInstanceName();
168  }
169  }
170  }
171 
172  void StateInstance::setCenter(const QPointF& newStateCenter)
173  {
174  if (std::isnan(newStateCenter.x()) || std::isnan(newStateCenter.y()))
175  {
176  ARMARX_WARNING_S << "new Position for " << getInstanceName() << " contains NaN. wont setting it";
177  return;
178  }
179 
180  // ARMARX_INFO_S << getInstanceName() << ": " << position << ": " << newStateCenter;
181 
182 
183  QPointF newPosition = newStateCenter - QPointF(boundingSquareSize * 0.5f, boundingSquareSize * 0.5f);
184  setPosition(newPosition);
185  }
186 
187  void StateInstance::setBoundingBox(float squareSize)
188  {
189  // ARMARX_INFO << "new bb size: " << squareSize;
190  if (boundingSquareSize == squareSize)
191  {
192  if (getParent())
193  {
194  emit getParent()->substateChanged(shared_from_this(), eUnchanged);
195  }
196  }
197  else
198  {
199  boundingSquareSize = squareSize;
200 
201  if (getParent())
202  {
203  emit getParent()->substateChanged(shared_from_this(), eChanged);
204  }
205  }
206  }
207 
209  {
210  if (getParent())
211  {
212  emit getParent()->substateChanged(shared_from_this(), eChanged);
213  }
214  }
215 
216  //void StateInstance::setScale(float newScale)
217  //{
218  // if(scale == newScale)
219  // return;
220  //// ARMARX_INFO_S << "new Scale: " << newScale;
221  // scale = newScale;
222  // if(parentState)
223  // emit parentState->substateChanged(shared_from_this(), eUpdated);
224  //}
225 
227  {
228  if (getStateClass())
229  {
230  return getStateClass()->getSize();
231  }
232  else
233  {
235  }
236  }
237 
238 
240  {
241  float xRatio = boundingSquareSize / getClassSize().width();
242  float yRatio = boundingSquareSize / getClassSize().height();
243  return std::min(xRatio, yRatio);
244  }
245 
246 
247 
248 
250  {
251  // return active;
252  if (getParent() && getParent()->getActiveSubstate() == shared_from_this())
253  {
254  return true;
255  }
256  else
257  {
258  return false;
259  }
260  }
261 
262  //void statechartmodel::StateInstance::setActive(bool status)
263  //{
264  // if (active != status)
265  // {
266  // active = status;
267  // emit getParent()->substateChanged(shared_from_this(), status ? eActivated : eDeactivated);
268 
269  // if (!status && getStateClass())
270  // {
271  // // this does not work with orthogonal states
272  // getStateClass()->clearActiveSubstates();
273  // }
274 
275  // }
276  // else
277  // {
278  // emit getParent()->substateChanged(shared_from_this(), eUnchanged);
279  // }
280  //}
281 }
armarx::statechartmodel::StateInstance::adjustPosition
QPointF adjustPosition(QPointF &newPos) const
Definition: StateInstance.cpp:107
armarx::statechartmodel::StateInstance::updateScale
void updateScale()
Definition: StateInstance.cpp:208
armarx::statechartmodel::StateInstance::getStateClass
virtual StatePtr getStateClass() const
Definition: StateInstance.h:55
armarx::statechartmodel::StateInstance::StateInstance
StateInstance(const QString &instanceName, StatePtr parentState=StatePtr())
Definition: StateInstance.cpp:33
armarx::statechartmodel::eUnchanged
@ eUnchanged
Definition: SignalType.h:36
armarx::statechartmodel::StateInstance::setCenter
void setCenter(const QPointF &newStateCenter)
Definition: StateInstance.cpp:172
armarx::statechartmodel::StateInstance::setPosition
void setPosition(QPointF newPosition)
Definition: StateInstance.cpp:134
armarx::statechartmodel::StateInstance::name
QString name
Definition: StateInstance.h:132
armarx::statechartmodel::StateInstance::getCenter
const QPointF getCenter() const
Definition: StateInstance.cpp:80
armarx::statechartmodel::StateInstance::setBoundingBox
void setBoundingBox(float squareSize)
Definition: StateInstance.cpp:187
armarx::statechartmodel::StateInstance::position
QPointF position
Definition: StateInstance.h:135
armarx::statechartmodel::StateInstance::isActive
bool isActive()
Definition: StateInstance.cpp:249
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::statechartmodel
Definition: XmlWriter.h:36
armarx::statechartmodel::StateInstance::getBoundingSquare
QRectF getBoundingSquare() const
getBoundingSquare return the maximum bounding box of this state instance in parent coordinate system
Definition: StateInstance.cpp:87
armarx::statechartmodel::StateInstance::getParent
StatePtr getParent() const
Definition: StateInstance.h:59
armarx::statechartmodel::StateInstance::getBounds
QRectF getBounds() const
getBounds returns the rectangle occupied by this state instance in the parent coordinate system.
Definition: StateInstance.h:87
armarx::statechartmodel::StateInstance::getTopLeft
const QPointF & getTopLeft() const
getTopLeft returns the topleft point of the stateinstance in the coordinate system of the parent stat...
Definition: StateInstance.h:68
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
StateInstance.h
armarx::statechartmodel::StateInstance::StateDefaultSize
static const QSizeF StateDefaultSize
Definition: StateInstance.h:110
armarx::statechartmodel::StateInstance::getScale
float getScale() const
getScale returns the scale of this state instance, which is the relation between boundingSquareSize a...
Definition: StateInstance.cpp:239
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
armarx::statechartmodel::eChanged
@ eChanged
Definition: SignalType.h:37
armarx::statechartmodel::StateInstance::setInstanceName
virtual void setInstanceName(const QString &value)
Definition: StateInstance.cpp:60
armarx::statechartmodel::StateInstance::getClassSize
QSizeF getClassSize() const
Definition: StateInstance.cpp:226
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::statechartmodel::StateInstance::boundingSquareSize
float boundingSquareSize
Definition: StateInstance.h:136
armarx::statechartmodel::StateInstance::getInstanceName
QString getInstanceName() const
Definition: StateInstance.cpp:44