-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcorepad.cpp
More file actions
executable file
·515 lines (450 loc) · 16.4 KB
/
corepad.cpp
File metadata and controls
executable file
·515 lines (450 loc) · 16.4 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
CorePad is document editor app
CorePad 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; version 2
of the License.
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 "corepad.h"
#include "ui_corepad.h"
corepad::corepad(QWidget *parent) : QWidget(parent), ui(new Ui::corepad)
{
ui->setupUi(this);
// set stylesheet from style.qrc
setStyleSheet(CPrime::ThemeFunc::getStyleSheetFileContent(CPrime::StyleTypeName::CorePadStyle));
// set window size
int x = static_cast<int>(CPrime::InfoFunc::screenSize().width() * .8);
int y = static_cast<int>(CPrime::InfoFunc::screenSize().height() * .7);
this->resize(x, y);
ui->searchBar->setVisible(false);
ui->fontSize->setVisible(0);
shotcuts();
}
corepad::~corepad()
{
delete ui;
}
void corepad::shotcuts()
{
QShortcut* shortcut;
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_N), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cNew_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cOpen_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cSave_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_S), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cSaveAs_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Z), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cUndo_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Y), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cRedo_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_C), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cCopy_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_X), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cCut_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_V), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_cPaste_clicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(shortcut, &QShortcut::activated, this, &corepad::quitClicked);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F), this);
connect(shortcut, &QShortcut::activated, this, &corepad::on_search_clicked);
}
void corepad::openText(const QString &filePath)
{
initializeNewTab(filePath);
}
bool corepad::initializeNewTab(const QString &filePath)
{
int tabsCount = ui->notes->count();
if (tabsCount < 4) {
text = new coreedit(filePath);
QString tabLabel;
bool isUpdate = 0, isSave = 0;
if (filePath.isEmpty()) {
workFilePath = "";
tabLabel = tr("untitled%1.txt").arg(tabsCount);
} else {
QFile file(filePath);
if (!file.open(QIODevice::Text | QIODevice::ReadOnly) && !file.exists())
return false; //Return an exception - There is no file with that name.
//return false; //Return an exception - It's not possible for me to open that file.
else {
workFilePath = filePath;
tabLabel = QFileInfo(filePath).fileName();
QTextStream in(&file);
text->setPlainText(in.readAll());
file.close();
isSave = 1;
}
}
tabInfo.append(tr("%1\t%2\t%3\t%4").arg(tabsCount).arg(isSave).arg(isUpdate).arg(workFilePath));
started = true;
text->setFont(QFont(text->font().family(), ui->fontSize->value()));
text->lineNumberArea_()->setFont(QFont(text->lineNumberArea_()->font().family(), ui->fontSize->value()));
ui->notes->insertTab(tabsCount, text, tabLabel);
ui->notes->setCurrentIndex(tabsCount);
connect(text, &coreedit::copyAvailable, this, &corepad::textCopyAvailable);
connect(text, &coreedit::undoAvailable, this, &corepad::textUndoAvailable);
connect(text, &coreedit::redoAvailable, this, &corepad::textRedoAvailable);
connect(text, &coreedit::textChanged, this, &corepad::textTextChanged);
return true;
} else {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Reached page limit.\nClose some tab.", CPrime::MessageType::Warning,this);
}
return false; //Return an exception - There is some other causes happening.
}
int corepad::tabsCount()
{
return ui->notes->count();
}
bool corepad::saveTo(const QString &filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::Truncate | QIODevice::Text | QIODevice::WriteOnly)) {
return false;
} else {
file.write(text->toPlainText().toLocal8Bit());
file.close();
return true;
}
}
void corepad::findS(QString searchS, bool reverse, QTextDocument::FindFlags flag)
{
if (!searchS.isNull()) {
QTextCursor cr;
text->find(searchS, flag);
cr.movePosition(reverse ? QTextCursor::End : QTextCursor::Start);
if (!(cr.position() < 0)) {
text->setTextCursor(cr);
}
}
}
bool corepad::closeTab(int index)
{
ui->notes->setCurrentIndex(index);
bool checkIsUpdate = isCurrentUpdated(index).toInt();
bool checkIsSaved = isCurrentSaved(index).toInt();
if ((checkIsUpdate && checkIsSaved) || (checkIsUpdate && !checkIsSaved)) {
QString msg = QString("This file contains unsaved changes.\nHow would you like to proceed?");
QMessageBox message(QMessageBox::Question, QString("Save Changes - \"%1\"").arg(currentFilePath(index)), msg,
QMessageBox::Cancel | QMessageBox::Discard | QMessageBox::Save, this);
message.setWindowIcon(QIcon(":/icons/corepad.svg"));
message.setStyleSheet(CPrime::ThemeFunc::getStyleSheetFileContent(CPrime::StyleTypeName::DialogStyle));
int reply = message.exec();
if (reply == QMessageBox::Save) {
bool closed = on_cSave_clicked();
if (closed) {
// Function from utilities.cpp
CPrime::InfoFunc::saveToRecent("CorePad", currentFilePath(index));
goto CLOSE_THE_TAB;
} else {
return false;
}
} else if (reply == QMessageBox::Discard) {
if (checkIsSaved)
// Function from utilities.cpp
CPrime::InfoFunc::saveToRecent("CorePad", currentFilePath(index));
goto CLOSE_THE_TAB;
} else {
return false;
}
} else if ((!checkIsUpdate && checkIsSaved) || (!checkIsUpdate && !checkIsSaved)) {
// Function from utilities.cpp
CPrime::InfoFunc::saveToRecent("CorePad", currentFilePath(index));
CLOSE_THE_TAB:
ui->notes->widget(index)->deleteLater();
ui->notes->removeTab(index);
tabInfo.removeAt(index);
return true;
}
return false;
}
//Accessing information through tab information
QString corepad::currentFilePath(int index)
{
for (int i = 0; i < tabInfo.count(); i++) {
if (tabInfo.at(i).startsWith(QString::number(index)))
return tabInfo.at(i).split("\t").at(3);
}
return "";
}
QString corepad::isCurrentSaved(int index)
{
for (int i = 0; i < tabInfo.count(); i++) {
if (tabInfo.at(i).startsWith(QString::number(index)))
return tabInfo.at(i).split("\t").at(1);
}
return "";
}
QString corepad::isCurrentUpdated(int index)
{
for (int i = 0; i < tabInfo.count(); i++) {
if (tabInfo.at(i).startsWith(QString::number(index)))
return tabInfo.at(i).split("\t").at(2);
}
return "";
}
//=============================================
//Saving information at tab information
bool corepad::setCurrent(int index, int isSaved, int isUpdated, const QString &filePath)
{
tabInfo.removeAt(index);
tabInfo.insert(index, QString("%1\t%2\t%3\t%4").arg(index).arg(isSaved).arg(isUpdated).arg(filePath));
return true;
}
//===============================================
void corepad::reIndex() {
int count = tabInfo.count();
for (int i = 0; i < count; i++) {
tabInfo.replace(i, QString(tabInfo.at(i)).replace(0, 1, QString::number(i)));
}
}
void corepad::textCopyAvailable(bool b)
{
ui->cCopy->setEnabled(b);
ui->cCut->setEnabled(b);
}
void corepad::textUndoAvailable(bool b)
{
ui->cUndo->setEnabled(b);
}
void corepad::textRedoAvailable(bool b)
{
ui->cRedo->setEnabled(b);
}
void corepad::textTextChanged()
{
if (started) {
started = false;
return;
}
int index = ui->notes->currentIndex();
if (text->toPlainText().count() > 0) {
if (isCurrentUpdated(index) == "0" && !ui->notes->tabText(index).startsWith("*")) {
ui->notes->setTabText(ui->notes->currentIndex(), "*" + ui->notes->tabText(ui->notes->currentIndex()));
setCurrent(index, isCurrentSaved(index).toInt(), 1, currentFilePath(index));
}
}
}
void corepad::on_notes_currentChanged(int index)
{
if(ui->notes->count() > 0){
text = static_cast<coreedit*>(ui->notes->widget(index));
started = 1;
//FIX ME:
/*Copy available need to fix when the current tab changed*/
textRedoAvailable(text->isUndoRedoEnabled());
textUndoAvailable(text->isUndoRedoEnabled());
textTextChanged();
on_searchHere_textChanged(ui->searchHere->text());
//
}
}
void corepad::on_notes_tabCloseRequested(int index)
{
ui->notes->setCurrentIndex(index);
reIndex();
closeTab(index);
reIndex();
}
void corepad::on_cOpen_clicked()
{
workFilePath = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath() + "/Documents", tr("Text Files (*.*)"));
if (workFilePath.isEmpty()) {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Empty File Name", CPrime::MessageType::Warning,this);
return;
}
else {
bool open = initializeNewTab(workFilePath);
if (open) CPrime::InfoFunc::messageEngine("File Opened", CPrime::MessageType::Info,this);
else CPrime::InfoFunc::messageEngine("Can't open file", CPrime::MessageType::Warning,this);
}
}
void corepad::on_cNew_clicked()
{
initializeNewTab("");
}
bool corepad::on_cSave_clicked()
{
workFilePath = currentFilePath(ui->notes->currentIndex());
int index = ui->notes->currentIndex();
bool saved = isCurrentSaved(index).toInt(), updated = isCurrentUpdated(index).toInt();
if (!saved && updated) {
workFilePath = QFileDialog::getSaveFileName(this, tr("Save File"), ui->notes->tabText(ui->notes->currentIndex()).remove(0, 1) ,tr("Text Files (*.*)"));
if (workFilePath.isEmpty()) {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Empty File Name", CPrime::MessageType::Warning,this);
return false;
} else {
goto SAVE_FILE;
}
}
else if (saved && updated) {
SAVE_FILE:
bool saveto = saveTo(workFilePath);
if (saveto) {
setCurrent(index, 1, 0, workFilePath);
ui->notes->setTabText(index, QFileInfo(workFilePath).fileName());
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("File Saved", CPrime::MessageType::Info,this);
return true;
} else {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Can't open file", CPrime::MessageType::Warning,this);
}
}
return false;
}
void corepad::on_cSaveAs_clicked()
{
workFilePath = currentFilePath(ui->notes->currentIndex());
QString file = "untitled.txt";
if (!workFilePath.isNull()) {
file = workFilePath;
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File As"),
file,
tr("Text Files (*.*)"));
int index = ui->notes->currentIndex();
if (fileName.isEmpty()) {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Empty File Name", CPrime::MessageType::Warning,this);
return;
}
else {
bool saveto = saveTo(workFilePath);
if (saveto) {
ui->notes->setTabText(index, QFileInfo(fileName).fileName());
setCurrent(index, 1, 0, fileName);
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("File Saved", CPrime::MessageType::Info,this);
} else {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("Can't open file", CPrime::MessageType::Warning,this);
return;
}
}
workFilePath = fileName;
}
void corepad::on_cCopy_clicked()
{
text->copy();
}
void corepad::on_cPaste_clicked()
{
text->paste();
}
void corepad::on_cCut_clicked()
{
text->cut();
}
void corepad::on_cUndo_clicked()
{
text->undo();
}
void corepad::on_cRedo_clicked()
{
text->redo();
}
void corepad::on_addDate_clicked()
{
int pos = text->textCursor().position();
text->setPlainText(text->toPlainText().insert(text->textCursor().position(), QDate::currentDate().toString("dd/MM/yyyy")));
text->textCursor().setPosition(pos + 10);
}
void corepad::on_bookMarkIt_clicked()
{
if (!currentFilePath(ui->notes->currentIndex()).isNull()) {
if (!QFileInfo(workFilePath).exists()) {
// Function from utilities.cpp
CPrime::InfoFunc::messageEngine("File Not saved.", CPrime::MessageType::Warning,this);
return;
}
bookmarkDialog bk;
bk.callBookMarkDialog(this,workFilePath);
}
}
void corepad::on_searchHere_textChanged(const QString &arg1)
{
findS(arg1, false);
}
void corepad::on_nextW_clicked()
{
findS(ui->searchHere->text(), false);
}
void corepad::on_previousW_clicked()
{
findS(ui->searchHere->text(), true, QTextDocument::FindBackward);
}
void corepad::quitClicked()
{
int tabCount = ui->notes->count();
int cc = 0;
for (int i = 0; i < tabCount; i++) {
if (closeTab(i - cc)) {
reIndex();
cc++;
}
}
if (ui->notes->count() == 0) {
if (this->close()) {
deleteLater();
}
}
else CPrime::InfoFunc::messageEngine("There are still some changes needs to be saved.", CPrime::MessageType::Warning,this);
}
void corepad::closeEvent(QCloseEvent *event)
{
event->ignore();
int tabCount = ui->notes->count();
int cc = 0;
for (int i = 0; i < tabCount; i++) {
if (closeTab(i - cc)) {
reIndex();
cc++;
}
}
if (ui->notes->count() == 0) {
event->accept();
}
else event->ignore();
}
void corepad::on_fontShow_clicked()
{
if(!ui->fontSize->isVisible()){
ui->fontSize->setVisible(1);
}else{
ui->fontSize->setVisible(0);
}
}
void corepad::on_fontSize_valueChanged(int arg1)
{
text->setFont(QFont(text->font().family(), arg1));
text->lineNumberArea_()->setFont(QFont(text->lineNumberArea_()->font().family(), arg1));
}
void corepad::on_search_clicked()
{
if (ui->searchBar->isVisible()) {
ui->searchBar->setVisible(false);
ui->searchHere->clear();
} else {
ui->searchBar->setVisible(true);
ui->searchHere->setFocus();
}
}
void corepad::sendFiles(const QStringList &paths)
{
if (paths.count()) {
foreach (QString str, paths) {
openText(CPrime::ValidityFunc::checkIsValidFile(str));
}
}
}