ActiveStateFollower.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 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 #include "ActiveStateFollower.h"
23 
25 
26 namespace armarx
27 {
28 
29  ActiveStateFollower::ActiveStateFollower(StatechartView* statechartView, QWidget* parent) :
30  QVariantAnimation(parent), statechartView(statechartView)
31  {
32  duration = 1000;
33  connect(&activeStateCheckerTimer, SIGNAL(timeout()), this, SLOT(checkActiveState()));
34  activeStateCheckerTimer.setInterval(1000);
35  // activeStateCheckerTimer.start();
36  }
37 
38  void
40  {
41  if (on)
42  {
44  }
45  else
46  {
47  stopFollowing();
48  }
49  }
50 
51  void
53  {
55  }
56 
57  void
59  {
61  }
62 
63  void
65  {
66  if (!statechartView)
67  {
68  return;
69  }
70  auto state = statechartView->getStateInstance();
71  if (!state || !state->getStateClass())
72  {
73  return;
74  }
75  statechartmodel::StateInstancePtr activeState = state->getStateClass()->getActiveSubstate();
76  while (activeState)
77  {
78  if (!activeState->getStateClass() || !activeState->getStateClass()->getActiveSubstate())
79  {
80  break;
81  }
82  activeState = activeState->getStateClass()->getActiveSubstate();
83  }
84 
85  if (!activeState || activeState == currentActiveState)
86  {
87  return;
88  }
89  auto items = statechartView->getScene()->items();
90  QPointer<StateItem> stateItem;
91  for (QGraphicsItem* item : items)
92  {
93  stateItem = dynamic_cast<StateItem*>(item);
94  if (stateItem && activeState == stateItem->getStateInstance())
95  {
96  break;
97  }
98  }
99  if (currentActiveStateItem == stateItem)
100  {
101  return;
102  }
103  stop();
104  currentActiveStateItem = stateItem;
105  currentTargetPos = stateItem->mapToScene(stateItem->scenePos());
106  startPos = statechartView->getGraphicsView()
107  ->mapToScene(statechartView->getGraphicsView()->viewport()->rect())
108  .boundingRect()
109  .center();
110  if (statechartView->getGraphicsView()
111  ->viewport()
112  ->rect()
113  .adjusted(50, 50, -50, -50)
114  .contains(statechartView->getGraphicsView()->mapFromScene(currentTargetPos)))
115  {
116  ARMARX_DEBUG_S << "already visible: " << currentTargetPos;
117  return;
118  }
119  ARMARX_INFO_S << "travel distance: " << (startPos - currentTargetPos).manhattanLength();
120  // if ((startPos - currentTargetPos).manhattanLength() < 1) // dont do marginal motions
121  // {
122  // return;
123  // }
124  startTime = IceUtil::Time::now();
125  setStartValue(startPos);
126  setEndValue(currentTargetPos);
127  setEasingCurve(QEasingCurve::InOutQuad);
128  setDuration(duration);
129  start();
130  }
131 
134  {
135  return statechartView;
136  }
137 
138  void
140  {
142  }
143 
144 } // namespace armarx
145 
146 void
148 {
149 }
150 
151 void
153 {
154  ARMARX_INFO_S << "Manually triggered centering on state";
155  currentActiveStateItem = NULL;
156  checkActiveState();
157 }
158 
159 void
161 {
162  // ARMARX_INFO_S << "cur ani value : " << value.toPointF();
163  statechartView->getGraphicsView()->centerOn(value.toPointF());
164  // statechartView->getGraphicsView()->ensureVisible(QRectF(value.toPointF(), QSizeF(1, 1)));
165 }
armarx::ActiveStateFollower::updatePos
void updatePos(QVariant value)
Definition: ActiveStateFollower.cpp:147
armarx::ActiveStateFollower::duration
int duration
Definition: ActiveStateFollower.h:59
armarx::ActiveStateFollower::statechartView
QPointer< StatechartView > statechartView
Definition: ActiveStateFollower.h:56
ActiveStateFollower.h
StateItem.h
armarx::ActiveStateFollower::activeStateCheckerTimer
QTimer activeStateCheckerTimer
Definition: ActiveStateFollower.h:57
armarx::ActiveStateFollower::currentActiveStateItem
QPointer< StateItem > currentActiveStateItem
Definition: ActiveStateFollower.h:54
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:146
armarx::ActiveStateFollower::startPos
QPointF startPos
Definition: ActiveStateFollower.h:55
armarx::ActiveStateFollower::startTime
IceUtil::Time startTime
Definition: ActiveStateFollower.h:58
armarx::StatechartView
Definition: StatechartView.h:45
armarx::ActiveStateFollower::currentActiveState
statechartmodel::StateInstancePtr currentActiveState
Definition: ActiveStateFollower.h:53
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::ActiveStateFollower::toggle
void toggle(bool on)
Definition: ActiveStateFollower.cpp:39
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:205
armarx::ActiveStateFollower::currentTargetPos
QPointF currentTargetPos
Definition: ActiveStateFollower.h:55
armarx::ActiveStateFollower::ActiveStateFollower
ActiveStateFollower(StatechartView *statechartView, QWidget *parent=NULL)
Definition: ActiveStateFollower.cpp:29
armarx::ActiveStateFollower::getStatechartView
StatechartView * getStatechartView() const
Definition: ActiveStateFollower.cpp:133
armarx::ActiveStateFollower::stopFollowing
void stopFollowing()
Definition: ActiveStateFollower.cpp:58
armarx::ActiveStateFollower::startFollowing
void startFollowing()
Definition: ActiveStateFollower.cpp:52
armarx::ActiveStateFollower::centerOnCurrentState
void centerOnCurrentState(bool toggle)
Definition: ActiveStateFollower.cpp:152
armarx::StateItem
Definition: StateItem.h:58
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
armarx::ActiveStateFollower::checkActiveState
void checkActiveState()
Definition: ActiveStateFollower.cpp:64
armarx::ActiveStateFollower::updateCurrentValue
void updateCurrentValue(const QVariant &value) override
Definition: ActiveStateFollower.cpp:160
armarx::ActiveStateFollower::setStatechartView
void setStatechartView(StatechartView *value)
Definition: ActiveStateFollower.cpp:139
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27