summaryrefslogtreecommitdiffstats
path: root/framework/src
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src')
-rw-r--r--framework/src/domain/documenthandler.cpp72
-rw-r--r--framework/src/domain/documenthandler.h17
2 files changed, 0 insertions, 89 deletions
diff --git a/framework/src/domain/documenthandler.cpp b/framework/src/domain/documenthandler.cpp
index 25a83592..1a323d9f 100644
--- a/framework/src/domain/documenthandler.cpp
+++ b/framework/src/domain/documenthandler.cpp
@@ -256,78 +256,6 @@ void DocumentHandler::setFontSize(int size)
256 emit fontSizeChanged(); 256 emit fontSizeChanged();
257} 257}
258 258
259QString DocumentHandler::fileName() const
260{
261 const QString filePath = QQmlFile::urlToLocalFileOrQrc(m_fileUrl);
262 const QString fileName = QFileInfo(filePath).fileName();
263 if (fileName.isEmpty())
264 return QStringLiteral("untitled.txt");
265 return fileName;
266}
267
268QString DocumentHandler::fileType() const
269{
270 return QFileInfo(fileName()).suffix();
271}
272
273QUrl DocumentHandler::fileUrl() const
274{
275 return m_fileUrl;
276}
277
278void DocumentHandler::load(const QUrl &fileUrl)
279{
280 if (fileUrl == m_fileUrl)
281 return;
282
283 QQmlEngine *engine = qmlEngine(this);
284 if (!engine) {
285 qWarning() << "load() called before DocumentHandler has QQmlEngine";
286 return;
287 }
288
289 const QUrl path = QQmlFileSelector::get(engine)->selector()->select(fileUrl);
290 const QString fileName = QQmlFile::urlToLocalFileOrQrc(path);
291 if (QFile::exists(fileName)) {
292 QFile file(fileName);
293 if (file.open(QFile::ReadOnly)) {
294 QByteArray data = file.readAll();
295 QTextCodec *codec = QTextCodec::codecForHtml(data);
296 if (QTextDocument *doc = textDocument())
297 doc->setModified(false);
298
299 emit loaded(codec->toUnicode(data));
300 reset();
301 }
302 }
303
304 m_fileUrl = fileUrl;
305 emit fileUrlChanged();
306}
307
308void DocumentHandler::saveAs(const QUrl &fileUrl)
309{
310 QTextDocument *doc = textDocument();
311 if (!doc)
312 return;
313
314 const QString filePath = fileUrl.toLocalFile();
315 const bool isHtml = QFileInfo(filePath).suffix().contains(QLatin1String("htm"));
316 QFile file(filePath);
317 if (!file.open(QFile::WriteOnly | QFile::Truncate | (isHtml ? QFile::NotOpen : QFile::Text))) {
318 emit error(tr("Cannot save: ") + file.errorString());
319 return;
320 }
321 file.write((isHtml ? doc->toHtml() : doc->toPlainText()).toUtf8());
322 file.close();
323
324 if (fileUrl == m_fileUrl)
325 return;
326
327 m_fileUrl = fileUrl;
328 emit fileUrlChanged();
329}
330
331void DocumentHandler::reset() 259void DocumentHandler::reset()
332{ 260{
333 emit fontFamilyChanged(); 261 emit fontFamilyChanged();
diff --git a/framework/src/domain/documenthandler.h b/framework/src/domain/documenthandler.h
index a6125bc3..74b7c4f9 100644
--- a/framework/src/domain/documenthandler.h
+++ b/framework/src/domain/documenthandler.h
@@ -80,10 +80,6 @@ class DocumentHandler : public QObject
80 80
81 Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) 81 Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
82 82
83 Q_PROPERTY(QString fileName READ fileName NOTIFY fileUrlChanged)
84 Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
85 Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged)
86
87public: 83public:
88 explicit DocumentHandler(QObject *parent = nullptr); 84 explicit DocumentHandler(QObject *parent = nullptr);
89 85
@@ -120,14 +116,6 @@ public:
120 int fontSize() const; 116 int fontSize() const;
121 void setFontSize(int size); 117 void setFontSize(int size);
122 118
123 QString fileName() const;
124 QString fileType() const;
125 QUrl fileUrl() const;
126
127public Q_SLOTS:
128 void load(const QUrl &fileUrl);
129 void saveAs(const QUrl &fileUrl);
130
131Q_SIGNALS: 119Q_SIGNALS:
132 void documentChanged(); 120 void documentChanged();
133 void cursorPositionChanged(); 121 void cursorPositionChanged();
@@ -145,10 +133,6 @@ Q_SIGNALS:
145 void fontSizeChanged(); 133 void fontSizeChanged();
146 134
147 void textChanged(); 135 void textChanged();
148 void fileUrlChanged();
149
150 void loaded(const QString &text);
151 void error(const QString &message);
152 136
153private: 137private:
154 void reset(); 138 void reset();
@@ -164,7 +148,6 @@ private:
164 148
165 QFont m_font; 149 QFont m_font;
166 int m_fontSize; 150 int m_fontSize;
167 QUrl m_fileUrl;
168}; 151};
169 152
170#endif // DOCUMENTHANDLER_H 153#endif // DOCUMENTHANDLER_H