diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-08-11 22:41:11 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-08-12 10:51:30 +0000 |
commit | 9a6bdf52a387b9266f78f2576dfee9bbf7285730 (patch) | |
tree | 0d9ec6240c03ef8f2274d86423890c868cf7367d /examples | |
parent | 90842fdc865c3b7e9937bbd75f3fd5d74ec588ef (diff) |
texteditor: replace setFileUrl() with load()
This allows to remove the text property to avoid storing the whole
document's initial content for no real purpose.
Change-Id: Iad59576b5304be9739c1dfb0a7d4c263323b0edb
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/quickcontrols2/texteditor/documenthandler.cpp | 20 | ||||
-rw-r--r-- | examples/quickcontrols2/texteditor/documenthandler.h | 11 | ||||
-rw-r--r-- | examples/quickcontrols2/texteditor/qml/texteditor.qml | 8 |
3 files changed, 10 insertions, 29 deletions
diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp index 57322512..45c0f4cb 100644 --- a/examples/quickcontrols2/texteditor/documenthandler.cpp +++ b/examples/quickcontrols2/texteditor/documenthandler.cpp @@ -253,20 +253,6 @@ void DocumentHandler::setFontSize(int size) emit fontSizeChanged(); } -QString DocumentHandler::text() const -{ - return m_text; -} - -void DocumentHandler::setText(const QString &text) -{ - if (text == m_text) - return; - - m_text = text; - emit textChanged(); -} - QString DocumentHandler::fileName() const { const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl); @@ -286,7 +272,7 @@ QUrl DocumentHandler::fileUrl() const return m_fileUrl; } -void DocumentHandler::setFileUrl(const QUrl &fileUrl) +void DocumentHandler::load(const QUrl &fileUrl) { if (fileUrl == m_fileUrl) return; @@ -297,12 +283,10 @@ void DocumentHandler::setFileUrl(const QUrl &fileUrl) if (file.open(QFile::ReadOnly)) { QByteArray data = file.readAll(); QTextCodec *codec = QTextCodec::codecForHtml(data); - setText(codec->toUnicode(data)); if (QTextDocument *doc = textDocument()) doc->setModified(false); - emit textChanged(); - + emit loaded(codec->toUnicode(data)); reset(); } } diff --git a/examples/quickcontrols2/texteditor/documenthandler.h b/examples/quickcontrols2/texteditor/documenthandler.h index 671bb5f3..dd3c3988 100644 --- a/examples/quickcontrols2/texteditor/documenthandler.h +++ b/examples/quickcontrols2/texteditor/documenthandler.h @@ -80,10 +80,9 @@ class DocumentHandler : public QObject Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) - Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged) Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged) - Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged) + Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged) public: explicit DocumentHandler(QObject *parent = nullptr); @@ -121,16 +120,12 @@ public: int fontSize() const; void setFontSize(int size); - QString text() const; - void setText(const QString &text); - QString fileName() const; QString fileType() const; - QUrl fileUrl() const; - void setFileUrl(const QUrl &fileUrl); public Q_SLOTS: + void load(const QUrl &fileUrl); void saveAs(const QUrl &fileUrl); Q_SIGNALS: @@ -152,6 +147,7 @@ Q_SIGNALS: void textChanged(); void fileUrlChanged(); + void loaded(const QString &text); void error(const QString &message); private: @@ -168,7 +164,6 @@ private: QFont m_font; int m_fontSize; - QString m_text; QUrl m_fileUrl; }; diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml index ca4f196d..21a187ba 100644 --- a/examples/quickcontrols2/texteditor/qml/texteditor.qml +++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml @@ -163,7 +163,7 @@ ApplicationWindow { id: openDialog fileMode: FileDialog.OpenFile nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"] - onFileSelected: document.fileUrl = file + onFileSelected: document.load(file) } FileDialog { @@ -361,7 +361,10 @@ ApplicationWindow { textColor: colorDialog.currentColor // TODO: if we don't do this, e.g. the bold button won't be checked // when it should be (the title is bold) - Component.onCompleted: document.fileUrl = "qrc:/texteditor.html" + Component.onCompleted: document.load("qrc:/texteditor.html") + onLoaded: { + textArea.text = text + } onError: { errorDialog.text = message errorDialog.visible = true @@ -375,7 +378,6 @@ ApplicationWindow { TextArea.flickable: TextArea { id: textArea - text: document.text textFormat: Qt.RichText wrapMode: TextArea.Wrap focus: true |