Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00221 void QwtTextLabel::paintEvent(QPaintEvent *event)
00222 {
00223 #if QT_VERSION >= 0x040000
00224 QPainter painter(this);
00225
00226 if ( !contentsRect().contains( event->rect() ) )
00227 {
00228 painter.save();
00229 painter.setClipRegion( event->region() & frameRect() );
00230 drawFrame( &painter );
00231 painter.restore();
00232 }
00233
00234 painter.setClipRegion(event->region() & contentsRect());
00235
00236 drawContents( &painter );
00237 #else // QT_VERSION < 0x040000
00238 QFrame::paintEvent(event);
00239 #endif
00240
00241 }
00242
00244 void QwtTextLabel::drawContents(QPainter *painter)
00245 {
00246 const QRect r = textRect();
00247 if ( r.isEmpty() )
00248 return;
00249
00250 painter->setFont(font());
00251 #if QT_VERSION < 0x040000
00252 painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
00253 #else
00254 painter->setPen(palette().color(QPalette::Active, QPalette::Text));
00255 #endif
00256
00257 drawText(painter, r);
00258
00259 if ( hasFocus() )
00260 {
00261 const int margin = 2;
00262
00263 QRect focusRect = contentsRect();
00264 focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
00265 focusRect.width() - 2 * margin - 2,
00266 focusRect.height() - 2 * margin - 2);
00267
00268 QwtPainter::drawFocusRect(painter, this, focusRect);
00269 }
00270 }
00271
00273 void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
00274 {
00275 d_data->text.draw(painter, textRect);
00276 }
00277
00282 QRect QwtTextLabel::textRect() const
00283 {
00284 QRect r = contentsRect();
00285
00286 if ( !r.isEmpty() && d_data->margin > 0 )
00287 {
00288 r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
00289 r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
00290 }
00291
00292 if ( !r.isEmpty() )
00293 {
00294 int indent = d_data->indent;
00295 if ( indent <= 0 )
00296 indent = defaultIndent();
00297
00298 if ( indent > 0 )
00299 {
00300 const int renderFlags = d_data->text.renderFlags();
00301
00302 if ( renderFlags & Qt::AlignLeft )
00303 r.setX(r.x() + indent);
00304 else if ( renderFlags & Qt::AlignRight )
00305 r.setWidth(r.width() - indent);
00306 else if ( renderFlags & Qt::AlignTop )
00307 r.setY(r.y() + indent);
00308 else if ( renderFlags & Qt::AlignBottom )
00309 r.setHeight(r.height() - indent);
00310 }
00311 }
00312
00313 return r;
00314 }
00315
00316 int QwtTextLabel::defaultIndent() const
00317 {
00318 if ( frameWidth() <= 0 )
00319 return 0;
00320
00321 QFont fnt;
00322 if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
00323 fnt = d_data->text.font();
00324 else
00325 fnt = font();
00326
00327 return QFontMetrics(fnt).width('x') / 2;
00328 }
00329