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