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 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 MRichTextEdit::list(bool checked, QTextListFormat::Style style) { QTextCursor cursor = f_textedit->textCursor(); cursor.beginEditBlock(); if (!checked) { QTextBlockFormat obfmt = cursor.blockFormat(); QTextBlockFormat bfmt; bfmt.setIndent(obfmt.indent()); cursor.setBlockFormat(bfmt); } else { QTextListFormat listFmt; if (cursor.currentList()) { listFmt = cursor.currentList()->format(); } listFmt.setStyle(style); cursor.createList(listFmt); } cursor.endEditBlock(); }
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(); }
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 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 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); } }
/*! * \brief Changes the list attribute of the selected text to \a v */ void QwwRichTextEdit::setList(bool v){ QTextCursor cur = textCursor(); if(v){ QTextListFormat listFormat; listFormat.setStyle(QTextListFormat::ListDisc); currentList = cur.createList(listFormat); } else { cur.setBlockFormat(QTextBlockFormat()); // cur.movePosition(QTextCursor::NextBlock); // cur.insertBlock(QTextBlockFormat()); setTextCursor(cur); currentList = 0; } }
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(); }
void MainWindow::insertList() { QTextCursor cursor = editor->textCursor(); cursor.beginEditBlock(); //! [add a styled, ordered list] QTextListFormat listFormat; listFormat.setStyle(QTextListFormat::ListDecimal); listFormat.setNumberPrefix("("); listFormat.setNumberSuffix(")"); cursor.insertList(listFormat); //! [add a styled, ordered list] cursor.endEditBlock(); }
void TextEditWidget::sl_ListButton_Triggered(QAction* action) { if (!action) { WARNING("Null pointer recieved"); return; } QTextListFormat::Style style = (QTextListFormat::Style)action->data().toInt(); QTextCursor cursor = this->textField->textCursor(); QTextList *textList = cursor.currentList(); if (!textList) { WARNING("Wrong button state"); return; } QTextListFormat format = textList->format(); format.setStyle(style); textList->setFormat(format); }
void MainWindow::on_insetList_clicked() { QInputDialog dlg; QStringList items; items << tr("○..., ○..., ○...") << tr("●..., ●..., ●...") << tr("■..., ■..., ■...") << tr("1..., 2..., 3...") << tr("a..., b..., c...") << tr("A..., B..., C...") << tr("i..., ii..., iii...") << tr("I..., II..., III..."); bool ok; QString item = QInputDialog::getItem(this, tr("请选择列表样式"), tr("列表前缀:"), items, 0, false, &ok); if (!ok || item.isEmpty()) return; QTextEdit *pEdit = textEditor.editor; QTextCursor cursor = pEdit->textCursor(); QTextListFormat listFormat; if(item == tr("○..., ○..., ○...")) listFormat.setStyle(QTextListFormat::ListCircle); else if(item == tr("●..., ●..., ●...")) listFormat.setStyle(QTextListFormat::ListDisc); else if(item == tr("■..., ■..., ■...")) listFormat.setStyle(QTextListFormat::ListSquare); else if(item == tr("1..., 2..., 3...")) listFormat.setStyle(QTextListFormat::ListDecimal); else if(item == tr("a..., b..., c...")) listFormat.setStyle(QTextListFormat::ListLowerAlpha); else if(item == tr("A..., B..., C...")) listFormat.setStyle(QTextListFormat::ListUpperAlpha); else if(item == tr("i..., ii..., iii...")) listFormat.setStyle(QTextListFormat::ListLowerRoman); else listFormat.setStyle(QTextListFormat::ListUpperRoman); cursor.insertList(listFormat); pEdit->setFocus(); }
void TextEditWidget::sl_ListButton_Toggled(bool toggle) { QTextCursor cursor = this->textField->textCursor(); if (toggle) { if (cursor.currentList()) { WARNING("Wrong button state"); return; } QTextListFormat format; format.setStyle(QTextListFormat::ListDisc); cursor.createList(format); } else { QTextList *textList = cursor.currentList(); if (!cursor.currentList()) { WARNING("Wrong button state"); return; } QTextBlock block = cursor.block(); textList->remove(block); QTextBlockFormat format = block.blockFormat(); format.setIndent(0); cursor.setBlockFormat(format); } }
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; } } }