18 #include <qtextobject.h> 19 #include <qtextdocument.h> 20 #include <qabstracttextdocumentlayout.h> 24 QString richText = text;
27 if ( flags & Qt::AlignJustify )
29 richText.prepend( QLatin1String(
"<div align=\"justify\">" ) );
30 richText.append( QLatin1String (
"</div>" ) );
32 else if ( flags & Qt::AlignRight )
34 richText.prepend( QLatin1String (
"<div align=\"right\">" ) );
35 richText.append( QLatin1String (
"</div>" ) );
37 else if ( flags & Qt::AlignHCenter )
39 richText.prepend( QLatin1String (
"<div align=\"center\">" ) );
40 richText.append( QLatin1String (
"</div>" ) );
48 class QwtRichTextDocument:
public QTextDocument
51 QwtRichTextDocument(
const QString &text,
int flags,
const QFont &font )
53 setUndoRedoEnabled(
false );
54 setDefaultFont( font );
58 (
void )documentLayout();
60 QTextOption option = defaultTextOption();
61 if ( flags & Qt::TextWordWrap )
62 option.setWrapMode( QTextOption::WordWrap );
64 option.setWrapMode( QTextOption::NoWrap );
66 option.setAlignment( static_cast<Qt::Alignment>( flags ) );
67 setDefaultTextOption( option );
69 QTextFrame *root = rootFrame();
70 QTextFrameFormat fm = root->frameFormat();
74 fm.setBottomMargin( 0 );
75 fm.setLeftMargin( 0 );
76 root->setFrameFormat( fm );
88 const QString fontKey = font.key();
91 d_ascentCache.constFind( fontKey );
93 if ( it != d_ascentCache.constEnd() )
96 const int ascent = findAscent( font );
97 d_ascentCache.insert( fontKey, ascent );
105 static const QString
dummy(
"E" );
106 static const QColor white( Qt::white );
108 const QFontMetrics fm( font );
115 p.drawText( 0, 0, pm.width(), pm.height(), 0,
dummy );
118 const QImage img = pm.toImage();
121 for ( row = 0; row < img.height(); row++ )
123 const QRgb *line =
reinterpret_cast<const QRgb *
>(
124 img.scanLine( row ) );
126 const int w = pm.width();
127 for (
int col = 0; col < w; col++ )
129 if ( line[col] != white.rgb() )
130 return fm.ascent() - row + 1;
173 const QString& text,
double width )
const 175 const QFontMetricsF fm( font );
176 const QRectF rect = fm.boundingRect(
177 QRectF( 0, 0, width, QWIDGETSIZE_MAX ), flags, text );
179 return rect.height();
192 int flags,
const QString& text )
const 194 const QFontMetricsF fm( font );
195 const QRectF rect = fm.boundingRect(
196 QRectF( 0, 0, QWIDGETSIZE_MAX, QWIDGETSIZE_MAX ), flags, text );
211 double &
left,
double &
right,
double &
top,
double &bottom )
const 213 left = right = top = 0;
215 const QFontMetricsF fm( font );
216 top = fm.ascent() - d_data->effectiveAscent( font );
217 bottom = fm.descent();
231 int flags,
const QString& text )
const 245 #ifndef QT_NO_RICHTEXT 263 const QString& text,
double width )
const 265 QwtRichTextDocument doc( text, flags, font );
267 doc.setPageSize( QSizeF( width, QWIDGETSIZE_MAX ) );
268 return doc.documentLayout()->documentSize().height();
282 int flags,
const QString& text )
const 284 QwtRichTextDocument doc( text, flags, font );
286 QTextOption option = doc.defaultTextOption();
287 if ( option.wrapMode() != QTextOption::NoWrap )
289 option.setWrapMode( QTextOption::NoWrap );
290 doc.setDefaultTextOption( option );
306 int flags,
const QString& text )
const 308 QwtRichTextDocument doc( text, flags, painter->font() );
333 return Qt::mightBeRichText( text );
345 double &
left,
double &
right,
double &
top,
double &bottom )
const 347 left = right = top = bottom = 0;
350 #endif // !QT_NO_RICHTEXT
QwtRichTextEngine()
Constructor.
virtual bool mightRender(const QString &) const QWT_OVERRIDE
virtual ~QwtPlainTextEngine()
Destructor.
static QString taggedRichText(const QString &text, int flags)
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const QWT_OVERRIDE
virtual bool mightRender(const QString &) const QWT_OVERRIDE
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const QWT_OVERRIDE
static void drawSimpleRichText(QPainter *, const QRectF &, int flags, const QTextDocument &)
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const QWT_OVERRIDE
QwtPlainTextEngine()
Constructor.
static void drawText(QPainter *, qreal x, qreal y, const QString &)
Wrapper for QPainter::drawText()
virtual double heightForWidth(const QFont &font, int flags, const QString &text, double width) const QWT_OVERRIDE
static int horizontalAdvance(const QFontMetrics &, const QString &)
virtual void draw(QPainter *, const QRectF &rect, int flags, const QString &text) const QWT_OVERRIDE
virtual QSizeF textSize(const QFont &font, int flags, const QString &text) const QWT_OVERRIDE
int effectiveAscent(const QFont &font) const
static int findAscent(const QFont &font)
QString taggedText(const QString &, int flags) const
QwtTextEngine()
Constructor.
QMap< QString, int > d_ascentCache
virtual void textMargins(const QFont &, const QString &, double &left, double &right, double &top, double &bottom) const QWT_OVERRIDE
virtual void draw(QPainter *, const QRectF &rect, int flags, const QString &text) const QWT_OVERRIDE
Draw the text in a clipping rectangle.
virtual ~QwtTextEngine()
Destructor.