pathplayer.cc 4.47 KB
Newer Older
1
#include "pathplayer.h"
Joseph Mirabel's avatar
Joseph Mirabel committed
2
3
4
5
6

#include <hpp/corbaserver/common.hh>
#include <hpp/corbaserver/client.hh>

#include "hpp/gui/mainwindow.h"
7
8
#include <hpp/gui/windows-manager.h>
#include <hpp/gui/osgwidget.h>
Joseph Mirabel's avatar
Joseph Mirabel committed
9

10
11
12
#include "ui_pathplayerwidget.h"

PathPlayer::PathPlayer (HppWidgetsPlugin *plugin, QWidget *parent) :
Joseph Mirabel's avatar
Joseph Mirabel committed
13
  QWidget (parent)
14
, ui_ (new Ui::PathPlayerWidget)
Joseph Mirabel's avatar
Joseph Mirabel committed
15
, frameRate_ (25)
16
, plugin_ (plugin)
Joseph Mirabel's avatar
Joseph Mirabel committed
17
{
18
  ui_->setupUi (this);
Joseph Mirabel's avatar
Joseph Mirabel committed
19
20
21
22
23
24
  pathIndex()->setMaximum(0);
  connect (pathSlider(), SIGNAL (sliderMoved (int)), this, SLOT (pathSliderChanged(int)));
  connect (pathIndex(), SIGNAL (valueChanged(int)), this, SLOT (pathIndexChanged(int)));
  connect (playPause(), SIGNAL (toggled (bool)), this, SLOT (playPauseToggled(bool)));
  connect (stop(), SIGNAL (clicked()), this, SLOT (stopClicked()));
  connect (record(), SIGNAL (toggled(bool)), this, SLOT (recordToggled(bool)));
25
26
27
28
29
30
  connect (ui_->refreshButton_path, SIGNAL (clicked()), this, SLOT (update()));
}

PathPlayer::~PathPlayer()
{
  delete ui_;
Joseph Mirabel's avatar
Joseph Mirabel committed
31
32
33
34
}

void PathPlayer::update ()
{
35
  CORBA::Short nbPath = plugin_->client()->problem ()->numberPaths ();
Joseph Mirabel's avatar
Joseph Mirabel committed
36
37
38
39
40
41
42
43
44
45
  if (nbPath > 0)
    {
      pathIndex()->setEnabled(true);
      pathSlider()->setEnabled(true);
      playPause()->setEnabled(true);
      stop()->setEnabled(true);
      if (pathIndex()->maximum() == 0) {
          // If path index value is 0, no signal valueChanged will
          // be emitted. Force a value changed.
          if (pathIndex()->value() == 0)
46
            pathIndexChanged(0);
Joseph Mirabel's avatar
Joseph Mirabel committed
47
48
49
50
51
52
53
54
55
          pathIndex()->setValue(0);
        }
      pathIndex()->setMaximum(nbPath - 1);
    }
  else {
      pathIndex()->setEnabled(false);
      pathSlider()->setEnabled(false);
      playPause()->setEnabled(false);
      stop()->setEnabled(false);
56
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
57
58
59
60
}

void PathPlayer::pathIndexChanged(int i)
{
Joseph Mirabel's avatar
Joseph Mirabel committed
61
  assert (i >= 0);
62
  pathLength_ = plugin_->client()->problem()->pathLength ((short unsigned int)i);
Joseph Mirabel's avatar
Joseph Mirabel committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  currentParam_= 0;
}

void PathPlayer::pathSliderChanged(int value)
{
  // The user moved the slider manually.
  currentParam_ = sliderToLength(value);
  updateConfiguration();
}

void PathPlayer::playPauseToggled(bool toggled)
{
  if (toggled) timerId_ = startTimer(0);
  else killTimer(timerId_);
}

void PathPlayer::stopClicked()
{
  // Pause
  playPause()->setChecked(false);
  // Reset current position.
  currentParam_ = 0;
  pathSlider()->setSliderPosition(0);
  updateConfiguration();
}

void PathPlayer::recordToggled(bool toggled)
{
  if (toggled) {
      std::string path = "/tmp/hpp-gui/record/img";
      std::string ext = "jpeg";
94
95
      MainWindow::instance()->osg ()->startCapture(
            MainWindow::instance()->centralWidget()->windowID(),
Joseph Mirabel's avatar
Joseph Mirabel committed
96
97
98
99
100
101
102
103
104
105
106
            path.c_str(),
            ext.c_str());
    }
}

void PathPlayer::pathPulse()
{
  if (playPause()->isChecked()) {
      // Stil playing
      currentParam_ += lengthBetweenRefresh();
      if (currentParam_ > pathLength_) {
Joseph Mirabel's avatar
Joseph Mirabel committed
107
          currentParam_ = pathLength_;
Joseph Mirabel's avatar
Joseph Mirabel committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
          playPause()->setChecked(false);
        }
      else
        timerId_ = startTimer(timeBetweenRefresh());
      pathSlider()->setSliderPosition(lengthToSlider(currentParam_));
      updateConfiguration();
    }
}

void PathPlayer::timerEvent(QTimerEvent *event)
{
  if (timerId_ == event->timerId()) {
      pathPulse();
    }
}

void PathPlayer::updateConfiguration ()
{
  hpp::floatSeq_var config =
127
128
      plugin_->client()->problem()->configAtParam ((short unsigned int)pathIndex()->value(),currentParam_);
  plugin_->client()->robot()->setCurrentConfig (config.in());
129
  MainWindow::instance()->requestApplyCurrentConfiguration();
Joseph Mirabel's avatar
Joseph Mirabel committed
130
131
132
133
134
135
136
137
138
}

inline double PathPlayer::sliderToLength(int v) const
{
  return ((double)v / (double)pathSlider()->maximum()) * pathLength_;
}

int PathPlayer::lengthToSlider(double l) const
{
Joseph Mirabel's avatar
Joseph Mirabel committed
139
  return (int) (pathSlider()->maximum() * l / pathLength_);
Joseph Mirabel's avatar
Joseph Mirabel committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}

inline double PathPlayer::timeToLength(double time) const
{
  return time * pathLength_ / timeSpinBox()->value();
}

inline int PathPlayer::timeBetweenRefresh() const
{
  return 1000/frameRate_;
}

double PathPlayer::lengthBetweenRefresh() const
{
  return pathLength_ / (timeSpinBox()->value() * frameRate_);
}

QDoubleSpinBox *PathPlayer::timeSpinBox() const
{
159
  return ui_->timeSpinBox;
Joseph Mirabel's avatar
Joseph Mirabel committed
160
161
162
163
}

QSpinBox *PathPlayer::pathIndex() const
{
164
  return ui_->pathIndexSpin;
Joseph Mirabel's avatar
Joseph Mirabel committed
165
166
167
168
}

QSlider *PathPlayer::pathSlider() const
{
169
  return ui_->pathSlider;
Joseph Mirabel's avatar
Joseph Mirabel committed
170
171
172
173
}

QPushButton *PathPlayer::playPause() const
{
174
  return ui_->playPauseButton;
Joseph Mirabel's avatar
Joseph Mirabel committed
175
176
177
178
}

QPushButton *PathPlayer::stop() const
{
179
  return ui_->stopButton;
Joseph Mirabel's avatar
Joseph Mirabel committed
180
181
182
183
}

QPushButton *PathPlayer::record() const
{
184
  return ui_->recordButton;
Joseph Mirabel's avatar
Joseph Mirabel committed
185
}