From 0d8e87d7f6765fec9f261030d63768d49cd7c6de Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 11 Aug 2016 20:53:30 +0200 Subject: texteditor: simplify saveAs() - let FileDialog handle the suffix Change-Id: Id1b415ebcbe12a9eeff9b54dcfa2cec67054a8d6 Reviewed-by: Mitch Curtis --- .../quickcontrols2/texteditor/documenthandler.cpp | 32 ++++++---------------- .../quickcontrols2/texteditor/documenthandler.h | 2 +- .../quickcontrols2/texteditor/qml/texteditor.qml | 4 +-- 3 files changed, 12 insertions(+), 26 deletions(-) (limited to 'examples') diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp index 2a13b211fc..43907db9de 100644 --- a/examples/quickcontrols2/texteditor/documenthandler.cpp +++ b/examples/quickcontrols2/texteditor/documenthandler.cpp @@ -139,35 +139,21 @@ void DocumentHandler::setText(const QString &text) emit textChanged(); } -void DocumentHandler::saveAs(const QUrl &fileUrl, const QString &fileType) +void DocumentHandler::saveAs(const QUrl &fileUrl) { if (!m_doc) return; - // TODO: remove this when FileDialog gets suffix property - QString localPath = fileUrl.toLocalFile(); - QString extension = fileType; - if (extension.isEmpty()) { - if (QFile::exists(localPath)) { - extension = QFileInfo(localPath).suffix(); - } else { - const int periodIndex = localPath.indexOf(QLatin1Char('.')); - if (periodIndex != -1) - extension = localPath.mid(periodIndex); - } - } - - const bool isHtml = extension.contains(QLatin1String("htm")); - if (!localPath.endsWith(extension)) - localPath += extension; - QFile f(localPath); - if (!f.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) { - emit error(tr("Cannot save: ") + f.errorString()); + 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; } - f.write((isHtml ? m_doc->toHtml() : m_doc->toPlainText()).toLocal8Bit()); - f.close(); - setFileUrl(QUrl::fromLocalFile(localPath)); + file.write((isHtml ? m_doc->toHtml() : m_doc->toPlainText()).toUtf8()); + file.close(); + setFileUrl(fileUrl); } QUrl DocumentHandler::fileUrl() const diff --git a/examples/quickcontrols2/texteditor/documenthandler.h b/examples/quickcontrols2/texteditor/documenthandler.h index 48e2c50941..9feb338ec2 100644 --- a/examples/quickcontrols2/texteditor/documenthandler.h +++ b/examples/quickcontrols2/texteditor/documenthandler.h @@ -125,7 +125,7 @@ public Q_SLOTS: void setFileUrl(const QUrl &fileUrl); void setText(const QString &text); - void saveAs(const QUrl &fileUrl, const QString &fileType); + void saveAs(const QUrl &fileUrl); void setDocumentTitle(const QString &documentTitle); diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml index 1188686b5f..a24a174b14 100644 --- a/examples/quickcontrols2/texteditor/qml/texteditor.qml +++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml @@ -169,9 +169,9 @@ ApplicationWindow { FileDialog { id: saveDialog fileMode: FileDialog.SaveFile + defaultSuffix: "html" nameFilters: openDialog.nameFilters - // TODO: will eventually pass FileDialog's suffix property here - onFileSelected: document.saveAs(file, "") + onFileSelected: document.saveAs(file) } FontDialog { -- cgit v1.2.3