qwt_thermo.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. QWidget::fontChange( f );
  383. layoutThermo();
  384. }
  385. //! Notify a scale change.
  386. void QwtThermo::scaleChange()
  387. {
  388. update();
  389. layoutThermo();
  390. }
  391. /*!
  392. Redraw the liquid in thermometer pipe.
  393. \param painter Painter
  394. */
  395. void QwtThermo::drawThermo( QPainter *painter )
  396. {
  397. int alarm = 0, taval = 0;
  398. QRect fRect;
  399. QRect aRect;
  400. QRect bRect;
  401. int inverted = ( d_data->maxValue < d_data->minValue );
  402. //
  403. // Determine if value exceeds alarm threshold.
  404. // Note: The alarm value is allowed to lie
  405. // outside the interval (minValue, maxValue).
  406. //
  407. if ( d_data->alarmEnabled )
  408. {
  409. if ( inverted )
  410. {
  411. alarm = ( ( d_data->alarmLevel >= d_data->maxValue )
  412. && ( d_data->alarmLevel <= d_data->minValue )
  413. && ( d_data->value >= d_data->alarmLevel ) );
  414. }
  415. else
  416. {
  417. alarm = ( ( d_data->alarmLevel >= d_data->minValue )
  418. && ( d_data->alarmLevel <= d_data->maxValue )
  419. && ( d_data->value >= d_data->alarmLevel ) );
  420. }
  421. }
  422. //
  423. // transform values
  424. //
  425. int tval = transform( d_data->value );
  426. if ( alarm )
  427. taval = transform( d_data->alarmLevel );
  428. //
  429. // calculate recangles
  430. //
  431. if ( d_data->orientation == Qt::Horizontal )
  432. {
  433. if ( inverted )
  434. {
  435. bRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  436. tval - d_data->thermoRect.x(),
  437. d_data->thermoRect.height() );
  438. if ( alarm )
  439. {
  440. aRect.setRect( tval, d_data->thermoRect.y(),
  441. taval - tval + 1,
  442. d_data->thermoRect.height() );
  443. fRect.setRect( taval + 1, d_data->thermoRect.y(),
  444. d_data->thermoRect.x() + d_data->thermoRect.width() - ( taval + 1 ),
  445. d_data->thermoRect.height() );
  446. }
  447. else
  448. {
  449. fRect.setRect( tval, d_data->thermoRect.y(),
  450. d_data->thermoRect.x() + d_data->thermoRect.width() - tval,
  451. d_data->thermoRect.height() );
  452. }
  453. }
  454. else
  455. {
  456. bRect.setRect( tval + 1, d_data->thermoRect.y(),
  457. d_data->thermoRect.width() - ( tval + 1 - d_data->thermoRect.x() ),
  458. d_data->thermoRect.height() );
  459. if ( alarm )
  460. {
  461. aRect.setRect( taval, d_data->thermoRect.y(),
  462. tval - taval + 1,
  463. d_data->thermoRect.height() );
  464. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  465. taval - d_data->thermoRect.x(),
  466. d_data->thermoRect.height() );
  467. }
  468. else
  469. {
  470. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  471. tval - d_data->thermoRect.x() + 1,
  472. d_data->thermoRect.height() );
  473. }
  474. }
  475. }
  476. else // Qt::Vertical
  477. {
  478. if ( tval < d_data->thermoRect.y() )
  479. tval = d_data->thermoRect.y();
  480. else
  481. {
  482. if ( tval > d_data->thermoRect.y() + d_data->thermoRect.height() )
  483. tval = d_data->thermoRect.y() + d_data->thermoRect.height();
  484. }
  485. if ( inverted )
  486. {
  487. bRect.setRect( d_data->thermoRect.x(), tval + 1,
  488. d_data->thermoRect.width(),
  489. d_data->thermoRect.height() - ( tval + 1 - d_data->thermoRect.y() ) );
  490. if ( alarm )
  491. {
  492. aRect.setRect( d_data->thermoRect.x(), taval,
  493. d_data->thermoRect.width(),
  494. tval - taval + 1 );
  495. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  496. d_data->thermoRect.width(),
  497. taval - d_data->thermoRect.y() );
  498. }
  499. else
  500. {
  501. fRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  502. d_data->thermoRect.width(),
  503. tval - d_data->thermoRect.y() + 1 );
  504. }
  505. }
  506. else
  507. {
  508. bRect.setRect( d_data->thermoRect.x(), d_data->thermoRect.y(),
  509. d_data->thermoRect.width(),
  510. tval - d_data->thermoRect.y() );
  511. if ( alarm )
  512. {
  513. aRect.setRect( d_data->thermoRect.x(), tval,
  514. d_data->thermoRect.width(),
  515. taval - tval + 1 );
  516. fRect.setRect( d_data->thermoRect.x(), taval + 1,
  517. d_data->thermoRect.width(),
  518. d_data->thermoRect.y() + d_data->thermoRect.height() - ( taval + 1 ) );
  519. }
  520. else
  521. {
  522. fRect.setRect( d_data->thermoRect.x(), tval,
  523. d_data->thermoRect.width(),
  524. d_data->thermoRect.y() + d_data->thermoRect.height() - tval );
  525. }
  526. }
  527. }
  528. //
  529. // paint thermometer
  530. //
  531. const QColor bgColor = palette().color( QPalette::Window );
  532. painter->fillRect( bRect, bgColor );
  533. if ( alarm )
  534. painter->fillRect( aRect, d_data->alarmBrush );
  535. painter->fillRect( fRect, d_data->fillBrush );
  536. }
  537. /*!
  538. Set the border width of the pipe.
  539. \param width Border width
  540. \sa borderWidth()
  541. */
  542. void QwtThermo::setBorderWidth( int width )
  543. {
  544. if ( ( width >= 0 ) && ( width < ( qMin( d_data->thermoRect.width(),
  545. d_data->thermoRect.height() ) + d_data->borderWidth ) / 2 - 1 ) )
  546. {
  547. d_data->borderWidth = width;
  548. layoutThermo();
  549. }
  550. }
  551. /*!
  552. Return the border width of the thermometer pipe.
  553. \sa setBorderWidth()
  554. */
  555. int QwtThermo::borderWidth() const
  556. {
  557. return d_data->borderWidth;
  558. }
  559. /*!
  560. \brief Set the range
  561. \param vmin value corresponding lower or left end of the thermometer
  562. \param vmax value corresponding to the upper or right end of the thermometer
  563. \param logarithmic logarithmic mapping, true or false
  564. */
  565. void QwtThermo::setRange( double vmin, double vmax, bool logarithmic )
  566. {
  567. d_data->minValue = vmin;
  568. d_data->maxValue = vmax;
  569. if ( logarithmic )
  570. setScaleEngine( new QwtLog10ScaleEngine );
  571. else
  572. setScaleEngine( new QwtLinearScaleEngine );
  573. /*
  574. There are two different maps, one for the scale, the other
  575. for the values. This is confusing and will be changed
  576. in the future. TODO ...
  577. */
  578. d_data->map.setTransformation( scaleEngine()->transformation() );
  579. d_data->map.setScaleInterval( d_data->minValue, d_data->maxValue );
  580. if ( autoScale() )
  581. rescale( d_data->minValue, d_data->maxValue );
  582. layoutThermo();
  583. }
  584. /*!
  585. \brief Change the brush of the liquid.
  586. \param brush New brush. The default brush is solid black.
  587. \sa fillBrush()
  588. */
  589. void QwtThermo::setFillBrush( const QBrush& brush )
  590. {
  591. d_data->fillBrush = brush;
  592. update();
  593. }
  594. /*!
  595. Return the liquid brush.
  596. \sa setFillBrush()
  597. */
  598. const QBrush& QwtThermo::fillBrush() const
  599. {
  600. return d_data->fillBrush;
  601. }
  602. /*!
  603. \brief Change the color of the liquid.
  604. \param c New color. The default color is black.
  605. \sa fillColor()
  606. */
  607. void QwtThermo::setFillColor( const QColor &c )
  608. {
  609. d_data->fillBrush.setColor( c );
  610. update();
  611. }
  612. /*!
  613. Return the liquid color.
  614. \sa setFillColor()
  615. */
  616. const QColor &QwtThermo::fillColor() const
  617. {
  618. return d_data->fillBrush.color();
  619. }
  620. /*!
  621. \brief Specify the liquid brush above the alarm threshold
  622. \param brush New brush. The default is solid white.
  623. \sa alarmBrush()
  624. */
  625. void QwtThermo::setAlarmBrush( const QBrush& brush )
  626. {
  627. d_data->alarmBrush = brush;
  628. update();
  629. }
  630. /*!
  631. Return the liquid brush above the alarm threshold.
  632. \sa setAlarmBrush()
  633. */
  634. const QBrush& QwtThermo::alarmBrush() const
  635. {
  636. return d_data->alarmBrush;
  637. }
  638. /*!
  639. \brief Specify the liquid color above the alarm threshold
  640. \param c New color. The default is white.
  641. */
  642. void QwtThermo::setAlarmColor( const QColor &c )
  643. {
  644. d_data->alarmBrush.setColor( c );
  645. update();
  646. }
  647. //! Return the liquid color above the alarm threshold.
  648. const QColor &QwtThermo::alarmColor() const
  649. {
  650. return d_data->alarmBrush.color();
  651. }
  652. /*!
  653. Specify the alarm threshold.
  654. \param level Alarm threshold
  655. \sa alarmLevel()
  656. */
  657. void QwtThermo::setAlarmLevel( double level )
  658. {
  659. d_data->alarmLevel = level;
  660. d_data->alarmEnabled = 1;
  661. update();
  662. }
  663. /*!
  664. Return the alarm threshold.
  665. \sa setAlarmLevel()
  666. */
  667. double QwtThermo::alarmLevel() const
  668. {
  669. return d_data->alarmLevel;
  670. }
  671. /*!
  672. Change the width of the pipe.
  673. \param width Width of the pipe
  674. \sa pipeWidth()
  675. */
  676. void QwtThermo::setPipeWidth( int width )
  677. {
  678. if ( width > 0 )
  679. {
  680. d_data->thermoWidth = width;
  681. layoutThermo();
  682. }
  683. }
  684. /*!
  685. Return the width of the pipe.
  686. \sa setPipeWidth()
  687. */
  688. int QwtThermo::pipeWidth() const
  689. {
  690. return d_data->thermoWidth;
  691. }
  692. /*!
  693. \brief Specify the distance between the pipe's endpoints
  694. and the widget's border
  695. The margin is used to leave some space for the scale
  696. labels. If a large font is used, it is advisable to
  697. adjust the margins.
  698. \param m New Margin. The default values are 10 for
  699. horizontal orientation and 20 for vertical
  700. orientation.
  701. \warning The margin has no effect if the scale is disabled.
  702. \warning This function is a NOOP because margins are determined
  703. automatically.
  704. */
  705. void QwtThermo::setMargin( int )
  706. {
  707. }
  708. /*!
  709. \brief Enable or disable the alarm threshold
  710. \param tf true (disabled) or false (enabled)
  711. */
  712. void QwtThermo::setAlarmEnabled( bool tf )
  713. {
  714. d_data->alarmEnabled = tf;
  715. update();
  716. }
  717. //! Return if the alarm threshold is enabled or disabled.
  718. bool QwtThermo::alarmEnabled() const
  719. {
  720. return d_data->alarmEnabled;
  721. }
  722. /*!
  723. \return the minimum size hint
  724. \sa minimumSizeHint()
  725. */
  726. QSize QwtThermo::sizeHint() const
  727. {
  728. return minimumSizeHint();
  729. }
  730. /*!
  731. \brief Return a minimum size hint
  732. \warning The return value depends on the font and the scale.
  733. \sa sizeHint()
  734. */
  735. QSize QwtThermo::minimumSizeHint() const
  736. {
  737. int w = 0, h = 0;
  738. if ( d_data->scalePos != NoScale )
  739. {
  740. const int sdExtent = qCeil( scaleDraw()->extent( font() ) );
  741. const int sdLength = scaleDraw()->minLength( font() );
  742. w = sdLength;
  743. h = d_data->thermoWidth + sdExtent +
  744. d_data->borderWidth + d_data->scaleDist;
  745. }
  746. else // no scale
  747. {
  748. w = 200;
  749. h = d_data->thermoWidth;
  750. }
  751. if ( d_data->orientation == Qt::Vertical )
  752. qSwap( w, h );
  753. w += 2 * d_data->borderWidth;
  754. h += 2 * d_data->borderWidth;
  755. return QSize( w, h );
  756. }
  757. int QwtThermo::transform( double value ) const
  758. {
  759. const double min = qMin( d_data->map.s1(), d_data->map.s2() );
  760. const double max = qMax( d_data->map.s1(), d_data->map.s2() );
  761. if ( value > max )
  762. value = max;
  763. if ( value < min )
  764. value = min;
  765. return qRound( d_data->map.transform( value ) );
  766. }