source: pacpusframework/branches/2.0-beta1/src/DBITEPlayerLib/DbtPlyUserInterface.cpp@ 105

Last change on this file since 105 was 89, checked in by morasjul, 11 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

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