Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_text_label.h"
00011 #include "qwt_text.h"
00012 #include "qwt_painter.h"
00013 #include <qstyle.h>
00014 #include <qstyleoption.h>
00015 #include <qpainter.h>
00016 #include <qevent.h>
00017 #include <qmath.h>
00018
00019 class QwtTextLabel::PrivateData
00020 {
00021 public:
00022 PrivateData():
00023 indent( 4 ),
00024 margin( 0 )
00025 {
00026 }
00027
00028 int indent;
00029 int margin;
00030 QwtText text;
00031 };
00032
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
00071 void QwtTextLabel::setPlainText( const QString &text )
00072 {
00073 setText( QwtText( text ) );
00074 }
00075
00082 QString QwtTextLabel::plainText() const
00083 {
00084 return d_data->text.text();
00085 }
00086
00094 void QwtTextLabel::setText( const QString &text,
00095 QwtText::TextFormat textFormat )
00096 {
00097 d_data->text.setText( text, textFormat );
00098
00099 update();
00100 updateGeometry();
00101 }
00102
00107 void QwtTextLabel::setText( const QwtText &text )
00108 {
00109 d_data->text = text;
00110
00111 update();
00112 updateGeometry();
00113 }
00114
00116 const QwtText &QwtTextLabel::text() const
00117 {
00118 return d_data->text;
00119 }
00120
00122 void QwtTextLabel::clear()
00123 {
00124 d_data->text = QwtText();
00125
00126 update();
00127 updateGeometry();
00128 }
00129
00131 int QwtTextLabel::indent() const
00132 {
00133 return d_data->indent;
00134 }
00135
00140 void QwtTextLabel::setIndent( int indent )
00141 {
00142 if ( indent < 0 )
00143 indent = 0;
00144
00145 d_data->indent = indent;
00146
00147 update();
00148 updateGeometry();
00149 }
00150
00152 int QwtTextLabel::margin() const
00153 {
00154 return d_data->margin;
00155 }
00156
00161 void QwtTextLabel::setMargin( int margin )
00162 {
00163 d_data->margin = margin;
00164
00165 update();
00166 updateGeometry();
00167 }
00168
00170 QSize QwtTextLabel::sizeHint() const
00171 {
00172 return minimumSizeHint();
00173 }
00174
00176 QSize QwtTextLabel::minimumSizeHint() const
00177 {
00178 QSizeF sz = d_data->text.textSize( font() );
00179
00180 int left, right, top, bottom;
00181 getContentsMargins( &left, &top, &right, &bottom );
00182
00183 int mw = left + right + 2 * d_data->margin;
00184 int mh = top + bottom + 2 * d_data->margin;
00185
00186 int indent = d_data->indent;
00187 if ( indent <= 0 )
00188 indent = defaultIndent();
00189
00190 if ( indent > 0 )
00191 {
00192 const int align = d_data->text.renderFlags();
00193 if ( align & Qt::AlignLeft || align & Qt::AlignRight )
00194 mw += d_data->indent;
00195 else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
00196 mh += d_data->indent;
00197 }
00198
00199 sz += QSizeF( mw, mh );
00200
00201 return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
00202 }
00203
00208 int QwtTextLabel::heightForWidth( int width ) const
00209 {
00210 const int renderFlags = d_data->text.renderFlags();
00211
00212 int indent = d_data->indent;
00213 if ( indent <= 0 )
00214 indent = defaultIndent();
00215
00216 int left, right, top, bottom;
00217 getContentsMargins( &left, &top, &right, &bottom );
00218
00219 width -= left + right - 2 * d_data->margin;
00220 if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
00221 width -= indent;
00222
00223 int height = qCeil( d_data->text.heightForWidth( width, font() ) );
00224 if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
00225 height += indent;
00226
00227 height += top + bottom + 2 * d_data->margin;
00228
00229 return height;
00230 }
00231
00236 void QwtTextLabel::paintEvent( QPaintEvent *event )
00237 {
00238 QPainter painter( this );
00239 painter.setClipRegion( event->region() );
00240
00241 QStyleOption opt;
00242 opt.init(this);
00243 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
00244
00245 if ( !contentsRect().contains( event->rect() ) )
00246 {
00247 painter.setClipRegion( event->region() & frameRect() );
00248 drawFrame( &painter );
00249 }
00250
00251 painter.setClipRegion( event->region() & contentsRect() );
00252
00253 drawContents( &painter );
00254 }
00255
00257 void QwtTextLabel::drawContents( QPainter *painter )
00258 {
00259 const QRect r = textRect();
00260 if ( r.isEmpty() )
00261 return;
00262
00263 painter->setFont( font() );
00264 painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
00265
00266 drawText( painter, QRectF( r ) );
00267
00268 if ( hasFocus() )
00269 {
00270 const int m = 2;
00271
00272 QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
00273
00274 QwtPainter::drawFocusRect( painter, this, focusRect );
00275 }
00276 }
00277
00279 void QwtTextLabel::drawText( QPainter *painter, const QRectF &textRect )
00280 {
00281 d_data->text.draw( painter, textRect );
00282 }
00283
00288 QRect QwtTextLabel::textRect() const
00289 {
00290 QRect r = contentsRect();
00291
00292 if ( !r.isEmpty() && d_data->margin > 0 )
00293 {
00294 const int m = d_data->margin;
00295 r.adjust( m, m, -m, -m );
00296 }
00297
00298 if ( !r.isEmpty() )
00299 {
00300 int indent = d_data->indent;
00301 if ( indent <= 0 )
00302 indent = defaultIndent();
00303
00304 if ( indent > 0 )
00305 {
00306 const int renderFlags = d_data->text.renderFlags();
00307
00308 if ( renderFlags & Qt::AlignLeft )
00309 {
00310 r.setX( r.x() + indent );
00311 }
00312 else if ( renderFlags & Qt::AlignRight )
00313 {
00314 r.setWidth( r.width() - indent );
00315 }
00316 else if ( renderFlags & Qt::AlignTop )
00317 {
00318 r.setY( r.y() + indent );
00319 }
00320 else if ( renderFlags & Qt::AlignBottom )
00321 {
00322 r.setHeight( r.height() - indent );
00323 }
00324 }
00325 }
00326
00327 return r;
00328 }
00329
00330 int QwtTextLabel::defaultIndent() const
00331 {
00332 if ( frameWidth() <= 0 )
00333 return 0;
00334
00335 QFont fnt;
00336 if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
00337 fnt = d_data->text.font();
00338 else
00339 fnt = font();
00340
00341 return QFontMetrics( fnt ).width( 'x' ) / 2;
00342 }
00343