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 update();
00090 }
00091
00096 void QwtTextLabel::setText(const QwtText &text)
00097 {
00098 d_data->text = text;
00099 update();
00100 }
00101
00103 const QwtText &QwtTextLabel::text() const
00104 {
00105 return d_data->text;
00106 }
00107
00109 void QwtTextLabel::clear()
00110 {
00111 d_data->text = QwtText();
00112 update();
00113 }
00114
00116 int QwtTextLabel::indent() const
00117 {
00118 return d_data->indent;
00119 }
00120
00125 void QwtTextLabel::setIndent(int indent)
00126 {
00127 if ( indent < 0 )
00128 indent = 0;
00129
00130 d_data->indent = indent;
00131 update();
00132 }
00133
00135 int QwtTextLabel::margin() const
00136 {
00137 return d_data->margin;
00138 }
00139
00144 void QwtTextLabel::setMargin(int margin)
00145 {
00146 d_data->margin = margin;
00147 update();
00148 }
00149
00151 QSize QwtTextLabel::sizeHint() const
00152 {
00153 return minimumSizeHint();
00154 }
00155
00157 QSize QwtTextLabel::minimumSizeHint() const
00158 {
00159 QSize sz = d_data->text.textSize(font());
00160
00161 int mw = 2 * (frameWidth() + d_data->margin);
00162 int mh = mw;
00163
00164 int indent = d_data->indent;
00165 if ( indent <= 0 )
00166 indent = defaultIndent();
00167
00168 if ( indent > 0 )
00169 {
00170 const int align = d_data->text.renderFlags();
00171 if ( align & Qt::AlignLeft || align & Qt::AlignRight )
00172 mw += d_data->indent;
00173 else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
00174 mh += d_data->indent;
00175 }
00176
00177 sz += QSize(mw, mh);
00178
00179 return sz;
00180 }
00181
00186 int QwtTextLabel::heightForWidth(int width) const
00187 {
00188 const int renderFlags = d_data->text.renderFlags();
00189
00190 int indent = d_data->indent;
00191 if ( indent <= 0 )
00192 indent = defaultIndent();
00193
00194 width -= 2 * frameWidth();
00195 if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
00196 width -= indent;
00197
00198 int height = d_data->text.heightForWidth(width, font());
00199 if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
00200 height += indent;
00201
00202 height += 2 * frameWidth();
00203
00204 return height;
00205 }
00206
00208 void QwtTextLabel::paintEvent(QPaintEvent *event)
00209 {
00210 #if QT_VERSION >= 0x040000
00211 QPainter painter(this);
00212
00213 if ( !contentsRect().contains( event->rect() ) )
00214 {
00215 painter.save();
00216 painter.setClipRegion( event->region() & frameRect() );
00217 drawFrame( &painter );
00218 painter.restore();
00219 }
00220
00221 painter.setClipRegion(event->region() & contentsRect());
00222
00223 drawContents( &painter );
00224 #else // QT_VERSION < 0x040000
00225 QFrame::paintEvent(event);
00226 #endif
00227
00228 }
00229
00231 void QwtTextLabel::drawContents(QPainter *painter)
00232 {
00233 const QRect r = textRect();
00234 if ( r.isEmpty() )
00235 return;
00236
00237 painter->setFont(font());
00238 #if QT_VERSION < 0x040000
00239 painter->setPen(palette().color(QPalette::Active, QColorGroup::Text));
00240 #else
00241 painter->setPen(palette().color(QPalette::Active, QPalette::Text));
00242 #endif
00243
00244 drawText(painter, r);
00245
00246 if ( hasFocus() )
00247 {
00248 const int margin = 2;
00249
00250 QRect focusRect = contentsRect();
00251 focusRect.setRect(focusRect.x() + margin, focusRect.y() + margin,
00252 focusRect.width() - 2 * margin - 2,
00253 focusRect.height() - 2 * margin - 2);
00254
00255 QwtPainter::drawFocusRect(painter, this, focusRect);
00256 }
00257 }
00258
00260 void QwtTextLabel::drawText(QPainter *painter, const QRect &textRect)
00261 {
00262 d_data->text.draw(painter, textRect);
00263 }
00264
00269 QRect QwtTextLabel::textRect() const
00270 {
00271 QRect r = contentsRect();
00272
00273 if ( !r.isEmpty() && d_data->margin > 0 )
00274 {
00275 r.setRect(r.x() + d_data->margin, r.y() + d_data->margin,
00276 r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
00277 }
00278
00279 if ( !r.isEmpty() )
00280 {
00281 int indent = d_data->indent;
00282 if ( indent <= 0 )
00283 indent = defaultIndent();
00284
00285 if ( indent > 0 )
00286 {
00287 const int renderFlags = d_data->text.renderFlags();
00288
00289 if ( renderFlags & Qt::AlignLeft )
00290 r.setX(r.x() + indent);
00291 else if ( renderFlags & Qt::AlignRight )
00292 r.setWidth(r.width() - indent);
00293 else if ( renderFlags & Qt::AlignTop )
00294 r.setY(r.y() + indent);
00295 else if ( renderFlags & Qt::AlignBottom )
00296 r.setHeight(r.height() - indent);
00297 }
00298 }
00299
00300 return r;
00301 }
00302
00303 int QwtTextLabel::defaultIndent() const
00304 {
00305 if ( frameWidth() <= 0 )
00306 return 0;
00307
00308 QFont fnt;
00309 if ( d_data->text.testPaintAttribute(QwtText::PaintUsingTextFont) )
00310 fnt = d_data->text.font();
00311 else
00312 fnt = font();
00313
00314 return QFontMetrics(fnt).width('x') / 2;
00315 }
00316