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