forked from n9dyfi/TextEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexteditor.cpp
285 lines (259 loc) · 9 KB
/
texteditor.cpp
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
#include "texteditor.h"
#include "recentfiles.h"
//#include <QDebug>
// QT_TRANSLATE_NOOP is required to translate static const strings outside any context
const char *TextEditor::UNTITLED = QT_TRANSLATE_NOOP("TextEditor","Untitled");
TextEditor::TextEditor(QObject *qml, RecentFiles *recentfiles, QObject *parent) :
QObject(parent)
{
currentFolder = "file:///home/user";
newFolder = currentFolder;
//currentFolder = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
//qDebug() << currentFolder;
currentFile = tr(UNTITLED);
currentContent = "";
recentFiles = recentfiles;
// connect QML signals to TextEditor slots
connect(qml, SIGNAL(menuOpenClicked(QString)),
this, SLOT(menuOpenClicked(QString)));
connect(qml, SIGNAL(toolSaveClicked(QString)),
this, SLOT(toolSaveClicked(QString)));
connect(qml, SIGNAL(toolRecentClicked()),
this, SLOT(toolRecentClicked()));
connect(qml, SIGNAL(menuSaveAsClicked()),
this, SLOT(menuSaveAsClicked()));
connect(qml, SIGNAL(saveAsRequested(QString,QString)),
this, SLOT(saveAsRequested(QString,QString)));
connect(qml, SIGNAL(newFolderChanged(QString)),
this, SLOT(newFolderChanged(QString)));
connect(qml, SIGNAL(fileOpenRequested(QString)),
this, SLOT(fileOpenRequested(QString)));
connect(qml, SIGNAL(saveAsConfirmed(QString)),
this, SLOT(saveAsConfirmed(QString)));
connect(qml, SIGNAL(newOrOpenConfirmed(QString)),
this, SLOT(newOrOpenConfirmed(QString)));
connect(qml, SIGNAL(openRecentConfirmed()),
this, SLOT(openRecentConfirmed()));
connect(qml, SIGNAL(menuQuitClicked(QString)),
this, SLOT(menuQuitClicked(QString)));
connect(qml, SIGNAL(saveBeforeClosed(QString)),
this, SLOT(saveBeforeClosed(QString)));
connect(qml, SIGNAL(menuNewClicked(QString)),
this, SLOT(menuNewClicked(QString)));
connect(qml, SIGNAL(recentFileClicked(QString,QString,QString)),
this, SLOT(recentFileClicked(QString,QString,QString)));
// connect TextEditor signals to QML signals
connect(this, SIGNAL(browseRequested(QString,bool)),
qml, SIGNAL(browseRequested(QString,bool)));
connect(this, SIGNAL(recentRequested()),
qml, SIGNAL(recentRequested()));
connect(this, SIGNAL(openCompleted(QString,QString,QString)),
qml, SIGNAL(openCompleted(QString,QString,QString)));
connect(this, SIGNAL(openFailed(QString,QString)),
qml, SIGNAL(openFailed(QString,QString)));
connect(this, SIGNAL(saveCompleted()),
qml, SIGNAL(saveCompleted()));
connect(this, SIGNAL(saveFailed(QString,QString)),
qml, SIGNAL(saveFailed(QString,QString)));
connect(this, SIGNAL(saveAsCompleted(QString,QString)),
qml, SIGNAL(saveAsCompleted(QString,QString)));
connect(this, SIGNAL(saveAsToBeConfirmed(QString)),
qml, SIGNAL(saveAsToBeConfirmed(QString)));
connect(this, SIGNAL(newOrOpenToBeConfirmed(QString,QString)),
qml, SIGNAL(newOrOpenToBeConfirmed(QString,QString)));
connect(this, SIGNAL(openRecentToBeConfirmed(QString)),
qml, SIGNAL(openRecentToBeConfirmed(QString)));
connect(this, SIGNAL(appCloseToBeConfirmed(QString)),
qml, SIGNAL(appCloseToBeConfirmed(QString)));
connect(this, SIGNAL(appToBeClosed()),
qml, SIGNAL(appToBeClosed()));
connect(this, SIGNAL(editorCleared(QString,QString)),
qml, SIGNAL(editorCleared(QString,QString)));
}
// QML menu: Open was clicked
void TextEditor::menuOpenClicked(QString content)
{
if(content!=currentContent)
{
emit newOrOpenToBeConfirmed("open",currentFile);
return;
}
bool saveRequested = false;
emit browseRequested(currentFolder, saveRequested);
}
// QML Toolbar>New clicked
void TextEditor::menuNewClicked(QString content)
{
if(content!=currentContent)
{
emit newOrOpenToBeConfirmed("new",currentFile);
return;
}
newOrOpenConfirmed("new");
}
// QML menu: Save was clicked
void TextEditor::toolSaveClicked(QString content)
{
if(currentFile==tr(UNTITLED))
{
menuSaveAsClicked();
return;
}
currentContent = content;
newFolder = currentFolder;
newFile = currentFile;
saveCurrentContent(SAVE);
}
// EditPage toolbar: Recent Files was clicked
void TextEditor::toolRecentClicked()
{
bool statusOk = recentFiles->readRecentFiles();
if (statusOk)
emit recentRequested();
}
// QML menu: Save As was clicked
void TextEditor::menuSaveAsClicked()
{
bool saveRequested = true;
emit browseRequested(currentFolder, saveRequested);
}
// Overwrite during Menu>Save As was confirmed.
void TextEditor::saveAsConfirmed(QString content)
{
currentContent = content;
saveCurrentContent(SAVE_AS);
}
// Discard changes and proceed with "New" or "Open"
void TextEditor::newOrOpenConfirmed(QString op)
{
if(op=="new")
{
currentFile = tr(UNTITLED);
currentContent = "";
emit editorCleared(currentFolder,currentFile);
} else if(op=="open") {
bool saveRequested = false;
emit browseRequested(currentFolder, saveRequested);
}
}
// Discard changes and proceed with "Open recent"
void TextEditor::openRecentConfirmed()
{
fileOpenRequested(newFile);
}
// Menu>Quit was selected.
void TextEditor::menuQuitClicked(QString content)
{
// Check if the content was changed.
if(content!=currentContent)
{
emit appCloseToBeConfirmed(currentFile);
return;
}
// Not changed -> ok to quit.
emit appToBeClosed();
}
// Saving changed content before quitting was confirmed.
void TextEditor::saveBeforeClosed(QString content)
{
// Did we have a valid file name?
if(currentFile==tr(UNTITLED))
{
menuSaveAsClicked();
return;
}
currentContent = content;
newFolder = currentFolder;
newFile = currentFile;
saveCurrentContent(SAVE);
}
void TextEditor::recentFileClicked(QString fileName, QString folderName, QString content)
{
newFolder = folderName;
newFile = fileName;
// Check if the content was changed.
if(content!=currentContent)
{
emit openRecentToBeConfirmed(currentFile);
return;
}
// Not changed -> ok to open.
fileOpenRequested(newFile);
}
// Save the current editor content to the current folder/file.
void TextEditor::saveCurrentContent(int saveMode)
{
bool fileIsWritable;
//qDebug() << "DBG> save"<<currentFile<<"to currentFolder:"<<currentFolder;
QUrl url(newFolder+"/"+newFile);
QString localFile = url.toLocalFile();
QFile file(localFile);
fileIsWritable = file.open(QIODevice::WriteOnly | QIODevice::Text);
if ( fileIsWritable ) {
QTextStream stream( &file );
stream<<currentContent;
file.close();
currentFolder = newFolder;
currentFile = newFile;
if(saveMode==SAVE)
emit saveCompleted();
else {
recentFiles->readRecentFiles();
recentFiles->addFile(currentFolder+"|"+currentFile);
emit saveAsCompleted(currentFolder,currentFile);
}
} else {
emit saveFailed(newFile,file.errorString());
}
}
// QML listView: file name was selected for saving
void TextEditor::saveAsRequested(QString content, QString fileName)
{
if (fileName.isEmpty())
return;
newFile = fileName;
QUrl url(newFolder+"/"+newFile);
QString localFile = url.toLocalFile();
QFile file(localFile);
if(file.exists())
{
emit saveAsToBeConfirmed(fileName);
} else {
currentContent = content;
saveCurrentContent(SAVE_AS);
}
}
// QML listView: folder was selected
void TextEditor::newFolderChanged(QString path)
{
newFolder = path;
}
// QML listView: file name was selected for reading
void TextEditor::fileOpenRequested(QString fileName)
{
bool fileIsReadable = false;
//qDebug() << "DBG> open"<<fileName<<"from currentFolder:"<<currentFolder;
QUrl url(newFolder+"/"+fileName);
QString localFile = url.toLocalFile();
QFile file(localFile);
fileIsReadable = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (fileIsReadable)
{
QTextStream stream( &file );
currentContent = stream.readAll();
file.close();
// update current folder and file name
currentFolder = newFolder;
currentFile = fileName;
// update the recent files list
recentFiles->readRecentFiles();
recentFiles->addFile(currentFolder+"|"+currentFile);
// pass the content to the TextArea component
emit openCompleted(currentContent,currentFolder,currentFile);
} else {
// remove the entry from the recent files list (if it exists there)
// do not touch the current folder and file names
recentFiles->removeFile(newFolder+"|"+fileName);
emit openFailed(fileName,file.errorString());
}
}