📄 qtextdocumentfragment.cpp
字号:
\fn QTextDocumentFragment &QTextDocumentFragment::operator=(const QTextDocumentFragment &other) Assigns the \a other fragment to this fragment.*/QTextDocumentFragment &QTextDocumentFragment::operator=(const QTextDocumentFragment &rhs){ QTextDocumentFragmentPrivate *x = rhs.d; if (x) x->ref.ref(); x = qAtomicSetPtr(&d, x); if (x && !x->ref.deref()) delete x; return *this;}/*! Destroys the document fragment.*/QTextDocumentFragment::~QTextDocumentFragment(){ if (d && !d->ref.deref()) delete d;}/*! Returns true if the fragment is empty; otherwise returns false.*/bool QTextDocumentFragment::isEmpty() const{ return !d || !d->doc || d->doc->docHandle()->length() <= 1;}/*! Returns the document fragment's text as plain text (i.e. with no formatting information). \sa toHtml()*/QString QTextDocumentFragment::toPlainText() const{ if (!d) return QString(); QString result = d->doc->toPlainText(); if (d->containsCompleteDocument && !result.isEmpty() && result.at(0) == QLatin1Char('\n')) result.remove(0, 1); return result;}/*! Returns the contents of the document fragment as HTML. \sa toPlainText()*/QString QTextDocumentFragment::toHtml() const{ if (!d) return QString(); QTextHtmlExporter exporter(d->doc); if (!d->containsCompleteDocument) exporter.setFragmentMarkers(true); return exporter.toHtml(QByteArray());}/*! Returns a document fragment that contains the given \a plainText. When inserting such a fragment into a QTextDocument the current char format of the QTextCursor used for insertion is used as format for the text.*/QTextDocumentFragment QTextDocumentFragment::fromPlainText(const QString &plainText){ QTextDocumentFragment res; res.d = new QTextDocumentFragmentPrivate; res.d->importedFromPlainText = true; QTextCursor cursor(res.d->doc); cursor.insertText(plainText); return res;}QTextHtmlImporter::QTextHtmlImporter(QTextDocument *_doc, const QString &_html) : indent(0), setNamedAnchorInNextOutput(false), doc(_doc), containsCompleteDoc(false){ cursor = QTextCursor(doc); QString html = _html; const int startFragmentPos = html.indexOf(QLatin1String("<!--StartFragment-->")); if (startFragmentPos != -1) { const int endFragmentPos = html.indexOf(QLatin1String("<!--EndFragment-->")); if (startFragmentPos < endFragmentPos) html = html.mid(startFragmentPos, endFragmentPos - startFragmentPos); else html = html.mid(startFragmentPos); html.prepend(QLatin1String("<meta name=\"qrichtext\" content=\"1\" />")); } parse(html);// dumpHtml();}static QTextListFormat::Style nextListStyle(QTextListFormat::Style style){ if (style == QTextListFormat::ListDisc) return QTextListFormat::ListCircle; else if (style == QTextListFormat::ListCircle) return QTextListFormat::ListSquare; return style;}void QTextHtmlImporter::import(){ cursor.beginEditBlock(); bool hasBlock = true; bool forceBlockMerging = false; for (int i = 0; i < count(); ++i) { const QTextHtmlParserNode *node = &at(i); /* * process each node in three stages: * 1) check if the hierarchy changed and we therefore passed the * equivalent of a closing tag -> we may need to finish off * some structures like tables * * 2) check if the current node is a special node like a * <table>, <ul> or <img> tag that requires special processing * * 3) if the node should result in a QTextBlock create one and * finally insert text that may be attached to the node */ /* emit 'closing' table blocks or adjust current indent level * if we * 1) are beyond the first node * 2) the current node not being a child of the previous node * means there was a tag closing in the input html */ if (i > 0 && (node->parent != i - 1)) { const bool blockTagClosed = closeTag(i); if (hasBlock && blockTagClosed) hasBlock = false; // make sure there's a block for 'Blah' after <ul><li>foo</ul>Blah if (blockTagClosed && !hasBlock && !node->isBlock && !node->text.isEmpty() && node->displayMode != QTextHtmlElement::DisplayNone) { QTextBlockFormat block = node->blockFormat(); block.setIndent(indent); appendBlock(block, node->charFormat()); hasBlock = true; } } if (node->displayMode == QTextHtmlElement::DisplayNone) { if (node->id == Html_title) doc->setMetaInformation(QTextDocument::DocumentTitle, node->text); // ignore explicitly 'invisible' elements continue; } else if (node->id == Html_body) { containsCompleteDoc = true; if (node->bgColor.isValid()) { QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); fmt.setBackground(node->bgColor); doc->rootFrame()->setFrameFormat(fmt); const_cast<QTextHtmlParserNode *>(node)->bgColor = QColor(); } } else if (node->isListStart) { QTextListFormat::Style style = node->listStyle; if (node->id == Html_ul && !node->hasOwnListStyle && node->parent) { const QTextHtmlParserNode *n = &at(node->parent); while (n) { if (n->id == Html_ul) { style = nextListStyle(node->listStyle); } if (n->parent) n = &at(n->parent); else n = 0; } } QTextListFormat listFmt; listFmt.setStyle(style); ++indent; if (node->hasCssListIndent) listFmt.setIndent(node->cssListIndent); else listFmt.setIndent(indent); List l; l.format = listFmt; lists.append(l); if (node->text.isEmpty()) continue; } else if (node->id == Html_table) { Table t = scanTable(i); tables.append(t); hasBlock = false; continue; } else if (node->id == Html_tr && !tables.isEmpty()) { continue; } else if (node->id == Html_img) { QTextImageFormat fmt; fmt.setName(node->imageName); QTextCharFormat nodeFmt = node->charFormat(); if (nodeFmt.hasProperty(QTextFormat::IsAnchor)) fmt.setAnchor(nodeFmt.isAnchor()); if (nodeFmt.hasProperty(QTextFormat::AnchorHref)) fmt.setAnchorHref(nodeFmt.anchorHref()); if (nodeFmt.hasProperty(QTextFormat::AnchorName)) fmt.setAnchorName(nodeFmt.anchorName()); if (node->imageWidth >= 0) fmt.setWidth(node->imageWidth); if (node->imageHeight >= 0) fmt.setHeight(node->imageHeight); QTextFrameFormat::Position f = QTextFrameFormat::Position(node->cssFloat); QTextFrameFormat ffmt; ffmt.setPosition(f); QTextObject *obj = doc->docHandle()->createObject(ffmt); fmt.setObjectIndex(obj->objectIndex()); cursor.insertImage(fmt); hasBlock = false; continue; } else if (node->id == Html_hr) { QTextBlockFormat blockFormat; blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, node->width); appendBlock(blockFormat); hasBlock = false; continue; } if (node->isBlock) { QTextBlockFormat block; QTextCharFormat charFmt; if (node->isTableCell && !tables.isEmpty()) { Table &t = tables.last(); if (!t.isTextFrame) { const QTextTableCell cell = t.currentCell.cell(); if (cell.isValid()) cursor.setPosition(cell.firstPosition()); } hasBlock = true; if (node->bgColor.isValid()) { charFmt.setBackground(QBrush(node->bgColor)); cursor.mergeBlockCharFormat(charFmt); } } if (hasBlock) { block = cursor.blockFormat(); charFmt = cursor.blockCharFormat(); } // collapse block.setTopMargin(qMax(block.topMargin(), (qreal)topMargin(i))); int bottomMargin = this->bottomMargin(i); // for list items we may want to collapse with the bottom margin of the // list. if (node->isListItem) { if (node->parent && at(node->parent).isListStart) { const int listId = node->parent; const QTextHtmlParserNode *list = &at(listId); if (list->children.last() == i /* == index of node */) bottomMargin = qMax(bottomMargin, this->bottomMargin(listId)); } } block.setBottomMargin(bottomMargin); block.setLeftMargin(leftMargin(i)); block.setRightMargin(rightMargin(i)); if (!node->isListItem && indent != 0 && (lists.isEmpty() || !hasBlock || !lists.last().list || lists.last().list->itemNumber(cursor.block()) == -1 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -