Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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 
00037 QwtTextLabel::QwtTextLabel(QWidget *parent):
00038     QFrame(parent)
00039 {
00040     init();
00041 }
00042 
00048 QwtTextLabel::QwtTextLabel(const QwtText &text, QWidget *parent):
00049     QFrame(parent)
00050 {
00051     init();
00052     d_data->text = text;
00053 }
00054 
00056 QwtTextLabel::~QwtTextLabel()
00057 {
00058     delete d_data;
00059 }
00060 
00061 void QwtTextLabel::init()
00062 {
00063     d_data = new PrivateData();
00064     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00065 }
00066 
00074 void QwtTextLabel::setText(const QString &text, QwtText::TextFormat textFormat)
00075 {
00076     d_data->text.setText(text, textFormat);
00077     update();
00078 }
00079 
00084 void QwtTextLabel::setText(const QwtText &text)
00085 {
00086     d_data->text = text;
00087     update();
00088 }
00089 
00091 const QwtText &QwtTextLabel::text() const
00092 {
00093     return d_data->text;
00094 }
00095 
00097 void QwtTextLabel::clear()
00098 {
00099     d_data->text = QwtText();
00100     update();
00101 }
00102 
00104 int QwtTextLabel::indent() const
00105 {
00106     return d_data->indent;
00107 }
00108 
00113 void QwtTextLabel::setIndent(int indent)
00114 {
00115     if ( indent < 0 )
00116         indent = 0;
00117 
00118     d_data->indent = indent;
00119     update();
00120 }
00121 
00123 int QwtTextLabel::margin() const
00124 {
00125     return d_data->margin;
00126 }
00127 
00132 void QwtTextLabel::setMargin(int margin)
00133 {
00134     d_data->margin = margin;
00135     update();
00136 }
00137 
00139 QSize QwtTextLabel::sizeHint() const
00140 {
00141     return minimumSizeHint();
00142 }
00143 
00145 QSize QwtTextLabel::minimumSizeHint() const
00146 {
00147     QSize sz = d_data->text.textSize(font());
00148 
00149     int mw = 2 * (frameWidth() + d_data->margin);
00150     int mh = mw;
00151 
00152     int indent = d_data->indent;
00153     if ( indent <= 0 )
00154         indent = defaultIndent();
00155 
00156     if ( indent > 0 )
00157     {
00158         const int align = d_data->text.flags();
00159         if ( align & Qt::AlignLeft || align & Qt::AlignRight )
00160             mw += d_data->indent;
00161         else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
00162             mh += d_data->indent;
00163     }
00164         
00165     sz += QSize(mw, mh);
00166 
00167     return sz;
00168 }
00169 
00174 int QwtTextLabel::heightForWidth(int width) const
00175 {
00176     const int align = d_data->text.flags();
00177 
00178     int indent = d_data->indent;
00179     if ( indent <= 0 )
00180         indent = defaultIndent();
00181 
00182     width -= 2 * frameWidth();
00183     if ( align & Qt::AlignLeft || align & Qt::AlignRight )
00184         width -= indent;
00185 
00186     int height = d_data->text.heightForWidth(width, font());
00187     if ( align & Qt::AlignTop || align & Qt::AlignBottom )
00188         height += indent;
00189 
00190     height += 2 * frameWidth();
00191 
00192     return height;
00193 }
00194 
00196 void QwtTextLabel::paintEvent(QPaintEvent *event)
00197 {
00198 #if QT_VERSION >= 0x040000
00199     QPainter painter(this);
00200 
00201     if ( !contentsRect().contains( event->rect() ) )
00202     {
00203         painter.save();
00204         painter.setClipRegion( event->region() & frameRect() );
00205         drawFrame( &painter );
00206         painter.restore();
00207     }
00208 
00209     painter.setClipRegion(event->region() & contentsRect());
00210 
00211     drawContents( &painter );
00212 #else // QT_VERSION < 0x040000
00213     QFrame::paintEvent(event);
00214 #endif
00215 
00216 }
00217 
00219 void QwtTextLabel::drawContents(QPainter *painter)
00220 {
00221     const QRect r = textRect();
00222     if ( r.isEmpty() )
00223         return;
00224 
00225     painter->setFont(font());
00226 #if QT_VERSION < 0x040000
00227     painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
00228 #else
00229     painter->setPen(palette().color(QPalette::Active, QPalette::Text));
00230 #endif
00231 
00232     drawText(painter, r);
00233 
00234     if ( hasFocus() )
00235     {
00236         const int margin = 2;
00237 
00238         QRect focusRect = contentsRect();
00239         focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
00240             focusRect.width() - 2 * margin - 2, 
00241             focusRect.height() - 2 * margin - 2);
00242 
00243         QwtPainter::drawFocusRect(painter, this, focusRect);
00244     }
00245 }
00246 
00248 void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
00249 {
00250     d_data->text.draw(painter, textRect);
00251 }
00252 
00257 QRect QwtTextLabel::textRect() const
00258 {
00259     QRect r = contentsRect();
00260 
00261     if ( !r.isEmpty() && d_data->margin > 0 )
00262     {
00263         r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
00264             r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
00265     }
00266 
00267     if ( !r.isEmpty() )
00268     {
00269         int indent = d_data->indent;
00270         if ( indent <= 0 )
00271             indent = defaultIndent();
00272 
00273         if ( indent > 0 )
00274         {
00275             const int align = d_data->text.flags();
00276 
00277             if ( align & Qt::AlignLeft )
00278                 r.setX(r.x() + indent);
00279             else if ( align & Qt::AlignRight )
00280                 r.setWidth(r.width() - indent);
00281             else if ( align & Qt::AlignTop )
00282                 r.setY(r.y() + indent);
00283             else if ( align & Qt::AlignBottom )
00284                 r.setHeight(r.height() - indent);
00285         }
00286     }
00287 
00288     return r;
00289 }
00290 
00291 int QwtTextLabel::defaultIndent() const
00292 {
00293     if ( frameWidth() <= 0 )
00294         return 0;
00295 
00296     QFont fnt;
00297     if ( d_data->text.paintAttributes() & QwtText::PaintUsingTextFont )
00298         fnt = d_data->text.font();
00299     else
00300         fnt = font();
00301 
00302     return QFontMetrics(fnt).width('x') / 2;
00303 }
00304 

Generated on Mon Jan 30 22:16:26 2006 for Qwt User's Guide by  doxygen 1.4.4