-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdocument.cpp
More file actions
342 lines (282 loc) · 6.94 KB
/
document.cpp
File metadata and controls
342 lines (282 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (C) 2024 Nick Egorrov, [email protected]
*
* This file is part of GCodeWorkShop.
*
* GCodeWorkShop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCloseEvent> // for QCloseEvent
#include <QFile> // for QFile
#include <QFileInfo> // for QFileInfo
#include <QIODevice> // for QIODevice, QIODevice::ReadOnly, QIODevice::WriteOnly
#include <QSize> // for QSize
#include <QSizePolicy> // for QSizePolicy, QSizePolicy::Preferred
#include <Qt> // for ConnectionType, DirectConnection, WheelFocus
#include <QtGlobal> // for Q_UNUSED
#include <QWidget> // for QWidget
#include <document.h> // IWYU pragma: associated
#include <documentinfo.h> // for DocumentInfo
#include "documentwidgetcloseeventfilter.h" // for DocumentWidgetCloseEventFilter
Document::Document(QObject* parent) :
QObject(parent),
m_widget(nullptr),
m_dir(QDir::current())
{
m_isUntitled = true;
m_isModified = false;
m_redoAvailable = false;
m_undoAvailable = false;
}
Document::~Document()
{
fileWatchStop();
}
QDir Document::dir() const
{
return m_dir;
}
void Document::setDir(const QDir& dir)
{
m_dir = dir;
}
QString Document::path() const
{
return m_dir.path();
}
void Document::setPath(const QString& path)
{
m_dir.setPath(path);
}
QString Document::fileName() const
{
return m_fileName;
}
void Document::setFileName(const QString& fileName)
{
m_fileName = fileName;
}
QString Document::filePath() const
{
return m_dir.filePath(m_fileName);
}
void Document::setFilePath(const QString& filePath)
{
QFileInfo fi(filePath);
m_fileName = fi.fileName();
m_dir = fi.dir();
}
DocumentInfo::Ptr Document::documentInfo() const
{
DocumentInfo* info = new DocumentInfo();
info->filePath = filePath();
info->geometry = widget()->parentWidget()->saveGeometry();
info->readOnly = isReadOnly();
return DocumentInfo::Ptr(info);
}
void Document::setDocumentInfo(const DocumentInfo::Ptr& info)
{
if (!info->geometry.isEmpty()) {
widget()->parentWidget()->restoreGeometry(info->geometry);
}
if (!info->filePath.isEmpty()) {
setFilePath(info->filePath);
}
setReadOnly(info->readOnly);
}
QString Document::guessFileName() const
{
return fileName();
}
QString Document::brief() const
{
return m_brief;
}
void Document::setBrief(const QString& brief)
{
if (m_brief != brief) {
m_brief = brief;
emit briefChanged(this);
}
}
QWidget* Document::widget() const
{
return m_widget;
}
void Document::setWidget(QWidget* widget)
{
m_widget = widget;
m_widget->setAttribute(Qt::WA_DeleteOnClose);
m_widget->resize(430, 380);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(m_widget->sizePolicy().hasHeightForWidth());
m_widget->setSizePolicy(sizePolicy);
m_widget->setMinimumSize(QSize(64, 48));
m_widget->setFocusPolicy(Qt::WheelFocus);
m_widget->setAutoFillBackground(true);
DocumentWidgetCloseEventFilter* filter = new DocumentWidgetCloseEventFilter(m_widget);
connect(filter, SIGNAL(closeRequested(QCloseEvent*)), this, SLOT(closeEvent(QCloseEvent*)),
Qt::ConnectionType::DirectConnection);
m_widget->installEventFilter(filter);
}
void Document::setWidgetTitle(const QString& title)
{
widget()->setWindowTitle(title + "[*]");
}
bool Document::close()
{
return widget()->parentWidget()->close();
}
void Document::closeEvent(QCloseEvent* event)
{
event->setAccepted(closeRequested(this));
if (event->isAccepted()) {
emit closed(this);
deleteLater();
}
}
QString Document::ioErrorString() const
{
return m_ioErrorString;
}
bool Document::loadFile(const QString& filePath, bool watch)
{
QFile file(filePath);
m_ioErrorString.clear();
if (!file.open(QIODevice::ReadOnly)) {
m_ioErrorString = file.errorString();
return false;
}
setRawData(file.readAll());
file.close();
if (watch) {
fileWatchStart(filePath);
}
return true;
}
bool Document::saveFile(const QString& filePath)
{
QFile file(filePath);
m_ioErrorString.clear();
// WARNING: If opened in QIODevice::WriteOnly mode, a false positive in QFileSystemWatcher
// occurs after writing and closing. This is correct for Windows 7.
// Update: it is possible that when opening a file with this flag, Windows
// immediately truncates the file size to zero and this happens before
// disabling monitoring in the code below.
if (!file.open(QIODevice::ReadWrite)) {
m_ioErrorString = file.errorString();
return false;
}
fileWatchStop();
file.resize(0);
file.write(rawData());
file.close();
fileWatchStart(filePath);
return true;
}
void Document::customContextMenuRequest(const QPoint& pos)
{
emit customContextMenuRequested(this, pos);
}
bool Document::isUntitled() const
{
return m_isUntitled;
}
void Document::setUntitled(bool untitled)
{
m_isUntitled = untitled;
}
bool Document::isReadOnly() const
{
return true;
}
void Document::setReadOnly(bool readonly)
{
Q_UNUSED(readonly);
}
bool Document::isModified() const
{
return m_isModified;
}
void Document::setModified(bool modified)
{
bool oldMod = m_isModified;
m_isModified = modified;
if (oldMod != m_isModified) {
widget()->setWindowModified(m_isModified);
emit modificationChanged(this, m_isModified);
}
}
void Document::cursorMoved()
{
emit cursorPositionChanged(this);
}
void Document::selectionUpdated()
{
emit selectionChanged(this);
}
void Document::redo()
{
}
void Document::undo()
{
}
void Document::clearUndoRedoStacks()
{
}
bool Document::isRedoAvailable() const
{
return m_redoAvailable;
}
bool Document::isUndoAvailable() const
{
return m_undoAvailable;
}
void Document::setRedoAvailable(bool available)
{
bool oldRedo = m_redoAvailable;
m_redoAvailable = available;
if (oldRedo != m_redoAvailable) {
emit redoAvailable(this, m_redoAvailable);
}
}
void Document::setUndoAvailable(bool available)
{
bool oldUndo = m_undoAvailable;
m_undoAvailable = available;
if (oldUndo != m_undoAvailable) {
emit undoAvailable(this, m_undoAvailable);
}
}
QString Document::watchedFile() const
{
return m_watchedFile;
}
void Document::fileWatchStart(const QString& watchedFile)
{
if (m_watchedFile != watchedFile) {
fileWatchStop();
m_watchedFile = watchedFile;
}
if (!m_watchedFile.isEmpty()) {
emit fileWatchRequested(m_watchedFile, true);
}
}
void Document::fileWatchStop()
{
if (!m_watchedFile.isEmpty()) {
emit fileWatchRequested(m_watchedFile, false);
}
}