27
27
#include " languageloader.h"
28
28
#include " languagemanager.h"
29
29
30
- DocumentHandler::DocumentHandler (QObject *parent) :
31
- QObject(parent),
32
- m_target(0 ),
33
- m_document(0 ),
34
- m_highlighter(0 ) {
35
-
30
+ DocumentHandler::DocumentHandler (QObject *parent)
31
+ : QObject(parent)
32
+ , m_target(0 )
33
+ , m_document(0 )
34
+ , m_highlighter(0 )
35
+ {
36
+
37
+ #ifndef QT_NO_FILESYSTEMWATCHER
36
38
m_watcher = new QFileSystemWatcher (this );
37
39
connect (m_watcher, &QFileSystemWatcher::fileChanged, this , &DocumentHandler::fileChanged);
40
+ #else
41
+ qWarning () << " Document change notification is not available on this platform" ;
42
+ #endif
38
43
39
44
m_defStyles = QSharedPointer<LanguageDefaultStyles>::create ();
40
45
}
41
46
42
- DocumentHandler::~DocumentHandler () {
47
+ DocumentHandler::~DocumentHandler ()
48
+ {
49
+ #ifndef QT_NO_FILESYSTEMWATCHER
43
50
delete m_watcher;
51
+ #endif
44
52
delete m_highlighter;
45
53
}
46
54
47
- void DocumentHandler::setTarget (QQuickItem *target) {
55
+ void DocumentHandler::setTarget (QQuickItem *target)
56
+ {
48
57
m_document = nullptr ;
49
58
m_target = target;
50
- if (!m_target)
59
+ if (!m_target)
51
60
return ;
52
61
53
62
QVariant doc = m_target->property (" textDocument" );
54
- if (doc.canConvert <QQuickTextDocument*>()) {
55
- QQuickTextDocument *qqdoc = doc.value <QQuickTextDocument*>();
56
- if (qqdoc) {
63
+ if (doc.canConvert <QQuickTextDocument *>()) {
64
+ QQuickTextDocument *qqdoc = doc.value <QQuickTextDocument *>();
65
+ if (qqdoc) {
57
66
m_document = qqdoc->textDocument ();
58
- connect (m_document, &QTextDocument::modificationChanged, this , &DocumentHandler::modifiedChanged);
59
- if (m_highlighter != nullptr )
67
+ connect (m_document, &QTextDocument::modificationChanged, this ,
68
+ &DocumentHandler::modifiedChanged);
69
+ if (m_highlighter != nullptr )
60
70
delete m_highlighter;
61
71
m_highlighter = new LiriSyntaxHighlighter (m_document);
62
72
m_highlighter->setDefaultStyles (m_defStyles);
@@ -65,28 +75,31 @@ void DocumentHandler::setTarget(QQuickItem *target) {
65
75
emit targetChanged ();
66
76
}
67
77
68
- bool DocumentHandler::setFileUrl (const QUrl &fileUrl) {
69
- if (fileUrl != m_fileUrl) {
70
- if (m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
78
+ bool DocumentHandler::setFileUrl (const QUrl &fileUrl)
79
+ {
80
+ if (fileUrl != m_fileUrl) {
81
+ #ifndef QT_NO_FILESYSTEMWATCHER
82
+ if (m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
71
83
m_watcher->removePath (m_fileUrl.toLocalFile ());
72
84
m_watcher->addPath (fileUrl.toLocalFile ());
85
+ #endif
73
86
m_fileUrl = fileUrl;
74
87
QString filename = m_fileUrl.toLocalFile ();
75
88
qDebug () << m_fileUrl << filename;
76
89
QFile file (filename);
77
- if (!file.open (QFile::ReadOnly)) {
90
+ if (!file.open (QFile::ReadOnly)) {
78
91
emit error (file.errorString ());
79
92
return false ;
80
93
}
81
94
QByteArray data = file.readAll ();
82
- if (file.error () != QFileDevice::NoError) {
95
+ if (file.error () != QFileDevice::NoError) {
83
96
emit error (file.errorString ());
84
97
file.close ();
85
98
return false ;
86
99
}
87
100
QTextCodec *codec = QTextCodec::codecForUtfText (data, QTextCodec::codecForLocale ());
88
101
setText (codec->toUnicode (data));
89
- if (m_document) {
102
+ if (m_document) {
90
103
m_document->setModified (false );
91
104
92
105
// Enable syntax highlighting
@@ -96,7 +109,7 @@ bool DocumentHandler::setFileUrl(const QUrl &fileUrl) {
96
109
auto language = ll.loadMainContextByMimeType (mimeType, m_fileUrl.fileName ());
97
110
m_highlighter->setLanguage (language, ll.styleMap ());
98
111
}
99
- if (m_fileUrl.isEmpty ())
112
+ if (m_fileUrl.isEmpty ())
100
113
m_documentTitle = QStringLiteral (" New Document" );
101
114
else
102
115
m_documentTitle = QFileInfo (filename).fileName ();
@@ -107,15 +120,17 @@ bool DocumentHandler::setFileUrl(const QUrl &fileUrl) {
107
120
return true ;
108
121
}
109
122
110
- void DocumentHandler::setDocumentTitle (const QString &title) {
111
- if (title != m_documentTitle) {
123
+ void DocumentHandler::setDocumentTitle (const QString &title)
124
+ {
125
+ if (title != m_documentTitle) {
112
126
m_documentTitle = title;
113
127
emit documentTitleChanged ();
114
128
}
115
129
}
116
130
117
- QString DocumentHandler::textFragment (int position, int blockCount) {
118
- if (m_highlighter) {
131
+ QString DocumentHandler::textFragment (int position, int blockCount)
132
+ {
133
+ if (m_highlighter) {
119
134
return m_highlighter->highlightedFragment (position, blockCount, m_document->defaultFont ());
120
135
} else {
121
136
QTextCursor cursor (m_document->findBlock (position));
@@ -132,26 +147,30 @@ QString DocumentHandler::textFragment(int position, int blockCount) {
132
147
}
133
148
}
134
149
135
- void DocumentHandler::setText (const QString &text) {
136
- if (text != m_text) {
150
+ void DocumentHandler::setText (const QString &text)
151
+ {
152
+ if (text != m_text) {
137
153
m_text = text;
138
154
emit textChanged ();
139
155
}
140
156
}
141
157
142
- bool DocumentHandler::saveAs (const QUrl &filename) {
158
+ bool DocumentHandler::saveAs (const QUrl &filename)
159
+ {
160
+ #ifndef QT_NO_FILESYSTEMWATCHER
143
161
// Stop monitoring file while saving
144
- if (m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
162
+ if (m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
145
163
m_watcher->removePath (m_fileUrl.toLocalFile ());
164
+ #endif
146
165
147
166
bool success = true ;
148
167
QString localPath = filename.toLocalFile ();
149
168
QFile file (localPath);
150
- if (!file.open (QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
169
+ if (!file.open (QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
151
170
emit error (file.errorString ());
152
171
success = false ;
153
172
} else {
154
- if (file.write (m_document->toPlainText ().toLocal8Bit ()) == -1 ) {
173
+ if (file.write (m_document->toPlainText ().toLocal8Bit ()) == -1 ) {
155
174
emit error (file.errorString ());
156
175
success = false ;
157
176
}
@@ -162,24 +181,27 @@ bool DocumentHandler::saveAs(const QUrl &filename) {
162
181
m_document->setModified (false );
163
182
}
164
183
184
+ #ifndef QT_NO_FILESYSTEMWATCHER
165
185
// Restart file watcher back after saving completes
166
- if (!m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
186
+ if (!m_watcher->files ().contains (m_fileUrl.toLocalFile ()))
167
187
m_watcher->addPath (m_fileUrl.toLocalFile ());
188
+ #endif
168
189
169
190
return success;
170
191
}
171
192
172
- bool DocumentHandler::reloadText () {
193
+ bool DocumentHandler::reloadText ()
194
+ {
173
195
QString filename = m_fileUrl.toLocalFile ();
174
196
QFile file (filename);
175
- if (!file.open (QFile::ReadOnly)) {
197
+ if (!file.open (QFile::ReadOnly)) {
176
198
emit error (file.errorString ());
177
199
return false ;
178
200
}
179
201
QByteArray data = file.readAll ();
180
202
QTextCodec *codec = QTextCodec::codecForUtfText (data, QTextCodec::codecForLocale ());
181
203
setText (codec->toUnicode (data));
182
- if (file.error () == QFileDevice::NoError) {
204
+ if (file.error () == QFileDevice::NoError) {
183
205
file.close ();
184
206
return true ;
185
207
} else {
@@ -189,8 +211,11 @@ bool DocumentHandler::reloadText() {
189
211
}
190
212
}
191
213
192
- void DocumentHandler::fileChanged (const QString &file) {
214
+ void DocumentHandler::fileChanged (const QString &file)
215
+ {
193
216
emit fileChangedOnDisk ();
194
- if (!m_watcher->files ().contains (file))
217
+ #ifndef QT_NO_FILESYSTEMWATCHER
218
+ if (!m_watcher->files ().contains (file))
195
219
m_watcher->addPath (file);
220
+ #endif
196
221
}
0 commit comments