/
katefiletreemodel.cpp
1516 lines (1234 loc) · 40.2 KB
/
katefiletreemodel.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
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2010 Thomas Fjellstrom <thomas@fjellstrom.ca>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "katefiletreemodel.h"
#include <QDir>
#include <QGuiApplication>
#include <QIcon>
#include <QList>
#include <QMimeData>
#include <QStack>
#include <QWidget>
#include <KColorScheme>
#include <KColorUtils>
#include <KLocalizedString>
#include <KTextEditor/Document>
#include <KTextEditor/MainWindow>
#include <ktexteditor/application.h>
#include <ktexteditor/editor.h>
#include "katefiletreedebug.h"
#include "ktexteditor_utils.h"
#include <variant>
static constexpr int MaxHistoryItems = 10;
class FileTreeMimeData : public QMimeData
{
Q_OBJECT
public:
FileTreeMimeData(const QModelIndex &index)
: m_index(index)
{
}
QModelIndex index() const
{
return m_index;
}
private:
QPersistentModelIndex m_index;
};
class ProxyItemDir;
class ProxyItem
{
friend class KateFileTreeModel;
public:
enum Flag {
None = 0,
Dir = 1,
Modified = 2,
ModifiedExternally = 4,
DeletedExternally = 8,
Empty = 16,
ShowFullPath = 32,
Host = 64,
Widget = 128,
};
Q_DECLARE_FLAGS(Flags, Flag)
ProxyItem(const QString &n, ProxyItemDir *p = nullptr, Flags f = ProxyItem::None);
~ProxyItem();
int addChild(ProxyItem *p);
void removeChild(ProxyItem *p);
ProxyItemDir *parent() const;
ProxyItem *child(int idx) const;
int childCount() const;
int row() const;
const QString &display() const;
const QString &documentName() const;
const QString &path() const;
void setPath(const QString &str);
void setHost(const QString &host);
const QString &host() const;
void setIcon(const QIcon &i);
const QIcon &icon() const;
const std::vector<ProxyItem *> &children() const;
std::vector<ProxyItem *> &children();
void setDoc(KTextEditor::Document *doc);
KTextEditor::Document *doc() const;
void setWidget(QWidget *);
QWidget *widget() const;
/**
* the view uses this to close all the documents under the folder
* @returns list of all the (nested) documents under this node
*/
QList<KTextEditor::Document *> docTree() const;
void setFlags(Flags flags);
void setFlag(Flag flag);
void clearFlag(Flag flag);
bool flag(Flag flag) const;
private:
QString m_path;
QString m_documentName;
ProxyItemDir *m_parent;
std::vector<ProxyItem *> m_children;
int m_row;
Flags m_flags;
QString m_display;
QIcon m_icon;
std::variant<KTextEditor::Document *, QWidget *> m_object;
QString m_host;
protected:
void updateDisplay();
void updateDocumentName();
};
QDebug operator<<(QDebug dbg, ProxyItem *item)
{
if (!item) {
dbg.nospace() << "ProxyItem(0x0) ";
return dbg.maybeSpace();
}
const void *parent = static_cast<void *>(item->parent());
dbg.nospace() << "ProxyItem(" << item << ",";
dbg.nospace() << parent << "," << item->row() << ",";
dbg.nospace() << item->doc() << "," << item->path() << ") ";
return dbg.maybeSpace();
}
class ProxyItemDir : public ProxyItem
{
public:
ProxyItemDir(const QString &n, ProxyItemDir *p = nullptr)
: ProxyItem(n, p)
{
setFlag(ProxyItem::Dir);
updateDisplay();
setIcon(QIcon::fromTheme(QStringLiteral("folder")));
}
};
QDebug operator<<(QDebug dbg, ProxyItemDir *item)
{
if (!item) {
dbg.nospace() << "ProxyItemDir(0x0) ";
return dbg.maybeSpace();
}
const void *parent = static_cast<void *>(item->parent());
dbg.nospace() << "ProxyItemDir(" << item << ",";
dbg.nospace() << parent << "," << item->row() << ",";
dbg.nospace() << item->path() << ", children:" << item->childCount() << ") ";
return dbg.maybeSpace();
}
Q_DECLARE_OPERATORS_FOR_FLAGS(ProxyItem::Flags)
// BEGIN ProxyItem
ProxyItem::ProxyItem(const QString &d, ProxyItemDir *p, ProxyItem::Flags f)
: m_path(d)
, m_parent(Q_NULLPTR)
, m_row(-1)
, m_flags(f)
{
updateDisplay();
if (f.testFlag(Widget) && f.testFlag(Dir)) {
m_documentName = display();
}
/**
* add to parent, if parent passed
* we assigned above nullptr to parent to not trigger
* remove from old parent here!
*/
if (p) {
p->addChild(this);
}
}
ProxyItem::~ProxyItem()
{
qDeleteAll(m_children);
}
void ProxyItem::updateDisplay()
{
// triggers only if this is a top level node and the root has the show full path flag set.
if (flag(ProxyItem::Dir) && m_parent && !m_parent->m_parent && m_parent->flag(ProxyItem::ShowFullPath)) {
m_display = m_path;
if (m_display.startsWith(QDir::homePath())) {
m_display.replace(0, QDir::homePath().length(), QStringLiteral("~"));
}
} else {
m_display = m_path.section(QLatin1Char('/'), -1, -1);
if (flag(ProxyItem::Host) && (!m_parent || (m_parent && !m_parent->m_parent))) {
const QString hostPrefix = QStringLiteral("[%1]").arg(host());
if (hostPrefix != m_display) {
m_display = hostPrefix + m_display;
}
}
}
}
int ProxyItem::addChild(ProxyItem *item)
{
// remove from old parent, is any
if (item->m_parent) {
item->m_parent->removeChild(item);
}
const int item_row = m_children.size();
item->m_row = item_row;
m_children.push_back(item);
item->m_parent = static_cast<ProxyItemDir *>(this);
item->updateDisplay();
return item_row;
}
void ProxyItem::removeChild(ProxyItem *item)
{
auto it = std::find(m_children.begin(), m_children.end(), item);
Q_ASSERT(it != m_children.end());
m_children.erase(it);
auto idx = std::distance(m_children.begin(), it);
for (size_t i = idx; i < m_children.size(); i++) {
m_children[i]->m_row = i;
}
item->m_parent = nullptr;
}
ProxyItemDir *ProxyItem::parent() const
{
return m_parent;
}
ProxyItem *ProxyItem::child(int idx) const
{
return (size_t(idx) >= m_children.size()) ? nullptr : m_children[idx];
}
int ProxyItem::childCount() const
{
return m_children.size();
}
int ProxyItem::row() const
{
return m_row;
}
const QIcon &ProxyItem::icon() const
{
return m_icon;
}
void ProxyItem::setIcon(const QIcon &i)
{
m_icon = i;
}
const QString &ProxyItem::documentName() const
{
return m_documentName;
}
const QString &ProxyItem::display() const
{
return m_display;
}
const QString &ProxyItem::path() const
{
return m_path;
}
void ProxyItem::setPath(const QString &p)
{
m_path = p;
updateDisplay();
}
const std::vector<ProxyItem *> &ProxyItem::children() const
{
return m_children;
}
std::vector<ProxyItem *> &ProxyItem::children()
{
return m_children;
}
void ProxyItem::setDoc(KTextEditor::Document *doc)
{
Q_ASSERT(doc);
m_object = doc;
updateDocumentName();
}
void ProxyItem::setWidget(QWidget *w)
{
Q_ASSERT(w);
m_object = w;
updateDocumentName();
}
QWidget *ProxyItem::widget() const
{
if (!std::holds_alternative<QWidget *>(m_object))
return nullptr;
return std::get<QWidget *>(m_object);
}
KTextEditor::Document *ProxyItem::doc() const
{
if (!std::holds_alternative<KTextEditor::Document *>(m_object))
return nullptr;
return std::get<KTextEditor::Document *>(m_object);
}
QList<KTextEditor::Document *> ProxyItem::docTree() const
{
QList<KTextEditor::Document *> result;
if (doc()) {
result.append(doc());
return result;
}
for (const ProxyItem *item : m_children) {
result.append(item->docTree());
}
return result;
}
bool ProxyItem::flag(Flag f) const
{
return m_flags & f;
}
void ProxyItem::setFlag(Flag f)
{
m_flags |= f;
}
void ProxyItem::setFlags(Flags f)
{
m_flags = f;
}
void ProxyItem::clearFlag(Flag f)
{
m_flags &= ~f;
}
void ProxyItem::setHost(const QString &host)
{
m_host = host;
if (host.isEmpty()) {
clearFlag(Host);
} else {
setFlag(Host);
}
updateDocumentName();
updateDisplay();
}
const QString &ProxyItem::host() const
{
return m_host;
}
void ProxyItem::updateDocumentName()
{
QString name;
if (doc()) {
name = doc()->documentName();
} else if (widget()) {
name = widget()->windowTitle();
}
if (flag(ProxyItem::Host)) {
m_documentName = QStringLiteral("[%1]%2").arg(m_host, name);
} else {
m_documentName = name;
}
}
// END ProxyItem
KateFileTreeModel::KateFileTreeModel(KTextEditor::MainWindow *mainWindow, QObject *p)
: QAbstractItemModel(p)
, m_mainWindow(mainWindow)
, m_root(new ProxyItemDir(QStringLiteral("m_root"), nullptr))
{
// setup default settings
// session init will set these all soon
const KColorScheme colors(QPalette::Active);
const QColor bg = colors.background().color();
m_editShade = KColorUtils::tint(bg, colors.foreground(KColorScheme::ActiveText).color(), 0.5);
m_viewShade = KColorUtils::tint(bg, colors.foreground(KColorScheme::VisitedText).color(), 0.5);
m_inactiveDocColor = colors.foreground(KColorScheme::InactiveText).color();
m_shadingEnabled = true;
m_listMode = false;
initModel();
// ensure palette change updates the colors properly
connect(KTextEditor::Editor::instance(), &KTextEditor::Editor::configChanged, this, [this]() {
m_inactiveDocColor = KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText).color();
updateBackgrounds(true);
});
}
KateFileTreeModel::~KateFileTreeModel()
{
delete m_root;
}
void KateFileTreeModel::setShadingEnabled(bool se)
{
if (m_shadingEnabled != se) {
updateBackgrounds(true);
m_shadingEnabled = se;
}
if (!se) {
m_viewHistory.clear();
m_editHistory.clear();
m_brushes.clear();
}
}
void KateFileTreeModel::setEditShade(const QColor &es)
{
m_editShade = es;
}
void KateFileTreeModel::setViewShade(const QColor &vs)
{
m_viewShade = vs;
}
bool KateFileTreeModel::showFullPathOnRoots(void) const
{
return m_root->flag(ProxyItem::ShowFullPath);
}
void KateFileTreeModel::setShowFullPathOnRoots(bool s)
{
if (s) {
m_root->setFlag(ProxyItem::ShowFullPath);
} else {
m_root->clearFlag(ProxyItem::ShowFullPath);
}
const auto rootChildren = m_root->children();
for (ProxyItem *root : rootChildren) {
root->updateDisplay();
}
}
void KateFileTreeModel::initModel()
{
beginInsertRows(QModelIndex(), 0, 0);
Q_ASSERT(!m_widgetsRoot);
m_widgetsRoot = new ProxyItem(i18nc("Open here is a description, i.e. 'list of widgets that are open' not a verb", "Open Widgets"),
nullptr,
ProxyItem::Flags(ProxyItem::Dir | ProxyItem::Widget));
m_widgetsRoot->setFlags(ProxyItem::Flags(ProxyItem::Dir | ProxyItem::Widget));
m_widgetsRoot->setIcon(QIcon::fromTheme(QStringLiteral("folder-windows")));
m_root->addChild(m_widgetsRoot);
endInsertRows();
// add already existing documents
const auto documents = KTextEditor::Editor::instance()->application()->documents();
for (KTextEditor::Document *doc : documents) {
documentOpened(doc);
}
if (m_mainWindow) {
QWidgetList widgets = m_mainWindow->widgets();
for (auto *w : std::as_const(widgets)) {
addWidget(w);
}
}
}
void KateFileTreeModel::clearModel()
{
// remove all items
// can safely ignore documentClosed here
beginResetModel();
delete m_root;
m_root = new ProxyItemDir(QStringLiteral("m_root"), nullptr);
m_widgetsRoot = nullptr;
m_docmap.clear();
m_viewHistory.clear();
m_editHistory.clear();
m_brushes.clear();
endResetModel();
}
void KateFileTreeModel::connectDocument(const KTextEditor::Document *doc)
{
connect(doc, &KTextEditor::Document::documentNameChanged, this, &KateFileTreeModel::documentNameChanged);
connect(doc, &KTextEditor::Document::documentUrlChanged, this, &KateFileTreeModel::documentNameChanged);
connect(doc, &KTextEditor::Document::modifiedChanged, this, &KateFileTreeModel::documentModifiedChanged);
connect(doc, &KTextEditor::Document::modifiedOnDisk, this, &KateFileTreeModel::documentModifiedOnDisc);
}
QModelIndex KateFileTreeModel::docIndex(const KTextEditor::Document *doc) const
{
auto it = m_docmap.find(doc);
if (it == m_docmap.end()) {
return {};
}
auto item = it.value();
return createIndex(item->row(), 0, item);
}
QModelIndex KateFileTreeModel::widgetIndex(QWidget *widget) const
{
ProxyItem *item = nullptr;
const auto items = m_widgetsRoot->children();
for (auto *it : items) {
if (it->widget() == widget) {
item = it;
break;
}
}
if (!item) {
return {};
}
return createIndex(item->row(), 0, item);
}
Qt::ItemFlags KateFileTreeModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled;
if (!index.isValid()) {
return Qt::NoItemFlags | Qt::ItemIsDropEnabled;
}
const ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (item) {
if (!item->flag(ProxyItem::Dir)) {
flags |= Qt::ItemIsSelectable;
}
if (item->flag(ProxyItem::Dir) && !item->flag(ProxyItem::Widget)) {
flags |= Qt::ItemIsDropEnabled;
}
if (item->doc() && item->doc()->url().isValid()) {
flags |= Qt::ItemIsDragEnabled;
}
}
return flags;
}
QVariant KateFileTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (!item) {
return QVariant();
}
switch (role) {
case KateFileTreeModel::PathRole:
// allow to sort with hostname + path, bug 271488
return (item->doc() && !item->doc()->url().isEmpty()) ? item->doc()->url().toString() : item->path();
case KateFileTreeModel::DocumentRole:
return QVariant::fromValue(item->doc());
case KateFileTreeModel::WidgetRole:
return QVariant::fromValue(item->widget());
case KateFileTreeModel::OpeningOrderRole:
return item->row();
case KateFileTreeModel::DocumentTreeRole:
return QVariant::fromValue(item->docTree());
case Qt::DisplayRole:
// in list mode we want to use kate's fancy names.
if (index.column() == 0) {
if (m_listMode) {
return item->documentName();
} else {
return item->display();
}
}
break;
case Qt::DecorationRole:
if (index.column() == 0) {
return item->icon();
}
break;
case Qt::ToolTipRole: {
QString tooltip = item->path();
if (item->flag(ProxyItem::DeletedExternally) || item->flag(ProxyItem::ModifiedExternally)) {
tooltip = i18nc("%1 is the full path", "<p><b>%1</b></p><p>The document has been modified by another application.</p>", item->path());
}
return tooltip;
}
case Qt::ForegroundRole: {
if (!item->flag(ProxyItem::Widget) && !item->flag(ProxyItem::Dir) && (!item->doc() || item->doc()->openingError())) {
return m_inactiveDocColor;
}
} break;
case Qt::BackgroundRole:
// TODO: do that funky shading the file list does...
if (m_shadingEnabled) {
if (auto it = m_brushes.find(item); it != m_brushes.end()) {
return it->second;
}
}
break;
}
return QVariant();
}
Qt::DropActions KateFileTreeModel::supportedDropActions() const
{
Qt::DropActions a = QAbstractItemModel::supportedDropActions();
a |= Qt::MoveAction;
return a;
}
QMimeData *KateFileTreeModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.size() != columnCount()) {
return nullptr;
}
ProxyItem *item = static_cast<ProxyItem *>(indexes.at(0).internalPointer());
QList<QUrl> urls;
if (!item || !item->doc() || !item->doc()->url().isValid()) {
return nullptr;
}
urls.append(item->doc()->url());
FileTreeMimeData *mimeData = new FileTreeMimeData(indexes.at(0));
mimeData->setUrls(urls);
return mimeData;
}
bool KateFileTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent) const
{
if (auto md = qobject_cast<const FileTreeMimeData *>(data)) {
return action == Qt::MoveAction && md->index().parent() == parent;
}
return false;
}
bool KateFileTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction, int row, int, const QModelIndex &parent)
{
auto md = qobject_cast<const FileTreeMimeData *>(data);
if (!md) {
return false;
}
const auto index = md->index();
Q_ASSERT(parent == index.parent()); // move is only allowed within the same parent
if (!index.isValid() || index.row() > rowCount(parent) || index.row() == row) {
return false;
}
auto parentItem = parent.isValid() ? static_cast<ProxyItemDir *>(parent.internalPointer()) : m_root;
auto &childs = parentItem->children();
int sourceRow = index.row();
beginMoveRows(index.parent(), index.row(), index.row(), parent, row);
childs.insert(childs.begin() + row, childs.at(index.row()));
if (sourceRow > row) {
sourceRow++;
}
childs.erase(childs.begin() + sourceRow);
// update row number of children
for (size_t i = 0; i < childs.size(); i++) {
childs[i]->m_row = i;
}
endMoveRows();
return true;
}
QVariant KateFileTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation);
Q_UNUSED(role);
if (section == 0) {
return QLatin1String("name");
}
return QVariant();
}
int KateFileTreeModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return m_root->childCount();
}
// we only have children for column 0
if (parent.column() != 0) {
return 0;
}
const ProxyItem *item = static_cast<ProxyItem *>(parent.internalPointer());
if (!item) {
return 0;
}
return item->childCount();
}
int KateFileTreeModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QModelIndex KateFileTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (!item) {
return QModelIndex();
}
if (!item->parent()) {
return QModelIndex();
}
if (item->parent() == m_root) {
return QModelIndex();
}
return createIndex(item->parent()->row(), 0, item->parent());
}
QModelIndex KateFileTreeModel::index(int row, int column, const QModelIndex &parent) const
{
const ProxyItem *p = nullptr;
if (column != 0 && column != 1) {
return QModelIndex();
}
if (!parent.isValid()) {
p = m_root;
} else {
p = static_cast<ProxyItem *>(parent.internalPointer());
}
if (!p) {
return QModelIndex();
}
if (row < 0 || row >= p->childCount()) {
return QModelIndex();
}
return createIndex(row, column, p->child(row));
}
bool KateFileTreeModel::hasChildren(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return m_root->childCount() > 0;
}
// we only have children for column 0
if (parent.column() != 0) {
return false;
}
const ProxyItem *item = static_cast<ProxyItem *>(parent.internalPointer());
if (!item) {
return false;
}
return item->childCount() > 0;
}
ProxyItem *KateFileTreeModel::itemForIndex(const QModelIndex &index) const
{
if (!index.isValid()) {
return m_root;
}
ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (!item) {
return nullptr;
}
return item;
}
bool KateFileTreeModel::isDir(const QModelIndex &index) const
{
if (auto item = itemForIndex(index)) {
return item->flag(ProxyItem::Dir) && !item->flag(ProxyItem::Widget);
}
return false;
}
bool KateFileTreeModel::isWidgetDir(const QModelIndex &index) const
{
if (auto item = itemForIndex(index)) {
return item->flag(ProxyItem::Dir) && item->flag(ProxyItem::Widget);
}
return false;
}
bool KateFileTreeModel::listMode() const
{
return m_listMode;
}
void KateFileTreeModel::setListMode(bool lm)
{
if (lm != m_listMode) {
m_listMode = lm;
clearModel();
initModel();
}
}
void KateFileTreeModel::documentOpened(KTextEditor::Document *doc)
{
ProxyItem *item = new ProxyItem(QString());
item->setDoc(doc);
updateItemPathAndHost(item);
setupIcon(item);
handleInsert(item);
m_docmap[doc] = item;
connectDocument(doc);
}
void KateFileTreeModel::documentsOpened(const QList<KTextEditor::Document *> &docs)
{
for (KTextEditor::Document *doc : docs) {
if (m_docmap.contains(doc)) {
documentNameChanged(doc);
} else {
documentOpened(doc);
}
}
}
void KateFileTreeModel::documentModifiedChanged(KTextEditor::Document *doc)
{
auto it = m_docmap.find(doc);
if (it == m_docmap.end()) {
return;
}
ProxyItem *item = it.value();
if (doc->isModified()) {
item->setFlag(ProxyItem::Modified);
} else {
item->clearFlag(ProxyItem::Modified);
item->clearFlag(ProxyItem::ModifiedExternally);
item->clearFlag(ProxyItem::DeletedExternally);
}
setupIcon(item);
const QModelIndex idx = createIndex(item->row(), 0, item);
Q_EMIT dataChanged(idx, idx);
}
void KateFileTreeModel::documentModifiedOnDisc(KTextEditor::Document *doc, bool modified, KTextEditor::Document::ModifiedOnDiskReason reason)
{
Q_UNUSED(modified);
auto it = m_docmap.find(doc);
if (it == m_docmap.end()) {
return;
}
ProxyItem *item = it.value();
// This didn't do what I thought it did, on an ignore
// we'd get !modified causing the warning icons to disappear
if (!modified) {
item->clearFlag(ProxyItem::ModifiedExternally);
item->clearFlag(ProxyItem::DeletedExternally);
} else {
if (reason == KTextEditor::Document::OnDiskDeleted) {
item->setFlag(ProxyItem::DeletedExternally);
} else if (reason == KTextEditor::Document::OnDiskModified) {
item->setFlag(ProxyItem::ModifiedExternally);
} else if (reason == KTextEditor::Document::OnDiskCreated) {
// with out this, on "reload" we don't get the icons removed :(
item->clearFlag(ProxyItem::ModifiedExternally);
item->clearFlag(ProxyItem::DeletedExternally);
}
}
setupIcon(item);
const QModelIndex idx = createIndex(item->row(), 0, item);
Q_EMIT dataChanged(idx, idx);
}
void KateFileTreeModel::documentActivated(const KTextEditor::Document *doc)
{
if (!m_shadingEnabled) {
return;
}
auto it = m_docmap.find(doc);
if (it == m_docmap.end()) {
return;
}
ProxyItem *item = it.value();
m_viewHistory.erase(std::remove(m_viewHistory.begin(), m_viewHistory.end(), item), m_viewHistory.end());
m_viewHistory.insert(m_viewHistory.begin(), item);
while (m_viewHistory.size() > MaxHistoryItems) {
m_viewHistory.pop_back();
}
updateBackgrounds();
}
void KateFileTreeModel::documentEdited(const KTextEditor::Document *doc)
{
if (!m_shadingEnabled) {
return;
}
auto it = m_docmap.find(doc);
if (it == m_docmap.end()) {
return;
}
ProxyItem *item = it.value();
m_editHistory.erase(std::remove(m_editHistory.begin(), m_editHistory.end(), item), m_editHistory.end());
m_editHistory.insert(m_editHistory.begin(), item);
while (m_editHistory.size() > MaxHistoryItems) {
m_editHistory.pop_back();
}