/*! 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; }
//----------------------------------------------------------------------------- int ctkFittedTextBrowser::heightForWidth(int _width) const { QTextDocument* doc = this->document(); qreal savedWidth = doc->textWidth(); // Fudge factor. This is the difference between the frame and the // viewport. int fudge = 2 * this->frameWidth(); // Do the calculation assuming no scrollbars doc->setTextWidth(_width - fudge); int noScrollbarHeight = doc->documentLayout()->documentSize().height() + fudge; // (If noScrollbarHeight is greater than the maximum height we'll be // allowed, then there will be scrollbars, and the actual required // height will be even higher. But since in this case we've already // hit the maximum height, it doesn't matter that we underestimate.) // Get minimum height (even if string is empty): one line of text int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge; int ret = qMax(noScrollbarHeight, _minimumHeight); doc->setTextWidth(savedWidth); return ret; }
void NoteItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyle* style = KApplication::style(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter); QRect rect = option.rect; rect.adjust( m_margin, m_margin, -m_margin, -m_margin ); Nepomuk2::Resource res = index.data( Nepomuk2::Utils::SimpleResourceModel::ResourceRole ).value<Nepomuk2::Resource>(); QString plainTextContent = res.property( NIE::plainTextContent() ).toString(); QDateTime creationDate = res.property( NAO::created() ).toDateTime(); //TODO: Find a way to convert this date into "4 hours ago" format QString dateString = creationDate.toLocalTime().toString(); painter->save(); QFont f = painter->font(); f.setBold( true ); painter->setFont( f ); style->drawItemText( painter, rect, Qt::AlignLeft | Qt::AlignTop, option.palette, true, dateString ); painter->restore(); rect.setY( rect.y() + QFontMetrics(f).height()/* + m_margin */); // // Draw the excerpt // QTextDocument textDocument; textDocument.setTextWidth( rect.width() ); QFont font = textDocument.defaultFont(); font.setItalic( true ); textDocument.setDefaultFont( font ); QFontMetrics fm( font ); int numLines = rect.height() / fm.height(); int charPerLine = rect.width() / fm.averageCharWidth(); int l = (numLines-2) * charPerLine; // one line less for ending, and one line for padding QString text; // FIXME: There may be a case where some part of the text gets repeated before and after the ... if( l < plainTextContent.length() ) { text = plainTextContent.left( l ); text += QLatin1String(" .... "); text += plainTextContent.right( charPerLine-10 ); } else { text = plainTextContent; } textDocument.setPlainText( text.simplified() ); painter->save(); painter->translate( rect.topLeft() ); textDocument.drawContents( painter ); painter->restore(); }
void QtShaderWindow::onSourcesListClicked(const QModelIndex& index) { if(QListWidgetItem* item = m_sourcesListWidget->item(index.row())) { QTextDocument* document = provideDocument(item->text()); m_syntaxHighlighter->setDocument(document); m_codeWidget->setDocument(document); m_codeWidget->setEnabled(true); QFont font = document->defaultFont(); QFontMetrics metrics(font); m_codeWidget->setTabStopWidth(4 * metrics.width(' ')); } }
void InsitucCADMainWindow::setupTextEditorMonospace(QTextEdit * textEdit) { QTextDocument * document = textEdit->document(); QFont font = document->defaultFont(); font.setFamily("monospace"); font.setStyleHint(QFont::Monospace); font.setFixedPitch(true); QFontMetrics metrics(font); //Q_ASSERT(QFontInfo(font).fixedPitch()); document->setDefaultFont(font); const int tabStop = 4; textEdit->setTabStopWidth(tabStop * metrics.width(' ')); }
void printerInvoice::printf(const QString &html) { QPrinter p; QTextDocument doc; QFont font = doc.defaultFont(); font.setBold(true); font.setPointSize(font.pointSize()); doc.setDefaultFont(font); QSizeF s = QSizeF(p.logicalDpiX() * 7.5, p.logicalDpiY() * 7.5); doc.setPageSize(s); p.setOutputFormat(QPrinter::NativeFormat); doc.setHtml(html); doc.print(&p); }
void testUpdateFormats() { // How to scale all fonts in the document by a given amount? (research for TextDocument::scaleFontsBy) // I keep this Qt-only code here, can be useful for Qt bug reports for instance. QTextDocument textDoc; fillDocWithVariousSizes( textDoc ); // Collect initial sizes, for comparison QList<qreal> initialPointSizes; { QTextCursor cursor( &textDoc ); Q_FOREVER { qreal cursorFontPointSize = cursor.charFormat().fontPointSize(); if ( cursorFontPointSize == 0 ) cursorFontPointSize = textDoc.defaultFont().pointSize(); initialPointSizes << cursorFontPointSize; if ( cursor.atEnd() ) break; cursor.movePosition( QTextCursor::NextCharacter ); } } //qDebug() << initialPointSizes; // Now scale { QTextCursor cursor( &textDoc ); qreal currentPointSize = -1.0; QTextCursor lastCursor( &textDoc ); Q_FOREVER { qreal cursorFontPointSize = cursor.charFormat().fontPointSize(); //qDebug() << cursorFontPointSize << "last=" << currentPointSize << cursor.block().text() << "position=" << cursor.position(); if ( cursorFontPointSize != currentPointSize ) { if ( currentPointSize != -1.0 ) { setFontSizeHelper( lastCursor, cursor.position() - 1, currentPointSize, 1.2 ); lastCursor.setPosition( cursor.position() - 1, QTextCursor::MoveAnchor ); } currentPointSize = cursorFontPointSize; } if ( cursor.atEnd() ) break; cursor.movePosition( QTextCursor::NextCharacter ); } if ( currentPointSize != -1.0 ) { setFontSizeHelper( lastCursor, cursor.position(), currentPointSize, 1.2 ); } } // Now check { QCOMPARE( textDoc.toPlainText(), QString("i128\n1618") ); // it better not have changed :) QTextCursor cursor( &textDoc ); int i = 0; Q_FOREVER { //qDebug() << i << cursor.charFormat().fontPointSize(); #if QT_VERSION < 0x040300 // Skip checking position 0 with Qt-4.2, it worked differently then if ( i > 0 ) #endif QCOMPARE( cursor.charFormat().fontPointSize(), initialPointSizes[i] * 1.2 ); if ( cursor.atEnd() ) break; cursor.movePosition( QTextCursor::NextCharacter ); ++i; } } // Now call the actual KDReports::TextDocument implementation KDReports::TextDocument myTextDoc; fillDocWithVariousSizes(myTextDoc.contentDocument()); //myTextDoc.prepareForLayout(); myTextDoc.scaleFontsBy(1.2); // Now check that KDReports itself works { QCOMPARE( myTextDoc.contentDocument().toPlainText(), QString("i128\n1618") ); // it better not have changed :) QTextCursor cursor( &myTextDoc.contentDocument() ); int i = 0; Q_FOREVER { //qDebug() << i << cursor.charFormat().fontPointSize(); #if QT_VERSION < 0x040300 // Skip checking position 0 with Qt-4.2, it worked differently then if ( i > 0 ) #endif QCOMPARE( cursor.charFormat().fontPointSize(), initialPointSizes[i] * 1.2 ); if ( cursor.atEnd() ) break; cursor.movePosition( QTextCursor::NextCharacter ); ++i; } } }
bool ChartShape::Private::loadOdfLabel(KoShape *label, KoXmlElement &labelElement, KoShapeLoadingContext &context) { TextLabelData *labelData = qobject_cast<TextLabelData*>(label->userData()); if (!labelData) return false; // Following will always return false cause KoTextShapeData::loadOdf will try to load // a frame while our text:p is not within a frame. So, let's just not call loadOdf then... //label->loadOdf(labelElement, context); // 1. set the text KoXmlElement pElement = KoXml::namedItemNS(labelElement, KoXmlNS::text, "p"); QTextDocument* doc = labelData->document(); doc->setPlainText(pElement.text()); // 2. set the position QPointF pos = label->position(); bool posChanged = false; if (labelElement.hasAttributeNS(KoXmlNS::svg, "x")) { pos.setX(KoUnit::parseValue(labelElement.attributeNS(KoXmlNS::svg, "x", QString()))); posChanged = true; } if (labelElement.hasAttributeNS(KoXmlNS::svg, "y")) { pos.setY(KoUnit::parseValue(labelElement.attributeNS(KoXmlNS::svg, "y", QString()))); posChanged = true; } if (posChanged) { label->setPosition(pos); } // 3. set the styles if (labelElement.hasAttributeNS(KoXmlNS::chart, "style-name")) { KoStyleStack &styleStack = context.odfLoadingContext().styleStack(); styleStack.clear(); context.odfLoadingContext().fillStyleStack(labelElement, KoXmlNS::chart, "style-name", "chart"); styleStack.setTypeProperties("chart"); if (styleStack.hasProperty(KoXmlNS::style, "rotation-angle")) { qreal rotationAngle = 360 - KoUnit::parseValue(styleStack.property(KoXmlNS::style, "rotation-angle")); label->rotate(rotationAngle); } styleStack.setTypeProperties("text"); if (styleStack.hasProperty(KoXmlNS::fo, "font-size")) { const qreal fontSize = KoUnit::parseValue(styleStack.property(KoXmlNS::fo, "font-size")); QFont font = doc->defaultFont(); font.setPointSizeF(fontSize); doc->setDefaultFont(font); } if (styleStack.hasProperty(KoXmlNS::fo, "font-family")) { const QString fontFamily = styleStack.property(KoXmlNS::fo, "font-family"); QFont font = doc->defaultFont(); font.setFamily(fontFamily); doc->setDefaultFont(font); } } // 4. set the size if (labelElement.hasAttributeNS(KoXmlNS::svg, "width") && labelElement.hasAttributeNS(KoXmlNS::svg, "height")) { const qreal width = KoUnit::parseValue(labelElement.attributeNS(KoXmlNS::svg, "width")); const qreal height = KoUnit::parseValue(labelElement.attributeNS(KoXmlNS::svg, "height")); label->setSize(QSizeF(width, height)); } else { QSizeF size = shape->size(); QRect r = QFontMetrics(doc->defaultFont()).boundingRect( labelData->shapeMargins().left, labelData->shapeMargins().top, qMax(CM_TO_POINT(5), qreal(size.width() - pos.x() * 2.0 - labelData->shapeMargins().right)), qMax(CM_TO_POINT(0.6), qreal(size.height() - labelData->shapeMargins().bottom)), Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, doc->toPlainText()); label->setSize(r.size()); } return true; }