datawidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 "datawidget.h"
  21. #include "mainwindow.h"
  22. #include "widgetwindowsmanager.h"
  23. #include "configurationmanager.h"
  24. #include <qwt_thermo.h>
  25. #include <qwt_plot.h>
  26. #include <qwt_dial.h>
  27. #include <qwt_dial_needle.h>
  28. #include <qwt_plot_curve.h>
  29. #include <qwt_plot.h>
  30. #include "qledindicator/qledindicator.h"
  31. #include <QAction>
  32. #include <QCloseEvent>
  33. #include <QLCDNumber>
  34. #include <QCheckBox>
  35. #include <QVBoxLayout>
  36. #include <QHBoxLayout>
  37. DataWidget::DataWidget(DataDescription *dataDescription,
  38. WidgetWindowsManager *widgetWindowsManager,
  39. MainWindow *mainWindow,
  40. bool inside) :
  41. AbstractWidgetWindow(widgetWindowsManager, mainWindow, inside)
  42. {
  43. _dataDescription = dataDescription;
  44. _internalWidget = 0;
  45. // For the plot widget
  46. _curve = 0;
  47. _curveData = 0;
  48. _dataWidgetNames = _mainWindow->dataWidgetNames();
  49. _dataWidgetPossibilities = _mainWindow->dataWidgetPossibilities() ->value(
  50. _dataDescription->type).values();
  51. // Init context menu actions
  52. QActionGroup *actionGroup = new QActionGroup(this);
  53. for (int i = 0; i < _dataWidgetPossibilities.count(); i++)
  54. {
  55. QAction *action = new QAction(
  56. _dataWidgetNames ->value(_dataWidgetPossibilities.at(i)),
  57. actionGroup);
  58. action->setCheckable(true);
  59. if (_dataWidgetPossibilities.at(i) == _dataDescription->widget)
  60. {
  61. action->setChecked(true);
  62. }
  63. QObject::connect(action, SIGNAL(triggered()), this,
  64. SLOT(widgetTypeChanged()));
  65. actionGroup->addAction(action);
  66. }
  67. addActions(actionGroup->actions());
  68. // Set attributes
  69. setContextMenuPolicy(Qt::ActionsContextMenu);
  70. // Init GUI
  71. setWindowTitle(_dataDescription->descriptionString);
  72. setWindowIcon(QIcon(":/images/widget.png"));
  73. // Set layout
  74. QVBoxLayout *globalLayout = new QVBoxLayout();
  75. setLayout(globalLayout);
  76. QHBoxLayout *topLayout = new QHBoxLayout();
  77. topLayout->addWidget(_inOutButton);
  78. QString labelText = _dataDescription->descriptionString;
  79. if (_dataDescription->descriptionString.size() > 14)
  80. {
  81. labelText.truncate(12);
  82. labelText.append("...");
  83. }
  84. QLabel *dataTitle = new QLabel(labelText);
  85. dataTitle->setToolTip(_dataDescription->descriptionString);
  86. topLayout->addWidget(dataTitle, 0, Qt::AlignHCenter);
  87. dataTitle->setFixedHeight(dataTitle->sizeHint().height());
  88. globalLayout->addLayout(topLayout);
  89. // Create internal widget
  90. createInternalWidget();
  91. setParent(0); // Needed if it is a flying window
  92. // Will be updated anyway if it goes into the MDI area
  93. // However the object HAS to be constructed with the main window as parent
  94. // so it can connect to its slots.
  95. // Setup automatic cleanup
  96. QObject::connect(this, SIGNAL(destroyed()), _mainWindow,
  97. SLOT(removeDestroyedDataWidgets()));
  98. qDebug() << "DataWidget : initializing [desc"
  99. << _dataDescription->descriptionString << "; id"
  100. << _dataDescription->id << "; type" << _dataDescription->type
  101. << "; min" << _dataDescription->valMin << "; max"
  102. << _dataDescription->valMax << "; widget"
  103. << _dataDescription->widget << "]";
  104. }
  105. DataWidget::~DataWidget()
  106. {
  107. qDebug() << "DataWidget" << _dataDescription->id << ": terminating";
  108. delete _internalWidget;
  109. }
  110. void DataWidget::closeEvent(QCloseEvent *ce)
  111. {
  112. if (isEnabled() == true)
  113. {// The widget is alive (session active)
  114. _mainWindow->updateDataWidgetType(_dataDescription->id,
  115. DATA_WIDGET_NONE);
  116. }
  117. ce->accept();
  118. }
  119. void DataWidget::recreateInternalWidget()
  120. {
  121. if (_internalWidget != 0)
  122. {
  123. _internalWidget->close();
  124. _internalWidget = 0;
  125. }
  126. createInternalWidget();
  127. adjustSize();
  128. if (isInside() == true)
  129. {
  130. parentWidget()->resize(minimumInternalWidgetSize() + QSize(85, 85));
  131. }
  132. updateAction(_dataDescription->widget);
  133. }
  134. void DataWidget::updateAction(DataWidgetType newWidget)
  135. {
  136. for (int i = 0; i < actions().count(); i++)
  137. {
  138. if (actions().at(i)->text().compare(_dataWidgetNames->value(newWidget))
  139. == 0)
  140. {
  141. actions().at(i)->setChecked(true);
  142. return;
  143. }
  144. }
  145. }
  146. void DataWidget::createInternalWidget()
  147. {
  148. qDebug() << "Creating the data widget for data id" << _dataDescription->id;
  149. switch (_dataDescription->widget)
  150. {
  151. case DATA_WIDGET_LCD:
  152. {
  153. _internalWidget = new QLCDNumber(this);
  154. _internalWidget->setMinimumSize(50, 50);
  155. break;
  156. }
  157. case DATA_WIDGET_PLOT:
  158. {
  159. _internalWidget = new QwtPlot(this);
  160. _curve = new QwtPlotCurve(_dataDescription->descriptionString);
  161. if (_mainWindow->configurationManager()->antialiasing() == true)
  162. {
  163. _curve->setRenderHint(QwtPlotItem::RenderAntialiased);
  164. }
  165. _curve->attach((QwtPlot*) _internalWidget);
  166. _curveData = new CurveData;
  167. _curveData->xData = new QVector<double> ();
  168. _curveData->yData = new QVector<double> ();
  169. _internalWidget->setMinimumSize(300, 200);
  170. break;
  171. }
  172. case DATA_WIDGET_LEVEL:
  173. {
  174. _internalWidget = new QwtThermo(this);
  175. QwtThermo *widget = (QwtThermo*) _internalWidget;
  176. widget->setRange(_dataDescription->valMin, _dataDescription->valMax);
  177. _internalWidget->setMinimumSize(100, 200);
  178. break;
  179. }
  180. case DATA_WIDGET_LED:
  181. {
  182. _internalWidget = new QLedIndicator(this);
  183. _internalWidget->setMinimumSize(50, 50);
  184. break;
  185. }
  186. case DATA_WIDGET_DIAL:
  187. {
  188. _internalWidget = new QwtDial(this);
  189. _internalWidget->setMinimumSize(250, 250);
  190. QwtDial *widget = (QwtDial*) _internalWidget;
  191. widget->setReadOnly(true);
  192. widget->setWrapping(false);
  193. widget->setOrigin(135.0);
  194. widget->setRange(_dataDescription->valMin, _dataDescription->valMax);
  195. widget->setScaleArc(0.0, 270.0);
  196. widget->scaleDraw()->setSpacing(8);
  197. QwtDialSimpleNeedle *needle = new QwtDialSimpleNeedle(
  198. QwtDialSimpleNeedle::Arrow, true, Qt::red,
  199. QColor(Qt::gray).light(130));
  200. widget->setNeedle(needle);
  201. widget->setScaleOptions(QwtDial::ScaleTicks | QwtDial::ScaleLabel);
  202. widget->setScaleTicks(0, 4, 8);
  203. }
  204. default:
  205. ;
  206. }
  207. _internalWidget->setAttribute(Qt::WA_DeleteOnClose);
  208. layout()->addWidget(_internalWidget);
  209. }
  210. /* -------------------------------------------------------------------------- */
  211. /* Getters */
  212. /* -------------------------------------------------------------------------- */
  213. DataDescription *DataWidget::description() const
  214. {
  215. return _dataDescription;
  216. }
  217. QSize DataWidget::minimumInternalWidgetSize() const
  218. {
  219. return _internalWidget->minimumSize();
  220. }
  221. /* -------------------------------------------------------------------------- */
  222. /* Setters */
  223. /* -------------------------------------------------------------------------- */
  224. void DataWidget::setValue(bool value)
  225. {
  226. switch (_dataDescription->widget)
  227. {
  228. case DATA_WIDGET_LED:
  229. {
  230. QLedIndicator *widget = qobject_cast<QLedIndicator*> (_internalWidget);
  231. if (widget != 0)
  232. {
  233. widget->setChecked(value);
  234. }
  235. else
  236. {
  237. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  238. }
  239. break;
  240. }
  241. default:
  242. ;
  243. }
  244. emit valueChanged(value, _dataDescription->id);
  245. }
  246. void DataWidget::setValue(int value)
  247. {
  248. switch (_dataDescription->widget)
  249. {
  250. case DATA_WIDGET_PLOT:
  251. {
  252. QwtPlot *widget = qobject_cast<QwtPlot*> (_internalWidget);
  253. if (widget != 0)
  254. {
  255. _curveData->xData->append(_mainWindow->effectiveRunningTime());
  256. _curveData->yData->append(value);
  257. #if QWT_VERSION >= 0x060000
  258. _curve->setRawSamples(_curveData->xData->data(),
  259. _curveData->yData->data(), _curveData->xData->size());
  260. #else
  261. # warning Old version of qwt being used, data aggregator will not work.
  262. #endif
  263. widget->replot();
  264. }
  265. else
  266. {
  267. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  268. }
  269. break;
  270. }
  271. case DATA_WIDGET_LCD:
  272. {
  273. QLCDNumber *widget = qobject_cast<QLCDNumber*> (_internalWidget);
  274. if (widget != 0)
  275. {
  276. widget->display(value);
  277. }
  278. else
  279. {
  280. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  281. }
  282. break;
  283. }
  284. case DATA_WIDGET_LEVEL:
  285. {
  286. QwtThermo *widget = qobject_cast<QwtThermo*> (_internalWidget);
  287. if (widget != 0)
  288. {
  289. widget->setValue(value);
  290. }
  291. else
  292. {
  293. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  294. }
  295. break;
  296. }
  297. case DATA_WIDGET_DIAL:
  298. {
  299. QwtDial *widget = qobject_cast<QwtDial*> (_internalWidget);
  300. if (widget != 0)
  301. {
  302. widget->setValue(value);
  303. }
  304. else
  305. {
  306. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  307. }
  308. break;
  309. }
  310. default:
  311. ;
  312. }
  313. emit valueChanged(value, _dataDescription->id);
  314. }
  315. void DataWidget::setValue(double value)
  316. {
  317. switch (_dataDescription->widget)
  318. {
  319. case DATA_WIDGET_PLOT:
  320. {
  321. QwtPlot *widget = qobject_cast<QwtPlot*> (_internalWidget);
  322. if (widget != 0)
  323. {
  324. _curveData->xData->append(_mainWindow->effectiveRunningTime());
  325. _curveData->yData->append(value);
  326. #if QWT_VERSION >= 0x060000
  327. _curve->setRawSamples(_curveData->xData->data(),
  328. _curveData->yData->data(), _curveData->xData->size());
  329. #else
  330. # warning Old version of qwt being used, data aggregator will not work.
  331. #endif
  332. widget->replot();
  333. }
  334. else
  335. {
  336. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  337. }
  338. break;
  339. }
  340. case DATA_WIDGET_LCD:
  341. {
  342. QLCDNumber *widget = qobject_cast<QLCDNumber*> (_internalWidget);
  343. if (widget != 0)
  344. {
  345. widget->display(value);
  346. }
  347. else
  348. {
  349. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  350. }
  351. break;
  352. }
  353. case DATA_WIDGET_LEVEL:
  354. {
  355. QwtThermo *widget = qobject_cast<QwtThermo*> (_internalWidget);
  356. if (widget != 0)
  357. {
  358. widget->setValue(value);
  359. }
  360. else
  361. {
  362. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  363. }
  364. break;
  365. }
  366. case DATA_WIDGET_DIAL:
  367. {
  368. QwtDial *widget = qobject_cast<QwtDial*> (_internalWidget);
  369. if (widget != 0)
  370. {
  371. widget->setValue(value);
  372. }
  373. else
  374. {
  375. qDebug() << "Bad data widget cast (DataWidget::setValue()) !";
  376. }
  377. break;
  378. }
  379. default:
  380. ;
  381. }
  382. emit valueChanged(value, _dataDescription->id);
  383. }
  384. void DataWidget::widgetTypeChanged()
  385. {
  386. QAction *action = (QAction*) QObject::sender();
  387. if(_dataDescription->widget != _dataWidgetNames->key(action->text()))
  388. {
  389. _mainWindow->updateDataWidgetType(_dataDescription->id,
  390. _dataWidgetNames->key(action->text()));
  391. }
  392. }