-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitfilestatustreewidget.cpp
More file actions
336 lines (309 loc) · 13.9 KB
/
Copy pathqgitfilestatustreewidget.cpp
File metadata and controls
336 lines (309 loc) · 13.9 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
#include "qgitfilestatustreewidget.h"
#include <QApplication>
#include <QStyle>
#include <QFileInfo>
#include <QHeaderView>
QGitFileStatusTreeWidget::QGitFileStatusTreeWidget(QWidget *parent)
: QTreeWidget(parent)
{
setHeaderHidden(true);
setColumnCount(1);
}
void QGitFileStatusTreeWidget::setViewLayoutMode(ViewLayoutMode mode)
{
m_layoutMode = mode;
if (m_layoutMode == FlatMulti)
{
setHeaderHidden(false);
setColumnCount(3);
QStringList headers = { tr("Name"), tr("Path"), tr("Status") };
setHeaderLabels(headers);
header()->setSectionResizeMode(0, QHeaderView::Interactive);
header()->setSectionResizeMode(1, QHeaderView::Stretch);
header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
}
else
{
setHeaderHidden(true);
setColumnCount(1);
}
}
QIcon QGitFileStatusTreeWidget::getFileIcon(git_status_t status, bool isStaged)
{
static QIcon iconNew(":/small/added");
static QIcon iconClean(":/small/clean");
static QIcon iconModified(":/small/modified");
static QIcon iconRemoved(":/small/deleted");
static QIcon iconRenamed(":/small/images/small/renamed.png");
static QIcon iconConflict(":/small/images/small/conflict.png");
static QIcon iconIgnored(":/small/ignored");
static QIcon iconUnknown(":/small/unknown");
if (isStaged) {
if (status & GIT_STATUS_INDEX_NEW) return iconNew;
else if (status & GIT_STATUS_INDEX_DELETED) return iconRemoved;
else if (status & GIT_STATUS_INDEX_RENAMED) return iconRenamed;
else return iconModified;
} else {
uint32_t wt_status = status & (GIT_STATUS_CURRENT | GIT_STATUS_WT_NEW | GIT_STATUS_WT_MODIFIED | GIT_STATUS_WT_DELETED | GIT_STATUS_IGNORED | GIT_STATUS_CONFLICTED | GIT_STATUS_WT_RENAMED);
if (wt_status == GIT_STATUS_CURRENT) return iconClean;
else if (wt_status & GIT_STATUS_WT_NEW) return iconNew;
else if (wt_status & GIT_STATUS_WT_MODIFIED) return iconModified;
else if (wt_status & GIT_STATUS_WT_DELETED) return iconRemoved;
else if (wt_status & GIT_STATUS_WT_RENAMED) return iconRenamed;
else if (wt_status & GIT_STATUS_IGNORED) return iconIgnored;
else if (wt_status & GIT_STATUS_CONFLICTED) return iconConflict;
else return iconUnknown;
}
}
QString QGitFileStatusTreeWidget::getStatusText(git_status_t status)
{
if (status & GIT_STATUS_CONFLICTED) return tr("Conflicted");
if (status & (GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_NEW)) return tr("Added");
if (status & (GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED)) return tr("Modified");
if (status & (GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_DELETED)) return tr("Deleted");
if (status & (GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED)) return tr("Renamed");
if (status & (GIT_STATUS_INDEX_TYPECHANGE | GIT_STATUS_WT_TYPECHANGE)) return tr("Typechange");
if (status & GIT_STATUS_IGNORED) return tr("Ignored");
return tr("Unknown");
}
void QGitFileStatusTreeWidget::populateFiles(
const QList<QPair<QString, git_status_t>> &files,
bool isStagedCategory,
bool isPendingCategory)
{
blockSignals(true);
clear();
setEnabled(true);
for (const auto &filePair : files)
{
const QString &file = filePair.first;
git_status_t status = filePair.second;
bool hasStaged = status & (GIT_STATUS_INDEX_NEW | GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_INDEX_DELETED | GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_TYPECHANGE);
bool hasUnstaged = (status == GIT_STATUS_CURRENT) || (status & (GIT_STATUS_WT_NEW | GIT_STATUS_WT_MODIFIED | GIT_STATUS_WT_DELETED | GIT_STATUS_WT_TYPECHANGE | GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_UNREADABLE | GIT_STATUS_IGNORED | GIT_STATUS_CONFLICTED));
if (isPendingCategory)
{
Qt::CheckState state = Qt::PartiallyChecked;
if (hasStaged && !hasUnstaged) state = Qt::Checked;
else if (!hasStaged && hasUnstaged) state = Qt::Unchecked;
QIcon icon = getFileIcon(status, hasStaged);
if (m_layoutMode == FlatSingle)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, state);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, file);
item->setIcon(0, icon);
}
else if (m_layoutMode == FlatMulti)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, state);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
QFileInfo info(file);
item->setText(0, info.fileName());
item->setIcon(0, icon);
item->setText(1, info.path() == "." ? "" : info.path());
item->setText(2, getStatusText(status));
}
else if (m_layoutMode == TreeView)
{
QStringList parts = file.split('/');
QTreeWidgetItem *parent = nullptr;
for (int i = 0; i < parts.size() - 1; ++i)
{
QString dirName = parts.at(i);
QTreeWidgetItem *found = nullptr;
int childCount = parent ? parent->childCount() : topLevelItemCount();
for (int j = 0; j < childCount; ++j)
{
QTreeWidgetItem *child = parent ? parent->child(j) : topLevelItem(j);
if (child->text(0) == dirName && child->data(0, Qt::UserRole).toString().isEmpty())
{
found = child;
break;
}
}
if (!found)
{
parent = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
parent->setText(0, dirName);
parent->setIcon(0, QApplication::style()->standardIcon(QStyle::SP_DirIcon));
parent->setFlags(parent->flags() | Qt::ItemIsUserCheckable);
parent->setCheckState(0, Qt::Unchecked);
}
else
{
parent = found;
}
}
QTreeWidgetItem *item = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, state);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, parts.last());
item->setIcon(0, icon);
}
}
else if (isStagedCategory && hasStaged)
{
if (m_layoutMode == FlatSingle)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Checked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, file);
item->setIcon(0, getFileIcon(status, true));
}
else if (m_layoutMode == FlatMulti)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Checked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
QFileInfo info(file);
item->setText(0, info.fileName());
item->setIcon(0, getFileIcon(status, true));
item->setText(1, info.path() == "." ? "" : info.path());
item->setText(2, getStatusText(status));
}
else if (m_layoutMode == TreeView)
{
QStringList parts = file.split('/');
QTreeWidgetItem *parent = nullptr;
for (int i = 0; i < parts.size() - 1; ++i)
{
QString dirName = parts.at(i);
QTreeWidgetItem *found = nullptr;
int childCount = parent ? parent->childCount() : topLevelItemCount();
for (int j = 0; j < childCount; ++j)
{
QTreeWidgetItem *child = parent ? parent->child(j) : topLevelItem(j);
if (child->text(0) == dirName && child->data(0, Qt::UserRole).toString().isEmpty())
{
found = child;
break;
}
}
if (!found)
{
parent = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
parent->setText(0, dirName);
parent->setIcon(0, QApplication::style()->standardIcon(QStyle::SP_DirIcon));
}
else
{
parent = found;
}
}
QTreeWidgetItem *item = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Checked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, parts.last());
item->setIcon(0, getFileIcon(status, true));
}
}
else if (!isStagedCategory && !isPendingCategory && hasUnstaged)
{
if (m_layoutMode == FlatSingle)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Unchecked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, file);
item->setIcon(0, getFileIcon(status, false));
}
else if (m_layoutMode == FlatMulti)
{
QTreeWidgetItem *item = new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Unchecked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
QFileInfo info(file);
item->setText(0, info.fileName());
item->setIcon(0, getFileIcon(status, false));
item->setText(1, info.path() == "." ? "" : info.path());
item->setText(2, getStatusText(status));
}
else if (m_layoutMode == TreeView)
{
QStringList parts = file.split('/');
QTreeWidgetItem *parent = nullptr;
for (int i = 0; i < parts.size() - 1; ++i)
{
QString dirName = parts.at(i);
QTreeWidgetItem *found = nullptr;
int childCount = parent ? parent->childCount() : topLevelItemCount();
for (int j = 0; j < childCount; ++j)
{
QTreeWidgetItem *child = parent ? parent->child(j) : topLevelItem(j);
if (child->text(0) == dirName && child->data(0, Qt::UserRole).toString().isEmpty())
{
found = child;
break;
}
}
if (!found)
{
parent = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
parent->setText(0, dirName);
parent->setIcon(0, QApplication::style()->standardIcon(QStyle::SP_DirIcon));
}
else
{
parent = found;
}
}
QTreeWidgetItem *item = parent ? new QTreeWidgetItem(parent) : new QTreeWidgetItem(this);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Unchecked);
item->setData(0, Qt::UserRole, file);
item->setData(0, Qt::UserRole + 1, (int)status);
item->setText(0, parts.last());
item->setIcon(0, getFileIcon(status, false));
}
}
}
if (m_layoutMode == TreeView) {
expandAll();
}
blockSignals(false);
}
void QGitFileStatusTreeWidget::updateFolderCheckStates(QTreeWidgetItem *item)
{
if (!item) return;
int checkedCount = 0;
int partiallyCheckedCount = 0;
int totalChildren = item->childCount();
if (totalChildren == 0) return;
for (int i = 0; i < totalChildren; ++i)
{
QTreeWidgetItem *child = item->child(i);
updateFolderCheckStates(child);
Qt::CheckState state = child->checkState(0);
if (state == Qt::Checked) {
checkedCount++;
} else if (state == Qt::PartiallyChecked) {
partiallyCheckedCount++;
}
}
if (checkedCount == totalChildren) {
item->setCheckState(0, Qt::Checked);
} else if (checkedCount > 0 || partiallyCheckedCount > 0) {
item->setCheckState(0, Qt::PartiallyChecked);
} else {
item->setCheckState(0, Qt::Unchecked);
}
}