Commit 3bf44eb7 authored by Frederik Gladhorn's avatar Frederik Gladhorn Committed by The Qt Project
Browse files

Add window title to text example.


Change-Id: Id1f7d3447bacd818c2aa64784f76b45a678f97e1
Reviewed-by: default avatarJens Bache-Wiig <jens.bache-wiig@digia.com>
parent 2725de20
Branches
Tags
No related merge requests found
Showing with 34 additions and 3 deletions
......@@ -50,6 +50,8 @@ ApplicationWindow {
minimumWidth: 400
minimumHeight: 300
title: document.documentTitle + " - Text Editor Example"
Action {
id: cut
text: "Cut"
......
......@@ -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) {
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment