00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include <qpainter.h>
00013
#include <qpalette.h>
00014
#include <qstylesheet.h>
00015
#include "qwt_painter.h"
00016
#include "qwt_text.h"
00017
00018 Qt::TextFormat QwtText::d_defaultFormat = Qt::AutoText;
00019
00031 QwtText::QwtText(
const QString &text,
const QFont &font,
00032
int align,
const QColor &color,
const QPen &pen,
const QBrush &brush):
00033 d_align(align),
00034 d_text(text),
00035 d_font(font),
00036 d_color(color),
00037 d_fm(font),
00038 d_rectPen(pen),
00039 d_rectBrush(brush)
00040 {
00041 }
00042
00044 QwtText::~QwtText()
00045 {
00046 }
00047
00061 QwtText *
QwtText::makeText(
const QString &text,
00062
int align,
const QFont &font,
const QColor &color,
00063
const QPen &pen,
const QBrush &brush)
00064 {
00065
return makeText(text, d_defaultFormat, align, font, color, pen, brush);
00066 }
00067
00081 QwtText *
QwtText::makeText(
const QString &text, Qt::TextFormat format,
00082
int align,
const QFont &font,
const QColor &color,
00083
const QPen &pen,
const QBrush &brush)
00084 {
00085
#ifndef QT_NO_RICHTEXT
00086
if (format == Qt::RichText || ((format == Qt::AutoText)
00087 && QStyleSheet::mightBeRichText(text)))
00088 {
00089
return new QwtRichText(text, font, align, color, pen, brush);
00090 }
00091
else
00092
#endif
00093
{
00094
return new QwtPlainText(text, font, align, color, pen, brush);
00095 }
00096 }
00097
00099 void QwtText::setDefaultFormat(Qt::TextFormat format)
00100 {
00101 d_defaultFormat = format;
00102 }
00103
00105 Qt::TextFormat
QwtText::defaultFormat()
00106 {
00107
return d_defaultFormat;
00108 }
00109
00111 void QwtText::setText(
const QString &text)
00112 {
00113 d_text = text;
00114 }
00115
00117 QString
QwtText::text()
const
00118
{
00119
return d_text;
00120 }
00121
00123 void QwtText::setFont(
const QFont &font)
00124 {
00125 d_font = font;
00126 d_fm = QFontMetrics(font);
00127 }
00128
00130 QFont
QwtText::font()
const
00131
{
00132
return d_font;
00133 }
00134
00136 QFontMetrics
QwtText::fontMetrics()
const
00137
{
00138
return d_fm;
00139 }
00140
00145 void QwtText::setAlignment(
int align)
00146 {
00147 d_align = align;
00148 }
00149
00151 int QwtText::alignment()
const
00152
{
00153
return d_align;
00154 }
00155
00157 void QwtText::setColor(
const QColor &color)
00158 {
00159 d_color = color;
00160 }
00161
00163 QColor
QwtText::color()
const
00164
{
00165
return d_color;
00166 }
00167
00169 void QwtText::setRectPen(
const QPen &pen)
00170 {
00171 d_rectPen = pen;
00172 }
00173
00175 QPen
QwtText::rectPen()
const
00176
{
00177
return d_rectPen;
00178 }
00179
00181 void QwtText::setRectBrush(
const QBrush &brush)
00182 {
00183 d_rectBrush = brush;
00184 }
00185
00187 QBrush
QwtText::rectBrush()
const
00188
{
00189
return d_rectBrush;
00190 }
00191
00203 QwtPlainText::QwtPlainText(
const QString &text,
const QFont &font,
00204
int align,
const QColor &color,
const QPen &pen,
const QBrush &brush):
00205
QwtText(text, font, align, color, pen, brush)
00206 {
00207 }
00208
00216 QwtPlainText::QwtPlainText(
const QString &text,
00217
const QFont &font,
const QColor &color):
00218
QwtText(text, font,
00219 Qt::AlignCenter | Qt::WordBreak | Qt::ExpandTabs, color)
00220 {
00221 }
00222
00224 QwtText *
QwtPlainText::clone()
const
00225
{
00226
return new QwtPlainText(
00227
text(),
font(),
alignment(),
color(),
rectPen(),
rectBrush());
00228 }
00229
00235 int QwtPlainText::heightForWidth(
int width)
const
00236
{
00237
const QwtLayoutMetrics metrics(QwtPainter::metricsMap());
00238
return metrics.heightForWidth(
text(), width,
alignment(),
fontMetrics());
00239 }
00240
00246 void QwtPlainText::draw(QPainter *painter,
const QRect &rect)
const
00247
{
00248 painter->save();
00249 painter->setPen(
rectPen());
00250 painter->setBrush(
rectBrush());
00251
QwtPainter::drawRect(painter, rect);
00252 painter->restore();
00253
00254 painter->save();
00255 painter->setFont(
font());
00256 painter->setPen(
color());
00257
QwtPainter::drawText(painter, rect,
alignment(),
text());
00258 painter->restore();
00259 }
00260
00261 QRect
QwtPlainText::boundingRect(QPainter *painter)
const
00262
{
00263
const QwtLayoutMetrics metrics(QwtPainter::metricsMap());
00264
00265
if (painter)
00266 {
00267 painter->save();
00268 painter->setFont(
font());
00269
const QRect rect = metrics.boundingRect(
text(),
00270
alignment(), painter);
00271 painter->restore();
00272
return rect;
00273 }
00274
00275
return metrics.boundingRect(
text(),
alignment(),
fontMetrics());
00276 }
00277
00278
#ifndef QT_NO_RICHTEXT
00279
00291 QwtRichText::QwtRichText(
const QString &text,
const QFont &font,
00292
int align,
const QColor &color,
const QPen &pen,
const QBrush &brush):
00293
QwtText(text, font, align, color, pen, brush),
00294 d_doc(new QSimpleRichText(text, font))
00295 {
00296
setText(text);
00297 }
00298
00306 QwtRichText::QwtRichText(
const QString &text,
const QFont &font,
00307
const QColor &color):
00308
QwtText(text, font,
00309 Qt::AlignCenter | Qt::WordBreak | Qt::ExpandTabs, color),
00310 d_doc(new QSimpleRichText(text, font))
00311 {
00312
setText(text);
00313 }
00314
00316 QwtRichText::~QwtRichText()
00317 {
00318
delete d_doc;
00319 }
00320
00322 QwtText *
QwtRichText::clone()
const
00323
{
00324
return new QwtRichText(
00325
text(),
font(),
alignment(),
color(),
rectPen(),
rectBrush());
00326 }
00327
00329 void QwtRichText::setText(
const QString &text)
00330 {
00331
QwtText::setText(text);
00332
00333
delete d_doc;
00334 d_doc =
new QSimpleRichText(taggedText(text,
alignment()),
font());
00335 }
00336
00338 void QwtRichText::setFont(
const QFont &font)
00339 {
00340
#if QT_VERSION >= 300
00341
d_doc->setDefaultFont(font);
00342
#endif
00343
QwtText::setFont(font);
00344 }
00345
00347 void QwtRichText::setAlignment(
int align)
00348 {
00349
QwtText::setAlignment(align);
00350
00351
delete d_doc;
00352 d_doc =
new QSimpleRichText(taggedText(
text(), align),
font());
00353 }
00354
00360 int QwtRichText::heightForWidth(
int width)
const
00361
{
00362
#if QT_VERSION < 300
00363
const QFont defaultFont = QFont::defaultFont();
00364 QFont::setDefaultFont(
font());
00365
#endif
00366
00367
const QwtLayoutMetrics metrics(QwtPainter::metricsMap());
00368
const int height = metrics.heightForWidth(*d_doc, width);
00369
00370
#if QT_VERSION < 300
00371
QFont::setDefaultFont(defaultFont);
00372
#endif
00373
00374
return height;
00375 }
00376
00382 void QwtRichText::draw(QPainter *painter,
const QRect &rect)
const
00383
{
00384 painter->save();
00385 painter->setPen(
rectPen());
00386 painter->setBrush(
rectBrush());
00387
QwtPainter::drawRect(painter, rect);
00388 painter->restore();
00389
00390 painter->save();
00391
00392 painter->setPen(
color());
00393
#if QT_VERSION < 300
00394
const QFont defaultFont = QFont::defaultFont();
00395 QFont::setDefaultFont(
font());
00396
#else
00397
painter->setFont(
font());
00398
#endif
00399
00400
QwtPainter::drawSimpleRichText(painter, rect,
alignment(), *d_doc);
00401
00402
#if QT_VERSION < 300
00403
QFont::setDefaultFont(defaultFont);
00404
#endif
00405
painter->restore();
00406 }
00407
00408 QRect
QwtRichText::boundingRect(QPainter *painter)
const
00409
{
00410
#if QT_VERSION < 300
00411
const QFont defaultFont = QFont::defaultFont();
00412 QFont::setDefaultFont(
font());
00413
#endif
00414
00415
const QwtLayoutMetrics metrics(QwtPainter::metricsMap());
00416
const QRect rect = metrics.boundingRect(*d_doc,
alignment(), painter);
00417
00418
#if QT_VERSION < 300
00419
QFont::setDefaultFont(defaultFont);
00420
#endif
00421
00422
return rect;
00423 }
00424
00426 QString QwtRichText::taggedText(
const QString &text,
int align)
const
00427
{
00428 QString rich = text;
00429
00430
00431
#if QT_VERSION >= 300
00432
if (align & Qt::AlignJustify)
00433 {
00434 rich.prepend(QString::fromLatin1(
"<div align=\"justify\">"));
00435 rich.append(QString::fromLatin1(
"</div>"));
00436 }
else
00437
#endif
00438
if (align & Qt::AlignRight)
00439 {
00440 rich.prepend(QString::fromLatin1(
"<div align=\"right\">"));
00441 rich.append(QString::fromLatin1(
"</div>"));
00442 }
00443
else if (align & Qt::AlignHCenter)
00444 {
00445 rich.prepend(QString::fromLatin1(
"<div align=\"center\">"));
00446 rich.append(QString::fromLatin1(
"</div>"));
00447 }
00448
00449
return rich;
00450 }
00451
00452
#endif
00453
00454
00455
00456
00457
00458