# qwt_text_label.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 // vim: expandtab
    00011
    00012 #include <qpainter.h>
    00013 #include <qevent.h>
    00014 #include "qwt_text.h"
    00015 #include "qwt_painter.h"
    00016 #include "qwt_text_label.h"
    00017
    00018 class QwtTextLabel::PrivateData
    00019 {
    00020 public:
    00021     PrivateData():
    00022         indent(4),
    00023         margin(0)
    00024     {
    00025     }
    00026
    00027     int indent;
    00028     int margin;
    00029     QwtText text;
    00030 };
    00031
    00036 QwtTextLabel::QwtTextLabel(QWidget *parent):
    00037     QFrame(parent)
    00038 {
    00039     init();
    00040 }
    00041
    00042 #if QT_VERSION < 0x040000
    00043
    00048 QwtTextLabel::QwtTextLabel(QWidget *parent, const char *name):
    00049     QFrame(parent, name)
    00050 {
    00051     init();
    00052 }
    00053 #endif
    00054
    00060 QwtTextLabel::QwtTextLabel(const QwtText &text, QWidget *parent):
    00061     QFrame(parent)
    00062 {
    00063     init();
    00064     d_data->text = text;
    00065 }
    00066
    00068 QwtTextLabel::~QwtTextLabel()
    00069 {
    00070     delete d_data;
    00071 }
    00072
    00073 void QwtTextLabel::init()
    00074 {
    00075     d_data = new PrivateData();
    00076     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    00077 }
    00078
    00086 void QwtTextLabel::setText(const QString &text, QwtText::TextFormat textFormat)
    00087 {
    00088     d_data->text.setText(text, textFormat);
    00089
    00090     update();
    00091     updateGeometry();
    00092 }
    00093
    00098 void QwtTextLabel::setText(const QwtText &text)
    00099 {
    00100     d_data->text = text;
    00101
    00102     update();
    00103     updateGeometry();
    00104 }
    00105
    00107 const QwtText &QwtTextLabel::text() const
    00108 {
    00109     return d_data->text;
    00110 }
    00111
    00113 void QwtTextLabel::clear()
    00114 {
    00115     d_data->text = QwtText();
    00116
    00117     update();
    00118     updateGeometry();
    00119 }
    00120
    00122 int QwtTextLabel::indent() const
    00123 {
    00124     return d_data->indent;
    00125 }
    00126
    00131 void QwtTextLabel::setIndent(int indent)
    00132 {
    00133     if ( indent < 0 )
    00134         indent = 0;
    00135
    00136     d_data->indent = indent;
    00137
    00138     update();
    00139     updateGeometry();
    00140 }
    00141
    00143 int QwtTextLabel::margin() const
    00144 {
    00145     return d_data->margin;
    00146 }
    00147
    00152 void QwtTextLabel::setMargin(int margin)
    00153 {
    00154     d_data->margin = margin;
    00155
    00156     update();
    00157     updateGeometry();
    00158 }
    00159
    00161 QSize QwtTextLabel::sizeHint() const
    00162 {
    00163     return minimumSizeHint();
    00164 }
    00165
    00167 QSize QwtTextLabel::minimumSizeHint() const
    00168 {
    00169     QSize sz = d_data->text.textSize(font());
    00170
    00171     int mw = 2 * (frameWidth() + d_data->margin);
    00172     int mh = mw;
    00173
    00174     int indent = d_data->indent;
    00175     if ( indent <= 0 )
    00176         indent = defaultIndent();
    00177
    00178     if ( indent > 0 )
    00179     {
    00180         const int align = d_data->text.renderFlags();
    00181         if ( align & Qt::AlignLeft || align & Qt::AlignRight )
    00182             mw += d_data->indent;
    00183         else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
    00184             mh += d_data->indent;
    00185     }
    00186
    00187     sz += QSize(mw, mh);
    00188
    00189     return sz;
    00190 }
    00191
    00196 int QwtTextLabel::heightForWidth(int width) const
    00197 {
    00198     const int renderFlags = d_data->text.renderFlags();
    00199
    00200     int indent = d_data->indent;
    00201     if ( indent <= 0 )
    00202         indent = defaultIndent();
    00203
    00204     width -= 2 * frameWidth();
    00205     if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
    00206         width -= indent;
    00207
    00208     int height = d_data->text.heightForWidth(width, font());
    00209     if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
    00210         height += indent;
    00211
    00212     height += 2 * frameWidth();
    00213
    00214     return height;
    00215 }
    00216
    00218 void QwtTextLabel::paintEvent(QPaintEvent *event)
    00219 {
    00220 #if QT_VERSION >= 0x040000
    00221     QPainter painter(this);
    00222
    00223     if ( !contentsRect().contains( event->rect() ) )
    00224     {
    00225         painter.save();
    00226         painter.setClipRegion( event->region() & frameRect() );
    00227         drawFrame( &painter );
    00228         painter.restore();
    00229     }
    00230
    00231     painter.setClipRegion(event->region() & contentsRect());
    00232
    00233     drawContents( &painter );
    00234 #else // QT_VERSION < 0x040000
    00235     QFrame::paintEvent(event);
    00236 #endif
    00237
    00238 }
    00239
    00241 void QwtTextLabel::drawContents(QPainter *painter)
    00242 {
    00243     const QRect r = textRect();
    00244     if ( r.isEmpty() )
    00245         return;
    00246
    00247     painter->setFont(font());
    00248 #if QT_VERSION < 0x040000
    00249     painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
    00250 #else
    00251     painter->setPen(palette().color(QPalette::Active, QPalette::Text));
    00252 #endif
    00253
    00254     drawText(painter, r);
    00255
    00256     if ( hasFocus() )
    00257     {
    00258         const int margin = 2;
    00259
    00260         QRect focusRect = contentsRect();
    00261         focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
    00262             focusRect.width() - 2 * margin - 2,
    00263             focusRect.height() - 2 * margin - 2);
    00264
    00265         QwtPainter::drawFocusRect(painter, this, focusRect);
    00266     }
    00267 }
    00268
    00270 void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
    00271 {
    00272     d_data->text.draw(painter, textRect);
    00273 }
    00274
    00279 QRect QwtTextLabel::textRect() const
    00280 {
    00281     QRect r = contentsRect();
    00282
    00283     if ( !r.isEmpty() && d_data->margin > 0 )
    00284     {
    00285         r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
    00286             r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
    00287     }
    00288
    00289     if ( !r.isEmpty() )
    00290     {
    00291         int indent = d_data->indent;
    00292         if ( indent <= 0 )
    00293             indent = defaultIndent();
    00294
    00295         if ( indent > 0 )
    00296         {
    00297             const int renderFlags = d_data->text.renderFlags();
    00298
    00299             if ( renderFlags & Qt::AlignLeft )
    00300                 r.setX(r.x() + indent);
    00301             else if ( renderFlags & Qt::AlignRight )
    00302                 r.setWidth(r.width() - indent);
    00303             else if ( renderFlags & Qt::AlignTop )
    00304                 r.setY(r.y() + indent);
    00305             else if ( renderFlags & Qt::AlignBottom )
    00306                 r.setHeight(r.height() - indent);
    00307         }
    00308     }
    00309
    00310     return r;
    00311 }
    00312
    00313 int QwtTextLabel::defaultIndent() const
    00314 {
    00315     if ( frameWidth() <= 0 )
    00316         return 0;
    00317
    00318     QFont fnt;
    00319     if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
    00320         fnt = d_data->text.font();
    00321     else
    00322         fnt = font();
    00323
    00324     return QFontMetrics(fnt).width('x') / 2;
    00325 }
    00326