-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqloghistoryitemdelegate.cpp
More file actions
316 lines (265 loc) · 10.8 KB
/
Copy pathqloghistoryitemdelegate.cpp
File metadata and controls
316 lines (265 loc) · 10.8 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
#include "qloghistoryitemdelegate.h"
#include "qloghistorytablewidget.h"
#include <QPalette>
#include <QPainter>
#include <QFontMetrics>
#include <QApplication>
#include <QPainterPath>
QLogHistoryItemDelegate::QLogHistoryItemDelegate(QWidget *parent)
: QStyledItemDelegate(parent)
{
}
void QLogHistoryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 0)
{
// First draw standard cell background/selection
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.text = "";
auto *widget = option.widget;
auto *style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
auto data = index.data(Qt::UserRole).toList();
auto *table = qobject_cast<const QLogHistoryTableWidget*>(option.widget);
if (!table) {
table = qobject_cast<const QLogHistoryTableWidget*>(parent());
}
QString sha;
if (table)
{
auto sibling = index.sibling(index.row(), 1);
sha = sibling.data(Qt::UserRole + 1).toString();
}
bool isCurrentCommit = false;
if (table && !sha.isEmpty())
{
auto refs = table->getReferences(sha);
for (const auto &ref : refs)
{
if (ref.type == QGitRef::CurrentBranch)
{
isCurrentCommit = true;
break;
}
}
}
if (data.count() >= 4)
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
int commitLane = data.at(0).toInt();
auto passingLines = data.at(1).toList();
auto parentConnections = data.at(2).toList();
auto activeLanesAtTop = data.at(3).toList();
int rowH = option.rect.height();
constexpr int padding = 5;
int circleRadius = (rowH / 2) - padding;
if (circleRadius < 4) circleRadius = 4;
auto getLaneColor = [](int lane) -> QColor {
static const QList<QColor> colors = {
QColor(71, 143, 178), // Blue
QColor(112, 191, 112), // Green
QColor(230, 126, 34), // Orange
QColor(155, 89, 182), // Purple
QColor(231, 76, 60), // Red
QColor(241, 196, 15), // Yellow
QColor(52, 152, 219), // Light Blue
QColor(26, 188, 156) // Turquoise
};
return colors.at(lane % colors.size());
};
auto getLaneX = [&](int lane) -> int {
return option.rect.left() + (rowH / 2) + padding + (rowH * lane);
};
int topY = option.rect.top();
int centerY = option.rect.top() + (rowH / 2);
int bottomY = option.rect.top() + rowH;
// 1. Draw passing lines (straight vertical)
for (const auto &laneVar : passingLines)
{
int lane = laneVar.toInt();
int x = getLaneX(lane);
painter->setPen(QPen(getLaneColor(lane), 2, Qt::SolidLine, Qt::RoundCap));
painter->drawLine(QPoint(x, topY), QPoint(x, bottomY));
}
int commitX = getLaneX(commitLane);
// 2. Draw line from top to node for commitLane (if active at the top)
bool activeAtTop = false;
for (const auto &laneVar : activeLanesAtTop)
{
if (laneVar.toInt() == commitLane)
{
activeAtTop = true;
break;
}
}
if (activeAtTop)
{
painter->setPen(QPen(getLaneColor(commitLane), 2, Qt::SolidLine, Qt::RoundCap));
painter->drawLine(QPoint(commitX, topY), QPoint(commitX, centerY));
}
// 3. Draw parent connections
for (int i = 0; i < parentConnections.size() - 1; i += 2)
{
int from = parentConnections.at(i).toInt();
int to = parentConnections.at(i + 1).toInt();
int fromX = getLaneX(from);
int toX = getLaneX(to);
if (from == to)
{
// Straight vertical line to bottom
painter->setPen(QPen(getLaneColor(from), 2, Qt::SolidLine, Qt::RoundCap));
painter->drawLine(QPoint(fromX, centerY), QPoint(fromX, bottomY));
}
else
{
// Straight diagonal split/merge line to bottom
painter->setPen(QPen(getLaneColor(to), 2, Qt::SolidLine, Qt::RoundCap));
painter->drawLine(QPoint(fromX, centerY), QPoint(toX, bottomY));
}
}
// 4. Draw child connections (merging from top to center)
if (data.count() >= 5)
{
auto childConnections = data.at(4).toList();
for (int i = 0; i < childConnections.size() - 1; i += 2)
{
int from = childConnections.at(i).toInt();
int to = childConnections.at(i + 1).toInt();
int fromX = getLaneX(from);
int toX = getLaneX(to);
// Straight child diagonal merge line from top
painter->setPen(QPen(getLaneColor(from), 2, Qt::SolidLine, Qt::RoundCap));
painter->drawLine(QPoint(fromX, topY), QPoint(toX, centerY));
}
}
// 5. Draw the commit node circle
QPoint circleCenter(commitX, centerY);
painter->setPen(QPen(Qt::black, 1));
painter->setBrush(QBrush(getLaneColor(commitLane)));
painter->drawEllipse(circleCenter, circleRadius, circleRadius);
// Premium visual design highlight: inner white circle for current checked-out commit
if (isCurrentCommit)
{
painter->setBrush(QBrush(Qt::white));
painter->drawEllipse(circleCenter, circleRadius / 2, circleRadius / 2);
}
painter->restore();
}
}
else if (index.column() == 1)
{
auto *table = qobject_cast<const QLogHistoryTableWidget*>(option.widget);
if (!table) {
table = qobject_cast<const QLogHistoryTableWidget*>(parent());
}
QString sha = index.data(Qt::UserRole + 1).toString();
QList<QGitRef> refs;
if (table && !sha.isEmpty())
{
refs = table->getReferences(sha);
}
// Draw standard background, selection, focus, etc. but without text to avoid overlap
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
opt.text = "";
auto *widget = option.widget;
auto *style = widget ? widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
int startX = option.rect.left() + 6;
int rowH = option.rect.height();
if (!refs.isEmpty())
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
// Font for badges: bold and slightly smaller
QFont badgeFont = painter->font();
badgeFont.setPointSizeF(badgeFont.pointSizeF() - 1.0);
badgeFont.setBold(true);
painter->setFont(badgeFont);
QFontMetrics fm(badgeFont);
for (const auto &ref : refs)
{
QString name = ref.name;
int textW = fm.horizontalAdvance(name);
int badgeH = fm.height() + 2; // small padding
int badgeW = textW + 10; // horizontal padding
// Ensure it fits within the option rect vertically
int badgeY = option.rect.top() + (rowH - badgeH) / 2;
QRect badgeRect(startX, badgeY, badgeW, badgeH);
QColor bg, border, textCol;
switch (ref.type)
{
case QGitRef::CurrentBranch:
bg = QColor(220, 245, 225);
border = QColor(30, 110, 45);
textCol = QColor(30, 110, 45);
break;
case QGitRef::LocalBranch:
bg = QColor(225, 240, 255);
border = QColor(20, 80, 160);
textCol = QColor(20, 80, 160);
break;
case QGitRef::RemoteBranch:
bg = QColor(255, 235, 230);
border = QColor(180, 50, 20);
textCol = QColor(180, 50, 20);
break;
case QGitRef::Tag:
bg = QColor(255, 245, 200);
border = QColor(120, 80, 0);
textCol = QColor(120, 80, 0);
break;
}
// Draw badge background & border
painter->setPen(QPen(border, 1));
painter->setBrush(QBrush(bg));
painter->drawRoundedRect(badgeRect, 3.0, 3.0);
// Draw badge text
painter->setPen(textCol);
painter->drawText(badgeRect, Qt::AlignCenter, name);
startX += badgeW + 4; // spacing between badges
}
painter->restore();
}
// Check if this commit represents the current checked-out HEAD
bool isCurrentCommit = false;
for (const auto &ref : refs)
{
if (ref.type == QGitRef::CurrentBranch)
{
isCurrentCommit = true;
break;
}
}
// Draw commit message text
painter->save();
QColor textColor;
if (option.state & QStyle::State_Selected)
{
textColor = option.palette.color(QPalette::HighlightedText);
}
else
{
textColor = option.palette.color(QPalette::Text);
}
painter->setPen(textColor);
if (isCurrentCommit)
{
QFont boldF = painter->font();
boldF.setBold(true);
painter->setFont(boldF);
}
QRect textRect = option.rect;
textRect.setLeft(startX + 2);
QString msg = index.data(Qt::DisplayRole).toString();
QString elidedMsg = painter->fontMetrics().elidedText(msg, Qt::ElideRight, textRect.width());
painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, elidedMsg);
painter->restore();
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}