qwt_text_engine.cpp

00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2003   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 <qpixmap.h>
00014 #include <qimage.h>
00015 #include <qmap.h>
00016 #include <qwidget.h>
00017 #include "qwt_math.h"
00018 #include "qwt_painter.h"
00019 #include "qwt_text_engine.h"
00020 
00021 #if QT_VERSION < 0x040000
00022 
00023 #include <qsimplerichtext.h>
00024 #include <qstylesheet.h>
00025 
00026 class QwtRichTextDocument: public QSimpleRichText
00027 {
00028 public:
00029     QwtRichTextDocument(const QString &text, const QFont &font):
00030         QSimpleRichText(text, font)
00031     {
00032     }
00033 };
00034 
00035 #else // QT_VERSION >= 0x040000
00036 
00037 #define USE_LABEL 1
00038 #ifdef USE_LABEL
00039 #include <qlabel.h>
00040 #else
00041 #include <qtextobject.h>
00042 #endif
00043 #include <qtextdocument.h>
00044 #include <qabstracttextdocumentlayout.h>
00045 
00046 class QwtRichTextDocument: public QTextDocument
00047 {
00048 public:
00049     QwtRichTextDocument(const QString &text, const QFont &font)
00050     {
00051         setUndoRedoEnabled(false);
00052         setDefaultFont(font);
00053         setHtml(text);
00054 
00055         // make sure we have a document layout
00056         (void)documentLayout();
00057     }
00058 };
00059 
00060 #endif
00061 
00062 class QwtPlainTextEngine::PrivateData
00063 {
00064 public:
00065     int effectiveAscent(const QFont &font) const
00066     {
00067         const QString fontKey = font.key();
00068 
00069         QMap<QString, int>::const_iterator it = 
00070             d_ascentCache.find(fontKey);
00071         if ( it == d_ascentCache.end() )
00072         {
00073             int ascent = findAscent(font);
00074             it = d_ascentCache.insert(fontKey, ascent);
00075         }
00076 
00077         return (*it);
00078     }
00079 
00080 private:
00081     int findAscent(const QFont &font) const
00082     {
00083         static const QString dummy("E");
00084         static const QColor white(Qt::white);
00085 
00086         const QFontMetrics fm(font);
00087         QPixmap pm(fm.width(dummy), fm.height()); 
00088         pm.fill(white);
00089 
00090         QPainter p(&pm);
00091         p.setFont(font);  
00092         p.drawText(0, 0,  pm.width(), pm.height(), 0, dummy);
00093         p.end();
00094 
00095     #if QT_VERSION < 0x040000
00096         const QImage img = pm.convertToImage();
00097     #else
00098         const QImage img = pm.toImage();
00099     #endif
00100 
00101         int row = 0;
00102         for ( row = 0; row < img.height(); row++ )
00103         {   
00104             const QRgb *line = (const QRgb *)img.scanLine(row);
00105 
00106             const int w = pm.width();
00107             for ( int col = 0; col < w; col++ )
00108             {   
00109                 if ( line[col] != white.rgb() )
00110                     return fm.ascent() - row + 1;
00111             }
00112         }
00113 
00114         return fm.ascent();
00115     }   
00116 
00117     mutable QMap<QString, int> d_ascentCache;
00118 };
00119 
00121 QwtTextEngine::QwtTextEngine()
00122 {
00123 }
00124 
00126 QwtTextEngine::~QwtTextEngine()
00127 {
00128 }
00129 
00131 QwtPlainTextEngine::QwtPlainTextEngine()
00132 {
00133     d_data = new PrivateData;
00134 }
00135 
00137 QwtPlainTextEngine::~QwtPlainTextEngine()
00138 {
00139     delete d_data;
00140 }
00141 
00152 int QwtPlainTextEngine::heightForWidth(const QFont& font, int flags,
00153         const QString& text, int width) const
00154 {
00155     const QFontMetrics fm(font);
00156     const QRect rect = fm.boundingRect(
00157         0, 0, width, QWIDGETSIZE_MAX, flags, text);
00158 
00159     return rect.height();
00160 }
00161 
00171 QSize QwtPlainTextEngine::textSize(const QFont &font,
00172     int flags, const QString& text) const
00173 {
00174     const QFontMetrics fm(font);
00175     const QRect rect = fm.boundingRect(
00176         0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX, flags, text);
00177 
00178     return rect.size();
00179 }
00180 
00190 void QwtPlainTextEngine::textMargins(const QFont &font, const QString &,
00191     int &left, int &right, int &top, int &bottom) const
00192 {
00193     left = right = top = 0;
00194 
00195     const QFontMetrics fm(font);
00196     top = fm.ascent() - d_data->effectiveAscent(font);
00197     bottom = fm.descent() + 1;
00198 }
00199 
00210 void QwtPlainTextEngine::draw(QPainter *painter, const QRect &rect,
00211     int flags, const QString& text) const
00212 {
00213     QwtPainter::drawText(painter, rect, flags, text);
00214 }
00215 
00220 bool QwtPlainTextEngine::mightRender(const QString &) const
00221 {
00222     return true;
00223 }
00224 
00225 #ifndef QT_NO_RICHTEXT
00226 
00228 QwtRichTextEngine::QwtRichTextEngine()
00229 {
00230 }
00231 
00242 int QwtRichTextEngine::heightForWidth(const QFont& font, int flags,
00243         const QString& text, int width) const
00244 {
00245     QwtRichTextDocument doc(taggedText(text, flags), font);
00246 
00247 #if QT_VERSION < 0x040000
00248     doc.setWidth(width);
00249     const int h = doc.height();
00250 #else
00251     doc.setPageSize(QSize(width, QWIDGETSIZE_MAX));
00252     const int h = qRound(doc.documentLayout()->documentSize().height());
00253 #endif
00254     return h;
00255 }
00256 
00267 QSize QwtRichTextEngine::textSize(const QFont &font,
00268     int flags, const QString& text) const
00269 {
00270     QwtRichTextDocument doc(taggedText(text, flags), font);
00271 
00272 #if QT_VERSION < 0x040000
00273     doc.setWidth(QWIDGETSIZE_MAX);
00274 
00275     int w = doc.widthUsed();
00276     int h = doc.height();
00277 #else
00278 #if USE_LABEL 
00279     /*
00280       Unfortunately offering the bounding rect calculation in the
00281       API of QTextDocument has been forgotten in Qt <= 4.1.x. It
00282       is planned to come with Qt 4.2.x.
00283       In the meantime we need a hack with a temporary QLabel,
00284       to reengineer the internal calculations.
00285      */
00286 
00287     static int off = 0;
00288     static QLabel *label = NULL;
00289     if ( label == NULL )
00290     {
00291         label = new QLabel;
00292         label->hide();
00293 
00294         const char *s = "XXXXX";
00295         label->setText(s);
00296         int w1 = label->sizeHint().width();
00297         const QFontMetrics fm(label->font());
00298         int w2 = fm.width(s);
00299         off = w1 - w2;
00300     }
00301     label->setFont(doc.defaultFont());
00302     label->setText(text);
00303 
00304     int w = qwtMax(label->sizeHint().width() - off, 0);
00305     doc.setPageSize(QSize(w, QWIDGETSIZE_MAX));
00306     
00307     int h = qRound(doc.documentLayout()->documentSize().height());
00308 #else
00309     QTextLayout *layout = doc.begin().layout();
00310     layout->beginLayout();
00311     for(qreal y = 0;;)  
00312     {
00313         QTextLine line = layout->createLine();
00314         if (!line.isValid())
00315             break;
00316         line.setPosition(QPointF(0, y));
00317         y += line.height();
00318     }
00319     layout->endLayout();
00320 
00321     int w = qRound(layout->maximumWidth());
00322     int h = qRound(layout->boundingRect().height());
00323 
00324     h += QFontMetrics(font).descent() + 4;
00325     w += 2 * 4;
00326 #endif
00327 #endif
00328 
00329     return QSize(w, h);
00330 }
00331 
00340 void QwtRichTextEngine::draw(QPainter *painter, const QRect &rect,
00341     int flags, const QString& text) const
00342 {
00343     QwtRichTextDocument doc(taggedText(text, flags), painter->font());
00344     QwtPainter::drawSimpleRichText(painter, rect, flags, doc);
00345 }
00346 
00355 QString QwtRichTextEngine::taggedText(const QString &text, int flags) const
00356 {
00357     QString richText = text;
00358 
00359     // By default QSimpleRichText is Qt::AlignLeft
00360     if (flags & Qt::AlignJustify)
00361     {
00362         richText.prepend(QString::fromLatin1("<div align=\"justify\">"));
00363         richText.append(QString::fromLatin1("</div>"));
00364     }
00365     else if (flags & Qt::AlignRight)
00366     {
00367         richText.prepend(QString::fromLatin1("<div align=\"right\">"));
00368         richText.append(QString::fromLatin1("</div>"));
00369     }
00370     else if (flags & Qt::AlignHCenter)
00371     {
00372         richText.prepend(QString::fromLatin1("<div align=\"center\">"));
00373         richText.append(QString::fromLatin1("</div>"));
00374     }
00375 
00376     return richText;
00377 }
00378 
00385 bool QwtRichTextEngine::mightRender(const QString &text) const
00386 {
00387 #if QT_VERSION < 0x040000
00388     return QStyleSheet::mightBeRichText(text);
00389 #else
00390     return Qt::mightBeRichText(text);
00391 #endif
00392 }
00393 
00402 void QwtRichTextEngine::textMargins(const QFont &, const QString &,
00403     int &left, int &right, int &top, int &bottom) const
00404 {
00405     left = right = top = bottom = 0;
00406 }
00407 
00408 #endif // !QT_NO_RICHTEXT

Generated on Thu Jan 18 10:39:40 2007 for Qwt User's Guide by  doxygen 1.4.6