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