/*! Draw a text document into a rectangle \param painter Painter \param rect Traget rectangle \param flags Alignments/Text flags, see QPainter::drawText() \param text Text document */ void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect, int flags, const QTextDocument &text ) { QTextDocument *txt = text.clone(); painter->save(); painter->setFont( txt->defaultFont() ); qwtUnscaleFont( painter ); txt->setDefaultFont( painter->font() ); txt->setPageSize( QSizeF( rect.width(), QWIDGETSIZE_MAX ) ); QAbstractTextDocumentLayout* layout = txt->documentLayout(); const double height = layout->documentSize().height(); double y = rect.y(); if ( flags & Qt::AlignBottom ) y += ( rect.height() - height ); else if ( flags & Qt::AlignVCenter ) y += ( rect.height() - height ) / 2; QAbstractTextDocumentLayout::PaintContext context; context.palette.setColor( QPalette::Text, painter->pen().color() ); painter->translate( rect.x(), y ); layout->draw( painter, context ); painter->restore(); delete txt; }
QTextDocument *QTextDocumentProto::clone(QObject *parent) const { QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject()); if (item) return item->clone(parent); return 0; }
QTextDocument *M_PageSize::nulldoc( QString htm ) { QTextDocument *doc = new QTextDocument; if (htm.size() > 2) { doc->setHtml(htm); } HandlePrint(doc); return doc->clone(); }
void testFindingTables() { // How to find all the tables in a QTextDocument? QTextDocument textDoc; QTextCursor c( &textDoc ); QTextTable* firstTable = c.insertTable( 2, 2 ); QTextTableCell bottomRight = firstTable->cellAt( 1, 1 ); QTextTable* secondTable = bottomRight.firstCursorPosition().insertTable( 3, 3 ); // a nested table c.movePosition( QTextCursor::End ); QTextTable* thirdTable = c.insertTable( 1, 1 ); thirdTable->firstCursorPosition().insertText( "in table" ); c.insertText( "Foo" ); QList<QTextTable *> origTables; origTables << firstTable << secondTable << thirdTable; // A generic and slow solution is // curs.currentTable() && !tablesFound.contains(curs.currentTable()) // for each cursor position. Surely there's better. // We could jump to currentFrame().lastCursorPosition() but then it would skip // nested tables. QTextDocument* clonedDoc = textDoc.clone(); QSet<QTextTable *> tablesFound; { QTextCursor curs(clonedDoc); while (!curs.atEnd()) { QTextTable* currentTable = curs.currentTable(); if ( currentTable && !tablesFound.contains(currentTable) ) { tablesFound.insert( currentTable ); } curs.movePosition( QTextCursor::NextCharacter ); } QCOMPARE( tablesFound.size(), 3 ); } // Let's do something else then, let's find them by cursor position QList<QTextTable *> tablesByPos; { // first test const int firstPos = firstTable->firstCursorPosition().position(); QTextCursor curs( clonedDoc ); curs.setPosition( firstPos ); QVERIFY( curs.currentTable() ); // generic loop. works this approach is in TextDocument::breakTables now. Q_FOREACH( QTextTable* origTable, origTables ) { QTextCursor curs( clonedDoc ); curs.setPosition( origTable->firstCursorPosition().position() ); tablesByPos.append( curs.currentTable() ); } QCOMPARE( tablesByPos.size(), 3 ); QCOMPARE( tablesByPos.toSet(), tablesFound ); }
/*! Draw a text document into a rectangle \param painter Painter \param rect Traget rectangle \param flags Alignments/Text flags, see QPainter::drawText() \param text Text document */ void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect, int flags, const QTextDocument &text ) { QTextDocument *txt = text.clone(); painter->save(); QRectF unscaledRect = rect; if ( painter->font().pixelSize() < 0 ) { const QSize res = qwtScreenResolution(); const QPaintDevice *pd = painter->device(); if ( pd->logicalDpiX() != res.width() || pd->logicalDpiY() != res.height() ) { QTransform transform; transform.scale( res.width() / double( pd->logicalDpiX() ), res.height() / double( pd->logicalDpiY() )); painter->setWorldTransform( transform, true ); unscaledRect = transform.inverted().mapRect(rect); } } txt->setDefaultFont( painter->font() ); txt->setPageSize( QSizeF( unscaledRect.width(), QWIDGETSIZE_MAX ) ); QAbstractTextDocumentLayout* layout = txt->documentLayout(); const double height = layout->documentSize().height(); double y = unscaledRect.y(); if ( flags & Qt::AlignBottom ) y += ( unscaledRect.height() - height ); else if ( flags & Qt::AlignVCenter ) y += ( unscaledRect.height() - height ) / 2; QAbstractTextDocumentLayout::PaintContext context; context.palette.setColor( QPalette::Text, painter->pen().color() ); painter->translate( unscaledRect.x(), y ); layout->draw( painter, context ); painter->restore(); delete txt; }