qwt_text_label.cpp 6.72 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   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
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
#include "qwt_text_label.h"
pixhawk's avatar
pixhawk committed
11 12
#include "qwt_text.h"
#include "qwt_painter.h"
Bryant's avatar
Bryant committed
13 14 15
#include <qpainter.h>
#include <qevent.h>
#include <qmath.h>
pixhawk's avatar
pixhawk committed
16 17 18 19 20

class QwtTextLabel::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
21 22 23
        indent( 4 ),
        margin( 0 )
    {
pixhawk's avatar
pixhawk committed
24 25 26 27 28 29 30
    }

    int indent;
    int margin;
    QwtText text;
};

31
/*!
pixhawk's avatar
pixhawk committed
32 33 34
  Constructs an empty label.
  \param parent Parent widget
*/
Bryant's avatar
Bryant committed
35 36
QwtTextLabel::QwtTextLabel( QWidget *parent ):
    QFrame( parent )
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45
{
    init();
}

/*!
  Constructs a label that displays the text, text
  \param parent Parent widget
  \param text Text
*/
Bryant's avatar
Bryant committed
46 47
QwtTextLabel::QwtTextLabel( const QwtText &text, QWidget *parent ):
    QFrame( parent )
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
    init();
    d_data->text = text;
}

//! Destructor
QwtTextLabel::~QwtTextLabel()
{
    delete d_data;
}

void QwtTextLabel::init()
{
    d_data = new PrivateData();
Bryant's avatar
Bryant committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
}

/*!
   Interface for the designer plugin - does the same as setText()
   \sa plainText()
 */
void QwtTextLabel::setPlainText( const QString &text )
{
    setText( QwtText( text ) );
}

/*!
   Interface for the designer plugin

   \return Text as plain text
   \sa setPlainText(), text()
 */
QString QwtTextLabel::plainText() const
{
    return d_data->text.text();
pixhawk's avatar
pixhawk committed
83 84 85 86 87 88 89 90 91
}

/*!
   Change the label's text, keeping all other QwtText attributes
   \param text New text
   \param textFormat Format of text

  \sa QwtText
*/
Bryant's avatar
Bryant committed
92 93
void QwtTextLabel::setText( const QString &text, 
    QwtText::TextFormat textFormat )
pixhawk's avatar
pixhawk committed
94
{
Bryant's avatar
Bryant committed
95
    d_data->text.setText( text, textFormat );
pixhawk's avatar
pixhawk committed
96 97 98 99 100 101 102 103 104

    update();
    updateGeometry();
}

/*!
   Change the label's text
   \param text New text
*/
Bryant's avatar
Bryant committed
105
void QwtTextLabel::setText( const QwtText &text )
pixhawk's avatar
pixhawk committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
{
    d_data->text = text;

    update();
    updateGeometry();
}

//! Return the text
const QwtText &QwtTextLabel::text() const
{
    return d_data->text;
}

//! Clear the text and all QwtText attributes
void QwtTextLabel::clear()
{
    d_data->text = QwtText();

    update();
    updateGeometry();
}

//! Return label's text indent in pixels
int QwtTextLabel::indent() const
{
    return d_data->indent;
}

/*!
  Set label's text indent in pixels
  \param indent Indentation in pixels
*/
Bryant's avatar
Bryant committed
138
void QwtTextLabel::setIndent( int indent )
pixhawk's avatar
pixhawk committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
    if ( indent < 0 )
        indent = 0;

    d_data->indent = indent;

    update();
    updateGeometry();
}

//! Return label's text indent in pixels
int QwtTextLabel::margin() const
{
    return d_data->margin;
}

/*!
  Set label's margin in pixels
  \param margin Margin in pixels
*/
Bryant's avatar
Bryant committed
159
void QwtTextLabel::setMargin( int margin )
pixhawk's avatar
pixhawk committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
{
    d_data->margin = margin;

    update();
    updateGeometry();
}

//! Return label's margin in pixels
QSize QwtTextLabel::sizeHint() const
{
    return minimumSizeHint();
}

//! Return a minimum size hint
QSize QwtTextLabel::minimumSizeHint() const
{
Bryant's avatar
Bryant committed
176
    QSizeF sz = d_data->text.textSize( font() );
pixhawk's avatar
pixhawk committed
177

Bryant's avatar
Bryant committed
178
    int mw = 2 * ( frameWidth() + d_data->margin );
pixhawk's avatar
pixhawk committed
179 180 181 182 183 184
    int mh = mw;

    int indent = d_data->indent;
    if ( indent <= 0 )
        indent = defaultIndent();

Bryant's avatar
Bryant committed
185 186
    if ( indent > 0 )
    {
pixhawk's avatar
pixhawk committed
187 188 189 190 191 192
        const int align = d_data->text.renderFlags();
        if ( align & Qt::AlignLeft || align & Qt::AlignRight )
            mw += d_data->indent;
        else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
            mh += d_data->indent;
    }
193

Bryant's avatar
Bryant committed
194
    sz += QSizeF( mw, mh );
pixhawk's avatar
pixhawk committed
195

Bryant's avatar
Bryant committed
196
    return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
pixhawk's avatar
pixhawk committed
197 198 199 200
}

/*!
   \param width Width
Bryant's avatar
Bryant committed
201
   \return Preferred height for this widget, given the width.
pixhawk's avatar
pixhawk committed
202
*/
Bryant's avatar
Bryant committed
203
int QwtTextLabel::heightForWidth( int width ) const
pixhawk's avatar
pixhawk committed
204 205 206 207 208 209 210 211 212 213 214
{
    const int renderFlags = d_data->text.renderFlags();

    int indent = d_data->indent;
    if ( indent <= 0 )
        indent = defaultIndent();

    width -= 2 * frameWidth();
    if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
        width -= indent;

Bryant's avatar
Bryant committed
215 216
    int height = qCeil( d_data->text.heightForWidth( width, font() ) );
    if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
pixhawk's avatar
pixhawk committed
217 218 219 220 221 222 223
        height += indent;

    height += 2 * frameWidth();

    return height;
}

Bryant's avatar
Bryant committed
224 225 226 227 228
/*!
   Qt paint event
   \param event Paint event
*/
void QwtTextLabel::paintEvent( QPaintEvent *event )
pixhawk's avatar
pixhawk committed
229
{
Bryant's avatar
Bryant committed
230
    QPainter painter( this );
pixhawk's avatar
pixhawk committed
231

Bryant's avatar
Bryant committed
232 233
    if ( !contentsRect().contains( event->rect() ) )
    {
pixhawk's avatar
pixhawk committed
234 235 236 237 238 239
        painter.save();
        painter.setClipRegion( event->region() & frameRect() );
        drawFrame( &painter );
        painter.restore();
    }

Bryant's avatar
Bryant committed
240
    painter.setClipRegion( event->region() & contentsRect() );
pixhawk's avatar
pixhawk committed
241 242 243 244 245

    drawContents( &painter );
}

//! Redraw the text and focus indicator
Bryant's avatar
Bryant committed
246
void QwtTextLabel::drawContents( QPainter *painter )
pixhawk's avatar
pixhawk committed
247 248 249 250 251
{
    const QRect r = textRect();
    if ( r.isEmpty() )
        return;

Bryant's avatar
Bryant committed
252 253
    painter->setFont( font() );
    painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
pixhawk's avatar
pixhawk committed
254

Bryant's avatar
Bryant committed
255
    drawText( painter, QRectF( r ) );
pixhawk's avatar
pixhawk committed
256

Bryant's avatar
Bryant committed
257 258 259
    if ( hasFocus() )
    {
        const int m = 2;
pixhawk's avatar
pixhawk committed
260

Bryant's avatar
Bryant committed
261
        QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
pixhawk's avatar
pixhawk committed
262

Bryant's avatar
Bryant committed
263
        QwtPainter::drawFocusRect( painter, this, focusRect );
pixhawk's avatar
pixhawk committed
264 265 266 267
    }
}

//! Redraw the text
Bryant's avatar
Bryant committed
268
void QwtTextLabel::drawText( QPainter *painter, const QRectF &textRect )
pixhawk's avatar
pixhawk committed
269
{
Bryant's avatar
Bryant committed
270
    d_data->text.draw( painter, textRect );
pixhawk's avatar
pixhawk committed
271 272
}

273
/*!
Bryant's avatar
Bryant committed
274 275
  Calculate geometry for the text in widget coordinates
  \return Geometry for the text
pixhawk's avatar
pixhawk committed
276 277 278 279 280
*/
QRect QwtTextLabel::textRect() const
{
    QRect r = contentsRect();

Bryant's avatar
Bryant committed
281 282 283 284
    if ( !r.isEmpty() && d_data->margin > 0 )
    {
        r.setRect( r.x() + d_data->margin, r.y() + d_data->margin,
            r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
pixhawk's avatar
pixhawk committed
285 286
    }

Bryant's avatar
Bryant committed
287 288
    if ( !r.isEmpty() )
    {
pixhawk's avatar
pixhawk committed
289 290 291 292
        int indent = d_data->indent;
        if ( indent <= 0 )
            indent = defaultIndent();

Bryant's avatar
Bryant committed
293 294
        if ( indent > 0 )
        {
pixhawk's avatar
pixhawk committed
295 296 297
            const int renderFlags = d_data->text.renderFlags();

            if ( renderFlags & Qt::AlignLeft )
Bryant's avatar
Bryant committed
298
                r.setX( r.x() + indent );
pixhawk's avatar
pixhawk committed
299
            else if ( renderFlags & Qt::AlignRight )
Bryant's avatar
Bryant committed
300
                r.setWidth( r.width() - indent );
pixhawk's avatar
pixhawk committed
301
            else if ( renderFlags & Qt::AlignTop )
Bryant's avatar
Bryant committed
302
                r.setY( r.y() + indent );
pixhawk's avatar
pixhawk committed
303
            else if ( renderFlags & Qt::AlignBottom )
Bryant's avatar
Bryant committed
304
                r.setHeight( r.height() - indent );
pixhawk's avatar
pixhawk committed
305 306 307 308 309 310 311 312 313 314 315 316
        }
    }

    return r;
}

int QwtTextLabel::defaultIndent() const
{
    if ( frameWidth() <= 0 )
        return 0;

    QFont fnt;
Bryant's avatar
Bryant committed
317
    if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
pixhawk's avatar
pixhawk committed
318 319 320 321
        fnt = d_data->text.font();
    else
        fnt = font();

Bryant's avatar
Bryant committed
322
    return QFontMetrics( fnt ).width( 'x' ) / 2;
pixhawk's avatar
pixhawk committed
323 324
}