void QLabelPrivate::ensureTextLayouted() const { if (!textLayoutDirty) return; ensureTextPopulated(); if (control) { QTextDocument *doc = control->document(); QTextOption opt = doc->defaultTextOption(); opt.setAlignment(QFlag(this->align)); if (this->align & Qt::TextWordWrap) opt.setWrapMode(QTextOption::WordWrap); else opt.setWrapMode(QTextOption::ManualWrap); doc->setDefaultTextOption(opt); QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); fmt.setMargin(0); doc->rootFrame()->setFrameFormat(fmt); doc->setTextWidth(documentRect().width()); } textLayoutDirty = false; }
void CDiaryEdit::slotPrintPreview() { CDiaryEditLock lock(this); collectData(); QPrinter printer; QPrintDialog dialog(&printer, this); dialog.setWindowTitle(tr("Print Diary")); if (dialog.exec() != QDialog::Accepted) return; QTextDocument doc; QSizeF pageSize = printer.pageRect(QPrinter::DevicePixel).size(); doc.setPageSize(pageSize); draw(doc); if(checkAddMap->isChecked()) { QImage img; theMainWindow->getCanvas()->print(img, pageSize.toSize() - QSize(10,10)); doc.rootFrame()->lastCursorPosition().insertImage(img); } doc.print(&printer); textEdit->clear(); textEdit->document()->setTextWidth(textEdit->size().width() - 20); draw(*textEdit->document()); }
QTextFrame *QTextDocumentProto::rootFrame() const { QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject()); if (item) return item->rootFrame(); return 0; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 获取文档对象 QTextDocument *document = ui->textEdit->document(); // 获取根框架 QTextFrame *rootFrame = document->rootFrame(); // 创建框架格式 QTextFrameFormat format; // 边界颜色 format.setBorderBrush(Qt::red); // 边界宽度 format.setBorder(3); // 框架使用格式 rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; // 设置背景颜色 frameFormat.setBackground(Qt::lightGray); // 设置边距 frameFormat.setMargin(10); // 设置填衬 frameFormat.setPadding(5); frameFormat.setBorder(2); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式 // 获取光标 QTextCursor cursor = ui->textEdit->textCursor(); // 在光标处插入框架 cursor.insertFrame(frameFormat); //以下是5.2.2节内容 QAction *action_textFrame = new QAction(tr("框架"),this); connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame())); // 在工具栏添加动作 ui->mainToolBar->addAction(action_textFrame); QAction *action_textBlock = new QAction(tr("文本块"),this); connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock())); ui->mainToolBar->addAction(action_textBlock); QAction *action_font = new QAction(tr("字体"),this); // 设置动作可以被选中 action_font->setCheckable(true); connect(action_font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool))); ui->mainToolBar->addAction(action_font); }
// 遍历框架 void MainWindow::showTextFrame() { QTextDocument *document = ui->textEdit->document(); QTextFrame *frame = document->rootFrame(); // 建立QTextFrame类的迭代器 QTextFrame::iterator it; for (it = frame->begin(); !(it.atEnd()); ++it) { // 获取当前框架的指针 QTextFrame *childFrame = it.currentFrame(); // 获取当前文本块 QTextBlock childBlock = it.currentBlock(); if (childFrame) qDebug() << "frame"; else if (childBlock.isValid()) qDebug() << "block:" << childBlock.text(); } }
void CDiaryEdit::draw(QTextDocument& doc) { CDiaryEditLock lock(this); QFontMetrics fm(QFont(font().family(),10)); bool hasGeoCaches = false; int cnt; int w = doc.textWidth(); int pointSize = ((10 * (w - 2 * ROOT_FRAME_MARGIN)) / (CHAR_PER_LINE * fm.width("X"))); if(pointSize == 0) return; doc.setUndoRedoEnabled(false); QFont f = textEdit->font(); f.setPointSize(pointSize); textEdit->setFont(f); QTextCharFormat fmtCharHeading1; fmtCharHeading1.setFont(f); fmtCharHeading1.setFontWeight(QFont::Black); fmtCharHeading1.setFontPointSize(f.pointSize() + 8); QTextCharFormat fmtCharHeading2; fmtCharHeading2.setFont(f); fmtCharHeading2.setFontWeight(QFont::Black); fmtCharHeading2.setFontPointSize(f.pointSize() + 4); QTextCharFormat fmtCharStandard; fmtCharStandard.setFont(f); QTextCharFormat fmtCharHeader; fmtCharHeader.setFont(f); fmtCharHeader.setBackground(Qt::darkBlue); fmtCharHeader.setFontWeight(QFont::Bold); fmtCharHeader.setForeground(Qt::white); QTextBlockFormat fmtBlockStandard; fmtBlockStandard.setTopMargin(10); fmtBlockStandard.setBottomMargin(10); fmtBlockStandard.setAlignment(Qt::AlignJustify); QTextFrameFormat fmtFrameStandard; fmtFrameStandard.setTopMargin(5); fmtFrameStandard.setBottomMargin(5); fmtFrameStandard.setWidth(w - 2 * ROOT_FRAME_MARGIN); QTextFrameFormat fmtFrameRoot; fmtFrameRoot.setTopMargin(ROOT_FRAME_MARGIN); fmtFrameRoot.setBottomMargin(ROOT_FRAME_MARGIN); fmtFrameRoot.setLeftMargin(ROOT_FRAME_MARGIN); fmtFrameRoot.setRightMargin(ROOT_FRAME_MARGIN); QTextTableFormat fmtTableStandard; fmtTableStandard.setBorder(1); fmtTableStandard.setBorderBrush(Qt::black); fmtTableStandard.setCellPadding(4); fmtTableStandard.setCellSpacing(0); fmtTableStandard.setHeaderRowCount(1); fmtTableStandard.setTopMargin(10); fmtTableStandard.setBottomMargin(20); fmtTableStandard.setWidth(w - 2 * ROOT_FRAME_MARGIN); QVector<QTextLength> constraints; constraints << QTextLength(QTextLength::FixedLength, 32); constraints << QTextLength(QTextLength::VariableLength, 50); constraints << QTextLength(QTextLength::VariableLength, 100); fmtTableStandard.setColumnWidthConstraints(constraints); doc.rootFrame()->setFrameFormat(fmtFrameRoot); QTextCursor cursor = doc.rootFrame()->firstCursorPosition(); cursor.insertText(diary.getName(), fmtCharHeading1); cursor.setCharFormat(fmtCharStandard); cursor.setBlockFormat(fmtBlockStandard); diary.diaryFrame = cursor.insertFrame(fmtFrameStandard); { QTextCursor cursor1(diary.diaryFrame); cursor1.setCharFormat(fmtCharStandard); cursor1.setBlockFormat(fmtBlockStandard); if(diary.getComment().isEmpty()) { cursor1.insertText(tr("Add your own text here...")); } else { cursor1.insertHtml(diary.getComment()); } cursor.setPosition(cursor1.position()+1); } if(!diary.getWpts().isEmpty()) { QList<CWpt*>& wpts = diary.getWpts(); cursor.insertText(tr("Waypoints"),fmtCharHeading2); QTextTable * table = cursor.insertTable(wpts.count()+1, eMax, fmtTableStandard); diary.tblWpt = table; table->cellAt(0,eSym).setFormat(fmtCharHeader); table->cellAt(0,eInfo).setFormat(fmtCharHeader); table->cellAt(0,eComment).setFormat(fmtCharHeader); table->cellAt(0,eInfo).firstCursorPosition().insertText(tr("Info")); table->cellAt(0,eComment).firstCursorPosition().insertText(tr("Comment")); cnt = 1; qSort(wpts.begin(), wpts.end(), qSortWptLessTime); foreach(CWpt * wpt, wpts) { table->cellAt(cnt,eSym).firstCursorPosition().insertImage(wpt->getIcon().toImage().scaledToWidth(16, Qt::SmoothTransformation)); table->cellAt(cnt,eInfo).firstCursorPosition().insertText(wpt->getName() + "\n" + wpt->getInfo(), fmtCharStandard); QTextCursor c = table->cellAt(cnt,eComment).firstCursorPosition(); c.setCharFormat(fmtCharStandard); c.setBlockFormat(fmtBlockStandard); c.insertHtml(wpt->getComment()); if(wpt->isGeoCache()) { hasGeoCaches = true; } cnt++; }
XmlConsole::XmlConsole(Client* client, QWidget *parent) : QWidget(parent), m_ui(new Ui::XmlConsole), m_client(client), m_filter(0x1f) { m_ui->setupUi(this); m_client->addXmlStreamHandler(this); QPalette pal = palette(); pal.setColor(QPalette::Base, Qt::black); pal.setColor(QPalette::Text, Qt::white); m_ui->xmlBrowser->viewport()->setPalette(pal); QTextDocument *doc = m_ui->xmlBrowser->document(); doc->setDocumentLayout(new QPlainTextDocumentLayout(doc)); doc->clear(); QTextFrameFormat format = doc->rootFrame()->frameFormat(); format.setBackground(QColor(Qt::black)); format.setMargin(0); doc->rootFrame()->setFrameFormat(format); QMenu *menu = new QMenu(m_ui->filterButton); menu->setSeparatorsCollapsible(false); menu->addSeparator()->setText(tr("Filter")); QActionGroup *group = new QActionGroup(menu); QAction *disabled = group->addAction(menu->addAction(tr("Disabled"))); disabled->setCheckable(true); disabled->setData(Disabled); QAction *jid = group->addAction(menu->addAction(tr("By JID"))); jid->setCheckable(true); jid->setData(ByJid); QAction *xmlns = group->addAction(menu->addAction(tr("By namespace uri"))); xmlns->setCheckable(true); xmlns->setData(ByXmlns); QAction *attrb = group->addAction(menu->addAction(tr("By all attributes"))); attrb->setCheckable(true); attrb->setData(ByAllAttributes); disabled->setChecked(true); connect(group, SIGNAL(triggered(QAction*)), this, SLOT(onActionGroupTriggered(QAction*))); menu->addSeparator()->setText(tr("Visible stanzas")); group = new QActionGroup(menu); group->setExclusive(false); QAction *iq = group->addAction(menu->addAction(tr("Information query"))); iq->setCheckable(true); iq->setData(XmlNode::Iq); iq->setChecked(true); QAction *message = group->addAction(menu->addAction(tr("Message"))); message->setCheckable(true); message->setData(XmlNode::Message); message->setChecked(true); QAction *presence = group->addAction(menu->addAction(tr("Presence"))); presence->setCheckable(true); presence->setData(XmlNode::Presence); presence->setChecked(true); QAction *custom = group->addAction(menu->addAction(tr("Custom"))); custom->setCheckable(true); custom->setData(XmlNode::Custom); custom->setChecked(true); connect(group, SIGNAL(triggered(QAction*)), this, SLOT(onActionGroupTriggered(QAction*))); m_ui->filterButton->setMenu(menu); m_stackBracketsColor = QColor(0x666666); m_stackIncoming.bodyColor = QColor(0xbb66bb); m_stackIncoming.tagColor = QColor(0x006666); m_stackIncoming.attributeColor = QColor(0x009933); m_stackIncoming.paramColor = QColor(0xcc0000); m_stackOutgoing.bodyColor = QColor(0x999999); m_stackOutgoing.tagColor = QColor(0x22aa22); m_stackOutgoing.attributeColor = QColor(0xffff33); m_stackOutgoing.paramColor = QColor(0xdd8811); QAction *action = new QAction(tr("Close"),this); action->setSoftKeyRole(QAction::NegativeSoftKey); connect(action, SIGNAL(triggered()), SLOT(close())); addAction(action); }
void AbstractDiagram::Private::addLabel( LabelPaintCache* cache, const QModelIndex& index, const CartesianDiagramDataCompressor::CachePosition* position, const PositionPoints& points, const Position& autoPositionPositive, const Position& autoPositionNegative, const qreal value, qreal favoriteAngle /* = 0.0 */ ) { CartesianDiagramDataCompressor::AggregatedDataValueAttributes allAttrs( aggregatedAttrs( index, position ) ); QMap<QModelIndex, DataValueAttributes>::const_iterator it; for ( it = allAttrs.constBegin(); it != allAttrs.constEnd(); ++it ) { DataValueAttributes dva = it.value(); if ( !dva.isVisible() ) { continue; } const bool isPositive = ( value >= 0.0 ); RelativePosition relPos( dva.position( isPositive ) ); relPos.setReferencePoints( points ); if ( relPos.referencePosition().isUnknown() ) { relPos.setReferencePosition( isPositive ? autoPositionPositive : autoPositionNegative ); } // Rotate the label position (not the label itself) if the diagram is rotated so that the defaults still work if ( isTransposed() ) { KChartEnums::PositionValue posValue = relPos.referencePosition().value(); if ( posValue >= KChartEnums::PositionNorthWest && posValue <= KChartEnums::PositionWest ) { // rotate 90 degrees clockwise posValue = static_cast< KChartEnums::PositionValue >( posValue + 2 ); if ( posValue > KChartEnums::PositionWest ) { // wraparound posValue = static_cast< KChartEnums::PositionValue >( posValue - ( KChartEnums::PositionWest - KChartEnums::PositionNorthWest ) ); } relPos.setReferencePosition( Position( posValue ) ); } } const QPointF referencePoint = relPos.referencePoint(); if ( !diagram->coordinatePlane()->isVisiblePoint( referencePoint ) ) { continue; } const qreal fontHeight = cachedFontMetrics( dva.textAttributes(). calculatedFont( plane, KChartEnums::MeasureOrientationMinimum ), diagram )->height(); // Note: When printing data value texts and padding's Measure is using automatic reference area // detection, the font height is used as reference size for both horizontal and vertical // padding. QSizeF relativeMeasureSize( fontHeight, fontHeight ); if ( !dva.textAttributes().hasRotation() ) { TextAttributes ta = dva.textAttributes(); ta.setRotation( favoriteAngle ); dva.setTextAttributes( ta ); } // get the size of the label text using a subset of the information going into the final layout const QString text = formatDataValueText( dva, index, value ); QTextDocument doc; doc.setDocumentMargin( 0 ); if ( Qt::mightBeRichText( text ) ) { doc.setHtml( text ); } else { doc.setPlainText( text ); } const QFont calculatedFont( dva.textAttributes() .calculatedFont( plane, KChartEnums::MeasureOrientationMinimum ) ); doc.setDefaultFont( calculatedFont ); const QRectF plainRect = doc.documentLayout()->frameBoundingRect( doc.rootFrame() ); /** * A few hints on how the positioning of the text frame is done: * * Let's assume we have a bar chart, a text for a positive value * to be drawn, and "North" as attrs.positivePosition(). * * The reference point (pos) is then set to the top center point * of a bar. The offset now depends on the alignment: * * Top: text is centered horizontally to the bar, bottom of * text frame starts at top of bar * * Bottom: text is centered horizontally to the bar, top of * text frame starts at top of bar * * Center: text is centered horizontally to the bar, center * line of text frame is same as top of bar * * TopLeft: right edge of text frame is horizontal center of * bar, bottom of text frame is top of bar. * * ... * * Positive and negative value labels are treated equally, "North" * also refers to the top of a negative bar, and *not* to the bottom. * * * "NorthEast" likewise refers to the top right edge of the bar, * "NorthWest" to the top left edge of the bar, and so on. * * In other words, attrs.positivePosition() always refers to a * position of the *bar*, and relPos.alignment() always refers * to an alignment of the text frame relative to this position. */ QTransform transform; { // move to the general area where the label should be QPointF calcPoint = relPos.calculatedPoint( relativeMeasureSize ); transform.translate( calcPoint.x(), calcPoint.y() ); // align the text rect; find out by how many half-widths / half-heights to move. int dx = -1; if ( relPos.alignment() & Qt::AlignLeft ) { dx -= 1; } else if ( relPos.alignment() & Qt::AlignRight ) { dx += 1; } int dy = -1; if ( relPos.alignment() & Qt::AlignTop ) { dy -= 1; } else if ( relPos.alignment() & Qt::AlignBottom ) { dy += 1; } transform.translate( qreal( dx ) * plainRect.width() * 0.5, qreal( dy ) * plainRect.height() * 0.5 ); // rotate the text rect around its center transform.translate( plainRect.center().x(), plainRect.center().y() ); int rotation = dva.textAttributes().rotation(); if ( !isPositive && dva.mirrorNegativeValueTextRotation() ) { rotation *= -1; } transform.rotate( rotation ); transform.translate( -plainRect.center().x(), -plainRect.center().y() ); } QPainterPath labelArea; //labelArea.addPolygon( transform.mapToPolygon( plainRect.toRect() ) ); //labelArea.closeSubpath(); // Not doing that because QTransform has a special case for 180° that gives a different than // usual ordering of the points in the polygon returned by mapToPolygon( const QRect & ). // We expect a particular ordering in paintDataValueTextsAndMarkers() by using elementAt( 0 ), // and similar things might happen elsewhere. labelArea.addPolygon( transform.map( QPolygon( plainRect.toRect(), true ) ) ); // store the label geometry and auxiliary data cache->paintReplay.append( LabelPaintInfo( it.key(), dva, labelArea, referencePoint, value >= 0.0, text ) ); } }