qwt_text_label.cpp 6.75 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- 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
 *****************************************************************************/

// vim: expandtab

#include <qpainter.h>
#include <qevent.h>
#include "qwt_text.h"
#include "qwt_painter.h"
#include "qwt_text_label.h"

class QwtTextLabel::PrivateData
{
public:
    PrivateData():
        indent(4),
23
        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 35 36 37 38 39 40 41
  Constructs an empty label.
  \param parent Parent widget
*/
QwtTextLabel::QwtTextLabel(QWidget *parent):
    QFrame(parent)
{
    init();
}

#if QT_VERSION < 0x040000
42
/*!
pixhawk's avatar
pixhawk committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  Constructs an empty label.
  \param parent Parent widget
  \param name Object name
*/
QwtTextLabel::QwtTextLabel(QWidget *parent, const char *name):
    QFrame(parent, name)
{
    init();
}
#endif

/*!
  Constructs a label that displays the text, text
  \param parent Parent widget
  \param text Text
*/
QwtTextLabel::QwtTextLabel(const QwtText &text, QWidget *parent):
    QFrame(parent)
{
    init();
    d_data->text = text;
}

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

void QwtTextLabel::init()
{
    d_data = new PrivateData();
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
}

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

  \sa QwtText
*/
void QwtTextLabel::setText(const QString &text, QwtText::TextFormat textFormat)
{
    d_data->text.setText(text, textFormat);

    update();
    updateGeometry();
}

/*!
   Change the label's text
   \param text New text
*/
void QwtTextLabel::setText(const QwtText &text)
{
    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
*/
void QwtTextLabel::setIndent(int indent)
{
    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
*/
void QwtTextLabel::setMargin(int margin)
{
    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
{
    QSize sz = d_data->text.textSize(font());

    int mw = 2 * (frameWidth() + d_data->margin);
    int mh = mw;

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

177
    if ( indent > 0 ) {
pixhawk's avatar
pixhawk committed
178 179 180 181 182 183
        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;
    }
184

pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    sz += QSize(mw, mh);

    return sz;
}

/*!
   Returns the preferred height for this widget, given the width.
   \param width Width
*/
int QwtTextLabel::heightForWidth(int width) const
{
    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;

    int height = d_data->text.heightForWidth(width, font());
    if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
        height += indent;

    height += 2 * frameWidth();

    return height;
}

//! Qt paint event
void QwtTextLabel::paintEvent(QPaintEvent *event)
{
#if QT_VERSION >= 0x040000
    QPainter painter(this);

221
    if ( !contentsRect().contains( event->rect() ) ) {
pixhawk's avatar
pixhawk committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        painter.save();
        painter.setClipRegion( event->region() & frameRect() );
        drawFrame( &painter );
        painter.restore();
    }

    painter.setClipRegion(event->region() & contentsRect());

    drawContents( &painter );
#else // QT_VERSION < 0x040000
    QFrame::paintEvent(event);
#endif

}

//! Redraw the text and focus indicator
void QwtTextLabel::drawContents(QPainter *painter)
{
    const QRect r = textRect();
    if ( r.isEmpty() )
        return;

    painter->setFont(font());
#if QT_VERSION < 0x040000
    painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
#else
    painter->setPen(palette().color(QPalette::Active, QPalette::Text));
#endif

    drawText(painter, r);

253
    if ( hasFocus() ) {
pixhawk's avatar
pixhawk committed
254 255 256 257
        const int margin = 2;

        QRect focusRect = contentsRect();
        focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
258 259
                          focusRect.width() - 2 * margin - 2,
                          focusRect.height() - 2 * margin - 2);
pixhawk's avatar
pixhawk committed
260 261 262 263 264 265 266 267 268 269 270

        QwtPainter::drawFocusRect(painter, this, focusRect);
    }
}

//! Redraw the text
void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
{
    d_data->text.draw(painter, textRect);
}

271
/*!
pixhawk's avatar
pixhawk committed
272 273 274 275 276 277 278
  Calculate the rect for the text in widget coordinates
  \return Text rect
*/
QRect QwtTextLabel::textRect() const
{
    QRect r = contentsRect();

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

284
    if ( !r.isEmpty() ) {
pixhawk's avatar
pixhawk committed
285 286 287 288
        int indent = d_data->indent;
        if ( indent <= 0 )
            indent = defaultIndent();

289
        if ( indent > 0 ) {
pixhawk's avatar
pixhawk committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
            const int renderFlags = d_data->text.renderFlags();

            if ( renderFlags & Qt::AlignLeft )
                r.setX(r.x() + indent);
            else if ( renderFlags & Qt::AlignRight )
                r.setWidth(r.width() - indent);
            else if ( renderFlags & Qt::AlignTop )
                r.setY(r.y() + indent);
            else if ( renderFlags & Qt::AlignBottom )
                r.setHeight(r.height() - indent);
        }
    }

    return r;
}

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

    QFont fnt;
    if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
        fnt = d_data->text.font();
    else
        fnt = font();

    return QFontMetrics(fnt).width('x') / 2;
}