void tst_QTextFormat::getSetTabs() { class Comparator { public: Comparator(const QList<QTextOption::Tab> &tabs, const QList<QTextOption::Tab> &tabs2) { QCOMPARE(tabs.count(), tabs2.count()); for(int i=0; i < tabs.count(); i++) { QTextOption::Tab t1 = tabs[i]; QTextOption::Tab t2 = tabs2[i]; QCOMPARE(t1.position, t2.position); QCOMPARE(t1.type, t2.type); QCOMPARE(t1.delimiter, t2.delimiter); } } }; QList<QTextOption::Tab> tabs; QTextBlockFormat format; format.setTabPositions(tabs); Comparator c1(tabs, format.tabPositions()); QTextOption::Tab tab1; tab1.position = 1234; tabs.append(tab1); format.setTabPositions(tabs); Comparator c2(tabs, format.tabPositions()); QTextOption::Tab tab2(3456, QTextOption::RightTab, QChar('x')); tabs.append(tab2); format.setTabPositions(tabs); Comparator c3(tabs, format.tabPositions()); }
void KDReports::TextDocumentData::updatePercentSizes( const QSizeF& size ) { QTextCursor c( m_document ); c.beginEditBlock(); // TODO only if we inserted resizable images do { c.movePosition( QTextCursor::NextCharacter ); QTextCharFormat format = c.charFormat(); if ( format.hasProperty( ResizableImageProperty ) ) { Q_ASSERT( format.isImageFormat() ); QTextImageFormat imageFormat = format.toImageFormat(); updatePercentSize( imageFormat, size ); //qDebug() << "updatePercentSizes: setting image to " << imageFormat.width() << "," << imageFormat.height(); c.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor ); c.setCharFormat( imageFormat ); c.movePosition( QTextCursor::NextCharacter ); } } while ( !c.atEnd() ); if (m_usesTabPositions) { QTextFrameFormat rootFrameFormat = m_document->rootFrame()->frameFormat(); const int rootFrameMargins = rootFrameFormat.leftMargin() + rootFrameFormat.rightMargin(); QTextBlock block = m_document->firstBlock(); do { QTextBlockFormat blockFormat = block.blockFormat(); QList<QTextOption::Tab> tabs = blockFormat.tabPositions(); //qDebug() << "Looking at block" << block.blockNumber() << "tabs:" << tabs.count(); if (!tabs.isEmpty()) { for (int i = 0; i < tabs.count(); ++i) { QTextOption::Tab& tab = tabs[i]; if ( tab.delimiter == QLatin1Char('P') /* means Page -- see rightAlignedTab*/) { if ( tab.type == QTextOption::RightTab ) { //qDebug() << "Adjusted RightTab from" << tab.position << "to" << size.width(); tab.position = size.width() - rootFrameMargins; } else if ( tab.type == QTextOption::CenterTab ) { tab.position = ( size.width() - rootFrameMargins ) / 2; } } } blockFormat.setTabPositions( tabs ); //qDebug() << "Adjusted tabs:" << tabs; c.setPosition( block.position() ); c.setBlockFormat( blockFormat ); } block = block.next(); } while ( block.isValid() ); } c.endEditBlock(); }
void QTextOdfWriter::writeBlockFormat(QXmlStreamWriter &writer, QTextBlockFormat format, int formatIndex) const { writer.writeStartElement(styleNS, QString::fromLatin1("style")); writer.writeAttribute(styleNS, QString::fromLatin1("name"), QString::fromLatin1("p%1").arg(formatIndex)); writer.writeAttribute(styleNS, QString::fromLatin1("family"), QString::fromLatin1("paragraph")); writer.writeStartElement(styleNS, QString::fromLatin1("paragraph-properties")); if (format.hasProperty(QTextFormat::BlockAlignment)) { const Qt::Alignment alignment = format.alignment() & Qt::AlignHorizontal_Mask; QString value; if (alignment == Qt::AlignLeading) value = QString::fromLatin1("start"); else if (alignment == Qt::AlignTrailing) value = QString::fromLatin1("end"); else if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute)) value = QString::fromLatin1("left"); else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute)) value = QString::fromLatin1("right"); else if (alignment == Qt::AlignHCenter) value = QString::fromLatin1("center"); else if (alignment == Qt::AlignJustify) value = QString::fromLatin1("justify"); else qWarning() << "QTextOdfWriter: unsupported paragraph alignment; " << format.alignment(); if (! value.isNull()) writer.writeAttribute(foNS, QString::fromLatin1("text-align"), value); } if (format.hasProperty(QTextFormat::BlockTopMargin)) writer.writeAttribute(foNS, QString::fromLatin1("margin-top"), pixelToPoint(qMax(qreal(0.), format.topMargin())) ); if (format.hasProperty(QTextFormat::BlockBottomMargin)) writer.writeAttribute(foNS, QString::fromLatin1("margin-bottom"), pixelToPoint(qMax(qreal(0.), format.bottomMargin())) ); if (format.hasProperty(QTextFormat::BlockLeftMargin) || format.hasProperty(QTextFormat::BlockIndent)) writer.writeAttribute(foNS, QString::fromLatin1("margin-left"), pixelToPoint(qMax(qreal(0.), format.leftMargin() + format.indent()))); if (format.hasProperty(QTextFormat::BlockRightMargin)) writer.writeAttribute(foNS, QString::fromLatin1("margin-right"), pixelToPoint(qMax(qreal(0.), format.rightMargin())) ); if (format.hasProperty(QTextFormat::TextIndent)) writer.writeAttribute(foNS, QString::fromLatin1("text-indent"), pixelToPoint(format.textIndent())); if (format.hasProperty(QTextFormat::PageBreakPolicy)) { if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysBefore) writer.writeAttribute(foNS, QString::fromLatin1("break-before"), QString::fromLatin1("page")); if (format.pageBreakPolicy() & QTextFormat::PageBreak_AlwaysAfter) writer.writeAttribute(foNS, QString::fromLatin1("break-after"), QString::fromLatin1("page")); } if (format.hasProperty(QTextFormat::BackgroundBrush)) { QBrush brush = format.background(); writer.writeAttribute(foNS, QString::fromLatin1("background-color"), brush.color().name()); } if (format.hasProperty(QTextFormat::BlockNonBreakableLines)) writer.writeAttribute(foNS, QString::fromLatin1("keep-together"), format.nonBreakableLines() ? QString::fromLatin1("true") : QString::fromLatin1("false")); if (format.hasProperty(QTextFormat::TabPositions)) { QList<QTextOption::Tab> tabs = format.tabPositions(); writer.writeStartElement(styleNS, QString::fromLatin1("tab-stops")); QList<QTextOption::Tab>::Iterator iterator = tabs.begin(); while(iterator != tabs.end()) { writer.writeEmptyElement(styleNS, QString::fromLatin1("tab-stop")); writer.writeAttribute(styleNS, QString::fromLatin1("position"), pixelToPoint(iterator->position) ); QString type; switch(iterator->type) { case QTextOption::DelimiterTab: type = QString::fromLatin1("char"); break; case QTextOption::LeftTab: type = QString::fromLatin1("left"); break; case QTextOption::RightTab: type = QString::fromLatin1("right"); break; case QTextOption::CenterTab: type = QString::fromLatin1("center"); break; } writer.writeAttribute(styleNS, QString::fromLatin1("type"), type); if (iterator->delimiter != 0) writer.writeAttribute(styleNS, QString::fromLatin1("char"), iterator->delimiter); ++iterator; } writer.writeEndElement(); // tab-stops } writer.writeEndElement(); // paragraph-properties writer.writeEndElement(); // style }