Пример #1
0
	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;
		}
	}
Пример #2
0
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();
}
Пример #3
0
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();
}
Пример #4
0
void MainWindow::insertList()
{
    QTextCursor cursor = editor->textCursor();
    cursor.beginEditBlock();

    QTextList *list = cursor.currentList();
//! [0]
    listFormat = QTextListFormat()
    if list:
        listFormat = list.format()
        listFormat.setIndent(listFormat.indent() + 1)

    listFormat.setStyle(QTextListFormat.ListDisc)
    cursor.insertList(listFormat)
//! [0]

    cursor.endEditBlock();
}
Пример #5
0
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();
}
Пример #6
0
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 &paragraph : 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();
}
Пример #7
0
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;
    }
  }
}