From 3bf44eb729b5c81125e374ad11aaf6c3e53d5c4a Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn <frederik.gladhorn@digia.com> Date: Wed, 20 Mar 2013 21:25:41 +0100 Subject: [PATCH] Add window title to text example. Change-Id: Id1f7d3447bacd818c2aa64784f76b45a678f97e1 Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com> --- examples/text/qml/main.qml | 2 ++ examples/text/src/documenthandler.cpp | 27 +++++++++++++++++++++++++-- examples/text/src/documenthandler.h | 8 +++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/examples/text/qml/main.qml b/examples/text/qml/main.qml index 4b5b10ec6..24aa10f55 100644 --- a/examples/text/qml/main.qml +++ b/examples/text/qml/main.qml @@ -50,6 +50,8 @@ ApplicationWindow { minimumWidth: 400 minimumHeight: 300 + title: document.documentTitle + " - Text Editor Example" + Action { id: cut text: "Cut" diff --git a/examples/text/src/documenthandler.cpp b/examples/text/src/documenthandler.cpp index b382e6f03..48b6d033c 100644 --- a/examples/text/src/documenthandler.cpp +++ b/examples/text/src/documenthandler.cpp @@ -43,6 +43,7 @@ #include <QtGui/QTextDocument> #include <QtGui/QTextCursor> #include <QtGui/QFontDatabase> +#include <QtCore/QFileInfo> DocumentHandler::DocumentHandler() : m_target(0) @@ -74,19 +75,41 @@ void DocumentHandler::setFileUrl(const QUrl &arg) { if (m_fileUrl != arg) { m_fileUrl = arg; - if (QFile::exists(QQmlFile::urlToLocalFileOrQrc(arg))) { - QFile file(QQmlFile::urlToLocalFileOrQrc(arg)); + QString fileName = QQmlFile::urlToLocalFileOrQrc(arg); + if (QFile::exists(fileName)) { + QFile file(fileName); if (file.open(QFile::ReadOnly)) { QByteArray data = file.readAll(); QTextCodec *codec = QTextCodec::codecForHtml(data); setText(codec->toUnicode(data)); + if (m_doc) + m_doc->setModified(false); + if (fileName.isEmpty()) + m_documentTitle = QStringLiteral("untitled.txt"); + else + m_documentTitle = QFileInfo(fileName).fileName(); + emit textChanged(); + emit documentTitleChanged(); } } emit fileUrlChanged(); } } +QString DocumentHandler::documentTitle() const +{ + return m_documentTitle; +} + +void DocumentHandler::setDocumentTitle(QString arg) +{ + if (m_documentTitle != arg) { + m_documentTitle = arg; + emit documentTitleChanged(); + } +} + void DocumentHandler::setText(const QString &arg) { if (m_text != arg) { diff --git a/examples/text/src/documenthandler.h b/examples/text/src/documenthandler.h index 804b16d06..c60bc1d51 100644 --- a/examples/text/src/documenthandler.h +++ b/examples/text/src/documenthandler.h @@ -74,9 +74,9 @@ class DocumentHandler : public QObject Q_PROPERTY(QStringList defaultFontSizes READ defaultFontSizes NOTIFY defaultFontSizesChanged) - Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle NOTIFY documentTitleChanged) public: DocumentHandler(); @@ -107,6 +107,8 @@ public: QUrl fileUrl() const; QString text() const; + QString documentTitle() const; + public Q_SLOTS: void setBold(bool arg); void setItalic(bool arg); @@ -116,6 +118,8 @@ public Q_SLOTS: void setFileUrl(const QUrl &arg); void setText(const QString &arg); + void setDocumentTitle(QString arg); + Q_SIGNALS: void targetChanged(); void cursorPositionChanged(); @@ -135,6 +139,7 @@ Q_SIGNALS: void fileUrlChanged(); void textChanged(); + void documentTitleChanged(); private: QTextCursor textCursor() const; @@ -154,6 +159,7 @@ private: int m_fontSize; QUrl m_fileUrl; QString m_text; + QString m_documentTitle; }; #endif -- GitLab