diff options
author | Shawn Rutledge <shawn.rutledge@digia.com> | 2015-03-18 13:03:11 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@digia.com> | 2015-03-20 08:20:27 +0000 |
commit | 9d52e45de4ff2b668c08216d3b8edd8bf0b78421 (patch) | |
tree | d8645716bfef075c57ecbe13306545494681e475 /examples | |
parent | 781e843d0344f08efc001b9c539c19915a158d62 (diff) | |
download | qtquickcontrols-9d52e45de4ff2b668c08216d3b8edd8bf0b78421.tar.gz |
texteditor example: implement file saving
Change-Id: I7c8321089d9187aeb0093d0c0361950385b92cba
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@theqtcompany.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/quick/controls/texteditor/qml/main.qml | 34 | ||||
-rw-r--r-- | examples/quick/controls/texteditor/src/documenthandler.cpp | 18 | ||||
-rw-r--r-- | examples/quick/controls/texteditor/src/documenthandler.h | 2 |
3 files changed, 51 insertions, 3 deletions
diff --git a/examples/quick/controls/texteditor/qml/main.qml b/examples/quick/controls/texteditor/qml/main.qml index 4e11a4f5..cefbf434 100644 --- a/examples/quick/controls/texteditor/qml/main.qml +++ b/examples/quick/controls/texteditor/qml/main.qml @@ -157,8 +157,13 @@ ApplicationWindow { FileDialog { id: fileDialog - nameFilters: ["Text files (*.txt)", "HTML files (*.html)"] - onAccepted: document.fileUrl = fileUrl + nameFilters: ["Text files (*.txt)", "HTML files (*.html, *.htm)"] + onAccepted: { + if (fileDialog.selectExisting) + document.fileUrl = fileUrl + else + document.saveAs(fileUrl, selectedNameFilter) + } } ColorDialog { @@ -172,13 +177,28 @@ ApplicationWindow { iconSource: "images/fileopen.png" iconName: "document-open" text: "Open" - onTriggered: fileDialog.open() + onTriggered: { + fileDialog.selectExisting = true + fileDialog.open() + } + } + + Action { + id: fileSaveAsAction + iconSource: "images/filesave.png" + iconName: "document-save" + text: "Save As…" + onTriggered: { + fileDialog.selectExisting = false + fileDialog.open() + } } menuBar: MenuBar { Menu { title: "&File" MenuItem { action: fileOpenAction } + MenuItem { action: fileSaveAsAction } MenuItem { text: "Quit"; onTriggered: Qt.quit() } } Menu { @@ -303,6 +323,10 @@ ApplicationWindow { Component.onCompleted: forceActiveFocus() } + MessageDialog { + id: errorDialog + } + DocumentHandler { id: document target: textArea @@ -325,5 +349,9 @@ ApplicationWindow { fontFamilyComboBox.special = false } } + onError: { + errorDialog.text = message + errorDialog.visible = true + } } } diff --git a/examples/quick/controls/texteditor/src/documenthandler.cpp b/examples/quick/controls/texteditor/src/documenthandler.cpp index 65ae9fb8..2e9987a1 100644 --- a/examples/quick/controls/texteditor/src/documenthandler.cpp +++ b/examples/quick/controls/texteditor/src/documenthandler.cpp @@ -119,6 +119,24 @@ void DocumentHandler::setText(const QString &arg) } } +void DocumentHandler::saveAs(const QUrl &arg, const QString &fileType) +{ + bool isHtml = fileType.contains(QLatin1String("htm")); + QLatin1String ext(isHtml ? ".html" : ".txt"); + QString localPath = arg.toLocalFile(); + if (!localPath.endsWith(ext)) + localPath += ext; + QFile f(localPath); + if (!f.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) { + emit error(tr("Cannot save: ") + f.errorString()); + return; + } + f.write((isHtml ? m_doc->toHtml() : m_doc->toPlainText()).toLocal8Bit()); + f.close(); + qDebug() << "saved to" << localPath; + setFileUrl(QUrl::fromLocalFile(localPath)); +} + QUrl DocumentHandler::fileUrl() const { return m_fileUrl; diff --git a/examples/quick/controls/texteditor/src/documenthandler.h b/examples/quick/controls/texteditor/src/documenthandler.h index 8359f68c..f2bd4eea 100644 --- a/examples/quick/controls/texteditor/src/documenthandler.h +++ b/examples/quick/controls/texteditor/src/documenthandler.h @@ -122,6 +122,7 @@ public Q_SLOTS: void setFileUrl(const QUrl &arg); void setText(const QString &arg); + void saveAs(const QUrl &arg, const QString &fileType); void setDocumentTitle(QString arg); @@ -146,6 +147,7 @@ Q_SIGNALS: void textChanged(); void documentTitleChanged(); + void error(QString message); private: void reset(); |