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();
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
215 const QFontMetricsF fm( 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
350 #endif // !QT_NO_RICHTEXT