katefiletreemodel.h 3.59 KB
Newer Older
1
/* This file is part of the KDE project
2
3
4
   SPDX-FileCopyrightText: 2010 Thomas Fjellstrom <thomas@fjellstrom.ca>

   SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
8
9
*/

#ifndef KATEFILETREEMODEL_H
#define KATEFILETREEMODEL_H

Christoph Cullmann's avatar
Christoph Cullmann committed
10
11
#include <unordered_map>

12
#include <QAbstractItemModel>
Waqar Ahmed's avatar
Waqar Ahmed committed
13
#include <QBrush>
14
#include <QColor>
15
16

#include <ktexteditor/modificationinterface.h>
Dominik Haumann's avatar
Dominik Haumann committed
17
18
19
namespace KTextEditor
{
class Document;
20
21
22
}

class ProxyItem;
23
24
25
26
class ProxyItemDir;

QDebug operator<<(QDebug dbg, ProxyItem *item);
QDebug operator<<(QDebug dbg, ProxyItemDir *item);
27
28
29

class KateFileTreeModel : public QAbstractItemModel
{
Dominik Haumann's avatar
Dominik Haumann committed
30
    Q_OBJECT
31

Dominik Haumann's avatar
Dominik Haumann committed
32
public:
33
    enum { DocumentRole = Qt::UserRole + 1, PathRole, OpeningOrderRole, DocumentTreeRole };
34

35
    KateFileTreeModel(QObject *p);
Kevin Funk's avatar
Kevin Funk committed
36
    ~KateFileTreeModel() override;
37

38
    /* QAbstractItemModel implementations */
Kevin Funk's avatar
Kevin Funk committed
39
40
41
42
43
44
45
46
47
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QModelIndex parent(const QModelIndex &index) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
48

49
50
    QMimeData *mimeData(const QModelIndexList &indexes) const override;

51
    /* extra api for view */
Michal Humpula's avatar
Michal Humpula committed
52
    QModelIndex docIndex(const KTextEditor::Document *) const;
53

54
    bool isDir(const QModelIndex &index) const;
55

56
    bool listMode() const;
57
58
59
60
    void setListMode(bool);

    void setShadingEnabled(bool);

Michal Humpula's avatar
Michal Humpula committed
61
    void setEditShade(const QColor &);
62

Michal Humpula's avatar
Michal Humpula committed
63
    void setViewShade(const QColor &);
64

Michal Humpula's avatar
Michal Humpula committed
65
    bool showFullPathOnRoots(void) const;
66
    void setShowFullPathOnRoots(bool);
67

Michal Humpula's avatar
Michal Humpula committed
68
69
    void documentsOpened(const QList<KTextEditor::Document *> &);
    /* used strictly for the item coloring */
Michal Humpula's avatar
Michal Humpula committed
70
    void documentActivated(const KTextEditor::Document *);
Michal Humpula's avatar
Michal Humpula committed
71
    void documentEdited(const KTextEditor::Document *);
72
    void resetHistory();
Michal Humpula's avatar
Michal Humpula committed
73

Dominik Haumann's avatar
Dominik Haumann committed
74
public Q_SLOTS:
75
76
77
    void documentOpened(KTextEditor::Document *);
    void documentClosed(KTextEditor::Document *);
    void documentNameChanged(KTextEditor::Document *);
Dominik Haumann's avatar
Dominik Haumann committed
78
79
    void documentModifiedChanged(KTextEditor::Document *);
    void documentModifiedOnDisc(KTextEditor::Document *, bool, KTextEditor::ModificationInterface::ModifiedOnDiskReason);
80

Dominik Haumann's avatar
Dominik Haumann committed
81
Q_SIGNALS:
82
    void triggerViewChangeAfterNameChange();
83

Dominik Haumann's avatar
Dominik Haumann committed
84
private:
Michal Humpula's avatar
Michal Humpula committed
85
86
    ProxyItemDir *findRootNode(const QString &name, const int r = 1) const;
    ProxyItemDir *findChildNode(const ProxyItemDir *parent, const QString &name) const;
87
    void insertItemInto(ProxyItemDir *root, ProxyItem *item);
88
    void handleInsert(ProxyItem *item);
Michal Humpula's avatar
Michal Humpula committed
89
    void handleNameChange(ProxyItem *item);
90
    void handleEmptyParents(ProxyItemDir *item);
Michal Humpula's avatar
Michal Humpula committed
91
    void setupIcon(ProxyItem *item) const;
Michal Humpula's avatar
Michal Humpula committed
92
    void updateItemPathAndHost(ProxyItem *item) const;
93
    void handleDuplicitRootDisplay(ProxyItemDir *item);
94

95
96
97
98
    void updateBackgrounds(bool force = false);

    void initModel();
    void clearModel();
99
    void connectDocument(const KTextEditor::Document *);
100

Dominik Haumann's avatar
Dominik Haumann committed
101
private:
102
103
104
105
106
    ProxyItemDir *m_root;
    QHash<const KTextEditor::Document *, ProxyItem *> m_docmap;

    bool m_shadingEnabled;

Waqar Ahmed's avatar
Waqar Ahmed committed
107
108
109
    std::vector<ProxyItem *> m_viewHistory;
    std::vector<ProxyItem *> m_editHistory;
    std::unordered_map<ProxyItem *, QBrush> m_brushes;
110
111
112
113
114

    QColor m_editShade;
    QColor m_viewShade;

    bool m_listMode;
115
116
117
};

#endif /* KATEFILETREEMODEL_H */