Repository navigation
Expand file tree
/
Copy pathtreemisc.cpp
More file actions
1356 lines (1171 loc) · 47.8 KB
/
Copy pathtreemisc.cpp
File metadata and controls
1356 lines (1171 loc) · 47.8 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
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
/* Treecle
*
* Copyright (c) Kartik Patel
* E-mail: letapk@gmail.com
* Download from: https://letapk.wordpress.com
*
* This program 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.
*
*/
//Last modified Oct 23, 2026
#include "treecle.h"
#include <QLockFile>
#include <QCryptographicHash>
#include <QSaveFile>
#include <QFileInfo>
#include <QEvent>
#include <QKeyEvent>
#include <QRegularExpression>
#include <QImage>
#include <QImageReader>
#include <QTextBlock>
#include <QTextDocumentFragment>
#include <QTextFragment>
#include <QToolButton>
#include <QApplication>
#include <QFile>
#include <QMetaProperty>
#include <utility>
//a sidecar line is a bare image filename; anything longer is corrupt/hostile
//and must not be allocated in full by readLine()
static const qint64 MAX_SIDECAR_LINE = 4096;
//scanning a huge .bak for <img> tags would allocate its whole content; above
//this size the backup is treated as opaque and orphan cleanup keeps every
//owned image (never delete one a restorable backup might still show)
static const qint64 MAX_BACKUP_SCAN = 64 * 1024 * 1024;
//Much of this is shamelessly copied and hacked from the examples within the Qt software
void MainWindow::setup_menu_and_toolbar ()
{
//file menu
filemenu = menuBar()->addMenu(tr("&File"));
QIcon newIcon = QIcon::fromTheme("", QIcon(":/images/filenew.png"));
QAction *newfile = new QAction(tr("&New file"), this);
filemenu->addAction(newfile);
newfile->setIcon(newIcon);
newfile->setShortcut(QKeySequence::New);
connect(newfile, &QAction::triggered, this, &MainWindow::new_file);
QIcon openIcon = QIcon::fromTheme("", QIcon(":/images/fileopen.png"));
QAction *openfile = new QAction(tr("&Open file"), this);
filemenu->addAction(openfile);
openfile->setIcon(openIcon);
openfile->setShortcut(QKeySequence::Open);
connect(openfile, &QAction::triggered, this, &MainWindow::open_file);
QIcon saveIcon = QIcon::fromTheme("", QIcon(":/images/filesave.png"));
QAction *savefile = new QAction(tr("&Save file"), this);
filemenu->addAction(savefile);
savefile->setIcon(saveIcon);
savefile->setShortcut(QKeySequence::Save);
connect(savefile, &QAction::triggered, this, &MainWindow::save_file);
QIcon saveIconas = QIcon::fromTheme("", QIcon(":/images/filesaveas.png"));
QAction *savefileas = new QAction(tr("&Save file as..."), this);
filemenu->addAction(savefileas);
savefileas->setIcon(saveIconas);
savefileas->setShortcut(QKeySequence::SaveAs);
connect(savefileas, &QAction::triggered, this, &MainWindow::save_file_as);
filemenu->addSeparator();
QAction *quit = new QAction(tr("E&xit"), this);
filemenu->addAction(quit);
quit->setShortcut(QKeySequence::Quit);
connect(quit, &QAction::triggered, this, &MainWindow::quit);
//tree menu
treemenu = menuBar()->addMenu(tr("&Tree"));
QIcon addbrIcon = QIcon::fromTheme("", QIcon( ":/images/add-branch.png"));
QAction *addbranch = new QAction(tr("&New Branch"), this);
treemenu->addAction(addbranch);
addbranch->setIcon(addbrIcon);
connect(addbranch, &QAction::triggered, this, &MainWindow::tree_addbranch);
QIcon addsubbrIcon = QIcon::fromTheme("", QIcon(":/images/add-subbranch.png"));
QAction *addsubbranch = new QAction(tr("New &Sub-branch"), this);
treemenu->addAction(addsubbranch);
addsubbranch->setIcon(addsubbrIcon);
connect(addsubbranch, &QAction::triggered, this, &MainWindow::tree_addsubbranch);
treemenu->addSeparator();
QIcon cutIcon = QIcon::fromTheme("", QIcon(":/images/editcut.png"));
QAction *cutbranch = new QAction(tr("Cu&t branch"), this);
treemenu->addAction(cutbranch);
cutbranch->setIcon(cutIcon);
connect(cutbranch, &QAction::triggered, this, &MainWindow::tree_cutbranch);
QIcon copyIcon = QIcon::fromTheme("", QIcon(":/images/editcopy.png"));
QAction *copybranch = new QAction(tr("&Copy branch"), this);
treemenu->addAction(copybranch);
copybranch->setIcon(copyIcon);
connect(copybranch, &QAction::triggered, this, &MainWindow::tree_copybranch);
QIcon pasteIcon = QIcon::fromTheme("", QIcon(":/images/editpaste.png"));
QAction *pastebranch = new QAction(tr("&Paste branch"), this);
treemenu->addAction(pastebranch);
pastebranch->setIcon(pasteIcon);
connect(pastebranch, &QAction::triggered, this, &MainWindow::tree_pastebranch);
treemenu->addSeparator();
QIcon expandIcon = QIcon::fromTheme("", QIcon(":/images/expandtree.png"));
QAction *expand = new QAction(tr("&Expand all branches"), this);
treemenu->addAction(expand);
expand->setIcon(expandIcon);
connect(expand, &QAction::triggered, this, &MainWindow::expand_tree);
QIcon collapseIcon = QIcon::fromTheme("", QIcon(":/images/collapsetree.png"));
QAction *collapse = new QAction(tr("Co&llapse all branches"), this);
treemenu->addAction(collapse);
collapse->setIcon(collapseIcon);
connect(collapse, &QAction::triggered, this, &MainWindow::collapse_tree);
treemenu->addSeparator();
QIcon sortAscIcon = QIcon::fromTheme("", QIcon(":/images/view-sort-ascending.png"));
QAction *sortAsc = new QAction(tr("&Sort tree in ascending order"), this);
treemenu->addAction(sortAsc);
sortAsc->setIcon(sortAscIcon);
connect(sortAsc, &QAction::triggered, this, &MainWindow::sort_asc_tree);
QIcon sortDescIcon = QIcon::fromTheme("", QIcon(":/images/view-sort-descending.png"));
QAction *sortDesc = new QAction(tr("&Sort tree in descending order"), this);
treemenu->addAction(sortDesc);
sortDesc->setIcon(sortDescIcon);
connect(sortDesc, &QAction::triggered, this, &MainWindow::sort_desc_tree);
treemenu->addSeparator();
QIcon delIcon = QIcon::fromTheme("", QIcon(":/images/editdelete.png"));
QAction *delbranch = new QAction(tr("&Delete branch"), this);
treemenu->addAction(delbranch);
delbranch->setIcon(delIcon);
connect(delbranch, &QAction::triggered, this, &MainWindow::tree_delbranch);
//help menu
helpmenu = menuBar()->addMenu(tr("&Help"));
QAction *helpitem = new QAction(tr("&Help"), this);
helpmenu->addAction(helpitem);
connect(helpitem, &QAction::triggered, this, &MainWindow::help);
QAction *aboutitem = new QAction(tr("&About"), this);
helpmenu->addAction(aboutitem);
connect(aboutitem, &QAction::triggered, this, &MainWindow::about);
helpmenu->addSeparator();
QAction *aboutQtitem = new QAction(tr("About &Qt"), this);
helpmenu->addAction(aboutQtitem);
connect(aboutQtitem, &QAction::triggered, qApp, &QApplication::aboutQt);
//toolbar
tb1 = new QToolBar(tr("Tree tools"), this);
tb1->setFloatable (false);
tb1->setMovable(false);
addToolBar(tb1);
//keep the icon-heavy toolbars on separate rows so nothing overflows the window
addToolBarBreak();
tb2 = new QToolBar(tr("Editor tools"), this);
tb2->setFloatable (false);
tb2->setMovable(false);
addToolBar(tb2);
actionTextBold = new QAction(QIcon::fromTheme("", QIcon(":/images/textbold.png")), tr("&Bold"), this);
actionTextBold->setShortcut(Qt::CTRL | Qt::Key_B);
actionTextBold->setPriority(QAction::LowPriority);
QFont bold;
bold.setBold(true);
actionTextBold->setFont(bold);
connect(actionTextBold, &QAction::triggered, this, &MainWindow::textBold);
actionTextBold->setCheckable(true);
actionTextItalic = new QAction(QIcon::fromTheme("", QIcon(":/images/textitalic.png")), tr("&Italic"), this);
actionTextItalic->setPriority(QAction::LowPriority);
actionTextItalic->setShortcut(Qt::CTRL | Qt::Key_I);
QFont italic;
italic.setItalic(true);
actionTextItalic->setFont(italic);
connect(actionTextItalic, &QAction::triggered, this, &MainWindow::textItalic);
actionTextItalic->setCheckable(true);
actionTextUnderline = new QAction(QIcon::fromTheme("", QIcon(":/images/textunder.png")), tr("&Underline"), this);
actionTextUnderline->setShortcut(Qt::CTRL | Qt::Key_U);
actionTextUnderline->setPriority(QAction::LowPriority);
QFont underline;
underline.setUnderline(true);
actionTextUnderline->setFont(underline);
connect(actionTextUnderline, &QAction::triggered, this, &MainWindow::textUnderline);
actionTextUnderline->setCheckable(true);
QActionGroup *grp = new QActionGroup(this);
connect(grp, &QActionGroup::triggered, this, &MainWindow::textAlign);
actionAlignLeft = new QAction(QIcon::fromTheme("", QIcon(":/images/textleft.png")),tr("&Left align text"), this);
actionAlignCenter = new QAction(QIcon::fromTheme("",QIcon(":/images/textcenter.png")),tr("C&entered text"), this);
actionAlignRight = new QAction(QIcon::fromTheme("",QIcon(":/images/textright.png")),tr("&Right align text"), this);
actionAlignJustify = new QAction(QIcon::fromTheme("",QIcon(":/images/textjustify.png")),tr("&Justify text"), this);
grp->addAction (actionAlignLeft);
grp->addAction (actionAlignCenter);
grp->addAction (actionAlignRight);
grp->addAction (actionAlignJustify);
actionAlignLeft->setShortcut(Qt::CTRL | Qt::Key_L);
actionAlignLeft->setCheckable(true);
actionAlignLeft->setPriority(QAction::LowPriority);
actionAlignCenter->setShortcut(Qt::CTRL | Qt::Key_E);
actionAlignCenter->setCheckable(true);
actionAlignCenter->setPriority(QAction::LowPriority);
actionAlignRight->setShortcut(Qt::CTRL | Qt::Key_R);
actionAlignRight->setCheckable(true);
actionAlignRight->setPriority(QAction::LowPriority);
actionAlignJustify->setShortcut(Qt::CTRL | Qt::Key_J);
actionAlignJustify->setCheckable(true);
actionAlignJustify->setPriority(QAction::LowPriority);
actionInsertImage= new QAction(QIcon::fromTheme("", QIcon(":/images/insert-image.png")), tr("&Insert image"), this);
actionInsertImage->setPriority(QAction::LowPriority);
connect(actionInsertImage, &QAction::triggered, this, &MainWindow::insertImage);
QPixmap pix(16, 16);
pix.fill(Qt::black);
actionTextColor = new QAction(pix, tr("Text color..."), this);
connect(actionTextColor, &QAction::triggered, this, &MainWindow::textColor);
comboFont = new QFontComboBox(tb2);
comboFont->setEditable(false);
comboFont->setFontFilters(QFontComboBox::ScalableFonts | QFontComboBox::ProportionalFonts);
comboFont->setCurrentFont(QApplication::font());
connect(comboFont, &QFontComboBox::currentFontChanged, this, &MainWindow::fontFamily);
comboSize = new QComboBox(tb2);
comboSize->setObjectName("comboSize");
comboSize->setEditable(false);
for (int size : QFontDatabase::standardSizes())
comboSize->addItem(QString::number(size));
connect(comboSize, QOverload<int>::of(&QComboBox::activated), this, &MainWindow::textSize);
//QApplication::font().setPointSize(6);
comboSize->setCurrentIndex(comboSize->findText(QString::number(6)));
srchbox = new QLineEdit (tb1);
srchbox->setObjectName("srchbox");
srchbox->setMaxLength(50);
srchbox->setPlaceholderText("Enter search text...");
srchbox->setMaximumWidth(200);
srchbox->setMinimumWidth(200);
QIcon srchnxtIcon = QIcon::fromTheme("", QIcon(":/images/go-down-search.png"));
//kept as members (not locals) so the event filter can exempt the arrows
//from ending a running search; the object name lets the tests find them
srchnxtaction = new QAction(tr("Search &next"), this);
srchnxtaction->setObjectName("srchnext");
srchnxtaction->setIcon(srchnxtIcon);
connect(srchnxtaction, &QAction::triggered, this, &MainWindow::tree_srch_nxt);
QIcon srchpreIcon = QIcon::fromTheme("", QIcon(":/images/go-up-search.png"));
srchpreaction = new QAction(tr("Search &previous"), this);
srchpreaction->setObjectName("srchprev");
srchpreaction->setIcon(srchpreIcon);
connect(srchpreaction, &QAction::triggered, this, &MainWindow::tree_srch_pre);
tb1->addAction(addbranch);
tb1->addAction(addsubbranch);
tb1->addSeparator();
tb1->addAction(expand);
tb1->addAction(collapse);
tb1->addSeparator();
tb1->addAction(sortAsc);
tb1->addAction(sortDesc);
tb1->addSeparator();
tb1->addAction(cutbranch);
tb1->addAction(copybranch);
tb1->addAction(pastebranch);
tb1->addSeparator();
tb1->addAction(delbranch);
tb1->addSeparator();
tb1->addWidget(srchbox);
tb1->addAction(srchnxtaction);
tb1->addAction(srchpreaction);
tb2->addAction(newfile);
tb2->addAction(openfile);
tb2->addAction(savefile);
tb2->addAction(savefileas);
tb2->addSeparator();
tb2->addAction(actionTextBold);
tb2->addAction(actionTextItalic);
tb2->addAction(actionTextUnderline);
tb2->addSeparator();
tb2->addActions(grp->actions());
tb2->addSeparator();
tb2->addAction(actionInsertImage);
tb2->addSeparator();
tb2->addAction(actionTextColor);
tb2->addWidget(comboFont);
tb2->addWidget(comboSize);
}
void MainWindow::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(actionTextItalic->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(actionTextUnderline->isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textAlign(QAction *a)
{
QTextEdit *editor;
Qt::Alignment al;
editor = leafview;
al = editor->alignment ();
if (a == actionAlignLeft)
editor->setAlignment(Qt::AlignLeft);
else if (a == actionAlignCenter)
editor->setAlignment(Qt::AlignHCenter);
else if (a == actionAlignRight)
editor->setAlignment(Qt::AlignRight);
else if (a == actionAlignJustify)
editor->setAlignment(Qt::AlignJustify);
}
void MainWindow::textColor()
{
QTextEdit *editor;
editor = leafview;
QColor col = QColorDialog::getColor(editor->textColor(), this);
if (!col.isValid())
return;
QTextCharFormat fmt;
fmt.setForeground(col);
mergeFormatOnWordOrSelection(fmt);
colorChanged(col);
}
void MainWindow::colorChanged(const QColor &c)
{
QPixmap pix(16, 16);
pix.fill(c);
actionTextColor->setIcon(pix);
}
void MainWindow::fontFamily()
{
QTextCharFormat fmt;
fmt.setFont(comboFont->currentFont ());
mergeFormatOnWordOrSelection(fmt);
}
void MainWindow::textSize(int index)
//index is the position of the highlighted number in the combobox
{
int i;
QString s;
QTextCharFormat fmt;
s = comboSize->itemText (index);
i = s.toInt ();
fmt.setFontPointSize((qreal)i);
mergeFormatOnWordOrSelection(fmt);
}
QString unique_data_dir_name (const QString &dataDir, const QString &fileName)
{
QFileInfo fi(fileName);
QString target, base;
int n = 1;
target = dataDir + "/" + fileName;
base = fi.completeBaseName();
while (QFile::exists(target)) {
target = dataDir + "/" + base + "_" + QString::number(n);
if (fi.suffix().isEmpty() == false) {
target.append(".");
target.append(fi.suffix());
}
n++;
}
return target;
}
//bare data-dir image filenames referenced by <img src> tags in one block of
//html; paths containing '/' or '://' are external/absolute and never cleanup
//candidates. Shared by the tree walk and the .bak protection scan
static QSet<QString> collect_image_refs_from_html (const QString &html)
{
QRegularExpression re("<img\\s[^>]*?src=\"([^\"]*)\"[^>]*>");
QRegularExpressionMatchIterator it;
QRegularExpressionMatch m;
QSet<QString> refs;
it = re.globalMatch(html);
while (it.hasNext()) {
m = it.next();
QString src = m.captured(1);
//the src attribute is html-escaped (e.g. "a&b.png"), while the
//owned copy is the actual data-dir filename ("a&b.png"); compare the
//decoded value or a still-referenced image would look orphaned. New
//inserts also percent-encode the filename (a&b.png -> a%26b.png), so
//accept both the raw and the URL-decoded spelling.
src = QTextDocumentFragment::fromHtml(src).toPlainText();
if (src.isEmpty())
continue;
if (src.indexOf('/') == -1 && src.indexOf("://") == -1)
refs.insert(src);
const QString decoded = QUrl::fromPercentEncoding(src.toUtf8());
if (decoded != src && decoded.indexOf('/') == -1 && decoded.indexOf("://") == -1)
refs.insert(decoded);
}
return refs;
}
//collect refs from one branch and its descendants (iteratively, so the tree's
//nesting cap is the only depth limit)
static QSet<QString> collect_image_refs_from_subtree (const QTreeWidgetItem *root)
{
QList<const QTreeWidgetItem *> stack;
QSet<QString> refs;
if (root != nullptr)
stack.append(root);
while (stack.isEmpty() == false) {
const QTreeWidgetItem *item = stack.takeLast();
refs.unite(collect_image_refs_from_html(item->text(1)));
for (int c = 0; c < item->childCount(); c++)
stack.append(item->child(c));
}
return refs;
}
//walk the whole tree, collecting the bare data-dir image filenames referenced
//by <img src> tags in every branch's text
QSet<QString> collect_image_refs (const QTreeWidget &tree)
{
QSet<QString> refs;
for (int i = 0; i < tree.topLevelItemCount(); i++)
refs.unite(collect_image_refs_from_subtree(tree.topLevelItem(i)));
return refs;
}
//bare image filenames referenced by a document's rolling ".bak" backup (refs
//is left unchanged when there is no backup), so orphan cleanup never deletes an
//image the restorable backup still displays. Returns false when a backup exists
//but is too large to scan safely; the caller then keeps every owned image
//instead of risking a wrong deletion.
static bool image_refs_from_backup (const QString &filepath, QSet<QString> &refs)
{
QFile bak(filepath + ".bak");
QByteArray data;
QString html;
if (bak.open(QIODevice::ReadOnly | QIODevice::Text) == false)
//a missing backup references nothing; an existing but unreadable one
//must keep every owned image instead of being treated as empty
return (QFile::exists(filepath + ".bak") == false);
//read at most one unit past the cap, so a growing file cannot turn this
//into an unbounded readAll()
data = bak.read(MAX_BACKUP_SCAN + 1);
if (data.size() > MAX_BACKUP_SCAN || bak.error() != QFileDevice::NoError)
return false;
html = QString::fromUtf8(data);
refs.unite(collect_image_refs_from_html(html));
return true;
}
//only writer of ownedImages alongside the (modal) insertImage(); kept separate
//so the orphan cleanup is testable without dialogs
void MainWindow::recordInsertedImage (const QString &basename)
{
if (basename.isEmpty() == false) {
ownedImages.insert(basename);
//remember it as belonging to no saved document yet, so a later Discard
//can delete the copy instead of leaking it into the data dir
sessionInserted.insert(basename);
//persist right away (when a real file is open), so a crash before the
//next save cannot orphan the copy; the set is rewritten on every save
if (Currentfile != "Noname.trc"
&& write_owned_images(image_own_name(DataDir, Currentfile), ownedImages) == false)
statustext->setText(tr("Image inserted, but image metadata could not be updated"));
}
}
void MainWindow::loadOwnedImages (const QString &filepath)
{
ownedImages = load_owned_images(image_own_name(DataDir, filepath));
sessionInserted.clear();
}
void MainWindow::discardUnsavedImages()
{
//copies inserted since the last save belong to no document on disk: when the
//user discards the edits, delete them (a successful save clears the set
//first, so this only ever fires on an actual Discard)
if (sessionInserted.isEmpty())
return;
for (const QString &name : std::as_const(sessionInserted)) {
if (is_safe_sidecar_name(name))
QFile::remove(DataDir + "/" + name);
ownedImages.remove(name);
}
sessionInserted.clear();
//the on-disk document still owns its other copies: persist the pruned set
if (Currentfile != "Noname.trc")
write_owned_images(image_own_name(DataDir, Currentfile), ownedImages);
}
bool MainWindow::gcOrphanedImages (const QString &filepath)
{
QSet<QString> refs, keep;
QString path = filepath;
bool ok = true, backupComplete = true;
if (path.isEmpty())
path = Currentfile;
if (ownedImages.isEmpty()) {
//nothing to prune, but still drop a sidecar the document no longer owns
//anything through (e.g. the previous path of a Save As)
if (path != "Noname.trc")
ok = write_owned_images(image_own_name(DataDir, path), QSet<QString>());
return ok;
}
refs = collect_image_refs(*tree);
//the internal clipboard can still hold a cut/copied branch whose images are
//not in the tree; keep them so a later paste cannot reference a file this
//save deleted (the clipboard is dropped when switching documents)
if (copy_branch != nullptr)
refs.unite(collect_image_refs_from_subtree(copy_branch));
//an image still referenced by the rolling "<path>.bak" backup is kept too:
//the backup must stay a restorable document, so its images survive until a
//later save rolls the backup away
if (path != "Noname.trc")
backupComplete = image_refs_from_backup(path, refs);
if (backupComplete == false) {
//the backup is too large to inspect safely: an owned image might still
//be referenced there, so keep the ownership set untouched
return true;
}
for (const QString &name : ownedImages) {
//a name that is not a plain filename (a hostile/corrupt sidecar) is never
//joined to the data dir or deleted
if (is_safe_sidecar_name(name) == false) {
keep.insert(name);
continue;
}
//keep every still-referenced image; unreferenced ones are copies owned
//by this document (and only this one) - the document is done with them
if (refs.contains(name)) {
keep.insert(name);
continue;
}
if (QFile::remove(DataDir + "/" + name) == false) {
//transient failure: leave the entry so a later save can retry
keep.insert(name);
continue;
}
}
ownedImages = keep;
//persist the pruned set so ownership survives the next session (no sidecar
//is ever written for the unsaved "Noname.trc" placeholder)
if (path != "Noname.trc")
ok = write_owned_images(image_own_name(DataDir, path), ownedImages);
return ok;
}
bool MainWindow::unshareImagesAfterSaveAs (const QString &prevFile, const QString &newFile)
{
//a plain save (unsaved placeholder, or Save As back to the same path)
//copies nothing: the session's image copies simply belong to newFile
if (prevFile == "Noname.trc" || trc_paths_differ(prevFile, newFile) == false)
return true;
//Save As copies the document, so both .trc files reference the same
//physical image copies from now on. A single owner could delete an image
//the other document still displays, so neither may own them: drop this
//document's set (the following gcOrphanedImages() persists that for
//newFile) and remove the previous file's sidecar. The copies themselves
//are left on disk - never deleted wrongly, only never cleaned up.
ownedImages.clear();
//false only when the previous sidecar exists but cannot be removed
return (QFile::exists(image_own_name(DataDir, prevFile)) == false)
|| QFile::remove(image_own_name(DataDir, prevFile));
}
void MainWindow::insertImage()
{
QString s, filters, fname;
QFileInfo fi;
QTextEdit *editor;
QMessageBox msgBox;
editor = leafview;
//an image needs a branch to live in; refusing before the dialog also
//avoids copying and recording an image that could never be referenced
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
filters += tr("Common Graphics (*.png *.jpg *.jpeg *.gif);;");
filters += tr("Portable Network Graphics (PNG) (*.png);;");
filters += tr("JPEG (*.jpg *.jpeg);;");
filters += tr("Graphics Interchange Format (*.gif);;");
filters += tr("All Files (*)");
QString file = QFileDialog::getOpenFileName(this, tr("Open image..."), QString(), filters);
if (file.isEmpty())
return;
if (!QFile::exists(file))
return;
fi = QFileInfo(file);
//always copy the image into the data dir, giving it a unique name if a
//file with that name is already there. Even an image already living in the
//data dir gets its own private numbered copy - two documents that insert
//the same picture therefore never share one physical file, so a file a
//document dropped is safe to delete again (gcOrphanedImages)
fname = unique_data_dir_name(DataDir, fi.fileName());
if (QFile::copy(fi.filePath(), fname) == false) {
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText(QObject::tr("Could not copy the image file to the Treecle data directory: ") + fname);
msgBox.setInformativeText(QObject::tr("The image was not inserted."));
msgBox.exec();
return;
}
recordInsertedImage(QFileInfo(fname).fileName());
s.append (QObject::tr("The image file has been copied to the Treecle data directory "));
s.append (DataDir);
s.append (QObject::tr("\nClick OK to continue"));
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(s);
msgBox.exec();
//store only the file name; the data dir path is prepended when displayed.
//Percent-encode URL metacharacters (#, ?, %, spaces, ...), then html-escape
//the attribute value, so the image still resolves when displayed/saved.
const QString imgName = QFileInfo(fname).fileName();
const QString imgSrc = QString::fromLatin1(QUrl::toPercentEncoding(imgName));
editor->insertHtml(QString("<img src=\"%1\">").arg(imgSrc.toHtmlEscaped()));
//immediately fit the new image to the panel width (aspect ratio kept)
fit_editor_images(qMax(60, leafview->viewport()->width() - 30));
}
void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextEdit *editor;
editor = leafview;
QTextCursor cursor = editor->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
editor->mergeCurrentCharFormat(format);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (document_modified == true) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Treecle"), tr("Do you wish to save or discard the current tree?\n"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Cancel) {//user cancels
statustext->setText(tr("The tree has not been saved "));
event->ignore();
return;
}
if (ret == QMessageBox::Save) {
if (save_file() == true) {//file saved by the user
statustext->setText(tr("Saved"));
writeprefs();
event->accept();
}
else {//user cancelled the save operation
statustext->setText(tr("The tree has not been saved"));
event->ignore();
}
return;
}
if (ret == QMessageBox::Discard) {
discardUnsavedImages();//never-saved image copies go with the edits
statustext->setText(tr("Discarded"));
writeprefs();
event->accept();
}
}
else {
writeprefs();
event->accept();
}
}
void MainWindow::help()
//open and show the user manual
{
bool ok;
QString s1;
QMessageBox msgBox;
QFile file(Helpfilename);
ok = file.exists();
if (ok == false) {
s1 = QObject::tr("The help file \"treeclehelp.pdf\"was not found\n");
s1.append (QObject::tr("Please make sure that it is present in the treecle data directory.\n"));
s1.append("\n");
s1.append (DataDir);
s1.append("\n");
msgBox.setText(s1);
msgBox.exec();
}
else {
ok = QDesktopServices::openUrl (QUrl::fromLocalFile (Helpfilename));
if (ok == false) {
msgBox.setText(QObject::tr("Could not open help file."));
msgBox.exec();
}
}
}
void MainWindow::about()
//open a window to show program information and copyright license
{
QFile file(Gnugplfilename);
QTextStream in(&file);
QTextBrowser *gnugpl;
bool ok;
ok = file.open(QFile::ReadOnly);
if (ok == false)
return;
gnugpl = new QTextBrowser ();
gnugpl->setAttribute(Qt::WA_DeleteOnClose);
gnugpl->setGeometry(100, 100, 800, 600);
gnugpl->setWindowTitle (QObject::tr("About Treecle"));
gnugpl->setPlainText(in.readAll());
gnugpl->setAlignment(Qt::AlignLeft);
gnugpl->show();
}
bool check_and_make_data_dir (const QString &dataDir)
{
QString qtpath, s1;
QDir qtdir;
QMessageBox msgBox;
qtpath.append (dataDir);
qtdir = QDir (qtpath);
if (qtdir.exists() == true)//not the first run in this account
return true;
//first run in this account: tell the user the data dir is being created;
//images, help, license and file locks are kept there
s1.append (QObject::tr("Treecle is running for the first time in this account.\n"));
s1.append (QObject::tr("The data directory\n\n"));
s1.append (qtpath);
s1.append (QObject::tr("\n\nis created for storing your images,\nthe help file (treeclehelp.pdf), the license (COPYING)\nand the temporary file locks.\n"));
s1.append (QObject::tr("You can safely ignore this message; it appears only once."));
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(s1);
msgBox.exec();
return qtdir.mkpath(qtpath);//mkdir for nested parents (e.g. ~/.local/share)
}
QString fit_images_to_width (const QString &html, const QString &dataDir, int maxWidth)
{
QRegularExpression tagRe("<img[^>]*>");
QRegularExpression srcRe("src=\"([^\"]*)\"");
QRegularExpression sizeRe("\\s+(width|height)=\\\"[^\\\"]*\\\"");
QRegularExpressionMatchIterator it;
QRegularExpressionMatch m;
QString out, tag, newTag, src, path;
QImageReader reader;
QSize orig;
int pos = 0, w = 0, h = 0;
if (maxWidth < 1)
return html;
it = tagRe.globalMatch(html);
while (it.hasNext()) {
m = it.next();
out.append(html.mid(pos, m.capturedStart() - pos));
pos = m.capturedEnd();
tag = m.captured();
newTag = tag;
src = srcRe.match(tag).captured(1);
path = QUrl::fromPercentEncoding(src.toUtf8());
//shrink only bare filenames that are expected in the data dir
if (path.isEmpty() == false && path.contains('/') == false &&
path.contains("://") == false) {
//src may be percent-encoded; the reader needs the real filename
QString file = QFileInfo(path).fileName();
if (QFile::exists(dataDir + "/" + file) == false) {
const QString rawFile = QFileInfo(src).fileName();
if (rawFile != file)
file = rawFile;
}
reader.setFileName(dataDir + "/" + file);
orig = reader.size();
if (orig.isValid() && orig.width() > maxWidth) {
w = maxWidth;
h = qMax(1, (int)((qreal)orig.height() * maxWidth / orig.width()));
//replace any existing size attributes, then impose ours, before the
//tag's closing ">" (or "/>" for self-closing img tags)
newTag.replace(sizeRe, QString());
int ins = newTag.lastIndexOf('>');
if (ins > 0 && newTag.at(ins - 1) == '/')
ins--;
newTag.insert(ins,
QString(" width=\"%1\" height=\"%2\"").arg(w).arg(h));
}
}
out.append(newTag);
}
out.append(html.mid(pos));
return out;
}
QString strip_image_sizes (const QString &html)
{
QRegularExpression tagRe("<img[^>]*>");
QRegularExpression sizeRe("\\s+(width|height)\\s*=\\s*\"[^\"]*\"");
QRegularExpressionMatchIterator it;
QRegularExpressionMatch m;
QString out, tag;
int pos = 0;
it = tagRe.globalMatch(html);
while (it.hasNext()) {
m = it.next();
out.append(html.mid(pos, m.capturedStart() - pos));
pos = m.capturedEnd();
tag = m.captured();
tag.replace(sizeRe, QString());
out.append(tag);
}
out.append(html.mid(pos));
return out;
}
//rescale every on-screen image so its width fits maxWidth (aspect ratio kept).
//Only the display is affected: the char data is untouched and toHtml still
//exports the bare filename link (sizes are stripped again when stored).
void MainWindow::fit_editor_images (int maxWidth)
{
struct ImgScale { int pos; int len; QTextImageFormat fmt; };
QImageReader reader;
QString name, path;
QSize orig;
int w, h;
QList<ImgScale> work;
if (maxWidth < 1)
return;
for (QTextBlock block = leafdoc->begin (); block.isValid (); block = block.next ()) {
for (QTextBlock::iterator it = block.begin (); !it.atEnd (); ++it) {
QTextFragment frag = it.fragment ();
if (frag.isValid () == false)
continue;
QTextCharFormat cf = frag.charFormat ();
if (cf.isImageFormat () == false)
continue;
QTextImageFormat img = cf.toImageFormat ();
name = img.name ();
if (name.isEmpty ())
continue;
//only bare data-dir filenames follow the panel width; external
//links and absolute paths are left exactly as authored. The name
//may be percent-encoded, so prefer the decoded spelling when that
//file actually exists (raw names containing '%' still work).
QString file = name;
const QString decoded = QUrl::fromPercentEncoding(name.toUtf8());
if (decoded != name && decoded.indexOf('/') == -1
&& QFile::exists(DataDir + "/" + decoded))
file = decoded;
if (file.indexOf('/') != -1)
continue;
reader.setFileName (DataDir + "/" + file);
orig = reader.size ();
if (orig.isValid () == false)
continue;
w = -1;
h = -1;
if (orig.width () > maxWidth) {
w = maxWidth;
h = qMax(1, (int)((qreal)orig.height () * maxWidth / orig.width ()));
}
else if (img.width () <= 0)
continue; //already at its intrinsic size
if (img.width () == w && img.height () == h)
continue; //already sized to the current panel
ImgScale s;
s.pos = frag.position ();
s.len = frag.length ();
s.fmt = img;
s.fmt.setWidth (w);
s.fmt.setHeight (h);
work.append (s);
}
}
if (work.isEmpty ())
return;
QTextCursor cur (leafdoc);
cur.beginEditBlock ();
for (const ImgScale &s : work) {
cur.setPosition (s.pos);
cur.setPosition (s.pos + s.len, QTextCursor::KeepAnchor);
cur.setCharFormat (s.fmt);
}
cur.endEditBlock ();
}
bool MainWindow::eventFilter (QObject *obj, QEvent *ev)
{
if (obj == leafview && ev->type () == QEvent::Resize) {
branch_display_in_progress = true;
fit_editor_images (qMax(60, leafview->viewport ()->width () - 30));
branch_display_in_progress = false;
}
if (ev->type() == QEvent::KeyPress) {
const QKeyEvent *ke = static_cast<const QKeyEvent *>(ev);
if (ke->key() == Qt::Key_Escape)