source: pacpusframework/branches/0.1.x/src/DBITEPlayerLib/DbtPlyUserInterface.cpp@ 254

Last change on this file since 254 was 152, checked in by Marek Kurdej, 11 years ago

Major update.
Renamed: addInput -> addInputs, addOutput -> addOutputs and made pure virtual (=0).
Transformed macro definitions into template methods: ADD_INPUT -> ComponentBase::addInput, ADD_OUTPUT -> ComponentBase::addOutput, GET_INPUT -> ComponentBase::getTypedInput, GET_OUTPUT -> ComponentBase::getTypedOutput.
Fixed: added public/protected set/get methods in ComponentBase, made member fields private.

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