qwt_text.h 6.02 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2003   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_TEXT_H
#define QWT_TEXT_H

Bryant's avatar
Bryant committed
13
#include "qwt_global.h"
pixhawk's avatar
pixhawk committed
14 15 16
#include <qstring.h>
#include <qsize.h>
#include <qfont.h>
Bryant's avatar
Bryant committed
17
#include <qmetatype.h>
pixhawk's avatar
pixhawk committed
18 19 20 21

class QColor;
class QPen;
class QBrush;
Bryant's avatar
Bryant committed
22
class QRectF;
pixhawk's avatar
pixhawk committed
23 24 25 26 27 28 29 30 31 32 33 34
class QPainter;
class QwtTextEngine;

/*!
  \brief A class representing a text

  A QwtText is a text including a set of attributes how to render it.

  - Format\n
    A text might include control sequences (f.e tags) describing
    how to render it. Each format (f.e MathML, TeX, Qt Rich Text)
    has its own set of control sequences, that can be handles by
Bryant's avatar
Bryant committed
35
    a special QwtTextEngine for this format.
pixhawk's avatar
pixhawk committed
36 37
  - Background\n
    A text might have a background, defined by a QPen and QBrush
Bryant's avatar
Bryant committed
38 39
    to improve its visibility. The corners of the background might
    be rounded.
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45
  - Font\n
    A text might have an individual font.
  - Color\n
    A text might have an individual color.
  - Render Flags\n
    Flags from Qt::AlignmentFlag and Qt::TextFlag used like in
Bryant's avatar
Bryant committed
46
    QPainter::drawText().
pixhawk's avatar
pixhawk committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60

  \sa QwtTextEngine, QwtTextLabel
*/

class QWT_EXPORT QwtText
{
public:

    /*!
      \brief Text format

      The text format defines the QwtTextEngine, that is used to render
      the text.

Bryant's avatar
Bryant committed
61
      \sa QwtTextEngine, setTextEngine()
pixhawk's avatar
pixhawk committed
62 63
    */

Bryant's avatar
Bryant committed
64 65 66 67 68 69 70 71
    enum TextFormat
    {
        /*!
          The text format is determined using QwtTextEngine::mightRender() for
          all available text engines in increasing order > PlainText.
          If none of the text engines can render the text is rendered
          like QwtText::PlainText.
         */
pixhawk's avatar
pixhawk committed
72
        AutoText = 0,
73

Bryant's avatar
Bryant committed
74
        //! Draw the text as it is, using a QwtPlainTextEngine.
pixhawk's avatar
pixhawk committed
75
        PlainText,
Bryant's avatar
Bryant committed
76 77

        //! Use the Scribe framework (Qt Rich Text) to render the text.
pixhawk's avatar
pixhawk committed
78 79
        RichText,

Bryant's avatar
Bryant committed
80 81 82 83 84 85 86 87
        /*!
          Use a MathML (http://en.wikipedia.org/wiki/MathML) render engine
          to display the text. The Qwt MathML extension offers such an engine
          based on the MathML renderer of the Qt solutions package. 
          To enable MathML support the following code needs to be added to the
          application:
\verbatim QwtText::setTextEngine(QwtText::MathMLText, new QwtMathMLTextEngine()); \endverbatim
         */
pixhawk's avatar
pixhawk committed
88
        MathMLText,
Bryant's avatar
Bryant committed
89 90 91 92 93

        /*!
          Use a TeX (http://en.wikipedia.org/wiki/TeX) render engine
          to display the text ( not implemented yet ).
         */
pixhawk's avatar
pixhawk committed
94 95
        TeXText,

Bryant's avatar
Bryant committed
96 97 98 99
        /*!
          The number of text formats can be extended using setTextEngine.
          Formats >= QwtText::OtherFormat are not used by Qwt.
         */
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105
        OtherFormat = 100
    };

    /*!
      \brief Paint Attributes

106
      Font and color and background are optional attributes of a QwtText.
pixhawk's avatar
pixhawk committed
107 108
      The paint attributes hold the information, if they are set.
    */
Bryant's avatar
Bryant committed
109 110 111 112 113 114 115 116 117 118
    enum PaintAttribute
    {
        //! The text has an individual font.
        PaintUsingTextFont = 0x01,

        //! The text has an individual color.
        PaintUsingTextColor = 0x02,

        //! The text has an individual background.
        PaintBackground = 0x04
pixhawk's avatar
pixhawk committed
119 120
    };

Bryant's avatar
Bryant committed
121 122 123
    //! Paint attributes
    typedef QFlags<PaintAttribute> PaintAttributes;

pixhawk's avatar
pixhawk committed
124 125 126 127
    /*!
      \brief Layout Attributes
      The layout attributes affects some aspects of the layout of the text.
    */
Bryant's avatar
Bryant committed
128 129 130 131 132 133 134 135 136
    enum LayoutAttribute
    {
        /*!
          Layout the text without its margins. This mode is useful if a
          text needs to be aligned accurately, like the tick labels of a scale.
          If QwtTextEngine::textMargins is not implemented for the format
          of the text, MinimumLayout has no effect.
         */
        MinimumLayout = 0x01
pixhawk's avatar
pixhawk committed
137 138
    };

Bryant's avatar
Bryant committed
139 140 141 142 143 144
    //! Layout attributes
    typedef QFlags<LayoutAttribute> LayoutAttributes;

    QwtText( const QString & = QString::null,
             TextFormat textFormat = AutoText );
    QwtText( const QwtText & );
pixhawk's avatar
pixhawk committed
145 146
    ~QwtText();

Bryant's avatar
Bryant committed
147
    QwtText &operator=( const QwtText & );
pixhawk's avatar
pixhawk committed
148

Bryant's avatar
Bryant committed
149 150
    bool operator==( const QwtText & ) const;
    bool operator!=( const QwtText & ) const;
pixhawk's avatar
pixhawk committed
151

Bryant's avatar
Bryant committed
152 153
    void setText( const QString &,
        QwtText::TextFormat textFormat = AutoText );
pixhawk's avatar
pixhawk committed
154 155
    QString text() const;

Bryant's avatar
Bryant committed
156 157
    bool isNull() const;
    bool isEmpty() const;
pixhawk's avatar
pixhawk committed
158

Bryant's avatar
Bryant committed
159
    void setFont( const QFont & );
pixhawk's avatar
pixhawk committed
160 161
    QFont font() const;

Bryant's avatar
Bryant committed
162
    QFont usedFont( const QFont & ) const;
pixhawk's avatar
pixhawk committed
163

Bryant's avatar
Bryant committed
164
    void setRenderFlags( int flags );
pixhawk's avatar
pixhawk committed
165 166
    int renderFlags() const;

Bryant's avatar
Bryant committed
167
    void setColor( const QColor & );
pixhawk's avatar
pixhawk committed
168 169
    QColor color() const;

Bryant's avatar
Bryant committed
170
    QColor usedColor( const QColor & ) const;
pixhawk's avatar
pixhawk committed
171

Bryant's avatar
Bryant committed
172 173
    void setBorderRadius( double );
    double borderRadius() const;
pixhawk's avatar
pixhawk committed
174

Bryant's avatar
Bryant committed
175 176 177 178
    void setBorderPen( const QPen & );
    QPen borderPen() const;

    void setBackgroundBrush( const QBrush & );
pixhawk's avatar
pixhawk committed
179 180
    QBrush backgroundBrush() const;

Bryant's avatar
Bryant committed
181 182
    void setPaintAttribute( PaintAttribute, bool on = true );
    bool testPaintAttribute( PaintAttribute ) const;
pixhawk's avatar
pixhawk committed
183

Bryant's avatar
Bryant committed
184 185
    void setLayoutAttribute( LayoutAttribute, bool on = true );
    bool testLayoutAttribute( LayoutAttribute ) const;
pixhawk's avatar
pixhawk committed
186

Bryant's avatar
Bryant committed
187 188
    double heightForWidth( double width, const QFont & = QFont() ) const;
    QSizeF textSize( const QFont & = QFont() ) const;
pixhawk's avatar
pixhawk committed
189

Bryant's avatar
Bryant committed
190
    void draw( QPainter *painter, const QRectF &rect ) const;
pixhawk's avatar
pixhawk committed
191

Bryant's avatar
Bryant committed
192 193
    static const QwtTextEngine *textEngine( 
        const QString &text, QwtText::TextFormat = AutoText );
pixhawk's avatar
pixhawk committed
194

Bryant's avatar
Bryant committed
195 196
    static const QwtTextEngine *textEngine( QwtText::TextFormat );
    static void setTextEngine( QwtText::TextFormat, QwtTextEngine * );
pixhawk's avatar
pixhawk committed
197 198 199 200 201 202 203 204 205

private:
    class PrivateData;
    PrivateData *d_data;

    class LayoutCache;
    LayoutCache *d_layoutCache;
};

Bryant's avatar
Bryant committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
//! \return text().isNull()
inline bool QwtText::isNull() const
{
    return text().isNull();
}

//! \return text().isEmpty()
inline bool QwtText::isEmpty() const
{
    return text().isEmpty();
}

Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::PaintAttributes )
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtText::LayoutAttributes )

Q_DECLARE_METATYPE( QwtText )

pixhawk's avatar
pixhawk committed
223
#endif