//段落标号、编号 void MyChild::setStyle(int style) { QTextCursor cursor = this->textCursor(); if (style != 0) { QTextListFormat::Style stylename = QTextListFormat::ListDisc; switch (style) { default: case 1: stylename = QTextListFormat::ListDisc; break; case 2: stylename = QTextListFormat::ListCircle; break; case 3: stylename = QTextListFormat::ListSquare; break; case 4: stylename = QTextListFormat::ListDecimal; break; case 5: stylename = QTextListFormat::ListLowerAlpha; break; case 6: stylename = QTextListFormat::ListUpperAlpha; break; case 7: stylename = QTextListFormat::ListLowerRoman; break; case 8: stylename = QTextListFormat::ListUpperRoman; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(stylename); cursor.createList(listFmt); cursor.endEditBlock(); } else { QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void KNoteEdit::textList() { QTextCursor c = textCursor(); c.beginEditBlock(); if ( m_textList->isChecked() ) { QTextListFormat lf; QTextBlockFormat bf = c.blockFormat(); lf.setIndent( bf.indent() + 1 ); bf.setIndent( 0 ); lf.setStyle( QTextListFormat::ListDisc ); c.setBlockFormat( bf ); c.createList( lf ); } else { QTextBlockFormat bf; bf.setObjectIndex( -1 ); c.setBlockFormat( bf ); } c.endEditBlock(); }
QTextList *tryReadList(QTextList *list, const QString &line) { QTextList *listOut = list; QRegularExpression exp("^( *)(\\d+\\.|\\*) (.*)$"); QRegularExpressionMatch match = exp.match(line); if (match.hasMatch()) { const int indent = match.captured(1).size() / 2 + 1; const QString contents = match.captured(3); const bool ordered = match.captured(2) != "*"; QTextListFormat fmt; fmt.setStyle(ordered ? QTextListFormat::ListDecimal : QTextListFormat::ListDisc); fmt.setIndent(indent); if (!listOut || fmt != listOut->format()) { listOut = cursor.insertList(fmt); } else { cursor.insertBlock(); } readInlineText(contents); listOut->add(cursor.block()); return listOut; } else { return 0; } }
void GraphicTextDialog::textStyle(int styleIndex) { QTextCursor cursor = textEdit->textCursor(); if(styleIndex != 0) { QTextListFormat::Style style = QTextListFormat::ListDisc; switch (styleIndex) { default: case 1: style = QTextListFormat::ListDisc; break; case 2: style = QTextListFormat::ListCircle; break; case 3: style = QTextListFormat::ListSquare; break; case 4: style = QTextListFormat::ListDecimal; break; case 5: style = QTextListFormat::ListLowerAlpha; break; case 6: style = QTextListFormat::ListUpperAlpha; break; } cursor.beginEditBlock(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; if(cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(style); cursor.createList(listFmt); cursor.endEditBlock(); } else { // #### QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void TextEditWidget::sl_IncreaseListIndent_Triggered() { QTextCursor cursor = this->textField->textCursor(); QTextList *textList = cursor.currentList(); if (!textList) {return;} QTextListFormat format = textList->format(); format.setIndent(format.indent() + 1); textList->setFormat(format); }
bool Converter::convertList( QTextCursor *cursor, const QDomElement &element ) { const QString styleName = element.attribute( QStringLiteral("style-name") ); const ListFormatProperty property = mStyleInformation->listProperty( styleName ); QTextListFormat format; if ( cursor->currentList() ) { // we are in a nested list format = cursor->currentList()->format(); format.setIndent( format.indent() + 1 ); } property.apply( &format, 0 ); QTextList *list = cursor->insertList( format ); QDomElement itemChild = element.firstChildElement(); int loop = 0; while ( !itemChild.isNull() ) { if ( itemChild.tagName() == QLatin1String( "list-item" ) ) { loop++; QDomElement childElement = itemChild.firstChildElement(); while ( !childElement.isNull() ) { QTextBlock prevBlock; if ( childElement.tagName() == QLatin1String( "p" ) ) { if ( loop > 1 ) cursor->insertBlock(); prevBlock = cursor->block(); if ( !convertParagraph( cursor, childElement, QTextBlockFormat(), true ) ) return false; } else if ( childElement.tagName() == QLatin1String( "list" ) ) { prevBlock = cursor->block(); if ( !convertList( cursor, childElement ) ) return false; } if( prevBlock.isValid() ) list->add( prevBlock ); childElement = childElement.nextSiblingElement(); } } itemChild = itemChild.nextSiblingElement(); } return true; }
void MainWindow::ShowSort(int index) { QTextCursor cursor=ui->sans_text->textCursor(); if(index!=0) { QTextListFormat::Style style=QTextListFormat::ListDisc; switch(index) //设置style属性值 { default: case 1: style=QTextListFormat::ListDisc; break; case 2: style=QTextListFormat::ListCircle; break; case 3: style=QTextListFormat::ListSquare; break; case 4: style=QTextListFormat::ListDecimal; break; case 5: style=QTextListFormat::ListLowerAlpha; break; case 6: style=QTextListFormat::ListUpperAlpha; break; case 7: style=QTextListFormat::ListLowerRoman; break; case 8: style=QTextListFormat::ListUpperRoman; break; } cursor.beginEditBlock(); //设置缩进值 QTextBlockFormat blockFmt=cursor.blockFormat(); QTextListFormat listFmt; if(cursor.currentList()) { listFmt= cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent()+1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(style); cursor.createList(listFmt); cursor.endEditBlock(); } else { QTextBlockFormat bfmt; bfmt.setObjectIndex(-1); cursor.mergeBlockFormat(bfmt); } }
void TextTools::orderedListClicked() { QTextCharFormat format = cursor()->charFormat(); QTextListFormat listFormat; QTextList* list = cursor()->currentList(); if (list) { listFormat = list->format(); int indent = listFormat.indent(); listFormat.setIndent(indent + 1); } listFormat.setStyle(QTextListFormat::ListDecimal); cursor()->insertList(listFormat); cursor()->setCharFormat(format); updateText(); }
void MainWindow::insertList() { QTextCursor cursor = editor->textCursor(); cursor.beginEditBlock(); QTextList *list = cursor.currentList(); QTextListFormat listFormat; if (list) listFormat = list->format(); listFormat.setStyle(QTextListFormat::ListDisc); listFormat.setIndent(listFormat.indent() + 1); cursor.insertList(listFormat); cursor.endEditBlock(); }
/*! * \internal */ bool QwwRichTextEdit::event(QEvent *e){ if(e->type()!=QEvent::KeyPress) return QTextEdit::event(e); QKeyEvent *ke = (QKeyEvent*)e; currentList = textCursor().currentList(); if(currentList){ if(ke->key()==Qt::Key_Tab || ke->key() ==Qt::Key_Backtab){ QTextCursor cur = textCursor(); QTextListFormat listFormat = currentList->format(); listFormat.setIndent(currentList->format().indent()+ (ke->key()== Qt::Key_Tab ? 1 : -1)); // listFormat.setStyle(QTextListFormat::ListDisc); currentList = cur.createList(listFormat); return true; } } return QTextEdit::event(e); }
void MainWindow::toggleList(QTextListFormat::Style style) { QTextCursor cursor = ui->textNote->textCursor(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextListFormat listFmt; bool list = (cursor.currentList() != 0); // change style if list exists and is a different style if(list && cursor.currentList()->format().style() != style) { listFmt.setStyle(style); cursor.currentList()->setFormat(listFmt); } // remove list if exists and matches style else if(list&& cursor.currentList()->format().style() == style) { cursor.currentList()->removeItem(0); blockFmt = ui->textNote->textCursor().blockFormat(); cursor = ui->textNote->textCursor(); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); // create list if not exists } else { cursor.beginEditBlock(); if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } else { listFmt.setIndent(blockFmt.indent() + 1); blockFmt.setIndent(0); cursor.setBlockFormat(blockFmt); } listFmt.setStyle(style); cursor.createList(listFmt); cursor.endEditBlock(); } updateMenus(); }
void QGithubMarkdown::read(const QByteArray &markdown, QTextDocument *target) { doc = target; doc->clear(); cursor = QTextCursor(doc); cursor.beginEditBlock(); const QList<Token> tokens = tokenize(clean(QString::fromUtf8(markdown))); const QList<Paragraph> paragraphs = paragraphize(tokens); const auto paralists = listize(paragraphs); //std::for_each(paragraphs.begin(), paragraphs.end(), [](const Paragraph &item){qDebug() << item;}); bool firstBlock = true; for (const auto paralist : paralists) { auto insertTokens = [&](const QList<Token> &tokens, const QTextCharFormat &format, const bool isCode) { QTextCharFormat fmt(format); QTextCharFormat codeFmt(format); codeFmt.setFontFamily("Monospace"); QListIterator<Token> iterator(tokens); while (iterator.hasNext()) { const Token token = iterator.next(); if (isCode) { cursor.insertText(token.source); } else { if (token.type == Token::Bold) { if (fmt.fontWeight() == QFont::Bold) { fmt.setFontWeight(QFont::Normal); } else { fmt.setFontWeight(QFont::Bold); } } else if (token.type == Token::Italic) { fmt.setFontItalic(!fmt.fontItalic()); } else if (token.type == Token::InlineCodeDelimiter) { while (iterator.hasNext()) { const Token next = iterator.next(); if (next.type == Token::InlineCodeDelimiter) { break; } else { cursor.insertText(token.source, codeFmt); } } } else if (token.type == Token::Character) { cursor.insertText(token.content.toChar(), fmt); } else { cursor.insertText(token.source, fmt); } } } }; if (paralist.second.indent == -1) { const Paragraph paragraph = paralist.first; QTextCharFormat charFmt; QTextBlockFormat blockFmt; blockFmt.setBottomMargin(5.0f); if (Paragraph::FirstHeading <= paragraph.type && paragraph.type <= Paragraph::LastHeading) { charFmt.setFontPointSize(sizeMap[paragraph.type]); } else if (paragraph.type == Paragraph::Quote) { blockFmt.setIndent(1); } else if (paragraph.type == Paragraph::Code) { blockFmt.setNonBreakableLines(true); charFmt.setFontFamily("Monospace"); } if (!firstBlock) { cursor.insertBlock(); } else { firstBlock = false; } cursor.setBlockFormat(blockFmt); cursor.block().setUserState(paragraph.type); insertTokens(paragraph.tokens, charFmt, paragraph.type == Paragraph::Code); } else { const List list = paralist.second; qDebug() << "##########################" << list.indent << list.ordered; std::for_each(list.paragraphs.begin(), list.paragraphs.end(), [](const Paragraph &item){qDebug() << item;}); cursor.setBlockFormat(QTextBlockFormat()); cursor.setBlockCharFormat(QTextCharFormat()); QTextListFormat listFormat; listFormat.setStyle(list.ordered ? QTextListFormat::ListDecimal : QTextListFormat::ListDisc); listFormat.setIndent(list.indent); QTextList *l = cursor.insertList(listFormat); qDebug() << "inserting list" << list.indent; bool firstBlock = true; for (const Paragraph ¶graph : list.paragraphs) { if (firstBlock) { firstBlock = false; } else { cursor.insertBlock(); qDebug() << "inserting block"; } insertTokens(paragraph.tokens, QTextCharFormat(), false); qDebug() << l->count(); l->add(cursor.block()); qDebug() << l->count(); qDebug() << "inserting characters"; } } } cursor.endEditBlock(); qDebug() << doc->toHtml(); }
QTextHtmlImporter::ProcessNodeResult QTextHtmlImporter::processSpecialNodes() { switch (currentNode->id) { case Html_body: if (currentNode->charFormat.background().style() != Qt::NoBrush) { QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); fmt.setBackground(currentNode->charFormat.background()); doc->rootFrame()->setFrameFormat(fmt); const_cast<QTextHtmlParserNode *>(currentNode)->charFormat.clearProperty(QTextFormat::BackgroundBrush); } break; case Html_ol: case Html_ul: { QTextListFormat::Style style = currentNode->listStyle; if (currentNode->id == Html_ul && !currentNode->hasOwnListStyle && currentNode->parent) { const QTextHtmlParserNode *n = &at(currentNode->parent); while (n) { if (n->id == Html_ul) { style = nextListStyle(currentNode->listStyle); } if (n->parent) n = &at(n->parent); else n = 0; } } QTextListFormat listFmt; listFmt.setStyle(style); ++indent; if (currentNode->hasCssListIndent) listFmt.setIndent(currentNode->cssListIndent); else listFmt.setIndent(indent); List l; l.format = listFmt; l.listNode = currentNodeIdx; lists.append(l); compressNextWhitespace = true; // broken html: <ul>Text here<li>Foo const QString simpl = currentNode->text.simplified(); if (simpl.isEmpty() || simpl.at(0).isSpace()) return ContinueWithNextNode; break; } case Html_table: { Table t = scanTable(currentNodeIdx); tables.append(t); hasBlock = false; return ContinueWithNextNode; } case Html_tr: return ContinueWithNextNode; case Html_img: { QTextImageFormat fmt; fmt.setName(currentNode->imageName); fmt.merge(currentNode->charFormat); if (currentNode->imageWidth >= 0) fmt.setWidth(currentNode->imageWidth); if (currentNode->imageHeight >= 0) fmt.setHeight(currentNode->imageHeight); cursor.insertImage(fmt, QTextFrameFormat::Position(currentNode->cssFloat)); cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); cursor.mergeCharFormat(currentNode->charFormat); cursor.movePosition(QTextCursor::Right); hasBlock = false; return ContinueWithNextNode; } case Html_hr: { QTextBlockFormat blockFormat = currentNode->blockFormat; blockFormat.setTopMargin(topMargin(currentNodeIdx)); blockFormat.setBottomMargin(bottomMargin(currentNodeIdx)); blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, currentNode->width); if (hasBlock && importMode == ImportToDocument) cursor.mergeBlockFormat(blockFormat); else appendBlock(blockFormat); hasBlock = false; return ContinueWithNextNode; } default: break; } return ContinueWithCurrentNode; }
void Format::parseFrame( QTextCursor &cursor, const QDomElement &element ) { QTextBlock extraBlock; bool hasExtraBlock = false; QDomNode n; for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) { QDomElement e = n.toElement(); if ( e.tagName() == "block" ) { if ( e.hasAttribute( "liststyle" ) ) { cursor.setCharFormat( TextFormats::normalCharFormat() ); QTextListFormat f; f.setIndent( e.attribute( "listindent" ).toInt() ); if ( e.attribute( "liststyle" ) == "decimal" ) { f.setStyle( QTextListFormat::ListDecimal ); } else { f.setStyle( QTextListFormat::ListDisc ); } cursor.mergeBlockFormat( TextFormats::normalBlockFormat() ); cursor.insertList( f ); } else if ( n != element.firstChild() ) { QTextBlockFormat f; if ( e.hasAttribute( "blockindent" ) ) { f.setIndent( e.attribute( "blockindent" ).toInt() ); } else { f.setIndent( 0 ); } if ( hasExtraBlock ) { QTextCursor c( extraBlock ); c.setBlockFormat( f ); hasExtraBlock = false; } else { cursor.insertBlock( f ); } } if ( e.hasAttribute( "lastmodified" ) ) { QString str = e.attribute( "lastmodified" ); QDateTime dt = QDateTime::fromString( str, Qt::ISODate ); TextFormats::setLastModified( cursor, dt ); } parseBlock( cursor, e ); if ( e.hasAttribute( "titlestyle" ) ) { if ( e.attribute( "titlestyle" ) == "title" ) { cursor.mergeBlockFormat( TextFormats::titleBlockFormat() ); } else if ( e.attribute( "titlestyle" ) == "subtitle" ) { cursor.mergeBlockFormat( TextFormats::subTitleBlockFormat() ); } } else { cursor.mergeBlockFormat( TextFormats::normalBlockFormat() ); } } else if ( e.tagName() == "frame" ) { QTextFrame *parentFrame = cursor.currentFrame(); QTextFrame *frame = cursor.insertFrame( TextFormats::codeFrameFormat() ); parseFrame( cursor, e ); if ( e.attribute( "type" ) == "code" ) { TextFormats::setCodeFrameFormats( frame ); } cursor = parentFrame->lastCursorPosition(); extraBlock = cursor.block(); hasExtraBlock = true; } } }