qwt_thermo.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #include "qwt_thermo.h"
  10. #include "qwt_math.h"
  11. #include "qwt_scale_engine.h"
  12. #include "qwt_scale_draw.h"
  13. #include "qwt_scale_map.h"
  14. #include <qpainter.h>
  15. #include <qevent.h>
  16. #include <qstyle.h>
  17. #include <qpixmap.h>
  18. #include <qdrawutil.h>
  19. #include <qalgorithms.h>
  20. #include <qmath.h>
  21. class QwtThermo::PrivateData
  22. {
  23. public:
  24. PrivateData():
  25. fillBrush( Qt::black ),
  26. alarmBrush( Qt::white ),
  27. orientation( Qt::Vertical ),
  28. scalePos( QwtThermo::LeftScale ),
  29. borderWidth( 2 ),
  30. scaleDist( 3 ),
  31. thermoWidth( 10 ),
  32. minValue( 0.0 ),
  33. maxValue( 1.0 ),
  34. value( 0.0 ),
  35. alarmLevel( 0.0 ),
  36. alarmEnabled( false )
  37. {
  38. map.setScaleInterval( minValue, maxValue );
  39. }
  40. QwtScaleMap map;
  41. QRect thermoRect;
  42. QBrush fillBrush;
  43. QBrush alarmBrush;
  44. Qt::Orientation orientation;
  45. ScalePos scalePos;
  46. int borderWidth;
  47. int scaleDist;
  48. int thermoWidth;
  49. double minValue;
  50. double maxValue;
  51. double value;
  52. double alarmLevel;
  53. bool alarmEnabled;
  54. };
  55. /*!
  56. Constructor
  57. \param parent Parent widget
  58. */
  59. QwtThermo::QwtThermo( QWidget *parent ):
  60. QWidget( parent )
  61. {
  62. initThermo();
  63. }
  64. void QwtThermo::initThermo()
  65. {
  66. d_data = new PrivateData;
  67. setRange( d_data->minValue, d_data->maxValue, false );
  68. QSizePolicy policy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
  69. if ( d_data->orientation == Qt::Vertical )
  70. policy.transpose();
  71. setSizePolicy( policy );
  72. setAttribute( Qt::WA_WState_OwnSizePolicy, false );
  73. }
  74. //! Destructor
  75. QwtThermo::~QwtThermo()
  76. {
  77. delete d_data;
  78. }
  79. /*!
  80. Set the maximum value.
  81. \param max Maximum value
  82. \sa maxValue(), setMinValue()
  83. */
  84. void QwtThermo::setMaxValue( double max )
  85. {
  86. setRange( d_data->minValue, max );
  87. }
  88. //! Return the maximum value.
  89. double QwtThermo::maxValue() const
  90. {
  91. return d_data->maxValue;
  92. }
  93. /*!
  94. Set the minimum value.
  95. \param min Minimum value
  96. \sa minValue(), setMaxValue()
  97. */
  98. void QwtThermo::setMinValue( double min )
  99. {
  100. setRange( min, d_data->maxValue );
  101. }
  102. //! Return the minimum value.
  103. double QwtThermo::minValue() const
  104. {
  105. return d_data->minValue;
  106. }
  107. /*!
  108. Set the current value.
  109. \param value New Value
  110. \sa value()
  111. */
  112. void QwtThermo::setValue( double value )
  113. {
  114. if ( d_data->value != value )
  115. {
  116. d_data->value = value;
  117. update();
  118. }
  119. }
  120. //! Return the value.
  121. double QwtThermo::value() const
  122. {
  123. return d_data->value;
  124. }
  125. /*!
  126. \brief Set a scale draw
  127. For changing the labels of the scales, it
  128. is necessary to derive from QwtScaleDraw and
  129. overload QwtScaleDraw::label().
  130. \param scaleDraw ScaleDraw object, that has to be created with
  131. new and will be deleted in ~QwtThermo or the next
  132. call of setScaleDraw().
  133. */
  134. void QwtThermo::setScaleDraw( QwtScaleDraw *scaleDraw )
  135. {
  136. setAbstractScaleDraw( scaleDraw );
  137. }
  138. /*!
  139. \return the scale draw of the thermo
  140. \sa setScaleDraw()
  141. */
  142. const QwtScaleDraw *QwtThermo::scaleDraw() const
  143. {
  144. return static_cast<const QwtScaleDraw *>( abstractScaleDraw() );
  145. }
  146. /*!
  147. \return the scale draw of the thermo
  148. \sa setScaleDraw()
  149. */
  150. QwtScaleDraw *QwtThermo::scaleDraw()
  151. {
  152. return static_cast<QwtScaleDraw *>( abstractScaleDraw() );
  153. }
  154. /*!
  155. Qt paint event.
  156. event Paint event
  157. */
  158. void QwtThermo::paintEvent( QPaintEvent *event )
  159. {
  160. // Use double-buffering
  161. const QRect &ur = event->rect();
  162. if ( ur.isValid() )
  163. {
  164. QPainter painter( this );
  165. draw( &painter, ur );
  166. }
  167. }
  168. /*!
  169. Draw the whole QwtThermo.
  170. \param painter Painter
  171. \param rect Update rectangle
  172. */
  173. void QwtThermo::draw( QPainter *painter, const QRect& rect )
  174. {
  175. if ( !d_data->thermoRect.contains( rect ) )
  176. {
  177. if ( d_data->scalePos != NoScale )
  178. scaleDraw()->draw( painter, palette() );
  179. qDrawShadePanel( painter,
  180. d_data->thermoRect.x() - d_data->borderWidth,
  181. d_data->thermoRect.y() - d_data->borderWidth,
  182. d_data->thermoRect.width() + 2 * d_data->borderWidth,
  183. d_data->thermoRect.height() + 2 * d_data->borderWidth,
  184. palette(), true, d_data->borderWidth, 0 );
  185. }
  186. drawThermo( painter );
  187. }
  188. //! Qt resize event handler
  189. void QwtThermo::resizeEvent( QResizeEvent * )
  190. {
  191. layoutThermo( false );
  192. }
  193. /*!
  194. Recalculate the QwtThermo geometry and layout based on
  195. the QwtThermo::rect() and the fonts.
  196. \param update_geometry notify the layout system and call update
  197. to redraw the scale
  198. */
  199. void QwtThermo::layoutThermo( bool update_geometry )
  200. {
  201. QRect r = rect();
  202. int mbd = 0;
  203. if ( d_data->scalePos != NoScale )
  204. {
  205. int d1, d2;
  206. scaleDraw()->getBorderDistHint( font(), d1, d2 );
  207. mbd = qMax( d1, d2 );
  208. }
  209. if ( d_data->orientation == Qt::Horizontal )
  210. {
  211. switch ( d_data->scalePos )
  212. {
  213. case TopScale:
  214. {
  215. d_data->thermoRect.setRect(
  216. r.x() + mbd + d_data->borderWidth,
  217. r.y() + r.height()
  218. - d_data->thermoWidth - 2*d_data->borderWidth,
  219. r.width() - 2*( d_data->borderWidth + mbd ),
  220. d_data->thermoWidth );
  221. scaleDraw()->setAlignment( QwtScaleDraw::TopScale );
  222. scaleDraw()->move( d_data->thermoRect.x(),
  223. d_data->thermoRect.y() - d_data->borderWidth
  224. - d_data->scaleDist );
  225. scaleDraw()->setLength( d_data->thermoRect.width() );
  226. break;
  227. }
  228. case BottomScale:
  229. case NoScale: // like Bottom but without scale
  230. default: // inconsistent orientation and scale position
  231. // Mapping between values and pixels requires
  232. // initialization of the scale geometry
  233. {
  234. d_data->thermoRect.setRect(
  235. r.x() + mbd + d_data->borderWidth,
  236. r.y() + d_data->borderWidth,
  237. r.width() - 2*( d_data->borderWidth + mbd ),
  238. d_data->thermoWidth );
  239. scaleDraw()->setAlignment( QwtScaleDraw::BottomScale );
  240. scaleDraw()->move(
  241. d_data->thermoRect.x(),
  242. d_data->thermoRect.y() + d_data->thermoRect.height()
  243. + d_data->borderWidth + d_data->scaleDist );
  244. scaleDraw()->setLength( d_data->thermoRect.width() );
  245. break;
  246. }
  247. }
  248. d_data->map.setPaintInterval( d_data->thermoRect.x(),
  249. d_data->thermoRect.x() + d_data->thermoRect.width() - 1 );
  250. }
  251. else // Qt::Vertical
  252. {
  253. switch ( d_data->scalePos )
  254. {
  255. case RightScale:
  256. {
  257. d_data->thermoRect.setRect(
  258. r.x() + d_data->borderWidth,
  259. r.y() + mbd + d_data->borderWidth,
  260. d_data->thermoWidth,
  261. r.height() - 2*( d_data->borderWidth + mbd ) );
  262. scaleDraw()->setAlignment( QwtScaleDraw::RightScale );
  263. scaleDraw()->move(
  264. d_data->thermoRect.x() + d_data->thermoRect.width()
  265. + d_data->borderWidth + d_data->scaleDist,
  266. d_data->thermoRect.y() );
  267. scaleDraw()->setLength( d_data->thermoRect.height() );
  268. break;
  269. }
  270. case LeftScale:
  271. case NoScale: // like Left but without scale
  272. default: // inconsistent orientation and scale position
  273. // Mapping between values and pixels requires
  274. // initialization of the scale geometry
  275. {
  276. d_data->thermoRect.setRect(
  277. r.x() + r.width() - 2*d_data->borderWidth - d_data->thermoWidth,
  278. r.y() + mbd + d_data->borderWidth,
  279. d_data->thermoWidth,
  280. r.height() - 2*( d_data->borderWidth + mbd ) );
  281. scaleDraw()->setAlignment( QwtScaleDraw::LeftScale );
  282. scaleDraw()->move(
  283. d_data->thermoRect.x() - d_data->scaleDist
  284. - d_data->borderWidth,
  285. d_data->thermoRect.y() );
  286. scaleDraw()->setLength( d_data->thermoRect.height() );
  287. break;
  288. }
  289. }
  290. d_data->map.setPaintInterval(
  291. d_data->thermoRect.y() + d_data->thermoRect.height() - 1,
  292. d_data->thermoRect.y() );
  293. }
  294. if ( update_geometry )
  295. {
  296. updateGeometry();
  297. update();
  298. }
  299. }
  300. /*!
  301. \brief Set the thermometer orientation and the scale position.
  302. The scale position NoScale disables the scale.
  303. \param o orientation. Possible values are Qt::Horizontal and Qt::Vertical.
  304. The default value is Qt::Vertical.
  305. \param s Position of the scale.
  306. The default value is NoScale.
  307. A valid combination of scale position and orientation is enforced:
  308. - a horizontal thermometer can have the scale positions TopScale,
  309. BottomScale or NoScale;
  310. - a vertical thermometer can have the scale positions LeftScale,
  311. RightScale or NoScale;
  312. - an invalid scale position will default to NoScale.
  313. \sa setScalePosition()
  314. */
  315. void QwtThermo::setOrientation( Qt::Orientation o, ScalePos s )
  316. {
  317. if ( o == d_data->orientation && s == d_data->scalePos )
  318. return;
  319. switch ( o )
  320. {
  321. case Qt::Horizontal:
  322. {
  323. if ( ( s == NoScale ) || ( s == BottomScale ) || ( s == TopScale ) )
  324. d_data->scalePos = s;
  325. else
  326. d_data->scalePos = NoScale;
  327. break;
  328. }
  329. case Qt::Vertical:
  330. {
  331. if ( ( s == NoScale ) || ( s == LeftScale ) || ( s == RightScale ) )
  332. d_data->scalePos = s;
  333. else
  334. d_data->scalePos = NoScale;
  335. break;
  336. }
  337. }
  338. if ( o != d_data->orientation )
  339. {
  340. if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
  341. {
  342. QSizePolicy sp = sizePolicy();
  343. sp.transpose();
  344. setSizePolicy( sp );
  345. setAttribute( Qt::WA_WState_OwnSizePolicy, false );
  346. }
  347. }
  348. d_data->orientation = o;
  349. layoutThermo();
  350. }
  351. /*!
  352. \brief Change the scale position (and thermometer orientation).
  353. \param scalePos Position of the scale.
  354. A valid combination of scale position and orientation is enforced:
  355. - if the new scale position is LeftScale or RightScale, the
  356. scale orientation will become Qt::Vertical;
  357. - if the new scale position is BottomScale or TopScale, the scale
  358. orientation will become Qt::Horizontal;
  359. - if the new scale position is NoScale, the scale orientation will not change.
  360. \sa setOrientation(), scalePosition()
  361. */
  362. void QwtThermo::setScalePosition( ScalePos scalePos )
  363. {
  364. if ( ( scalePos == BottomScale ) || ( scalePos == TopScale ) )
  365. setOrientation( Qt::Horizontal, scalePos );
  366. else if ( ( scalePos == LeftScale ) || ( scalePos == RightScale ) )
  367. setOrientation( Qt::Vertical, scalePos );
  368. else
  369. setOrientation( d_data->orientation, NoScale );
  370. }
  371. /*!
  372. Return the scale position.
  373. \sa setScalePosition()
  374. */
  375. QwtThermo::ScalePos QwtThermo::scalePosition() const
  376. {
  377. return d_data->scalePos;
  378. }
  379. //! Notify a font change.
  380. void QwtThermo::fontChange( const QFont &f )
  381. {
  382. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  383. QWidget::fontChange( f );
  384. #endif
  385. layoutThermo();
  386. }
  387. //! Notify a scale change.
  388. void QwtThermo::scaleChange()
  389. {
  390. update();
  391. layoutThermo();
  392. }
  393. /*!
  394. Redraw the liquid in thermometer pipe.
  395. \param painter Painter
  396. */
  397. void QwtThermo::drawThermo( QPainter *painter )
  398. {
  399. int alarm = 0, taval = 0;
  400. QRect fRect;
  401. QRect aRect;
  402. QRect bRect;
  403. int inverted = ( d_data->maxValue < d_data->minValue );
  404. //
  405. // Determine if value exceeds alarm threshold.
  406. // Note: The alarm value is allowed to lie
  407. // outside the interval (minValue, maxValue).
  408. //
  409. if ( d_data->alarmEnabled )
  410. {
  411. if ( inverted )
  412. {
  413. alarm = ( ( d_data->alarmLevel >= d_data->maxValue )
  414. && ( d_data->alarmLevel <= d_data->minValue )
  415. && ( d_data->value >= d_data->alarmLevel ) );
  416. }
  417. else
  418. {
  419. alarm = ( ( d_data->alarmLevel >= d_data->minValue )
  420. && ( d_data->alarmLevel <= d_data->maxValue )
  421. && ( d_data->value >= d_data->alarmLevel ) );
  422. }
  423. }
  424. //
  425. // transform values
  426. //
  427. int tval = transform( d_data->value );
  428. if ( alarm )
  429. taval = transform( d_data->alarmLevel );
  430. //
  431. // calculate recangles
  432. //
  433. if ( d_data->orientation == Qt::Horizontal )
  434. {
  435. if ( inverted )
  436. {
  437. bRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  438. tval - d_data->thermoRect.x(),
  439. d_data->thermoRect.height() );
  440. if ( alarm )
  441. {
  442. aRect.setRect( tval, d_data->thermoRect.y(),
  443. taval - tval + 1,
  444. d_data->thermoRect.height() );
  445. fRect.setRect( taval + 1, d_data->thermoRect.y(),
  446. d_data->thermoRect.x() + d_data->thermoRect.width() - ( taval + 1 ),
  447. d_data->thermoRect.height() );
  448. }
  449. else
  450. {
  451. fRect.setRect( tval, d_data->thermoRect.y(),
  452. d_data->thermoRect.x() + d_data->thermoRect.width() - tval,
  453. d_data->thermoRect.height() );
  454. }
  455. }
  456. else
  457. {
  458. bRect.setRect( tval + 1, d_data->thermoRect.y(),
  459. d_data->thermoRect.width() - ( tval + 1 - d_data->thermoRect.x() ),
  460. d_data->thermoRect.height() );
  461. if ( alarm )
  462. {
  463. aRect.setRect( taval, d_data->thermoRect.y(),
  464. tval - taval + 1,
  465. d_data->thermoRect.height() );
  466. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  467. taval - d_data->thermoRect.x(),
  468. d_data->thermoRect.height() );
  469. }
  470. else
  471. {
  472. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  473. tval - d_data->thermoRect.x() + 1,
  474. d_data->thermoRect.height() );
  475. }
  476. }
  477. }
  478. else // Qt::Vertical
  479. {
  480. if ( tval < d_data->thermoRect.y() )
  481. tval = d_data->thermoRect.y();
  482. else
  483. {
  484. if ( tval > d_data->thermoRect.y() + d_data->thermoRect.height() )
  485. tval = d_data->thermoRect.y() + d_data->thermoRect.height();
  486. }
  487. if ( inverted )
  488. {
  489. bRect.setRect( d_data->thermoRect.x(), tval + 1,
  490. d_data->thermoRect.width(),
  491. d_data->thermoRect.height() - ( tval + 1 - d_data->thermoRect.y() ) );
  492. if ( alarm )
  493. {
  494. aRect.setRect( d_data->thermoRect.x(), taval,
  495. d_data->thermoRect.width(),
  496. tval - taval + 1 );
  497. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  498. d_data->thermoRect.width(),
  499. taval - d_data->thermoRect.y() );
  500. }
  501. else
  502. {
  503. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  504. d_data->thermoRect.width(),
  505. tval - d_data->thermoRect.y() + 1 );
  506. }
  507. }
  508. else
  509. {
  510. bRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  511. d_data->thermoRect.width(),
  512. tval - d_data->thermoRect.y() );
  513. if ( alarm )
  514. {
  515. aRect.setRect( d_data->thermoRect.x(), tval,
  516. d_data->thermoRect.width(),
  517. taval - tval + 1 );
  518. fRect.setRect( d_data->thermoRect.x(), taval + 1,
  519. d_data->thermoRect.width(),
  520. d_data->thermoRect.y() + d_data->thermoRect.height() - ( taval + 1 ) );
  521. }
  522. else
  523. {
  524. fRect.setRect( d_data->thermoRect.x(), tval,
  525. d_data->thermoRect.width(),
  526. d_data->thermoRect.y() + d_data->thermoRect.height() - tval );
  527. }
  528. }
  529. }
  530. //
  531. // paint thermometer
  532. //
  533. const QColor bgColor = palette().color( QPalette::Window );
  534. painter->fillRect( bRect, bgColor );
  535. if ( alarm )
  536. painter->fillRect( aRect, d_data->alarmBrush );
  537. painter->fillRect( fRect, d_data->fillBrush );
  538. }
  539. /*!
  540. Set the border width of the pipe.
  541. \param width Border width
  542. \sa borderWidth()
  543. */
  544. void QwtThermo::setBorderWidth( int width )
  545. {
  546. if ( ( width >= 0 ) && ( width < ( qMin( d_data->thermoRect.width(),
  547. d_data->thermoRect.height() ) + d_data->borderWidth ) / 2 - 1 ) )
  548. {
  549. d_data->borderWidth = width;
  550. layoutThermo();
  551. }
  552. }
  553. /*!
  554. Return the border width of the thermometer pipe.
  555. \sa setBorderWidth()
  556. */
  557. int QwtThermo::borderWidth() const
  558. {
  559. return d_data->borderWidth;
  560. }
  561. /*!
  562. \brief Set the range
  563. \param vmin value corresponding lower or left end of the thermometer
  564. \param vmax value corresponding to the upper or right end of the thermometer
  565. \param logarithmic logarithmic mapping, true or false
  566. */
  567. void QwtThermo::setRange( double vmin, double vmax, bool logarithmic )
  568. {
  569. d_data->minValue = vmin;
  570. d_data->maxValue = vmax;
  571. if ( logarithmic )
  572. setScaleEngine( new QwtLog10ScaleEngine );
  573. else
  574. setScaleEngine( new QwtLinearScaleEngine );
  575. /*
  576. There are two different maps, one for the scale, the other
  577. for the values. This is confusing and will be changed
  578. in the future. TODO ...
  579. */
  580. d_data->map.setTransformation( scaleEngine()->transformation() );
  581. d_data->map.setScaleInterval( d_data->minValue, d_data->maxValue );
  582. if ( autoScale() )
  583. rescale( d_data->minValue, d_data->maxValue );
  584. layoutThermo();
  585. }
  586. /*!
  587. \brief Change the brush of the liquid.
  588. \param brush New brush. The default brush is solid black.
  589. \sa fillBrush()
  590. */
  591. void QwtThermo::setFillBrush( const QBrush& brush )
  592. {
  593. d_data->fillBrush = brush;
  594. update();
  595. }
  596. /*!
  597. Return the liquid brush.
  598. \sa setFillBrush()
  599. */
  600. const QBrush& QwtThermo::fillBrush() const
  601. {
  602. return d_data->fillBrush;
  603. }
  604. /*!
  605. \brief Change the color of the liquid.
  606. \param c New color. The default color is black.
  607. \sa fillColor()
  608. */
  609. void QwtThermo::setFillColor( const QColor &c )
  610. {
  611. d_data->fillBrush.setColor( c );
  612. update();
  613. }
  614. /*!
  615. Return the liquid color.
  616. \sa setFillColor()
  617. */
  618. const QColor &QwtThermo::fillColor() const
  619. {
  620. return d_data->fillBrush.color();
  621. }
  622. /*!
  623. \brief Specify the liquid brush above the alarm threshold
  624. \param brush New brush. The default is solid white.
  625. \sa alarmBrush()
  626. */
  627. void QwtThermo::setAlarmBrush( const QBrush& brush )
  628. {
  629. d_data->alarmBrush = brush;
  630. update();
  631. }
  632. /*!
  633. Return the liquid brush above the alarm threshold.
  634. \sa setAlarmBrush()
  635. */
  636. const QBrush& QwtThermo::alarmBrush() const
  637. {
  638. return d_data->alarmBrush;
  639. }
  640. /*!
  641. \brief Specify the liquid color above the alarm threshold
  642. \param c New color. The default is white.
  643. */
  644. void QwtThermo::setAlarmColor( const QColor &c )
  645. {
  646. d_data->alarmBrush.setColor( c );
  647. update();
  648. }
  649. //! Return the liquid color above the alarm threshold.
  650. const QColor &QwtThermo::alarmColor() const
  651. {
  652. return d_data->alarmBrush.color();
  653. }
  654. /*!
  655. Specify the alarm threshold.
  656. \param level Alarm threshold
  657. \sa alarmLevel()
  658. */
  659. void QwtThermo::setAlarmLevel( double level )
  660. {
  661. d_data->alarmLevel = level;
  662. d_data->alarmEnabled = 1;
  663. update();
  664. }
  665. /*!
  666. Return the alarm threshold.
  667. \sa setAlarmLevel()
  668. */
  669. double QwtThermo::alarmLevel() const
  670. {
  671. return d_data->alarmLevel;
  672. }
  673. /*!
  674. Change the width of the pipe.
  675. \param width Width of the pipe
  676. \sa pipeWidth()
  677. */
  678. void QwtThermo::setPipeWidth( int width )
  679. {
  680. if ( width > 0 )
  681. {
  682. d_data->thermoWidth = width;
  683. layoutThermo();
  684. }
  685. }
  686. /*!
  687. Return the width of the pipe.
  688. \sa setPipeWidth()
  689. */
  690. int QwtThermo::pipeWidth() const
  691. {
  692. return d_data->thermoWidth;
  693. }
  694. /*!
  695. \brief Specify the distance between the pipe's endpoints
  696. and the widget's border
  697. The margin is used to leave some space for the scale
  698. labels. If a large font is used, it is advisable to
  699. adjust the margins.
  700. \param m New Margin. The default values are 10 for
  701. horizontal orientation and 20 for vertical
  702. orientation.
  703. \warning The margin has no effect if the scale is disabled.
  704. \warning This function is a NOOP because margins are determined
  705. automatically.
  706. */
  707. void QwtThermo::setMargin( int )
  708. {
  709. }
  710. /*!
  711. \brief Enable or disable the alarm threshold
  712. \param tf true (disabled) or false (enabled)
  713. */
  714. void QwtThermo::setAlarmEnabled( bool tf )
  715. {
  716. d_data->alarmEnabled = tf;
  717. update();
  718. }
  719. //! Return if the alarm threshold is enabled or disabled.
  720. bool QwtThermo::alarmEnabled() const
  721. {
  722. return d_data->alarmEnabled;
  723. }
  724. /*!
  725. \return the minimum size hint
  726. \sa minimumSizeHint()
  727. */
  728. QSize QwtThermo::sizeHint() const
  729. {
  730. return minimumSizeHint();
  731. }
  732. /*!
  733. \brief Return a minimum size hint
  734. \warning The return value depends on the font and the scale.
  735. \sa sizeHint()
  736. */
  737. QSize QwtThermo::minimumSizeHint() const
  738. {
  739. int w = 0, h = 0;
  740. if ( d_data->scalePos != NoScale )
  741. {
  742. const int sdExtent = qCeil( scaleDraw()->extent( font() ) );
  743. const int sdLength = scaleDraw()->minLength( font() );
  744. w = sdLength;
  745. h = d_data->thermoWidth + sdExtent +
  746. d_data->borderWidth + d_data->scaleDist;
  747. }
  748. else // no scale
  749. {
  750. w = 200;
  751. h = d_data->thermoWidth;
  752. }
  753. if ( d_data->orientation == Qt::Vertical )
  754. qSwap( w, h );
  755. w += 2 * d_data->borderWidth;
  756. h += 2 * d_data->borderWidth;
  757. return QSize( w, h );
  758. }
  759. int QwtThermo::transform( double value ) const
  760. {
  761. const double min = qMin( d_data->map.s1(), d_data->map.s2() );
  762. const double max = qMax( d_data->map.s1(), d_data->map.s2() );
  763. if ( value > max )
  764. value = max;
  765. if ( value < min )
  766. value = min;
  767. return qRound( d_data->map.transform( value ) );
  768. }