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

Last change on this file since 75 was 73, checked in by Marek Kurdej, 12 years ago

Minor: line-endings.

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