interactivewidget.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. = StarPU-Top for StarPU =
  3. Copyright (C) 2011
  4. William Braik
  5. Yann Courtois
  6. Jean-Marie Couteyen
  7. Anthony Roy
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "interactivewidget.h"
  21. #include "mainwindow.h"
  22. #include <QCloseEvent>
  23. #include <QAction>
  24. #include <QCheckBox>
  25. #include <QSpinBox>
  26. #include <QDial>
  27. #include <qwt_knob.h>
  28. #include <qwt_wheel.h>
  29. #include <qwt_slider.h>
  30. InteractiveWidget::InteractiveWidget(ParamDescription *paramDescription,
  31. MainWindow *mainWindow) :
  32. QWidget(mainWindow)
  33. {
  34. _mainWindow = mainWindow;
  35. _paramDescription = paramDescription;
  36. _internalWidget = 0;
  37. _interactiveWidgetNames = _mainWindow->interactiveWidgetNames();
  38. _interactiveWidgetPossibilities
  39. = _mainWindow->interactiveWidgetPossibilities() ->value(
  40. _paramDescription->type).values();
  41. // Init context menu actions
  42. QActionGroup *actionGroup = new QActionGroup(this);
  43. for (int i = 0; i < _interactiveWidgetPossibilities.count(); i++)
  44. {
  45. QAction *action = new QAction(
  46. _interactiveWidgetNames ->value(
  47. _interactiveWidgetPossibilities.at(i)), actionGroup);
  48. action->setCheckable(true);
  49. if (_interactiveWidgetPossibilities.at(i) == _paramDescription->widget)
  50. {
  51. action->setChecked(true);
  52. }
  53. QObject::connect(action, SIGNAL(triggered()), this,
  54. SLOT(widgetTypeChanged()));
  55. actionGroup->addAction(action);
  56. }
  57. addActions(actionGroup->actions());
  58. // Set attributes
  59. setAttribute(Qt::WA_DeleteOnClose);
  60. setContextMenuPolicy(Qt::ActionsContextMenu);
  61. // Init GUI
  62. QString labelText = _paramDescription->descriptionString;
  63. if (_paramDescription->descriptionString.size() > 14)
  64. {
  65. labelText.truncate(12);
  66. labelText.append("...");
  67. }
  68. _label = new QLabel(labelText);
  69. _label->setToolTip(_paramDescription->descriptionString);
  70. // Set layout
  71. _layout = new QHBoxLayout(this);
  72. setLayout( _layout);
  73. _layout->addWidget(_label);
  74. // Create internal widget
  75. createInternalWidget();
  76. // Bind notifications
  77. QObject::connect(this, SIGNAL(paramValueChanged(int,bool)),
  78. _mainWindow, SLOT(interactiveWidgetUpdated(int,bool)));
  79. QObject::connect(this, SIGNAL(paramValueChanged(int,int)),
  80. _mainWindow, SLOT(interactiveWidgetUpdated(int,int)));
  81. QObject::connect(this, SIGNAL(paramValueChanged(int,double)),
  82. _mainWindow, SLOT(interactiveWidgetUpdated(int,double)));
  83. // Setup automatic cleanup
  84. QObject::connect(this, SIGNAL(destroyed()), _mainWindow,
  85. SLOT(removeDestroyedInteractiveWidgets()));
  86. qDebug() << "InteractiveWidget : initializing [desc"
  87. << _paramDescription->descriptionString << "; id"
  88. << _paramDescription->id << "; type" << _paramDescription->type
  89. << "; min" << _paramDescription->valMin << "; max"
  90. << _paramDescription->valMax << "; init bool"
  91. << _paramDescription->valInitBool << "; init double"
  92. << _paramDescription->valInitDouble << "; init enum"
  93. << _paramDescription->valInitEnum << "; init int"
  94. << _paramDescription->valInitInt << "; widget"
  95. << _paramDescription->widget << "]";
  96. }
  97. InteractiveWidget::~InteractiveWidget()
  98. {
  99. qDebug() << "InteractiveWidget" << _paramDescription->id << ": terminating";
  100. }
  101. void InteractiveWidget::closeEvent(QCloseEvent *ce)
  102. {
  103. ce->accept();
  104. }
  105. void InteractiveWidget::recreateInternalWidget()
  106. {
  107. if (_internalWidget != 0)
  108. {
  109. _internalWidget->close();
  110. _internalWidget = 0;
  111. }
  112. createInternalWidget();
  113. adjustSize();
  114. updateAction(_paramDescription->widget);
  115. }
  116. void InteractiveWidget::updateAction(InteractiveWidgetType newWidget)
  117. {
  118. for (int i = 0; i < actions().count(); i++)
  119. {
  120. if (actions().at(i)->text().compare(
  121. _interactiveWidgetNames->value(newWidget)) == 0)
  122. {
  123. actions().at(i)->setChecked(true);
  124. return;
  125. }
  126. }
  127. }
  128. void InteractiveWidget::createInternalWidget()
  129. {
  130. qDebug() << "Creating the interactive widget for param id"
  131. << _paramDescription->id;
  132. switch (_paramDescription->widget)
  133. {
  134. case INTERACTIVE_WIDGET_SLIDER:
  135. {
  136. _internalWidget = new QwtSlider(this);
  137. _internalWidget->setMinimumWidth(100);
  138. QwtSlider *widget = (QwtSlider*) _internalWidget;
  139. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  140. widget->setScalePosition(QwtSlider::BottomScale);
  141. widget->setValue(_paramDescription->valInitInt);
  142. QObject::connect(widget, SIGNAL(sliderReleased()), this,
  143. SLOT(sliderValueChanged()));
  144. break;
  145. }
  146. case INTERACTIVE_WIDGET_KNOB:
  147. {
  148. _internalWidget = new QwtKnob(this);
  149. _internalWidget->setMinimumSize(50, 50);
  150. QwtKnob *widget = (QwtKnob*) _internalWidget;
  151. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  152. widget->setValue(_paramDescription->valInitDouble);
  153. QObject::connect(widget, SIGNAL(sliderReleased()), this,
  154. SLOT(knobValueChanged()));
  155. break;
  156. }
  157. case INTERACTIVE_WIDGET_WHEEL:
  158. {
  159. _internalWidget = new QwtWheel(this);
  160. _internalWidget->setMinimumSize(50, 30);
  161. QwtWheel *widget = (QwtWheel*) _internalWidget;
  162. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  163. widget->setValue(_paramDescription->valInitDouble);
  164. QObject::connect(widget, SIGNAL(sliderReleased()), this,
  165. SLOT(wheelValueChanged()));
  166. break;
  167. }
  168. case INTERACTIVE_WIDGET_CHECKBOX:
  169. {
  170. _internalWidget = new QCheckBox(this);
  171. _internalWidget->setMinimumSize(25, 25);
  172. QCheckBox *widget = (QCheckBox*) _internalWidget;
  173. widget->setChecked(_paramDescription->valInitBool);
  174. QObject::connect(widget, SIGNAL(clicked(bool)),
  175. this, SLOT(notifyValueChanged(bool)));
  176. break;
  177. }
  178. case INTERACTIVE_WIDGET_SPINBOX:
  179. {
  180. _internalWidget = new QSpinBox(this);
  181. _internalWidget->setMinimumSize(75, 30);
  182. QSpinBox *widget = (QSpinBox*) _internalWidget;
  183. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  184. widget->setValue(_paramDescription->valInitInt);
  185. QObject::connect(widget, SIGNAL(valueChanged(int)),
  186. this, SLOT(notifyValueChanged(int)));
  187. break;
  188. }
  189. case INTERACTIVE_WIDGET_DOUBLESPINBOX:
  190. {
  191. _internalWidget = new QDoubleSpinBox(this);
  192. _internalWidget->setMinimumSize(75, 30);
  193. QDoubleSpinBox *widget = (QDoubleSpinBox*) _internalWidget;
  194. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  195. widget->setValue(_paramDescription->valInitDouble);
  196. QObject::connect(widget, SIGNAL(valueChanged(double)),
  197. this, SLOT(notifyValueChanged(double)));
  198. break;
  199. }
  200. case INTERACTIVE_WIDGET_DIAL:
  201. {
  202. _internalWidget = new QDial(this);
  203. _internalWidget->setMinimumSize(80, 40);
  204. QDial *widget = (QDial*) _internalWidget;
  205. widget->setRange(_paramDescription->valMin, _paramDescription->valMax);
  206. widget->setValue(_paramDescription->valInitInt);
  207. QObject::connect(widget, SIGNAL(sliderReleased()), this,
  208. SLOT(dialValueChanged()));
  209. break;
  210. }
  211. case INTERACTIVE_WIDGET_COMBOBOX:
  212. {
  213. _internalWidget = new QComboBox(this);
  214. _internalWidget->setMinimumSize(75, 25);
  215. QComboBox *widget = (QComboBox*) _internalWidget;
  216. widget->addItems(_paramDescription->enumValues);
  217. widget->setCurrentIndex(_paramDescription->valInitEnum);
  218. QObject::connect(widget, SIGNAL(currentIndexChanged(int)),
  219. this, SLOT(notifyValueChanged(int)));
  220. break;
  221. }
  222. default:
  223. ;
  224. }
  225. _internalWidget->setAttribute(Qt::WA_DeleteOnClose);
  226. layout()->addWidget(_internalWidget);
  227. }
  228. /* -------------------------------------------------------------------------- */
  229. /* Getters */
  230. /* -------------------------------------------------------------------------- */
  231. ParamDescription *InteractiveWidget::description() const
  232. {
  233. return _paramDescription;
  234. }
  235. QSize InteractiveWidget::minimumInternalWidgetSize() const
  236. {
  237. return _internalWidget->minimumSize();
  238. }
  239. /* -------------------------------------------------------------------------- */
  240. /* Setters */
  241. /* -------------------------------------------------------------------------- */
  242. void InteractiveWidget::setValue(bool value)
  243. {
  244. switch (_paramDescription->widget)
  245. {
  246. case INTERACTIVE_WIDGET_CHECKBOX:
  247. {
  248. QCheckBox *widget = qobject_cast<QCheckBox*> (_internalWidget);
  249. if (widget != 0)
  250. {
  251. widget->setChecked(value);
  252. }
  253. else
  254. {
  255. qDebug()
  256. << "Bad interactive widget cast "
  257. "(InteractiveWidget::setValue()) !";
  258. }
  259. break;
  260. }
  261. default:
  262. ;
  263. }
  264. }
  265. void InteractiveWidget::setValue(int value)
  266. {
  267. switch (_paramDescription->widget)
  268. {
  269. case INTERACTIVE_WIDGET_SLIDER:
  270. {
  271. QwtSlider *widget = qobject_cast<QwtSlider*> (_internalWidget);
  272. if (widget != 0)
  273. {
  274. widget->setValue(value);
  275. }
  276. else
  277. {
  278. qDebug()
  279. << "Bad interactive widget cast "
  280. "(InteractiveWidget::setValue()) !";
  281. }
  282. break;
  283. }
  284. case INTERACTIVE_WIDGET_KNOB:
  285. {
  286. QwtKnob *widget = qobject_cast<QwtKnob*> (_internalWidget);
  287. if (widget != 0)
  288. {
  289. widget->setValue(value);
  290. }
  291. else
  292. {
  293. qDebug()
  294. << "Bad interactive widget cast "
  295. "(InteractiveWidget::setValue()) !";
  296. }
  297. break;
  298. }
  299. case INTERACTIVE_WIDGET_WHEEL:
  300. {
  301. QwtWheel *widget = qobject_cast<QwtWheel*> (_internalWidget);
  302. if (widget != 0)
  303. {
  304. widget->setValue(value);
  305. }
  306. else
  307. {
  308. qDebug()
  309. << "Bad interactive widget cast "
  310. "(InteractiveWidget::setValue()) !";
  311. }
  312. break;
  313. }
  314. case INTERACTIVE_WIDGET_SPINBOX:
  315. {
  316. QSpinBox *widget = qobject_cast<QSpinBox*> (_internalWidget);
  317. if (widget != 0)
  318. {
  319. widget->setValue(value);
  320. }
  321. else
  322. {
  323. qDebug()
  324. << "Bad interactive widget cast "
  325. "(InteractiveWidget::setValue()) !";
  326. }
  327. break;
  328. }
  329. case INTERACTIVE_WIDGET_DOUBLESPINBOX:
  330. {
  331. QDoubleSpinBox *widget =
  332. qobject_cast<QDoubleSpinBox*> (_internalWidget);
  333. if (widget != 0)
  334. {
  335. widget->setValue(value);
  336. }
  337. else
  338. {
  339. qDebug()
  340. << "Bad interactive widget cast "
  341. "(InteractiveWidget::setValue()) !";
  342. }
  343. break;
  344. }
  345. case INTERACTIVE_WIDGET_DIAL:
  346. {
  347. QDial *widget = qobject_cast<QDial*> (_internalWidget);
  348. if (widget != 0)
  349. {
  350. widget->setValue(value);
  351. }
  352. else
  353. {
  354. qDebug()
  355. << "Bad interactive widget cast "
  356. "(InteractiveWidget::setValue()) !";
  357. }
  358. break;
  359. }
  360. case INTERACTIVE_WIDGET_COMBOBOX:
  361. {
  362. QComboBox *widget = qobject_cast<QComboBox*> (_internalWidget);
  363. if (widget != 0)
  364. {
  365. if (value >= _paramDescription->valMin && value
  366. <= _paramDescription->valMax)
  367. {
  368. widget->setCurrentIndex(value);
  369. }
  370. else
  371. {
  372. qDebug()
  373. << "Trying to set out of range enum value for enum widget "
  374. << _paramDescription->id << "value" << value;
  375. }
  376. }
  377. else
  378. {
  379. qDebug()
  380. << "Bad interactive widget cast "
  381. "(InteractiveWidget::setValue()) !";
  382. }
  383. break;
  384. }
  385. default:
  386. ;
  387. }
  388. }
  389. void InteractiveWidget::setValue(double value)
  390. {
  391. switch (_paramDescription->widget)
  392. {
  393. case INTERACTIVE_WIDGET_SLIDER:
  394. {
  395. QwtSlider *widget = qobject_cast<QwtSlider*> (_internalWidget);
  396. if (widget != 0)
  397. {
  398. widget->setValue(value);
  399. }
  400. else
  401. {
  402. qDebug()
  403. << "Bad interactive widget cast "
  404. "(InteractiveWidget::setValue()) !";
  405. }
  406. break;
  407. }
  408. case INTERACTIVE_WIDGET_KNOB:
  409. {
  410. QwtKnob *widget = qobject_cast<QwtKnob*> (_internalWidget);
  411. if (widget != 0)
  412. {
  413. widget->setValue(value);
  414. }
  415. else
  416. {
  417. qDebug()
  418. << "Bad interactive widget cast "
  419. "(InteractiveWidget::setValue()) !";
  420. }
  421. break;
  422. }
  423. case INTERACTIVE_WIDGET_WHEEL:
  424. {
  425. QwtWheel *widget = qobject_cast<QwtWheel*> (_internalWidget);
  426. if (widget != 0)
  427. {
  428. widget->setValue(value);
  429. }
  430. else
  431. {
  432. qDebug()
  433. << "Bad interactive widget cast "
  434. "(InteractiveWidget::setValue()) !";
  435. }
  436. break;
  437. }
  438. case INTERACTIVE_WIDGET_DOUBLESPINBOX:
  439. {
  440. QDoubleSpinBox *widget =
  441. qobject_cast<QDoubleSpinBox*> (_internalWidget);
  442. if (widget != 0)
  443. {
  444. widget->setValue(value);
  445. }
  446. else
  447. {
  448. qDebug()
  449. << "Bad interactive widget cast "
  450. "(InteractiveWidget::setValue()) !";
  451. }
  452. break;
  453. }
  454. case INTERACTIVE_WIDGET_DIAL:
  455. {
  456. QDial *widget = qobject_cast<QDial*> (_internalWidget);
  457. if (widget != 0)
  458. {
  459. widget->setValue(value);
  460. }
  461. else
  462. {
  463. qDebug()
  464. << "Bad interactive widget cast "
  465. "(InteractiveWidget::setValue()) !";
  466. }
  467. break;
  468. }
  469. default:
  470. ;
  471. }
  472. }
  473. /* -------------------------------------------------------------------------- */
  474. /* Other methods */
  475. /* -------------------------------------------------------------------------- */
  476. void InteractiveWidget::sliderValueChanged()
  477. {
  478. QwtSlider *widget = (QwtSlider*) QObject::sender();
  479. if (_paramDescription->type == PARAM_TYPE_INT)
  480. {
  481. int valueInt = (int) widget->value();
  482. notifyValueChanged(valueInt);
  483. }
  484. else
  485. { // Param type is double
  486. double valueDouble = (double) widget->value();
  487. notifyValueChanged(valueDouble);
  488. }
  489. }
  490. void InteractiveWidget::wheelValueChanged()
  491. {
  492. QwtWheel *widget = (QwtWheel*) QObject::sender();
  493. if (_paramDescription->type == PARAM_TYPE_INT)
  494. {
  495. int valueInt = (int) widget->value();
  496. notifyValueChanged(valueInt);
  497. }
  498. else
  499. { // Param type is double
  500. double valueDouble = (double) widget->value();
  501. notifyValueChanged(valueDouble);
  502. }
  503. }
  504. void InteractiveWidget::knobValueChanged()
  505. {
  506. QwtKnob *widget = (QwtKnob*) QObject::sender();
  507. if (_paramDescription->type == PARAM_TYPE_INT)
  508. {
  509. int valueInt = (int) widget->value();
  510. notifyValueChanged(valueInt);
  511. }
  512. else
  513. { // Param type is double
  514. double valueDouble = (double) widget->value();
  515. notifyValueChanged(valueDouble);
  516. }
  517. }
  518. void InteractiveWidget::dialValueChanged()
  519. {
  520. QDial *widget = (QDial*) QObject::sender();
  521. if (_paramDescription->type == PARAM_TYPE_INT)
  522. {
  523. int valueInt = (int) widget->value();
  524. notifyValueChanged(valueInt);
  525. }
  526. else
  527. { // Param type is double
  528. double valueDouble = (double) widget->value();
  529. notifyValueChanged(valueDouble);
  530. }
  531. }
  532. void InteractiveWidget::notifyValueChanged(bool value)
  533. {
  534. emit paramValueChanged(_paramDescription->id, value);
  535. }
  536. void InteractiveWidget::notifyValueChanged(int value)
  537. {
  538. emit paramValueChanged(_paramDescription->id, value);
  539. }
  540. void InteractiveWidget::notifyValueChanged(double value)
  541. {
  542. emit paramValueChanged(_paramDescription->id, value);
  543. }
  544. void InteractiveWidget::widgetTypeChanged()
  545. {
  546. QAction *action = (QAction*) QObject::sender();
  547. if(_paramDescription->widget != _interactiveWidgetNames->key(action->text()))
  548. {
  549. _mainWindow->updateInteractiveWidgetType
  550. (_paramDescription->id,
  551. _interactiveWidgetNames->key(action->text()));
  552. }
  553. }