This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
1328 lines (1225 loc) · 45.9 KB
/
mainwindow.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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtWidgets>
#include "JoineryTS/aviewwidget.h"
#include "mainwindow.h"
//VIEW INCLUDES
#include "JoineryTS/statsview.h"
#include "JoineryTS/bloat.h"
#include "JoineryTS/elements.h"
#include "JoineryTS/hello.h"
#include "JoineryTS/internut.h"
#include "JoineryTS/jockey.h"
#include "JoineryTS/turing.h"
#include "JoineryTS/finddialog.h"
#include "JoineryTS/utfdialog.h"
//FORMATTING
#define para "</p><p>"
#define bold(X) " <b>" + X + "</b> "
//===================================================
// HELP
//===================================================
void MainWindow::aboutQt() {
qApp->aboutQt();
}
void MainWindow::about() {
QMessageBox::about(this, tr("About QtAp"),
bold(tr("QtAp")) +
tr("for text document control and "
"generation of information views.") +
para +
tr("Wrote in C++ using Qt5 kit and Creator. "
"It requires GIT for sync to work, which in turn depends on SSH.") +
para +
tr("(C) K Ring Technologies Ltd, BSD Licence terms extending a "
"copyrighted work of The Qt Company Ltd.") +
para +
tr("The main work of the application has been to build a framework "
"to run a library of code, which in turn could be using some "
"calls to some other libraries of code released under different "
"terms. Nothing in the code base is currently prohibited from "
"extension under the BSD Licence.") +
para +
tr("Version: ") + QCoreApplication::applicationVersion()
);
}
//===================================================
// MAIN WINDOW MANAGEMENT
//===================================================
void MainWindow::setMain(QWidget *widget, bool input) {
if(input) {
textEdit = (ATextEdit *)widget;
}
if(center->indexOf(widget) < 0) {
center->addWidget(widget);
}
if(getMain() != widget) {
if(widget != settings) {
holdWhileSettings = settings;
settings->writeSettings(settingsStore);
QList<AViewWidget *>::iterator i;//restore
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
settingsStore->beginGroup((*i)->getExtension());
(*i)->readSettings(settingsStore);
settingsStore->endGroup();
}
QList<ATextEdit *>::iterator j;//restore
for(j = listOfInputs.begin(); j != listOfInputs.end(); ++j) {
settingsStore->beginGroup((*j)->getBaseExtension());
(*j)->readSettings(settingsStore);
settingsStore->endGroup();
}
} else {
settings->readSettings(settingsStore);
}
center->setCurrentWidget(widget);
}
//flicker fix by delete all before adding again
QList<QAction *>::iterator j;
for(j = inInputActions.begin(); j != inInputActions.end(); ++j) {
(*j)->setVisible(false);
}
AViewWidget *k = (AViewWidget *)widget;
QMap<AViewWidget *, QList<QAction *>>::iterator i;
for(i = inViewActions.begin(); i != inViewActions.end(); ++i) {
QList<QAction *>::iterator j;
for(j = (*i).begin(); j != (*i).end(); ++j) {
(*j)->setVisible(false);
}
}
for(i = inViewActions.begin(); i != inViewActions.end(); ++i) {
QList<QAction *>::iterator j;
for(j = (*i).begin(); j != (*i).end(); ++j) {
if(k == i.key()) (*j)->setVisible(true);//and for view
}
}
for(j = inInputActions.begin(); j != inInputActions.end(); ++j) {
if(textEdit == widget) (*j)->setVisible(true);//and for input
}
checkSave(isModified());//test save avail?
checkPaste();
checkCopy(lastCopy);
checkCut(lastCut);
checkUndo(lastUndo);
checkRedo(lastRedo);
setNeedsText(isTextMain());
}
QWidget *MainWindow::getMain() {
return center->currentWidget();
}
bool MainWindow::isTextMain() {
return (getMain() == textEdit);
}
bool MainWindow::isSettingsMain() {
return (getMain() == settings);
}
MainWindow::MainWindow()
: textEdit(new ATextEdit(this)) {
QLOAD;
handle = QRESOLVE("handle");
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QString style = styleSheet();
if(style == nullptr) style = QString();
setStyleSheet(style + "\n" + loadStyle());
exitCheck = false;
//QIcon::setThemeName("gnome");//TODO maybe become a setting
QIcon ico = getIconRC("view-text");
setWindowIcon(ico);
tray = new QSystemTrayIcon(this);
tray->setIcon(ico);
tray->setToolTip(QCoreApplication::applicationName());
connect(tray, &QSystemTrayIcon::activated, this, &MainWindow::checkTray);
inViewActions.clear();
inInputActions.clear();
//add in the extra output views
listOfViews.append(new StatsView(this));
listOfViews.append(new Bloat(this));
listOfViews.append(new Elements(this));
listOfViews.append(new Hello(this));
listOfViews.append(new Internut(this));
listOfViews.append(new Jockey(this));
listOfViews.append(new Turing(this));
//create menus
createActions();
createStatusBar();
//input views
setInputCommand(textEdit);
//all actions
fillCommands();
settingsStore = new QSettings(QCoreApplication::organizationName(),
QCoreApplication::applicationName());
settings = new Settings();
holdWhileSettings = settings;
settings->setMainWindow(this);//for link back
center = new QStackedWidget(this);
setCentralWidget(center);
readSettings();
hasRepo();
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
(*i)->_clear();//first new
}
setMain(textEdit);
#ifndef QT_NO_SESSIONMANAGER
QGuiApplication::setFallbackSessionManagementEnabled(false);
connect(qApp, &QGuiApplication::commitDataRequest,
this, &MainWindow::commitData);
#endif
setCurrentFile(QString());
setUnifiedTitleAndToolBarOnMac(true);
connect(textEdit, &QPlainTextEdit::copyAvailable,
this, &MainWindow::checkCopy);
connect(textEdit, &QPlainTextEdit::copyAvailable,
this, &MainWindow::checkCut);
connect(textEdit->document(), &QTextDocument::undoAvailable,
this, &MainWindow::checkUndo);
connect(textEdit->document(), &QTextDocument::redoAvailable,
this, &MainWindow::checkRedo);
connect(textEdit->document(), &QTextDocument::modificationChanged,
this, &MainWindow::checkSave);
connect(QGuiApplication::clipboard(), &QClipboard::dataChanged,
this, &MainWindow::checkPaste);
}
QString MainWindow::loadStyle() {
QFile file(":/style.css");
if(!file.open(QFile::ReadOnly)) return "";
QTextStream in(&file);
return in.readAll();
}
QWidget *MainWindow::getQWebEngineView() {
return QWIDGETPTR(handle->getJSHost());
}
void MainWindow::setCommand(QAction *action, AViewWidget *view) {
inViewActions[view].append(action);
commands->addAction(action);
commandToolBar->addAction(action);
}
void setNewInput(ATextEdit *input, MainWindow *main) {
if(main->maybeSave()) {
if(main->getFilename().isEmpty()) {
main->setMain(input, true);//allow alteration of input widgets
main->setCurrentFile(QString(), true);
} else {
QStringList old = main->editOldBase().split(".");
QString newBase = input->getBaseExtension();
main->setMain(input, true);//allow alteration of input widgets
QStringList name = main->getFilename().split(".");
QStringList::iterator i;
for(i = old.begin(); i != old.end(); ++i) {
name.removeLast();//remove one for each
}
main->setCurrentFile(name.join(".") + "." + newBase, true);//with no clear!!
}
}
}
void MainWindow::setInputCommand(ATextEdit *input) {
if(!hasInitiated) {
hasInitiated = true;
document = input->document();
} else {
QTextDocument *old = input->document();
input->setDocument(document);
delete old;//ensure consistent document
}
const QIcon newIcon = getIconRC(input->getIconName());
QAction *newAct = new QAction(newIcon, input->getInputName(), this);
if(input->getShortcut() > 0) newAct->setShortcut(input->getShortcut());
if(input->getHelpText() != nullptr) newAct->setStatusTip(input->getHelpText());
connect(newAct, &QAction::triggered, this,
[this, input]{ setNewInput(input, this); });
inInputActions.append(newAct);
listOfInputs.append(input);
commands->addAction(newAct);
commandToolBar->addAction(newAct);
}
QString MainWindow::getFilename() {
return curFile;
}
QString MainWindow::editOldBase() {
return textEdit->getBaseExtension();
}
bool MainWindow::fileExtensionOK(QString file) {
bool ok = false;
QList<ATextEdit *>::iterator i;
for(i = listOfInputs.begin(); i != listOfInputs.end(); ++i) {
int j = file.lastIndexOf((*i)->getBaseExtension());
if(j + (*i)->getBaseExtension().length() == file.length()) {
ok = true;
}
}
return ok;
}
void MainWindow::fillCommands() {
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
(*i)->setCommands();
}
commandToolBar->addSeparator();
}
void MainWindow::setInBackground(QString view, QString command) {
backgrounded = true;//sets for auto save and other options
QMap<AViewWidget *, QList<QAction *>>::iterator i;
for(i = inViewActions.begin(); i != inViewActions.end(); ++i) {
if(i.key()->getViewName().toLower().remove("&").replace(" ", "-") == view) {
setMain(i.key());//set view
if(!command.isEmpty()) {
QList<QAction *>::iterator j;
for(j = (*i).begin(); j != (*i).end(); ++j) {
if((*j)->text().toLower().remove("&").replace(" ", "-") == command)
(*j)->trigger();//and for view
}
}
save();
close();
}
}
}
QString MainWindow::getText() {
return textEdit->document()->toPlainText();
}
QWidget *MainWindow::focused(QWidget *top) {
QWidget *f = QApplication::focusWidget();
QList<QWidget *> wl = top->findChildren<QWidget *>();
QList<QWidget *>::iterator i;
for(i = wl.begin(); i != wl.end(); ++i) {
if((*i) == f) {
return f;
}
}
return top;//saves quite a lot of checking
}
void MainWindow::status(QString display) {
statusBar()->showMessage(display, 2000);
}
void MainWindow::setModified() {
textEdit->document()->setModified();
}
bool MainWindow::isModified() {
return textEdit->document()->isModified();
}
QList<AViewWidget *>::iterator MainWindow::begin() {
return listOfViews.begin();
}
QList<AViewWidget *>::iterator MainWindow::end() {
return listOfViews.end();
}
//===================================================
// PROXY ENABLE ACTION CHECKS
//===================================================
void MainWindow::checkPaste() {
bool hasText = false;
if(QGuiApplication::clipboard()->mimeData()->hasText() &&
QGuiApplication::clipboard()->text().length() > 0) {//check paste sensible
hasText = true;
}
if(isTextMain()) {
setPaste(hasText & textEdit->canPaste());//check to see if paste
return;
}
setPaste(hasText & ((AViewWidget *)getMain())->canPaste());
}
void MainWindow::checkCopy(bool active) {
//intecept for selection auto processing
lastCopy = active;
if(isTextMain()) {
setCopy(active);
return;
}
setCopy(((AViewWidget *)getMain())->canCopy());
}
void MainWindow::checkCut(bool active) {
//intecept for selection auto processing
lastCut = active;
if(isTextMain()) {
setCut(active & textEdit->canPaste());//edit check!!
return;
}
setCut(((AViewWidget *)getMain())->canCut());
}
void MainWindow::checkUndo(bool active) {
//intercept to change allowed undo
lastUndo = active;
if(isTextMain()) {
setUndo(active);
return;
}
setUndo(((AViewWidget *)getMain())->canUndo());
}
void MainWindow::checkRedo(bool active) {
//intercept to change allowed redo
lastRedo = active;
if(isTextMain()) {
setRedo(active);
return;
}
setRedo(((AViewWidget *)getMain())->canRedo());
}
void MainWindow::checkSave(bool active) {
checkAvailable(active);//only views of saved
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
active |= (*i)->_needsSave();
}
setSave(active);
setWindowModified(active);
}
void MainWindow::checkTray(QSystemTrayIcon::ActivationReason reason) {
if(reason == QSystemTrayIcon::Trigger) {
setVisible(!isVisible());
}
}
void MainWindow::checkAvailable(bool notSaved) {
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
(*i)->_checkAvailable(false);
}
if(!notSaved) for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
(*i)->_checkAvailable(true);//restore all possible not safe (saved) bets
}
}
void MainWindow::checkEvents() {
QCoreApplication::processEvents();//botch loop
}
//===================================================
// GIT MANAGEMENT
//===================================================
int MainWindow::quietBash(QString proc) {
//NB. NO PROTECTION FROM directory CHANGE
return QProcess::execute("bash", QStringList()
<< "-c"
<< "cd \"" + directory + "\" && " + proc);
}
int MainWindow::bash(QString proc, QString undo) {
prohibits();
QStringList sl = proc.split("&&&");//split notation
QProgressDialog mb(tr("Please wait."),
(undo == nullptr) ? tr("Abort") :
tr("Cancel"), 0, sl.length(), this);
mb.setModal(true);
mb.setValue(0);
int j = 0;
int baulk = 0;
QStringList::iterator i;
for(i = sl.begin(); i != sl.end(); ++i) {
j = quietBash(*i);
mb.setValue(mb.value() + 1);//update
baulk++;
if(j != 0 || mb.wasCanceled()) break;//exit early
}
mb.hide();
if(mb.wasCanceled()) {
if(undo == nullptr) {
QMessageBox::critical(this, tr("Abort Error"),
tr("No undo technique is supplied."));
hasRepo();
return j;
}
//undo processing
QStringList sl2 = undo.split("&&&");//split notation
if(sl.length() != sl2.length()) {//sanity debug steps!!!
QMessageBox::critical(this, tr("Cancel Undo Error"),
tr("No undo is correctly programmed."));
hasRepo();
return j;
}
//1 <= baulk < sl.length()
QProgressDialog mb(tr("Please wait undoing actions."),
tr("Abort Undo"), 0, sl2.length(), this);
mb.setModal(true);
mb.setValue(0);
int j = 0;
int toskip = sl.length() - baulk;
int skipped = 0;
QStringList::iterator k;
for(k = sl2.begin(); k != sl2.end(); ++k) {
if(toskip <= skipped) {//count down number required
j = quietBash(*k);//so order is based on first undo does last
} else {
skipped++;
}
mb.setValue(mb.value() + 1);//update
if(j != 0 || mb.wasCanceled()) break;//exit early
}
mb.hide();
}
hasRepo();
return j;
}
void MainWindow::publish() {
if(maybeSave()) {
QString now = QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd hh:mm:ss");
if(bash(//"git stash push &&&"
//"git pull &&&"
//"git stash pop &&&"
"git commit -a -m \"" + now + "\" &&&"
"git push"
) != 0) {
QMessageBox::warning(this, tr("Cloud Publication Error"),
tr("The repository could not be published. "
"There maybe a complex data merge issue if many users "
"update the same documents. If you did not use SSH to "
"clone and used HTTPS instead, you may not have write "
"and update privilages. Are you a member of the repository "
"commit group? Is there anything to merge?"));
return;
}
status(tr("Published all saved edits"));
//reload();
}
}
void MainWindow::read() {
if(bash("git stash push -a || exit 0 &&&"
"git pull &&&"
"git stash pop || exit 0"
) != 0) { //incorporate
QMessageBox::warning(this, tr("Cloud Reading Error"),
tr("The repository could not be read. "
"There maybe a complex data merge issue if many users "
"update the same documents."));
return;
}
status(tr("Read all updates and restored saved edits"));
reload();
}
void MainWindow::root() {
prohibits();
QFileDialog dialog(this, tr("Set Sync Working Directory"), directory);
dialog.setWindowModality(Qt::WindowModal);
dialog.setFileMode(QFileDialog::DirectoryOnly);
dialog.setNameFilterDetailsVisible(false);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setLabelText(QFileDialog::Accept, tr("Set"));
//dialog.setOption(QFileDialog::DontUseNativeDialog);
if (dialog.exec() != QDialog::Accepted) {
hasRepo();
return;
}
if (!dialog.selectedFiles().first().isEmpty()) {
directory = dialog.selectedFiles().first();
status(tr("Working directory set"));
hasRepo();
}
}
void MainWindow::subscribe() {
bool ok;
QString text = QInputDialog::getText(this, tr("Clone (with SSH to write)"),
tr("GIT repository identifier"), QLineEdit::Normal,
"[email protected]:jackokring/qtap-tests-and-progress.git", &ok);
if (!ok) return;
if(bash("git clone \"" + text + "\" \"" + directory + "\"") != 0) {
QMessageBox::warning(this, tr("Subscription Error"),
tr("The repository could not be subscribed. "
"The working directory may have things in "
"which prevent a clone subscription as that "
"requires the working directory to be empty."));
return;
}
status(tr("Subscription created by cloning repository"));
reload();
}
bool MainWindow::hasRepo() {//and restore prohibits
setDirectory(true);
checkSave(isModified());//test save
if(quietBash("git status") != 0) {
if(quietBash("git --version") != 0) {
//no git
if(!hasGitTestShown) {
QMessageBox::warning(this, tr("Git Availability Error"),
tr("Git is not on your system. Some features will not work."));
hasGitTestShown = true;//only once
return false;
}
}
setClone(true);
setSync(false);
return false;
}
setSync(true);
setClone(false);
return true;
}
void MainWindow::prohibits() {//for bash IO locking
setDirectory(false);
setClone(false);
setSync(false);
setSave(false);//prohibit write
}
//===================================================
// NEW, OPEN AND SAVE ACTIONS
//===================================================
void MainWindow::asNewShow() {
show();
//(this->*fp)();
}
void MainWindow::newFile() {
//asNewShow();
if (maybeSave()) {
textEdit->clear();
setCurrentFile(QString());
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
(*i)->_recycle();
(*i)->_clear();
}
}
}
void MainWindow::open() {
openBoth(false);
}
void MainWindow::openFix() {
openBoth(true);
}
void MainWindow::openBoth(bool fix) {
//asNewShow();
if (maybeSave()) {
QFileDialog dialog(this, tr("Open File"), directory);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
//dialog.setOption(QFileDialog::DontUseNativeDialog);
QStringList kinds;
QString base = textEdit->getBaseExtension();
kinds << textEdit->getBaseTypeDescription() + " (*." +
base + ")";
if(!fix) {
QList<AViewWidget *>::iterator i;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
if((*i)->hasRegenerate()) {
kinds << (*i)->getViewName().remove("&") +
" (*." + base + "." + (*i)->getExtension() + ")";
}
}
}
dialog.setNameFilters(kinds);
if (dialog.exec() != QDialog::Accepted)
return;
//TODO: maybe need to do view transform on type
if (!dialog.selectedFiles().first().isEmpty()) {
QString filter = dialog.selectedNameFilter();
if(filter == kinds.first()) {
loadFile(dialog.selectedFiles().first(), false, fix);
} else {
loadFile(dialog.selectedFiles().first(), true);//regenarate
}
}
setMain(textEdit);//show open view
}
}
void MainWindow::save() {
saved = true;
if (curFile.isEmpty()) {
saveAs();
} else {
saveFile(curFile);
}
}
void MainWindow::saveAs() {
QFileDialog dialog(this, tr("Save File"), directory);
dialog.setWindowModality(Qt::WindowModal);
dialog.setAcceptMode(QFileDialog::AcceptSave);
//dialog.setOption(QFileDialog::DontUseNativeDialog);
QStringList kinds;
kinds << textEdit->getBaseTypeDescription() + " (*." +
textEdit->getBaseExtension() + ")";
dialog.setNameFilters(kinds);
saved = true;
if (dialog.exec() != QDialog::Accepted) {
saved = false;
return;
}
//TODO: maybe need to do view transform on type
saveFile(dialog.selectedFiles().first());
}
void MainWindow::reload() {
if(maybeSave(true)) {//reload message and avoid no change exit
loadFile(curFile);
}
}
//===================================================
// MENU AND ICON UTILITIES
//===================================================
constexpr Spec operator|(Spec X, Spec Y) {
return static_cast<Spec>(
static_cast<unsigned int>(X) | static_cast<unsigned int>(Y));
}
Spec& operator|=(Spec& X, Spec Y) {
X = X | Y; return X;
}
QIcon MainWindow::getIconRC(QString named) {
return QIcon::fromTheme(named, QIcon(":/images/" + named + ".png"));
}
QMenu* MainWindow::addMenu(QString menu, void(MainWindow::*fp)(),
QString named, QString entry,
QKeySequence shorty,
QString help, Spec option, AViewWidget *view) {
static QMenu *aMenu;
static QMenu *trayMenu = new QMenu(this);
static QToolBar *aToolBar;
static int count = 0;
if(menu != nullptr) {
aMenu = menuBar()->addMenu(menu);
if(!(option & noAddBarThisMenu)) {
if(aToolBar != nullptr && !(option & doOwnSpacerPrevious)) {
//add separator at end unless following is not added
//in this case the help menu surpresses the final
//separator as it is a noAddBarThisMenu
aToolBar->addSeparator();
}
aToolBar = addToolBar(menu.remove("&"));
aToolBar->setMovable(false);
}
if(count > 0) trayMenu->addSeparator();
count = 0;
}
if(aMenu == nullptr) {
aMenu = menuBar()->addMenu("&Menu");
aToolBar = addToolBar("Menu");
}
if(named == nullptr) {
named = QString("void");//icon name
option |= noBar;//as sensible
}
if(fp == nullptr) {// a blank with not pointer
commands = aMenu;
commandToolBar = aToolBar;
return aMenu;//so don't even add it
}
const QIcon newIcon = getIconRC(named);
if(entry == nullptr) entry = ">>Blank entry<<";
QAction *newAct = new QAction(newIcon, entry, this);
if(shorty > 0) newAct->setShortcut(shorty);
if(help != nullptr) newAct->setStatusTip(help);
aMenu->addAction(newAct);
if(option & inTray) {
tray->setContextMenu(trayMenu);
tray->setVisible(true);
trayMenu->addAction(newAct);
count++;
//tray show on selection
connect(newAct, &QAction::triggered, this, &MainWindow::asNewShow);
}
//must be done after show. maybe bad but is automatic for menu build.
if(fp != nullptr) {
connect(newAct, &QAction::triggered, this, fp);
}
if(view != nullptr) {
connect(newAct, &QAction::triggered, view, &AViewWidget::selectView);
//newAct->setEnabled(false);
connect(view, &AViewWidget::setAvailable, newAct, &QAction::setEnabled);
}
if(option & canCopy) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setCopy, newAct, &QAction::setEnabled);
}
if(option & canCut) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setCut, newAct, &QAction::setEnabled);
}
if(option & canUndo) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setUndo, newAct, &QAction::setEnabled);
}
if(option & canRedo) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setRedo, newAct, &QAction::setEnabled);
}
if(option & canPaste) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setPaste, newAct, &QAction::setEnabled);
}
if(option & canSave) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setSave, newAct, &QAction::setEnabled);
}
if(option & canClone) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setClone, newAct, &QAction::setEnabled);
}
if(option & canSync) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setSync, newAct, &QAction::setEnabled);
}
if(option & canSync) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setDirectory, newAct, &QAction::setEnabled);
}
if(option & auxNeedsText) {
//newAct->setEnabled(false);
connect(this, &MainWindow::setNeedsText, newAct, &QAction::setEnabled);
}
//more options
if(option & noBar) return aMenu;
if(option & afterBarSpace) {
aToolBar->addSeparator();
}
aToolBar->addAction(newAct);
return aMenu;
}
QMenu* MainWindow::addViewMenu(Spec option) {
QList<AViewWidget *>::iterator i;
QMenu *menu;
for(i = listOfViews.begin(); i != listOfViews.end(); ++i) {
menu = addMenu(nullptr, &MainWindow::viewText, //does default before show
(*i)->getIconName(), (*i)->getViewName(),
(*i)->getShortCut(), (*i)->getToolTipHelp(), option, *i);
(*i)->setMainWindow(this);
}
return menu;
}
//===================================================
// BASIC PROXY ACTIONS
//===================================================
void MainWindow::closeEvent(QCloseEvent *event) {
if(event == nullptr) QApplication::exit();//straight out end
if(!exitCheck && QSystemTrayIcon::isSystemTrayAvailable()) {
hide();
event->ignore();//to tray
} else {
if (maybeSave()) {
writeSettings();
event->accept();
} else {
event->ignore();
exitCheck = false;
}
}
}
void MainWindow::close() {
exitCheck = true;
QWidget::close();
}
void MainWindow::undo() {
if(isTextMain()) {
textEdit->undo();
return;
}
((AViewWidget *)getMain())->undo();
}
void MainWindow::redo() {
if(isTextMain()) {
textEdit->redo();
return;
}
((AViewWidget *)getMain())->redo();
}
void MainWindow::cut() {
if(isTextMain()) {
textEdit->cut();
return;
}
((AViewWidget *)getMain())->cut();
}
void MainWindow::copy() {
if(isTextMain()) {
textEdit->copy();
return;
}
((AViewWidget *)getMain())->copy();
}
void MainWindow::paste() {
if(isTextMain()) {
textEdit->paste();
return;
}
((AViewWidget *)getMain())->paste();
}
void MainWindow::find() {
FindDialog find(this);
find.setModal(true);
find.setText(textEdit);
if (find.exec() == QDialog::Accepted) {
return;
}
}
//===================================================
// VIEW MANAGEMENT
//===================================================
void MainWindow::viewText() {
setMain(textEdit);
}
void MainWindow::viewSettings() {
if(holdWhileSettings == settings) {
holdWhileSettings = getMain();//for restore
setMain(settings);
} else {
setMain(holdWhileSettings);
}
}
void MainWindow::font() {
bool ok;
QFont f = QFontDialog::getFont(&ok, textEdit->font());
textEdit->setFont(f);
}
//===================================================
// MENU AND STATUS BAR CREATION
//===================================================
void MainWindow::createActions() {
addMenu(tr("&File"), &MainWindow::newFile,
"document-new", tr("&New"), QKeySequence::New,//N
tr("Create a new file"), inTray);
addMenu(nullptr, &MainWindow::open,
"document-open", tr("&Open..."), QKeySequence::Open,//O
tr("Open an existing file"), inTray);
addMenu(nullptr, &MainWindow::openFix,
"document-open", tr("UTF &Fix Open..."), QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O),//+O
tr("Open and fix the UTF-8 encoding of an existing file"), noBar);
addMenu(nullptr, &MainWindow::save,
"document-save", tr("&Save"), QKeySequence::Save,//S
tr("Save the document to disk"), canSave);