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

Last change on this file since 290 was 290, checked in by Marek Kurdej, 10 years ago

Some clean-up.

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