Add manual test for QTextCursor::insert*

Visualize what's going on with tst_QTextCursor::insertMarkdown() at least.
But this could be expanded for other purposes.

It's also interesting to test drag-and-drop with this. And you can save
the result to any supported text format.

Change-Id: I363c32ff9b1bd7c53c562b08c58de95a69be0aa9
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Shawn Rutledge 2023-03-13 08:20:59 +01:00
parent 185627557e
commit 63eaa3a989
5 changed files with 476 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
qt_internal_add_manual_test(qtextcursorinsert
GUI
SOURCES
main.cpp
widget.cpp widget.h widget.ui
LIBRARIES
Qt::Gui
Qt::Widgets
ENABLE_AUTOGEN_TOOLS
uic
)

View File

@ -0,0 +1,14 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

View File

@ -0,0 +1,188 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "widget.h"
#include "./ui_widget.h"
#include <QBuffer>
#include <QShortcut>
#include <QTextBlock>
#include <QTextDocumentWriter>
#include <QTextList>
using namespace Qt::StringLiterals;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
m_texts.insert(u"0-numbered html list"_s, u"<ol start=\"0\">\n<li>eggs</li>\n<li>maple syrup</li>\n</ol>"_s);
m_texts.insert(u"0-numbered markdown list"_s, u"0) eggs\n1) maple syrup\n"_s);
m_texts.insert(u"lorem ipsum markdown"_s,
u"Lorem ipsum dolor sit amet quod scimus quomodo legere et scribere Markdown, non solum applicationes interrete."_s);
m_texts.insert(u"markdown checkboxes"_s,
u"- [ ] kürbis-kernöl\n- [ ] mon cheri (große schachtel)\n- [ ] bergkäse\n- [ ] mannerwaffln\n"_s);
m_texts.insert(u"markdown checkboxes (3 hidden list styles)"_s,
u"- [ ] kürbis-kernöl\n- [ ] mon cheri (große schachtel)\n+ [ ] bergkäse\n* [ ] mannerwaffln\n"_s);
m_texts.insert(u"numbered html list"_s, u"<ol>\n<li>eggs</li>\n<li>maple syrup</li>\n</ol>"_s);
m_texts.insert(u"numbered markdown list"_s, u"1. bread\n1. milk\n"_s);
for (auto it = m_texts.constBegin(); it != m_texts.constEnd(); ++it) {
ui->richTextCB->addItem(it.key(), it.value());
ui->plainTextCB->addItem(it.key(), it.value());
}
ui->richTextCB->setCurrentIndex(m_texts.count() - 1);
ui->plainTextCB->setCurrentIndex(1);
m_fileDialog.setWindowTitle(tr("Save rich text"));
m_fileDialog.setAcceptMode(QFileDialog::AcceptSave);
m_fileDialog.setMimeTypeFilters({"text/markdown", "text/html", "text/plain",
"application/vnd.oasis.opendocument.text"});
connect(&m_fileDialog, &QFileDialog::fileSelected, this, &Widget::onSave);
connect(new QShortcut(QKeySequence::Save, this), &QShortcut::activated, [this]() { m_fileDialog.open(); });
connect(new QShortcut(QKeySequence::Quit, this), &QShortcut::activated, [this]() { qApp->quit(); });
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_insertMarkdownButton_clicked()
{
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->moveToBeginningRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::StartOfBlock);
if (ui->moveToEndRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::EndOfBlock);
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
ui->textEdit->textCursor().insertMarkdown(ui->plainTextEdit->toPlainText());
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
}
void Widget::on_insertHtmlButton_clicked()
{
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->moveToBeginningRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::StartOfBlock);
if (ui->moveToEndRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::EndOfBlock);
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
ui->textEdit->insertHtml(ui->plainTextEdit->toPlainText());
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
}
void Widget::on_insertPlainButton_clicked()
{
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->moveToBeginningRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::StartOfBlock);
if (ui->moveToEndRB->isChecked())
ui->textEdit->moveCursor(QTextCursor::EndOfBlock);
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
ui->textEdit->insertPlainText(ui->plainTextEdit->toPlainText());
if (ui->newBlockBeforeCB->isChecked()) {
if (ui->defaultBlockFormatCB->isChecked())
ui->textEdit->textCursor().insertBlock(QTextBlockFormat());
else
ui->textEdit->textCursor().insertBlock();
}
}
void Widget::on_plainTextCB_activated(int index)
{
if (index < m_texts.size()) {
auto it = m_texts.constBegin();
std::advance(it, index);
ui->plainTextEdit->setPlainText(it.value());
}
}
void Widget::on_richTextCB_activated(int index)
{
if (index < m_texts.size()) {
auto it = m_texts.constBegin();
std::advance(it, index);
if (it.key().contains(u"markdown", Qt::CaseInsensitive))
ui->textEdit->setMarkdown(it.value());
else if (it.key().contains(u"html", Qt::CaseInsensitive))
ui->textEdit->setHtml(it.value());
else
ui->textEdit->setPlainText(it.value());
}
}
void Widget::on_textEdit_cursorPositionChanged()
{
QTextBlock block = ui->textEdit->textCursor().block();
ui->blockDesc->setText(u"%1 of %2"_s
.arg(block.blockNumber())
.arg(ui->textEdit->document()->blockCount()));
QTextList *list = block.textList();
if (list) {
QTextBlock first = list->item(0);
ui->listDesc->setText(u"index %1: \"%2\" is in list\nwith style %3 and %4 items starting with %5: %6 %7"_s
.arg(list->itemNumber(block))
.arg(block.text())
.arg(list->format().style())
.arg(list->count())
.arg(list->format().start())
.arg(list->format().style() > QTextListFormat::ListDecimal ? u""_s : list->itemText(first))
.arg(first.text()) );
} else {
ui->listDesc->setText(u"not in a list"_s);
}
}
void Widget::on_saveButton_clicked()
{
m_fileDialog.open();
}
void Widget::onSave(const QString &file)
{
QFile f(file);
if (f.open(QFile::WriteOnly)) {
if (m_fileDialog.selectedMimeTypeFilter() == u"text/markdown")
f.write(ui->textEdit->toMarkdown().toUtf8());
else if (m_fileDialog.selectedMimeTypeFilter() == u"text/html")
f.write(ui->textEdit->toHtml().toUtf8());
else if (m_fileDialog.selectedMimeTypeFilter() == u"text/plain")
f.write(ui->textEdit->toPlainText().toUtf8());
else if (m_fileDialog.selectedMimeTypeFilter() == u"application/vnd.oasis.opendocument.text") {
QBuffer buffer;
QTextDocumentWriter writer(&buffer, "ODF");
writer.write(ui->textEdit->document());
buffer.close();
f.write(buffer.data());
}
f.close();
}
}

View File

@ -0,0 +1,38 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef WIDGET_H
#define WIDGET_H
#include <QFileDialog>
#include <QMap>
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_insertMarkdownButton_clicked();
void on_insertHtmlButton_clicked();
void on_insertPlainButton_clicked();
void on_plainTextCB_activated(int index);
void on_richTextCB_activated(int index);
void on_textEdit_cursorPositionChanged();
void on_saveButton_clicked();
void onSave(const QString &file);
private:
Ui::Widget *ui;
QMap<QString, QString> m_texts;
QFileDialog m_fileDialog;
};
#endif // WIDGET_H

View File

@ -0,0 +1,222 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1200</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Try inserting (or dragging) text on right into text on left</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTextEdit" name="textEdit">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;meta charset=&quot;utf-8&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
hr { height: 1px; border-width: 0; }
li.unchecked::marker { content: &quot;\2610&quot;; }
li.checked::marker { content: &quot;\2612&quot;; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;ol style=&quot;margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;&quot;&gt;
&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;bread&lt;/li&gt;
&lt;li style=&quot; margin-top:0px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;milk&lt;/li&gt;&lt;/ol&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="plainText">
<string>0) eggs
1) maple syrup</string>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QFormLayout" name="formLayout">
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>List</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="listDesc">
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="richTextCB"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Change to</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Block</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="blockDesc">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="plainTextCB"/>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Before</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="4" column="0">
<widget class="QCheckBox" name="newBlockBeforeCB">
<property name="text">
<string>new block</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QRadioButton" name="moveToEndRB">
<property name="text">
<string>move to end of block</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="moveToBeginningRB">
<property name="text">
<string>move to beginning of block</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>nothing</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="defaultBlockFormatCB">
<property name="text">
<string>default block format</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Insert</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="insertMarkdownButton">
<property name="text">
<string>Markdown</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="insertHtmlButton">
<property name="text">
<string>HTML</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="insertPlainButton">
<property name="text">
<string>Plain</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>After</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QCheckBox" name="newBlockAfterCB">
<property name="text">
<string>new block</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>