gitstatusmodel.cpp 4.54 KB
Newer Older
Waqar Ahmed's avatar
Waqar Ahmed committed
1
2
3
4
5
/*
    SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/
6
7
8
#include "gitstatusmodel.h"

#include <QDebug>
9
#include <QFileInfo>
10
11
#include <QFont>
#include <QIcon>
Waqar Ahmed's avatar
Waqar Ahmed committed
12
#include <QMimeDatabase>
13

Waqar Ahmed's avatar
Waqar Ahmed committed
14
15
#include <KLocalizedString>

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
static constexpr int Staged = 0;
static constexpr int Changed = 1;
static constexpr int Conflict = 2;
static constexpr int Untrack = 3;
static constexpr quintptr Root = 0xFFFFFFFF;

GitStatusModel::GitStatusModel(QObject *parent)
    : QAbstractItemModel(parent)
{
    // setup root rows
    beginInsertRows(QModelIndex(), 0, 3);
    endInsertRows();
}

QModelIndex GitStatusModel::index(int row, int column, const QModelIndex &parent) const
{
    auto rootIndex = Root;
    if (parent.isValid()) {
        if (parent.internalId() == Root) {
            rootIndex = parent.row();
        }
    }
    return createIndex(row, column, rootIndex);
}

QModelIndex GitStatusModel::parent(const QModelIndex &child) const
{
    if (!child.isValid()) {
        return QModelIndex();
    }

    return createIndex(child.internalId(), 0, Root);
}

int GitStatusModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return 4;
    }

    if (parent.internalId() == Root) {
Waqar Ahmed's avatar
Waqar Ahmed committed
57
58
        if (parent.row() < 0 || parent.row() > 3) {
            return 0;
59
        }
Waqar Ahmed's avatar
Waqar Ahmed committed
60
61

        return m_nodes[parent.row()].size();
62
63
64
65
66
67
    }
    return 0;
}

int GitStatusModel::columnCount(const QModelIndex &) const
{
68
    return 2;
69
70
71
72
73
74
75
76
77
78
79
80
}

QVariant GitStatusModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return {};
    }

    auto row = index.row();

    if (index.internalId() == Root) {
        if (role == Qt::DisplayRole) {
81
82
            if (index.column() == 1) {
                return QString::number(m_nodes[row].count());
83
            } else {
84
85
86
87
88
89
90
91
92
93
94
                if (row == Staged) {
                    return i18n("Staged");
                } else if (row == Untrack) {
                    return i18n("Untracked");
                } else if (row == Conflict) {
                    return i18n("Conflict");
                } else if (row == Changed) {
                    return i18n("Modified");
                } else {
                    Q_UNREACHABLE();
                }
95
96
97
98
99
            }
        } else if (role == Qt::FontRole) {
            QFont bold;
            bold.setBold(true);
            return bold;
Waqar Ahmed's avatar
Waqar Ahmed committed
100
        } else if (role == Role::TreeItemType) {
101
            return NodeStage + row;
102
103
104
105
106
107
        } else if (role == Qt::TextAlignmentRole) {
            if (index.column() == 0) {
                return Qt::AlignLeft;
            } else {
                return Qt::AlignRight;
            }
108
109
110
111
112
113
114
        }
    } else {
        int rootIndex = index.internalId();
        if (rootIndex < 0 || rootIndex > 3) {
            return QVariant();
        }

Waqar Ahmed's avatar
Waqar Ahmed committed
115
        if (role == Qt::DisplayRole) {
116
117
118
119
120
121
122
123
124
125
            if (index.column() == 0) {
                const auto filename = QFileInfo(m_nodes[rootIndex].at(row).file).fileName();
                if (filename.isEmpty()) {
                    return m_nodes[rootIndex].at(row).file;
                }
                return filename;
            } else {
                return QString(QLatin1Char(m_nodes[rootIndex].at(row).statusChar));
            }
        } else if (role == FileNameRole) {
Waqar Ahmed's avatar
Waqar Ahmed committed
126
            return m_nodes[rootIndex].at(row).file;
Waqar Ahmed's avatar
Waqar Ahmed committed
127
        } else if (role == Qt::DecorationRole) {
128
129
            if (index.column() == 0)
                return QIcon::fromTheme(QMimeDatabase().mimeTypeForFile(m_nodes[rootIndex].at(row).file, QMimeDatabase::MatchExtension).iconName());
Waqar Ahmed's avatar
Waqar Ahmed committed
130
        } else if (role == Role::TreeItemType) {
131
            return ItemType::NodeFile;
132
133
        } else if (role == Qt::ToolTipRole) {
            return QString(m_nodes[rootIndex].at(row).file + GitUtils::statusString(m_nodes[rootIndex].at(row).status));
134
135
136
137
138
139
        } else if (role == Qt::TextAlignmentRole) {
            if (index.column() == 0) {
                return Qt::AlignLeft;
            } else {
                return Qt::AlignRight;
            }
Waqar Ahmed's avatar
Waqar Ahmed committed
140
        }
141
142
143
144
    }

    return {};
}
145
void GitStatusModel::addItems(GitUtils::GitParsedStatus status)
146
147
{
    beginResetModel();
148
149
150
151
    m_nodes[Staged] = std::move(status.staged);
    m_nodes[Changed] = std::move(status.changed);
    m_nodes[Conflict] = std::move(status.unmerge);
    m_nodes[Untrack] = std::move(status.untracked);
152
153
    endResetModel();
}
Waqar Ahmed's avatar
Waqar Ahmed committed
154
155
156
157

QVector<int> GitStatusModel::emptyRows()
{
    QVector<int> empty;
Waqar Ahmed's avatar
Waqar Ahmed committed
158
159
160
161
    for (int i = 0; i < 4; ++i) {
        if (m_nodes[i].isEmpty()) {
            empty.append(i);
        }
Waqar Ahmed's avatar
Waqar Ahmed committed
162
163
164
    }
    return empty;
}