qwt_text_label.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_text_label.h"
  10. #include "qwt_text.h"
  11. #include "qwt_painter.h"
  12. #include <qpainter.h>
  13. #include <qevent.h>
  14. #include <qmath.h>
  15. class QwtTextLabel::PrivateData
  16. {
  17. public:
  18. PrivateData():
  19. indent( 4 ),
  20. margin( 0 )
  21. {
  22. }
  23. int indent;
  24. int margin;
  25. QwtText text;
  26. };
  27. /*!
  28. Constructs an empty label.
  29. \param parent Parent widget
  30. */
  31. QwtTextLabel::QwtTextLabel( QWidget *parent ):
  32. QFrame( parent )
  33. {
  34. init();
  35. }
  36. /*!
  37. Constructs a label that displays the text, text
  38. \param parent Parent widget
  39. \param text Text
  40. */
  41. QwtTextLabel::QwtTextLabel( const QwtText &text, QWidget *parent ):
  42. QFrame( parent )
  43. {
  44. init();
  45. d_data->text = text;
  46. }
  47. //! Destructor
  48. QwtTextLabel::~QwtTextLabel()
  49. {
  50. delete d_data;
  51. }
  52. void QwtTextLabel::init()
  53. {
  54. d_data = new PrivateData();
  55. setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
  56. }
  57. /*!
  58. Change the label's text, keeping all other QwtText attributes
  59. \param text New text
  60. \param textFormat Format of text
  61. \sa QwtText
  62. */
  63. void QwtTextLabel::setText( const QString &text, QwtText::TextFormat textFormat )
  64. {
  65. d_data->text.setText( text, textFormat );
  66. update();
  67. updateGeometry();
  68. }
  69. /*!
  70. Change the label's text
  71. \param text New text
  72. */
  73. void QwtTextLabel::setText( const QwtText &text )
  74. {
  75. d_data->text = text;
  76. update();
  77. updateGeometry();
  78. }
  79. //! Return the text
  80. const QwtText &QwtTextLabel::text() const
  81. {
  82. return d_data->text;
  83. }
  84. //! Clear the text and all QwtText attributes
  85. void QwtTextLabel::clear()
  86. {
  87. d_data->text = QwtText();
  88. update();
  89. updateGeometry();
  90. }
  91. //! Return label's text indent in pixels
  92. int QwtTextLabel::indent() const
  93. {
  94. return d_data->indent;
  95. }
  96. /*!
  97. Set label's text indent in pixels
  98. \param indent Indentation in pixels
  99. */
  100. void QwtTextLabel::setIndent( int indent )
  101. {
  102. if ( indent < 0 )
  103. indent = 0;
  104. d_data->indent = indent;
  105. update();
  106. updateGeometry();
  107. }
  108. //! Return label's text indent in pixels
  109. int QwtTextLabel::margin() const
  110. {
  111. return d_data->margin;
  112. }
  113. /*!
  114. Set label's margin in pixels
  115. \param margin Margin in pixels
  116. */
  117. void QwtTextLabel::setMargin( int margin )
  118. {
  119. d_data->margin = margin;
  120. update();
  121. updateGeometry();
  122. }
  123. //! Return label's margin in pixels
  124. QSize QwtTextLabel::sizeHint() const
  125. {
  126. return minimumSizeHint();
  127. }
  128. //! Return a minimum size hint
  129. QSize QwtTextLabel::minimumSizeHint() const
  130. {
  131. QSizeF sz = d_data->text.textSize( font() );
  132. int mw = 2 * ( frameWidth() + d_data->margin );
  133. int mh = mw;
  134. int indent = d_data->indent;
  135. if ( indent <= 0 )
  136. indent = defaultIndent();
  137. if ( indent > 0 )
  138. {
  139. const int align = d_data->text.renderFlags();
  140. if ( align & Qt::AlignLeft || align & Qt::AlignRight )
  141. mw += d_data->indent;
  142. else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
  143. mh += d_data->indent;
  144. }
  145. sz += QSizeF( mw, mh );
  146. return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
  147. }
  148. /*!
  149. Returns the preferred height for this widget, given the width.
  150. \param width Width
  151. */
  152. int QwtTextLabel::heightForWidth( int width ) const
  153. {
  154. const int renderFlags = d_data->text.renderFlags();
  155. int indent = d_data->indent;
  156. if ( indent <= 0 )
  157. indent = defaultIndent();
  158. width -= 2 * frameWidth();
  159. if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
  160. width -= indent;
  161. int height = d_data->text.heightForWidth( width, font() );
  162. if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
  163. height += indent;
  164. height += 2 * frameWidth();
  165. return height;
  166. }
  167. /*!
  168. Qt paint event
  169. \param event Paint event
  170. */
  171. void QwtTextLabel::paintEvent( QPaintEvent *event )
  172. {
  173. QPainter painter( this );
  174. if ( !contentsRect().contains( event->rect() ) )
  175. {
  176. painter.save();
  177. painter.setClipRegion( event->region() & frameRect() );
  178. drawFrame( &painter );
  179. painter.restore();
  180. }
  181. painter.setClipRegion( event->region() & contentsRect() );
  182. drawContents( &painter );
  183. }
  184. //! Redraw the text and focus indicator
  185. void QwtTextLabel::drawContents( QPainter *painter )
  186. {
  187. const QRect r = textRect();
  188. if ( r.isEmpty() )
  189. return;
  190. painter->setFont( font() );
  191. painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
  192. drawText( painter, r );
  193. if ( hasFocus() )
  194. {
  195. const int margin = 2;
  196. QRect focusRect = contentsRect();
  197. focusRect.setRect( focusRect.x() + margin, focusRect.y() + margin,
  198. focusRect.width() - 2 * margin - 2,
  199. focusRect.height() - 2 * margin - 2 );
  200. QwtPainter::drawFocusRect( painter, this, focusRect );
  201. }
  202. }
  203. //! Redraw the text
  204. void QwtTextLabel::drawText( QPainter *painter, const QRect &textRect )
  205. {
  206. d_data->text.draw( painter, textRect );
  207. }
  208. /*!
  209. Calculate the rect for the text in widget coordinates
  210. \return Text rect
  211. */
  212. QRect QwtTextLabel::textRect() const
  213. {
  214. QRect r = contentsRect();
  215. if ( !r.isEmpty() && d_data->margin > 0 )
  216. {
  217. r.setRect( r.x() + d_data->margin, r.y() + d_data->margin,
  218. r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
  219. }
  220. if ( !r.isEmpty() )
  221. {
  222. int indent = d_data->indent;
  223. if ( indent <= 0 )
  224. indent = defaultIndent();
  225. if ( indent > 0 )
  226. {
  227. const int renderFlags = d_data->text.renderFlags();
  228. if ( renderFlags & Qt::AlignLeft )
  229. r.setX( r.x() + indent );
  230. else if ( renderFlags & Qt::AlignRight )
  231. r.setWidth( r.width() - indent );
  232. else if ( renderFlags & Qt::AlignTop )
  233. r.setY( r.y() + indent );
  234. else if ( renderFlags & Qt::AlignBottom )
  235. r.setHeight( r.height() - indent );
  236. }
  237. }
  238. return r;
  239. }
  240. int QwtTextLabel::defaultIndent() const
  241. {
  242. if ( frameWidth() <= 0 )
  243. return 0;
  244. QFont fnt;
  245. if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
  246. fnt = d_data->text.font();
  247. else
  248. fnt = font();
  249. return QFontMetrics( fnt ).width( 'x' ) / 2;
  250. }