source: pacpusframework/trunk/src/DBITEPlayerLib/DbtPlyUserInterface.cpp@ 20

Last change on this file since 20 was 3, checked in by sgosseli, 12 years ago
  • Add the existing Pacpus files from pacpusdev and pacpuscore.
  • Provide a clean build system based on multiple CMake files.
File size: 10.6 KB
Line 
1////////////////////////////////////////////////////////////////////////////////
2// created: 2007/04/12 - 16:30
3//
4// author: Elie Al Alam & Gerald Dherbomez
5// Heudiasyc UMR UTC/CNRS 6599 Copyright
6//
7// version: $Id: DbtPlyUserInterface.cpp 1009 2012-08-01 19:03:32Z morasjul $
8//
9// purpose: Dbite Player User interface implementation
10////////////////////////////////////////////////////////////////////////////////
11
12#include "DbitePlayer/DbtPlyUserInterface.h"
13
14#include <cassert>
15#include <cmath> // fabs
16#include <qapplication.h> // quit()
17#include <qboxlayout.h>
18#include <qbuttongroup.h>
19#include <qcheckbox.h>
20#include <qgroupbox.h>
21#include <qlabel.h>
22#include <qpushbutton.h>
23#include <qslider.h>
24#include <qtablewidget.h>
25
26#include "kernel/ComponentManager.h"
27#include "kernel/Log.h"
28
29namespace pacpus {
30
31class DbtPlyEngineState;
32
33DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyUserInterface");
34
35//static ComponentFactory<DbtPlyUserInterface> factory("DbtPlyUserInterface");
36
37////////////////////////////////////////////////////////////////////////////////
38DbtPlyUserInterface::DbtPlyUserInterface(QString name)
39 : ComponentBase(name)
40 , componentTableWidget(NULL)
41 , mEngine(NULL)
42{
43 relTime_ = absTime_ = tMin_ = tMax_ = 0;
44
45 setLayout(createMainLayout());
46
47 show();
48}
49
50////////////////////////////////////////////////////////////////////////////////
51DbtPlyUserInterface::~DbtPlyUserInterface()
52{
53}
54
55////////////////////////////////////////////////////////////////////////////////
56ComponentBase::COMPONENT_CONFIGURATION DbtPlyUserInterface::configureComponent(XmlComponentConfig /*config*/)
57{
58 ComponentManager * mgr = ComponentManager::getInstance();
59 mEngine = dynamic_cast<DbtPlyEngine *>(mgr->getComponent("dbiteEngine"));
60 if (NULL == mEngine) {
61 LOG_FATAL("cannot get a pointer of the 'dbiteEngine' component");
62 return CONFIGURED_FAILED;
63 }
64
65 connectButtons();
66 connectDisplay();
67 connectSlider();
68
69 updateComponentList();
70
71 return ComponentBase::CONFIGURED_OK;
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Starts activity.
76void DbtPlyUserInterface::startActivity()
77{
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Stops activity.
82void DbtPlyUserInterface::stopActivity()
83{
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Overloaded.
88/// Invoked on close event.
89void DbtPlyUserInterface::closeEvent(QCloseEvent * /*event*/)
90{
91 qApp->quit();
92}
93
94void DbtPlyUserInterface::displayStateSlot(DbtPlyEngineState * state, float speed)
95{
96 LOG_DEBUG("displayStateSlot, state = " << state->toString() << ", speed = " << speed);
97
98 QString speedString;
99 if (fabs(speed) >= 1) {
100 speedString = speedString.setNum((int)speed);
101 } else {
102 speedString = "1/" + speedString.setNum((int)(1.0 / speed));
103 }
104
105 QString stateAndSpeedText;
106 stateAndSpeedText = state->toString() + " - speed : " + speedString;
107
108 lab->setText(stateAndSpeedText);
109 lab->adjustSize();
110}
111
112void DbtPlyUserInterface::displayMinMaxTime(road_time_t min, road_time_t max)
113{
114 timeMinValue->setText(QString::number(min));
115 tMin_ = min;
116 timeMaxValue->setText(QString::number(max));
117 tMax_ = max;
118 timeSlider->setMaximum(tMax_-tMin_);
119}
120
121void DbtPlyUserInterface::displayTime(road_time_t time)
122{
123 absTime_ = time;
124 if ((time - tMin_ - relTime_) > 40000 )
125 {
126 // refresh at 25Hz rate
127 relTime_ = (int)(time - tMin_);
128 timeCurValue->setText(QString::number(relTime_/1e6) + " - " + QString::number(absTime_));
129 timeSlider->setValue(relTime_);
130 }
131}
132
133int DbtPlyUserInterface::getTime()
134{
135 return timeSlider->value();
136}
137
138void DbtPlyUserInterface::resetTime()
139{
140 timeSlider->setValue(0);
141}
142
143QLayout * DbtPlyUserInterface::createMainLayout()
144{
145 QGroupBox * mainGroupBox = createControlGroupBox();
146 QGroupBox * componentListGroupBox = createComponentListGroupBox();
147
148 QGridLayout * generalLayout = new QGridLayout(this);
149 generalLayout->addWidget(mainGroupBox, /* row = */ 0, /* column = */ 0, /* rowSpan = */ 1, /* columnSpan = */ 3);
150 generalLayout->addWidget(componentListGroupBox, /* row = */ 1, /* column = */ 0, /* rowSpan = */ 1, /* columnSpan = */ 1);
151
152 return generalLayout;
153}
154
155QGroupBox * DbtPlyUserInterface::createControlGroupBox()
156{
157 const int kButtonWidthInPixels = 20;
158 const int kButtonHeightInPixels = 20;
159
160 QGridLayout * mainLayout = new QGridLayout(this);
161
162 butGroup = new QButtonGroup (this );
163 playBut = new QPushButton ("|>", this);
164 playBut->resize(kButtonWidthInPixels, kButtonHeightInPixels);
165 butGroup->addButton(playBut,1);
166 mainLayout->addWidget(playBut,0,0);
167
168 pauseBut = new QPushButton ("||", this);
169 pauseBut->resize(kButtonWidthInPixels, kButtonHeightInPixels);
170 butGroup->addButton(pauseBut,2);
171 mainLayout->addWidget(pauseBut,0,1);
172
173 stopBut = new QPushButton ("O", this);
174 stopBut->resize(kButtonWidthInPixels, kButtonHeightInPixels);
175 butGroup->addButton(stopBut,3);
176 mainLayout->addWidget(stopBut,0,2);
177
178 speedUpBut = new QPushButton ("+", this);
179 speedUpBut->resize(kButtonWidthInPixels, kButtonHeightInPixels);
180 butGroup->addButton(speedUpBut,4);
181 mainLayout->addWidget(speedUpBut,1,0);
182
183 speedDownBut = new QPushButton ("-", this);
184 speedDownBut->resize(kButtonWidthInPixels, kButtonHeightInPixels);
185 butGroup->addButton(speedDownBut,5);
186 mainLayout->addWidget(speedDownBut,1,1);
187
188 rev = new QCheckBox(tr("Reverse"), this);
189 mainLayout->addWidget(rev,1,2);
190
191 mainLayout->columnMinimumWidth(1);
192
193 lab = new QLabel (this);
194 lab->setText(tr("Player State"));
195 lab->adjustSize();
196 mainLayout->addWidget(lab,2,0,1,3);
197
198 timeMinTitle = new QLabel(this);
199 timeMinTitle->setText(tr("Min time"));
200 timeMinTitle->adjustSize();
201 mainLayout->addWidget(timeMinTitle,3,0);
202
203 timeMinValue = new QLabel(this);
204 timeMinValue->setText("0");
205 timeMinValue->adjustSize();
206 timeMinValue->setFrameStyle(QFrame::Panel | QFrame::Sunken);
207 mainLayout->addWidget(timeMinValue,3,1,1,2);
208
209 timeMaxTitle = new QLabel(this);
210 timeMaxTitle->setText(tr("Max time"));
211 timeMaxTitle->adjustSize();
212 mainLayout->addWidget(timeMaxTitle,4,0);
213
214 timeMaxValue = new QLabel(this);
215 timeMaxValue->setText("0");
216 timeMaxValue->adjustSize();
217 timeMaxValue->setFrameStyle(QFrame::Panel | QFrame::Sunken);
218 mainLayout->addWidget(timeMaxValue,4,1,1,2);
219
220 timeCurTitle = new QLabel(this);
221 timeCurTitle->setText(tr("Current time"));
222 timeCurTitle->adjustSize();
223 mainLayout->addWidget(timeCurTitle,5,0);
224
225 timeCurValue = new QLabel(this);
226 timeCurValue->setText("0 - 0");
227 timeCurValue->adjustSize();
228 timeCurValue->setFrameStyle(QFrame::Panel | QFrame::Sunken);
229 mainLayout->addWidget(timeCurValue,5,1,1,2);
230
231 timeSlider = new QSlider(Qt::Horizontal, this);
232 timeSlider->setMinimum(0);
233 timeSlider->adjustSize();
234 mainLayout->addWidget(timeSlider, /* row = */ 6, /* column = */ 0, /* rowSpan = */ 1, /* columnSpan = */ 3);
235
236 QGroupBox * mainGroupBox = new QGroupBox(tr("Control"));
237 mainGroupBox->setLayout(mainLayout);
238
239 return mainGroupBox;
240}
241
242QGroupBox * DbtPlyUserInterface::createComponentListGroupBox()
243{
244 QGroupBox * componentListGroupBox = new QGroupBox(tr("Component list"));
245
246 QBoxLayout * componentListLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
247 componentListGroupBox->setLayout(componentListLayout);
248
249 componentTableWidget = new QTableWidget(this);
250 componentListLayout->addWidget(componentTableWidget);
251
252 return componentListGroupBox;
253}
254
255void DbtPlyUserInterface::updateComponentList()
256{
257 ComponentManager * mgr = ComponentManager::getInstance();
258 QStringList componentNames = mgr->getAllComponentsName();
259
260 componentTableWidget->setRowCount(componentNames.size());
261 componentTableWidget->setColumnCount(2);
262 QStringList labels;
263 labels << tr("Name") << tr("State") << tr("Details");
264 componentTableWidget->setHorizontalHeaderLabels(labels);
265
266 int idx = 0;
267 for (QStringList::const_iterator it = componentNames.constBegin(), itend = componentNames.constEnd(); it != itend; ++it, ++idx) {
268 QString componentName = *it;
269 LOG_DEBUG("adding to component list: " << componentName);
270 componentTableWidget->setItem(idx, 0, new QTableWidgetItem(componentName));
271
272 ComponentBase * component = mgr->getComponent(componentName);
273 if (component) {
274 COMPONENT_STATE state = component->getState();
275
276 QString stateString;
277 switch (state) {
278 case STOPPED:
279 stateString = tr("Stopped");
280 break;
281 case NOT_MONITORED:
282 stateString = tr("Not monitored");
283 break;
284 case MONITOR_OK:
285 stateString = tr("Monitor OK");
286 break;
287 case MONITOR_NOK:
288 stateString = tr("Monitor wrong");
289 break;
290
291 default:
292 stateString = tr("UNKNOWN");
293 break;
294 }
295 componentTableWidget->setItem(idx, 1, new QTableWidgetItem(stateString));
296
297 // TODO: ADD component type and some detailed information (e.g. parameters)
298 //QString componentInfo = component->getDetails();
299 //componentTableWidget->setItem(idx, 2, new QTableWidgetItem(componentInfo));
300 }
301 }
302}
303
304void DbtPlyUserInterface::connectButtons()
305{
306 connect(playBut, SIGNAL(clicked()),
307 mEngine, SLOT(playEvent()));
308 connect(pauseBut, SIGNAL(clicked()),
309 mEngine, SLOT(pauseEvent()));
310 connect(stopBut, SIGNAL(clicked()),
311 mEngine, SLOT(stopEvent()));
312 connect(speedUpBut, SIGNAL(clicked()),
313 mEngine, SLOT(speedUpEvent()));
314 connect(speedDownBut, SIGNAL(clicked()),
315 mEngine, SLOT(speedDownEvent()));
316}
317
318void DbtPlyUserInterface::connectDisplay()
319{
320 connect(mEngine, SIGNAL(displayStateSig(DbtPlyEngineState *, float)),
321 this, SLOT(displayStateSlot(DbtPlyEngineState *, float)));
322
323 connect (mEngine, SIGNAL(timeMinMax(road_time_t, road_time_t)),
324 this, SLOT(displayMinMaxTime(road_time_t , road_time_t)));
325 connect (mEngine, SIGNAL(curReplayTime(road_time_t)),
326 this, SLOT(displayTime(road_time_t)));
327 connect (rev, SIGNAL(toggled(bool)),
328 mEngine, SLOT(changeDirection(bool)));
329}
330
331void DbtPlyUserInterface::connectSlider()
332{
333 connect (timeSlider, SIGNAL(sliderPressed()),
334 mEngine, SLOT(pauseEvent()));
335 connect (timeSlider, SIGNAL(sliderReleased()),
336 mEngine, SLOT(playEvent()));
337}
338
339} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.