aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2016-08-11 22:29:26 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2016-08-12 10:51:27 +0000
commit90842fdc865c3b7e9937bbd75f3fd5d74ec588ef (patch)
treeaa4900db16b0411f9895aaa5650aab8f4792df61 /examples
parentbb3453aeadf1aa3a53f2bf3c8d55b3ae3f4543b0 (diff)
texteditor: add fileName & fileType
'document.fileName' reads better than 'document.documentTitle', and 'document.fileType' can be set as the default suffix for the file save dialog. Change-Id: I935586296c91d3efdd5edea03d81c685e7edcab2 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/quickcontrols2/texteditor/documenthandler.cpp31
-rw-r--r--examples/quickcontrols2/texteditor/documenthandler.h9
-rw-r--r--examples/quickcontrols2/texteditor/qml/texteditor.qml4
3 files changed, 22 insertions, 22 deletions
diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp
index 7a7934b4..57322512 100644
--- a/examples/quickcontrols2/texteditor/documenthandler.cpp
+++ b/examples/quickcontrols2/texteditor/documenthandler.cpp
@@ -267,18 +267,18 @@ void DocumentHandler::setText(const QString &text)
emit textChanged();
}
-QString DocumentHandler::documentTitle() const
+QString DocumentHandler::fileName() const
{
- return m_documentTitle;
+ const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl);
+ const QString fileName = QFileInfo(filePath).fileName();
+ if (fileName.isEmpty())
+ return QStringLiteral("untitled.txt");
+ return fileName;
}
-void DocumentHandler::setDocumentTitle(const QString &title)
+QString DocumentHandler::fileType() const
{
- if (title == m_documentTitle)
- return;
-
- m_documentTitle = title;
- emit documentTitleChanged();
+ return QFileInfo(fileName()).suffix();
}
QUrl DocumentHandler::fileUrl() const
@@ -291,7 +291,6 @@ void DocumentHandler::setFileUrl(const QUrl &fileUrl)
if (fileUrl == m_fileUrl)
return;
- m_fileUrl = fileUrl;
QString fileName = QQmlFile::urlToLocalFileOrQrc(fileUrl);
if (QFile::exists(fileName)) {
QFile file(fileName);
@@ -301,17 +300,14 @@ void DocumentHandler::setFileUrl(const QUrl &fileUrl)
setText(codec->toUnicode(data));
if (QTextDocument *doc = textDocument())
doc->setModified(false);
- if (fileName.isEmpty())
- m_documentTitle = QStringLiteral("untitled.txt");
- else
- m_documentTitle = QFileInfo(fileName).fileName();
emit textChanged();
- emit documentTitleChanged();
reset();
}
}
+
+ m_fileUrl = fileUrl;
emit fileUrlChanged();
}
@@ -330,7 +326,12 @@ void DocumentHandler::saveAs(const QUrl &fileUrl)
}
file.write((isHtml ? doc->toHtml() : doc->toPlainText()).toUtf8());
file.close();
- setFileUrl(fileUrl);
+
+ if (fileUrl == m_fileUrl)
+ return;
+
+ m_fileUrl = fileUrl;
+ emit fileUrlChanged();
}
void DocumentHandler::reset()
diff --git a/examples/quickcontrols2/texteditor/documenthandler.h b/examples/quickcontrols2/texteditor/documenthandler.h
index dc6c9b22..671bb5f3 100644
--- a/examples/quickcontrols2/texteditor/documenthandler.h
+++ b/examples/quickcontrols2/texteditor/documenthandler.h
@@ -81,7 +81,8 @@ 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 documentTitle READ documentTitle WRITE setDocumentTitle NOTIFY documentTitleChanged)
+ 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)
public:
@@ -123,8 +124,8 @@ public:
QString text() const;
void setText(const QString &text);
- QString documentTitle() const;
- void setDocumentTitle(const QString &title);
+ QString fileName() const;
+ QString fileType() const;
QUrl fileUrl() const;
void setFileUrl(const QUrl &fileUrl);
@@ -149,7 +150,6 @@ Q_SIGNALS:
void fontSizeChanged();
void textChanged();
- void documentTitleChanged();
void fileUrlChanged();
void error(const QString &message);
@@ -169,7 +169,6 @@ private:
QFont m_font;
int m_fontSize;
QString m_text;
- QString m_documentTitle;
QUrl m_fileUrl;
};
diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml
index a24a174b..ca4f196d 100644
--- a/examples/quickcontrols2/texteditor/qml/texteditor.qml
+++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml
@@ -53,7 +53,7 @@ ApplicationWindow {
width: 1024
height: 600
visible: true
- title: document.documentTitle + " - Text Editor Example"
+ title: document.fileName + " - Text Editor Example"
Component.onCompleted: {
x = Screen.width / 2 - width / 2
@@ -169,7 +169,7 @@ ApplicationWindow {
FileDialog {
id: saveDialog
fileMode: FileDialog.SaveFile
- defaultSuffix: "html"
+ defaultSuffix: document.fileType
nameFilters: openDialog.nameFilters
onFileSelected: document.saveAs(file)
}