qwt_text.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef QWT_TEXT_H
  10. #define QWT_TEXT_H
  11. #include "qwt_global.h"
  12. #include <qstring.h>
  13. #include <qsize.h>
  14. #include <qfont.h>
  15. #include <qmetatype.h>
  16. class QColor;
  17. class QPen;
  18. class QBrush;
  19. class QRectF;
  20. class QPainter;
  21. class QwtTextEngine;
  22. /*!
  23. \brief A class representing a text
  24. A QwtText is a text including a set of attributes how to render it.
  25. - Format\n
  26. A text might include control sequences (f.e tags) describing
  27. how to render it. Each format (f.e MathML, TeX, Qt Rich Text)
  28. has its own set of control sequences, that can be handles by
  29. a special QwtTextEngine for this format.
  30. - Background\n
  31. A text might have a background, defined by a QPen and QBrush
  32. to improve its visibility. The corners of the background might
  33. be rounded.
  34. - Font\n
  35. A text might have an individual font.
  36. - Color\n
  37. A text might have an individual color.
  38. - Render Flags\n
  39. Flags from Qt::AlignmentFlag and Qt::TextFlag used like in
  40. QPainter::drawText().
  41. \sa QwtTextEngine, QwtTextLabel
  42. */
  43. class QWT_EXPORT QwtText
  44. {
  45. public:
  46. /*!
  47. \brief Text format
  48. The text format defines the QwtTextEngine, that is used to render
  49. the text.
  50. \sa QwtTextEngine, setTextEngine()
  51. */
  52. enum TextFormat
  53. {
  54. /*!
  55. The text format is determined using QwtTextEngine::mightRender() for
  56. all available text engines in increasing order > PlainText.
  57. If none of the text engines can render the text is rendered
  58. like QwtText::PlainText.
  59. */
  60. AutoText = 0,
  61. //! Draw the text as it is, using a QwtPlainTextEngine.
  62. PlainText,
  63. //! Use the Scribe framework (Qt Rich Text) to render the text.
  64. RichText,
  65. /*!
  66. Use a MathML (http://en.wikipedia.org/wiki/MathML) render engine
  67. to display the text. The Qwt MathML extension offers such an engine
  68. based on the MathML renderer of the former Qt solutions package.
  69. To enable MathML support the following code needs to be added to the
  70. application:
  71. \code
  72. QwtText::setTextEngine( QwtText::MathMLText, new QwtMathMLTextEngine() );
  73. \endcode
  74. */
  75. MathMLText,
  76. /*!
  77. Use a TeX (http://en.wikipedia.org/wiki/TeX) render engine
  78. to display the text ( not implemented yet ).
  79. */
  80. TeXText,
  81. /*!
  82. The number of text formats can be extended using setTextEngine.
  83. Formats >= QwtText::OtherFormat are not used by Qwt.
  84. */
  85. OtherFormat = 100
  86. };
  87. /*!
  88. \brief Paint Attributes
  89. Font and color and background are optional attributes of a QwtText.
  90. The paint attributes hold the information, if they are set.
  91. */
  92. enum PaintAttribute
  93. {
  94. //! The text has an individual font.
  95. PaintUsingTextFont = 0x01,
  96. //! The text has an individual color.
  97. PaintUsingTextColor = 0x02,
  98. //! The text has an individual background.
  99. PaintBackground = 0x04
  100. };
  101. //! Paint attributes
  102. typedef QFlags<PaintAttribute> PaintAttributes;
  103. /*!
  104. \brief Layout Attributes
  105. The layout attributes affects some aspects of the layout of the text.
  106. */
  107. enum LayoutAttribute
  108. {
  109. /*!
  110. Layout the text without its margins. This mode is useful if a
  111. text needs to be aligned accurately, like the tick labels of a scale.
  112. If QwtTextEngine::textMargins is not implemented for the format
  113. of the text, MinimumLayout has no effect.
  114. */
  115. MinimumLayout = 0x01
  116. };
  117. //! Layout attributes
  118. typedef QFlags<LayoutAttribute> LayoutAttributes;
  119. QwtText( const QString & = QString(),
  120. TextFormat textFormat = AutoText );
  121. QwtText( const QwtText & );
  122. ~QwtText();
  123. QwtText &operator=( const QwtText & );
  124. bool operator==( const QwtText & ) const;
  125. bool operator!=( const QwtText & ) const;
  126. void setText( const QString &,
  127. QwtText::TextFormat textFormat = AutoText );
  128. QString text() const;
  129. bool isNull() const;
  130. bool isEmpty() const;
  131. void setFont( const QFont & );
  132. QFont font() const;
  133. QFont usedFont( const QFont & ) const;
  134. void setRenderFlags( int );
  135. int renderFlags() const;
  136. void setColor( const QColor & );
  137. QColor color() const;
  138. QColor usedColor( const QColor & ) const;
  139. void setBorderRadius( double );
  140. double borderRadius() const;
  141. void setBorderPen( const QPen & );
  142. QPen borderPen() const;
  143. void setBackgroundBrush( const QBrush & );
  144. QBrush backgroundBrush() const;
  145. void setPaintAttribute( PaintAttribute, bool on = true );
  146. bool testPaintAttribute( PaintAttribute ) const;
  147. void setLayoutAttribute( LayoutAttribute, bool on = true );
  148. bool testLayoutAttribute( LayoutAttribute ) const;
  149. double heightForWidth( double width, const QFont & = QFont() ) const;
  150. QSizeF textSize( const QFont & = QFont() ) const;
  151. void draw( QPainter *painter, const QRectF &rect ) const;
  152. static const QwtTextEngine *textEngine(
  153. const QString &text, QwtText::TextFormat = AutoText );
  154. static const QwtTextEngine *textEngine( QwtText::TextFormat );
  155. static void setTextEngine( QwtText::TextFormat, QwtTextEngine * );
  156. private:
  157. class PrivateData;
  158. PrivateData *d_data;
  159. class LayoutCache;
  160. LayoutCache *d_layoutCache;
  161. };
  162. //! \return text().isNull()
  163. inline bool QwtText::isNull() const
  164. {
  165. return text().isNull();
  166. }
  167. //! \return text().isEmpty()
  168. inline bool QwtText::isEmpty() const
  169. {
  170. return text().isEmpty();
  171. }
  172. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::PaintAttributes )
  173. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::LayoutAttributes )
  174. Q_DECLARE_METATYPE( QwtText )
  175. #endif