aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols
diff options
context:
space:
mode:
authorShawn Rutledge <[email protected]>2023-11-22 13:45:46 -0700
committerShawn Rutledge <[email protected]>2023-12-09 01:52:31 -0700
commit98d409a66e568467dd04a0ff5454d7ae11858a9c (patch)
tree47dda4b23976cc37d2e7e567f2d74c2589b46ad8 /examples/quickcontrols
parentde0efe8ae9f5a069f893f5c89163bda32300a0e7 (diff)
Get rid of DocumentHandler in the Text Editor example
...and fix a couple of minor issues that remained. Fixes: QTBUG-81022 Change-Id: I5fc1547fb09caef5a9e24d95c2ca7b8d78ee104a Reviewed-by: Axel Spoerl <[email protected]>
Diffstat (limited to 'examples/quickcontrols')
-rw-r--r--examples/quickcontrols/texteditor/CMakeLists.txt1
-rw-r--r--examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc31
-rw-r--r--examples/quickcontrols/texteditor/documenthandler.cpp348
-rw-r--r--examples/quickcontrols/texteditor/documenthandler.h121
-rw-r--r--examples/quickcontrols/texteditor/qml/+touch/texteditor.qml26
-rw-r--r--examples/quickcontrols/texteditor/qml/texteditor.qml40
-rw-r--r--examples/quickcontrols/texteditor/texteditor.cpp4
-rw-r--r--examples/quickcontrols/texteditor/texteditor.pro6
8 files changed, 22 insertions, 555 deletions
diff --git a/examples/quickcontrols/texteditor/CMakeLists.txt b/examples/quickcontrols/texteditor/CMakeLists.txt
index 2736881e68..7ce4433938 100644
--- a/examples/quickcontrols/texteditor/CMakeLists.txt
+++ b/examples/quickcontrols/texteditor/CMakeLists.txt
@@ -15,7 +15,6 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quickcontrols/texteditor")
find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick QuickControls2 OPTIONAL_COMPONENTS Widgets)
qt_add_executable(texteditorexample WIN32 MACOSX_BUNDLE
- documenthandler.cpp
texteditor.cpp
)
diff --git a/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc b/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc
index f0cd574f4d..8ee4f01155 100644
--- a/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc
+++ b/examples/quickcontrols/texteditor/doc/src/qtquickcontrols-texteditor.qdoc
@@ -66,36 +66,5 @@
\skipto /\bDialog\b/
\printuntil /^\s{4}\}$/
- \section1 C++ Backend
-
- Both user interfaces use the same C++ backend, which supports opening, formatting,
- and editing a document. The C++ class, \c DocumentHandler, extends QObject and is
- registered as a QML type under the namespace \c {io.qt.examples.texteditor 1.0}.
-
- The following snippets show how the type is registered under a namespace and later
- imported and instantiated by \e main.qml. For more information about registering C++
- classes as QML types, see \l {Defining QML Types from C++}.
-
- QML type registration:
-
- \code
- #include <QtQml/qqml.h>
- ...
- qmlRegisterType<DocumentHandler>("io.qt.examples.texteditor", 1, 0, "DocumentHandler");
- ...
- \endcode
-
- QML namespace import:
-
- \code
- import io.qt.examples.texteditor 1.0
- \endcode
-
- QML instance:
-
- \quotefromfile texteditor/qml/texteditor.qml
- \skipto DocumentHandler
- \printuntil /^\s{4}\}$/
-
\include examples-run.qdocinc
*/
diff --git a/examples/quickcontrols/texteditor/documenthandler.cpp b/examples/quickcontrols/texteditor/documenthandler.cpp
deleted file mode 100644
index 471ae59b75..0000000000
--- a/examples/quickcontrols/texteditor/documenthandler.cpp
+++ /dev/null
@@ -1,348 +0,0 @@
-// Copyright (C) 2017 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "documenthandler.h"
-
-#include <QFile>
-#include <QFileInfo>
-#include <QFileSelector>
-#include <QMimeDatabase>
-#include <QQmlFile>
-#include <QQmlFileSelector>
-#include <QQuickTextDocument>
-#include <QTextCharFormat>
-#include <QStringDecoder>
-#include <QTextDocument>
-#include <QDebug>
-
-DocumentHandler::DocumentHandler(QObject *parent)
- : QObject(parent)
- , m_document(nullptr)
- , m_cursorPosition(-1)
- , m_selectionStart(0)
- , m_selectionEnd(0)
-{
-}
-
-QQuickTextDocument *DocumentHandler::document() const
-{
- return m_document;
-}
-
-void DocumentHandler::setDocument(QQuickTextDocument *document)
-{
- if (document == m_document)
- return;
-
- if (m_document)
- disconnect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged);
- m_document = document;
- if (m_document)
- connect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged);
- emit documentChanged();
-}
-
-int DocumentHandler::cursorPosition() const
-{
- return m_cursorPosition;
-}
-
-void DocumentHandler::setCursorPosition(int position)
-{
- if (position == m_cursorPosition)
- return;
-
- m_cursorPosition = position;
- reset();
- emit cursorPositionChanged();
-}
-
-int DocumentHandler::selectionStart() const
-{
- return m_selectionStart;
-}
-
-void DocumentHandler::setSelectionStart(int position)
-{
- if (position == m_selectionStart)
- return;
-
- m_selectionStart = position;
- emit selectionStartChanged();
-}
-
-int DocumentHandler::selectionEnd() const
-{
- return m_selectionEnd;
-}
-
-void DocumentHandler::setSelectionEnd(int position)
-{
- if (position == m_selectionEnd)
- return;
-
- m_selectionEnd = position;
- emit selectionEndChanged();
-}
-
-QColor DocumentHandler::textColor() const
-{
- QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return QColor(Qt::black);
- QTextCharFormat format = cursor.charFormat();
- return format.foreground().color();
-}
-
-void DocumentHandler::setTextColor(const QColor &color)
-{
- QTextCharFormat format;
- format.setForeground(QBrush(color));
- mergeFormatOnWordOrSelection(format);
- emit textColorChanged();
-}
-
-Qt::Alignment DocumentHandler::alignment() const
-{
- QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return Qt::AlignLeft;
- return textCursor().blockFormat().alignment();
-}
-
-void DocumentHandler::setAlignment(Qt::Alignment alignment)
-{
- QTextBlockFormat format;
- format.setAlignment(alignment);
- QTextCursor cursor = textCursor();
- cursor.mergeBlockFormat(format);
- emit alignmentChanged();
-}
-
-QString DocumentHandler::fileName() const
-{
- const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl);
- const QString fileName = QFileInfo(filePath).fileName();
- if (fileName.isEmpty())
- return QStringLiteral("untitled.txt");
- return fileName;
-}
-
-QString DocumentHandler::fileType() const
-{
- return QFileInfo(fileName()).suffix();
-}
-
-QUrl DocumentHandler::fileUrl() const
-{
- return m_fileUrl;
-}
-
-void DocumentHandler::load(const QUrl &fileUrl)
-{
- if (fileUrl == m_fileUrl)
- return;
-
- QQmlEngine *engine = qmlEngine(this);
- if (!engine) {
- qWarning() << "load() called before DocumentHandler has QQmlEngine";
- return;
- }
-
- const QUrl path = engine->interceptUrl(fileUrl, QQmlAbstractUrlInterceptor::UrlString);
- const QString fileName = QQmlFile::urlToLocalFileOrQrc(path);
- if (QFile::exists(fileName)) {
- QMimeType mime = QMimeDatabase().mimeTypeForFile(fileName);
- QFile file(fileName);
- if (file.open(QFile::ReadOnly | QFile::Text)) {
- QByteArray data = file.readAll();
- if (QTextDocument *doc = textDocument()) {
- doc->setBaseUrl(path.adjusted(QUrl::RemoveFilename));
- doc->setModified(false);
- if (mime.inherits("text/markdown")) {
- emit loaded(QString::fromUtf8(data), Qt::MarkdownText);
- } else {
- auto encoding = QStringConverter::encodingForHtml(data);
- if (encoding) {
- QStringDecoder decoder(*encoding);
- emit loaded(decoder(data), Qt::AutoText);
- } else {
- // fall back to utf8
- emit loaded(QString::fromUtf8(data), Qt::AutoText);
- }
- }
- }
-
- reset();
- }
- }
-
- m_fileUrl = fileUrl;
- emit fileUrlChanged();
-}
-
-void DocumentHandler::saveAs(const QUrl &fileUrl)
-{
- QTextDocument *doc = textDocument();
- if (!doc)
- return;
-
- const QString filePath = fileUrl.toLocalFile();
- const bool isHtml = QFileInfo(filePath).suffix().contains(QLatin1String("htm"));
- QFile file(filePath);
- if (!file.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) {
- emit error(tr("Cannot save: ") + file.errorString());
- return;
- }
- file.write((isHtml ? doc->toHtml() : doc->toPlainText()).toUtf8());
- file.close();
-
- if (fileUrl == m_fileUrl)
- return;
-
- m_fileUrl = fileUrl;
- emit fileUrlChanged();
-}
-
-void DocumentHandler::reset()
-{
- emit alignmentChanged();
- emit textColorChanged();
- emit fontChanged();
-}
-
-QTextCursor DocumentHandler::textCursor() const
-{
- QTextDocument *doc = textDocument();
- if (!doc)
- return QTextCursor();
-
- QTextCursor cursor = QTextCursor(doc);
- if (m_selectionStart != m_selectionEnd) {
- cursor.setPosition(m_selectionStart);
- cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor);
- } else {
- cursor.setPosition(m_cursorPosition);
- }
- return cursor;
-}
-
-QTextDocument *DocumentHandler::textDocument() const
-{
- if (!m_document)
- return nullptr;
-
- return m_document->textDocument();
-}
-
-void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
-{
- QTextCursor cursor = textCursor();
- if (!cursor.hasSelection())
- cursor.select(QTextCursor::WordUnderCursor);
- cursor.mergeCharFormat(format);
-}
-
-bool DocumentHandler::modified() const
-{
- return m_document && m_document->textDocument()->isModified();
-}
-
-void DocumentHandler::setModified(bool m)
-{
- if (m_document)
- m_document->textDocument()->setModified(m);
-}
-
-QFont DocumentHandler::font() const
-{
- QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return m_document->textDocument()->defaultFont();
- QTextCharFormat format = cursor.charFormat();
- return format.font();
-}
-
-void DocumentHandler::setFont(const QFont & font){
-
- QTextCursor cursor = textCursor();
- if (!cursor.isNull() && cursor.charFormat().font() == font)
- return;
-
- QTextCharFormat format;
- format.setFont(font);
- mergeFormatOnWordOrSelection(format);
-
- emit fontChanged();
-}
-
-bool DocumentHandler::bold() const
-{
- const QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return m_document->textDocument()->defaultFont().bold();
- return cursor.charFormat().font().bold();
-}
-
-void DocumentHandler::setBold(bool bold)
-{
- const QTextCursor cursor = textCursor();
- if (!cursor.isNull() && cursor.charFormat().font().bold() == bold)
- return;
-
- QFont font = cursor.charFormat().font();
- font.setBold(bold);
- QTextCharFormat format;
- format.setFont(font);
- mergeFormatOnWordOrSelection(format);
-
- emit boldChanged();
-}
-
-bool DocumentHandler::underline() const
-{
- const QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return m_document->textDocument()->defaultFont().underline();
- return cursor.charFormat().font().underline();
-}
-
-void DocumentHandler::setUnderline(bool underline)
-{
- const QTextCursor cursor = textCursor();
- if (!cursor.isNull() && cursor.charFormat().font().underline() == underline)
- return;
-
- QFont font = cursor.charFormat().font();
- font.setUnderline(underline);
- QTextCharFormat format;
- format.setFont(font);
- mergeFormatOnWordOrSelection(format);
-
- emit underlineChanged();
-}
-
-bool DocumentHandler::italic() const
-{
- const QTextCursor cursor = textCursor();
- if (cursor.isNull())
- return m_document->textDocument()->defaultFont().italic();
- return cursor.charFormat().font().italic();
-}
-
-void DocumentHandler::setItalic(bool italic)
-{
- const QTextCursor cursor = textCursor();
- if (!cursor.isNull() && cursor.charFormat().font().italic() == italic)
- return;
-
- QFont font = cursor.charFormat().font();
- font.setItalic(italic);
- QTextCharFormat format;
- format.setFont(font);
- mergeFormatOnWordOrSelection(format);
-
- emit italicChanged();
-}
-
-#include "moc_documenthandler.cpp"
diff --git a/examples/quickcontrols/texteditor/documenthandler.h b/examples/quickcontrols/texteditor/documenthandler.h
deleted file mode 100644
index b750997885..0000000000
--- a/examples/quickcontrols/texteditor/documenthandler.h
+++ /dev/null
@@ -1,121 +0,0 @@
-// Copyright (C) 2017 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef DOCUMENTHANDLER_H
-#define DOCUMENTHANDLER_H
-
-#include <QFont>
-#include <QObject>
-#include <QTextCursor>
-#include <QUrl>
-
-QT_BEGIN_NAMESPACE
-class QTextDocument;
-class QQuickTextDocument;
-QT_END_NAMESPACE
-
-class DocumentHandler : public QObject
-{
- Q_OBJECT
-
- Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
- Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
- Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
- Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
-
- Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
- Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
-
- Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
-
- Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
- Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
- Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
-
- Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged)
- Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
- Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged)
-
- Q_PROPERTY(bool modified READ modified WRITE setModified NOTIFY modifiedChanged)
-
-public:
- explicit DocumentHandler(QObject *parent = nullptr);
-
- QQuickTextDocument *document() const;
- void setDocument(QQuickTextDocument *document);
-
- int cursorPosition() const;
- void setCursorPosition(int position);
-
- int selectionStart() const;
- void setSelectionStart(int position);
-
- int selectionEnd() const;
- void setSelectionEnd(int position);
-
- QColor textColor() const;
- void setTextColor(const QColor &color);
-
- Qt::Alignment alignment() const;
- void setAlignment(Qt::Alignment alignment);
-
- QFont font() const;
- void setFont(const QFont & font);
-
- bool bold() const;
- void setBold(bool bold);
-
- bool underline() const;
- void setUnderline(bool underline);
-
- bool italic() const;
- void setItalic(bool italic);
-
- QString fileName() const;
- QString fileType() const;
- QUrl fileUrl() const;
-
- bool modified() const;
- void setModified(bool m);
-
-public Q_SLOTS:
- void load(const QUrl &fileUrl);
- void saveAs(const QUrl &fileUrl);
-
-Q_SIGNALS:
- void documentChanged();
- void cursorPositionChanged();
- void selectionStartChanged();
- void selectionEndChanged();
-
- void fontChanged();
- void boldChanged();
- void underlineChanged();
- void italicChanged();
- void textColorChanged();
- void alignmentChanged();
-
- void textChanged();
- void fileUrlChanged();
-
- void loaded(const QString &text, int format);
- void error(const QString &message);
-
- void modifiedChanged();
-
-private:
- void reset();
- QTextCursor textCursor() const;
- QTextDocument *textDocument() const;
- void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
-
- QQuickTextDocument *m_document;
-
- int m_cursorPosition;
- int m_selectionStart;
- int m_selectionEnd;
-
- QUrl m_fileUrl;
-};
-
-#endif // DOCUMENTHANDLER_H
diff --git a/examples/quickcontrols/texteditor/qml/+touch/texteditor.qml b/examples/quickcontrols/texteditor/qml/+touch/texteditor.qml
index 2a0d9f117f..f0418bd95a 100644
--- a/examples/quickcontrols/texteditor/qml/+touch/texteditor.qml
+++ b/examples/quickcontrols/texteditor/qml/+touch/texteditor.qml
@@ -6,8 +6,6 @@ import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Window
-import io.qt.examples.texteditor
-
// TODO:
// - make designer-friendly
@@ -56,23 +54,6 @@ ApplicationWindow {
}
}
- DocumentHandler {
- id: document
- document: textArea.textDocument
- cursorPosition: textArea.cursorPosition
- selectionStart: textArea.selectionStart
- selectionEnd: textArea.selectionEnd
- // textColor: TODO
- Component.onCompleted: document.load("qrc:/texteditor.html")
- onLoaded: {
- textArea.text = text
- }
- onError: {
- errorDialog.text = message
- errorDialog.visible = true
- }
- }
-
Flickable {
id: flickable
flickableDirection: Flickable.VerticalFlick
@@ -94,6 +75,13 @@ ApplicationWindow {
background: null
onLinkActivated: Qt.openUrlExternally(link)
+
+ Component.onCompleted: textDocument.source = "qrc:/texteditor.html"
+
+ textDocument.onError: function (message) {
+ errorDialog.text = message
+ errorDialog.visible = true
+ }
}
ScrollBar.vertical: ScrollBar {}
diff --git a/examples/quickcontrols/texteditor/qml/texteditor.qml b/examples/quickcontrols/texteditor/qml/texteditor.qml
index 03f987fc5b..9c0747937b 100644
--- a/examples/quickcontrols/texteditor/qml/texteditor.qml
+++ b/examples/quickcontrols/texteditor/qml/texteditor.qml
@@ -8,8 +8,6 @@ import QtQuick.Window
import QtQuick.Dialogs
import Qt.labs.platform as Platform
-import io.qt.examples.texteditor
-
// TODO:
// - make designer-friendly
@@ -40,6 +38,7 @@ ApplicationWindow {
Action {
id: saveAction
shortcut: StandardKey.Save
+ enabled: textArea.textDocument.modified
onTriggered: textArea.textDocument.save()
}
@@ -272,7 +271,12 @@ ApplicationWindow {
title: qsTr("Quit?")
text: qsTr("The file has been modified. Quit anyway?")
buttons: MessageDialog.Yes | MessageDialog.No
- onButtonClicked: function (button, role) { if (role === MessageDialog.YesRole) Qt.quit() }
+ onButtonClicked: function (button, role) {
+ if (role === MessageDialog.YesRole) {
+ textArea.textDocument.modified = false
+ Qt.quit()
+ }
+ }
}
MessageDialog {
@@ -454,29 +458,6 @@ ApplicationWindow {
}
}
- DocumentHandler {
- id: document
- document: textArea.textDocument
- cursorPosition: textArea.cursorPosition
- selectionStart: textArea.selectionStart
- selectionEnd: textArea.selectionEnd
-
- Component.onCompleted: {
- if (Qt.application.arguments.length === 2)
- textArea.textDocument.source = "file:" + Qt.application.arguments[1];
- else
- textArea.textDocument.source = "qrc:/texteditor.html";
- }
- onLoaded: function (text, format) {
- textArea.textFormat = format
- textArea.text = text
- }
- onError: function (message) {
- errorDialog.text = message
- errorDialog.open()
- }
- }
-
Flickable {
id: flickable
flickableDirection: Flickable.VerticalFlick
@@ -508,6 +489,13 @@ ApplicationWindow {
Qt.openUrlExternally(link)
}
+ Component.onCompleted: {
+ if (Qt.application.arguments.length === 2)
+ textDocument.source = "file:" + Qt.application.arguments[1];
+ else
+ textDocument.source = "qrc:/texteditor.html";
+ }
+
textDocument.onError: function (message) {
errorDialog.text = message
errorDialog.open()
diff --git a/examples/quickcontrols/texteditor/texteditor.cpp b/examples/quickcontrols/texteditor/texteditor.cpp
index 79cd6d8071..dd4800f7b5 100644
--- a/examples/quickcontrols/texteditor/texteditor.cpp
+++ b/examples/quickcontrols/texteditor/texteditor.cpp
@@ -12,8 +12,6 @@
#include <QQmlContext>
#include <QQuickStyle>
-#include "documenthandler.h"
-
int main(int argc, char *argv[])
{
QGuiApplication::setApplicationName("Text Editor");
@@ -28,8 +26,6 @@ int main(int argc, char *argv[])
if (QFontDatabase::addApplicationFont(":/fonts/fontello.ttf") == -1)
qWarning() << "Failed to load fontello.ttf";
- qmlRegisterType<DocumentHandler>("io.qt.examples.texteditor", 1, 0, "DocumentHandler");
-
QStringList selectors;
#ifdef QT_EXTRA_FILE_SELECTOR
selectors += QT_EXTRA_FILE_SELECTOR;
diff --git a/examples/quickcontrols/texteditor/texteditor.pro b/examples/quickcontrols/texteditor/texteditor.pro
index 5f021865d3..86817d1ba4 100644
--- a/examples/quickcontrols/texteditor/texteditor.pro
+++ b/examples/quickcontrols/texteditor/texteditor.pro
@@ -5,12 +5,8 @@ qtHaveModule(widgets): QT += widgets
cross_compile: DEFINES += QT_EXTRA_FILE_SELECTOR=\\\"touch\\\"
-HEADERS += \
- documenthandler.h
-
SOURCES += \
- texteditor.cpp \
- documenthandler.cpp
+ texteditor.cpp
OTHER_FILES += \
qml/*.qml