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

Last change on this file since 33 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

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