forked from wtr9987/gvtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagtree.cpp
382 lines (312 loc) · 9.78 KB
/
tagtree.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
/* --------------------------------------------- */
/* */
/* Copyright (C) 2021 Wolfgang Trummer */
/* Contact: [email protected] */
/* */
/* gvtree V1.8-0 */
/* */
/* git version tree browser */
/* */
/* 9. October 2022 */
/* */
/* This program is licensed under */
/* GNU GENERAL PUBLIC LICENSE */
/* Version 3, 29 June 2007 */
/* */
/* --------------------------------------------- */
#include <iostream>
#include <QMenu>
#include <QAction>
#include "tagtree.h"
#include "mainwindow.h"
#include "graphwidget.h"
using namespace std;
TagTree::TagTree(GraphWidget* _graph, MainWindow* _mwin) : QTreeView(_mwin),
graph(_graph),
mwin(_mwin),
treemodel(NULL),
root(NULL),
search(NULL)
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(onCustomContextMenu(const QPoint&)));
connect(this, SIGNAL(clicked(const QModelIndex&)),
this, SLOT(focusSelection(const QModelIndex&)));
treemodel = new QStandardItemModel(NULL);
setModel(treemodel);
setSortingEnabled(true);
resetTagTree();
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
header()->setSectionResizeMode(QHeaderView::Stretch);
#else
header()->setResizeMode(QHeaderView::Stretch);
#endif
show();
}
void TagTree::updateSearchResult(QList<Version*>& _matches)
{
// Remove nodes under "Search Result" node
QStandardItem* p = NULL;
for (int i = 0; i < root->rowCount(); i++)
{
if (root->child(i)->text() == "Search Result")
{
p = root->child(i);
break;
}
}
if (!p)
{
// error
return;
}
while (p->rowCount() > 0)
{
treemodel->removeRow(p->rowCount() - 1, p->index());
}
// Insert matches from search dialog
foreach(Version * v, _matches)
{
QStandardItem* c1 = new QStandardItem("");
c1->setIcon(QIcon(":/images/gvt_dot.png"));
c1->setEditable(false);
QStandardItem* c2 = new QStandardItem(v->getKeyInformation().find("Commit Date").value().join(" "));
c2->setData(QVariant::fromValue(VersionPointer(v)), Qt::UserRole + 1);
c2->setEditable(false);
QList<QStandardItem*> row = QList<QStandardItem*>() << c1 << c2;
p->appendRow(row);
}
// Set folder symbol if needed
p->setIcon(_matches.size() ? QIcon().fromTheme("folder") : QIcon());
// display search results
expand(p->index());
}
void TagTree::compress(QStandardItem* _p)
{
_p = (_p == NULL) ? root : _p;
if (_p->rowCount() == 1 && !_p->child(0, 0)->hasChildren())
{
QStandardItem* c = _p->takeChild(0, 1);
if (c && c->data(Qt::UserRole + 1).value<VersionPointer>() && _p->parent())
{
_p->setIcon(QIcon(":/images/gvt_dot.png"));
_p->parent()->setChild(_p->row(), 1, c);
treemodel->removeRow(0, _p->index());
return;
}
else
{
_p->setIcon(QIcon().fromTheme("folder"));
}
}
if (_p->rowCount() > 1)
{
_p->setIcon(QIcon().fromTheme("folder"));
for (int i = 0; i < _p->rowCount(); i++)
{
QStandardItem* c0 = _p->child(i, 0);
QStandardItem* c1 = _p->child(i, 1);
if (c1 && c1->data(Qt::UserRole + 1).value<VersionPointer>()
&& (c0->text() == c1->text()))
{
c1->setText("");
}
}
}
for (int i = 0; i < _p->rowCount(); i++)
compress(_p->child(i));
}
void TagTree::addData(const Version* _v)
{
QString timestamp;
QMap<QString, QStringList>::const_iterator dt = _v->getKeyInformation().find("Commit Date");
if (dt != _v->getKeyInformation().end())
timestamp = dt.value().join(" ");
for (QMap<QString, QStringList>::const_iterator it = _v->getKeyInformation().begin();
it != _v->getKeyInformation().end();
it++)
{
QString key = it.key();
if (key == "_input" || key == "Comment")
continue;
else if (key == "CommentRaw")
key = "Comment";
// level 1 : taginfo key
QStandardItem* p = findOrInsert(root, key, false);
if (key == "Commit Date")
{
QString year = timestamp.mid(0, 4);
QString month = timestamp.mid(5, 2);
QString day = timestamp.mid(8, 2);
QString daytime = timestamp.mid(11, 8);
QStringList ts = QStringList() << year << month << day << daytime;
foreach(const QString &tmp, ts)
{
p = findOrInsert(p, tmp);
}
insertLeaf(p, timestamp, _v);
}
else
{
foreach(const QString &val, it.value())
{
insertLeaf(findOrInsert(p, val), timestamp, _v);
}
}
}
}
QStandardItem* TagTree::findOrInsert(QStandardItem* _p, const QString& _val, bool _sort)
{
int i = 0;
for (; i < _p->rowCount(); i++)
{
if (_p->child(i)->text() == _val)
return _p->child(i);
if (_sort && _p->child(i)->text() > _val)
break;
}
QStandardItem* t = new QStandardItem(_val);
t->setEditable(false);
if (_sort)
_p->insertRow(i, t);
else
_p->appendRow(t);
return t;
}
void TagTree::insertLeaf(QStandardItem* _p, const QString& _timestamp, const Version* _v)
{
QList<QStandardItem*> columns;
columns << new QStandardItem(_timestamp) << new QStandardItem(_timestamp);
columns.front()->setEditable(false);
columns.front()->setIcon(QIcon(":/images/gvt_dot.png"));
columns.back()->setData(QVariant::fromValue(VersionPointer(_v)), Qt::UserRole + 1);
columns.back()->setEditable(false);
_p->appendRow(columns);
}
void TagTree::resetTagTree()
{
if (!treemodel)
return;
treemodel->clear();
treemodel->setHorizontalHeaderItem(0, new QStandardItem(QString("Tag Information")));
treemodel->setHorizontalHeaderItem(1, new QStandardItem(QString("")));
root = treemodel->invisibleRootItem();
root->setEditable(false);
QStringList nodeInfo;
nodeInfo << "Search Result"
<< "HEAD"
<< "Release Label"
<< "Baseline Label"
<< "Commit Date"
<< "Branch"
<< "FIX/PQT Label"
<< "HO Label"
<< "Other Tags"
<< "User Name" // ... many versions
<< "Comment" // ... don't know if useful, maby not
<< "Hash"; // ... not useful, use search instead
foreach(const QString &key, nodeInfo)
{
QStandardItem* t = new QStandardItem(key);
t->setEditable(false);
root->appendRow(t);
}
search = new QLineEdit(this);
connect(search, SIGNAL(textEdited(const QString&)), this, SLOT(lookupId(const QString&)));
connect(search, SIGNAL(returnPressed()), this, SLOT(focusGraph()));
setIndexWidget(treemodel->index(0, 1), search);
}
void TagTree::onCustomContextMenu(const QPoint& point)
{
QMenu* menu = new QMenu(this);
QAction* act = new QAction("Collapse all", this);
menu->addAction(act);
connect(act, SIGNAL(triggered()), this, SLOT(collapseAll()));
act = new QAction("Expand all", this);
menu->addAction(act);
connect(act, SIGNAL(triggered()), this, SLOT(expandAll()));
menu->exec(viewport()->mapToGlobal(point));
}
void TagTree::mousePressEvent (QMouseEvent* event)
{
if (event->button() == Qt::RightButton)
{
event->accept();
}
else
QTreeView::mousePressEvent(event);
}
void TagTree::focusSelection(const QModelIndex&)
{
const QModelIndexList& sel = selectedIndexes();
graph->resetMatches();
foreach (const QModelIndex &idx, sel)
{
if (idx.column() == 0)
{
QList<Version*> collect;
collectSubitems(idx, collect);
graph->focusElements(collect);
graph->setFocus();
break;
}
}
}
void TagTree::collectSubitems(const QModelIndex& _p, QList<Version*>& _collect)
{
// check for version
QModelIndex sib = _p.sibling(_p.row(), 1);
if (sib.isValid())
{
Version* v = sib.data(Qt::UserRole + 1).value<VersionPointer>();
if (v)
{
QString arg = _p.parent().data(Qt::DisplayRole).toString();
if (arg == "Search Result")
{
QRegExp pattern(search->text());
v->findMatch(pattern, search->text(), false);
}
else
{
v->addLocalVersionInfo(arg);
}
_collect.push_back(v);
return;
}
}
//
int rows = treemodel->rowCount(_p);
for (int i = 0; i < rows; i++)
{
collectSubitems(treemodel->index(i, 0, _p), _collect);
}
}
void TagTree::lookupId(const QString& _text, bool _exactMatch)
{
QString pattern = _text;
QList<Version*> matches;
if (pattern.size() < 3)
{
graph->resetMatches();
pattern = QString("");
}
else
{
graph->matchVersions(pattern, matches, _exactMatch);
if (matches.size())
graph->focusElements(matches);
}
updateSearchResult(matches);
// keep focus
search->setFocus();
}
void TagTree::focusGraph()
{
graph->setFocus();
}
QLineEdit* TagTree::getSearch()
{
return search;
}