texteditor: replace setFileUrl() with load()

This allows to remove the text property to avoid storing the whole
document's initial content for no real purpose.

Change-Id: Iad59576b5304be9739c1dfb0a7d4c263323b0edb
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
This commit is contained in:
J-P Nurmi 2016-08-11 22:41:11 +02:00
parent 90842fdc86
commit 9a6bdf52a3
3 changed files with 10 additions and 29 deletions

View File

@ -253,20 +253,6 @@ void DocumentHandler::setFontSize(int size)
emit fontSizeChanged();
}
QString DocumentHandler::text() const
{
return m_text;
}
void DocumentHandler::setText(const QString &text)
{
if (text == m_text)
return;
m_text = text;
emit textChanged();
}
QString DocumentHandler::fileName() const
{
const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl);
@ -286,7 +272,7 @@ QUrl DocumentHandler::fileUrl() const
return m_fileUrl;
}
void DocumentHandler::setFileUrl(const QUrl &fileUrl)
void DocumentHandler::load(const QUrl &fileUrl)
{
if (fileUrl == m_fileUrl)
return;
@ -297,12 +283,10 @@ void DocumentHandler::setFileUrl(const QUrl &fileUrl)
if (file.open(QFile::ReadOnly)) {
QByteArray data = file.readAll();
QTextCodec *codec = QTextCodec::codecForHtml(data);
setText(codec->toUnicode(data));
if (QTextDocument *doc = textDocument())
doc->setModified(false);
emit textChanged();
emit loaded(codec->toUnicode(data));
reset();
}
}

View File

@ -80,10 +80,9 @@ 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 fileName READ fileName NOTIFY fileUrlChanged)
Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged)
Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged)
public:
explicit DocumentHandler(QObject *parent = nullptr);
@ -121,16 +120,12 @@ public:
int fontSize() const;
void setFontSize(int size);
QString text() const;
void setText(const QString &text);
QString fileName() const;
QString fileType() const;
QUrl fileUrl() const;
void setFileUrl(const QUrl &fileUrl);
public Q_SLOTS:
void load(const QUrl &fileUrl);
void saveAs(const QUrl &fileUrl);
Q_SIGNALS:
@ -152,6 +147,7 @@ Q_SIGNALS:
void textChanged();
void fileUrlChanged();
void loaded(const QString &text);
void error(const QString &message);
private:
@ -168,7 +164,6 @@ private:
QFont m_font;
int m_fontSize;
QString m_text;
QUrl m_fileUrl;
};

View File

@ -163,7 +163,7 @@ ApplicationWindow {
id: openDialog
fileMode: FileDialog.OpenFile
nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"]
onFileSelected: document.fileUrl = file
onFileSelected: document.load(file)
}
FileDialog {
@ -361,7 +361,10 @@ ApplicationWindow {
textColor: colorDialog.currentColor
// TODO: if we don't do this, e.g. the bold button won't be checked
// when it should be (the title is bold)
Component.onCompleted: document.fileUrl = "qrc:/texteditor.html"
Component.onCompleted: document.load("qrc:/texteditor.html")
onLoaded: {
textArea.text = text
}
onError: {
errorDialog.text = message
errorDialog.visible = true
@ -375,7 +378,6 @@ ApplicationWindow {
TextArea.flickable: TextArea {
id: textArea
text: document.text
textFormat: Qt.RichText
wrapMode: TextArea.Wrap
focus: true