aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols2/texteditor/documenthandler.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2016-08-11 20:53:30 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2016-08-12 07:56:03 +0000
commit0d8e87d7f6765fec9f261030d63768d49cd7c6de (patch)
tree37c1135a066c939b4b5fa172754fd42d5b2dfbc6 /examples/quickcontrols2/texteditor/documenthandler.cpp
parent4a981527124b628fbd05f3d8d6462a2b20bb9567 (diff)
texteditor: simplify saveAs() - let FileDialog handle the suffix
Change-Id: Id1b415ebcbe12a9eeff9b54dcfa2cec67054a8d6 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'examples/quickcontrols2/texteditor/documenthandler.cpp')
-rw-r--r--examples/quickcontrols2/texteditor/documenthandler.cpp32
1 files changed, 9 insertions, 23 deletions
diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp
index 2a13b211..43907db9 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