-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqgitdiffwidget.cpp
More file actions
511 lines (407 loc) · 14.1 KB
/
qgitdiffwidget.cpp
File metadata and controls
511 lines (407 loc) · 14.1 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "qgitdiffwidget.h"
#include <QStyleOptionFocusRect>
#include <QStylePainter>
#include <QPaintEvent>
#include <QScrollBar>
#include <QObject>
class QGitDiffWidgetPrivateLine
{
public:
QByteArray content;
git_off_t offset = 0;
int new_lineno = 0;
int old_lineno = 0;
char origin = '\0';
QRect rect;
};
class QGitDiffWidgetPrivateHunk
{
public:
QString header;
int new_lines = -1;
int new_start = -1;
int old_lines = -1;
int old_start = -1;
QVector<QGitDiffWidgetPrivateLine> lines;
QRect rect;
};
class QGitDiffWidgetPrivateFile
{
public:
QGitDiffFileItem new_file;
QGitDiffFileItem old_file;
uint32_t flags = 0;
int nfiles = 0;
int simularity = 0;
int status = 0;
QVector<QGitDiffWidgetPrivateHunk> hunks;
QRect rect;
};
class QGitDiffWidgetPrivate : public QObject
{
public:
explicit QGitDiffWidgetPrivate(QObject *parent = nullptr): QObject(parent)
{
}
void clear()
{
files.clear();
rect = QRect();
}
QVector<QGitDiffWidgetPrivateFile> files;
QRect rect;
};
QGitDiffWidget::QGitDiffWidget(QWidget *parent)
: QWidget(parent)
{
m_private = new QGitDiffWidgetPrivate(this);
m_font = font();
setMouseTracking(true);
}
void QGitDiffWidget::setGitDiff(const QString &first, const QString &second, const QList<QString> &files)
{
m_private->clear();
m_requestedFirst = first;
m_requestedSecond = second;
m_requestedFiles = files;
emit requestGitDiff(first, second, files, 3);
update();
}
void QGitDiffWidget::setReadonly(bool readonly)
{
if (m_readonly != readonly)
{
m_readonly = readonly;
setMouseTracking(!m_readonly);
update();
}
}
void QGitDiffWidget::refresh()
{
if (!m_requestedFiles.isEmpty())
{
emit requestGitDiff(m_requestedFirst, m_requestedSecond, m_requestedFiles, 3);
}
}
bool QGitDiffWidget::readonly() const
{
return m_readonly;
}
void QGitDiffWidget::responseGitDiff(QString first, QString second, QList<QGitDiffFile> diff, QGitError error)
{
int y = 0, file_h = 0, lineMax = 0;
QSize newSize;
QFontMetrics fm(m_font);
Q_UNUSED(first)
Q_UNUSED(second)
Q_UNUSED(error)
m_private->clear();
m_fontHeight = fm.height();
m_fontAscent = fm.ascent();
for(const QGitDiffFile &diffFile: diff)
{
const auto &hunks = diffFile.hunks();
for(const QGitDiffHunk &diffHunk: hunks)
{
const auto &lines = diffHunk.lines();
for(const QGitDiffLine &diffLine: lines)
{
auto line = diffLine.content();
int currentLine = fm.horizontalAdvance(QString::fromUtf8(line));
if (currentLine > lineMax)
lineMax = currentLine;
}
}
}
for(int c = 0; c < diff.count(); c++)
{
const auto &file_item = diff.at(c);
QGitDiffWidgetPrivateFile file;
y += contentsMargins().top();
file.new_file = file_item.new_file();
file.old_file = file_item.old_file();
file.flags = file_item.flags();
file.nfiles = file_item.nfiles();
file.simularity = file_item.simularity();
file.status = file_item.status();
file.rect.setTop(y);
file.rect.setLeft(contentsMargins().left());
file.rect.setWidth(100 + lineMax); // TODO: Unhardcode 100
file_h = m_fontHeight * 2;
for(int pos_hunk = 0; pos_hunk < file_item.hunks().count(); pos_hunk++)
{
const auto &hunk_item = file_item.hunks().at(pos_hunk);
QGitDiffWidgetPrivateHunk hunk;
int hunk_h = 0;
hunk.header = hunk_item.header();
hunk.new_lines = hunk_item.new_lines();
hunk.new_start = hunk_item.new_start();
hunk.old_lines = hunk_item.old_lines();
hunk.old_start = hunk_item.old_start();
if (pos_hunk > 0)
file_h += m_fontHeight + 1;
hunk.rect.setTop(y + file_h);
hunk.rect.setLeft(contentsMargins().left());
hunk.rect.setWidth(100 + lineMax); // TODO: Unhardcode 100
for(int pos_line = 0; pos_line < hunk_item.lines().count(); pos_line++)
{
const auto &line_item = hunk_item.lines().at(pos_line);
QGitDiffWidgetPrivateLine line;
line.content = line_item.content();
line.offset = line_item.offset();
line.new_lineno = line_item.new_lineno();
line.old_lineno = line_item.old_lineno();
line.origin = line_item.origin();
line.rect.setTop(y + file_h + hunk_h);
line.rect.setLeft(contentsMargins().left());
line.rect.setWidth(100 + lineMax); // TODO: Unhardcode 100
hunk_h += m_fontHeight + 1;
line.rect.setHeight(m_fontHeight + 1);
hunk.lines.push_back(line);
}
hunk.rect.setHeight(hunk_h);
file_h += hunk_h;
file.hunks.push_back(hunk);
}
file.rect.setHeight(file_h);
y += file_h;
m_private->files.push_back(file);
}
newSize = QSize(contentsMargins().left() + 100 + lineMax + contentsMargins().right(), contentsMargins().top() + y + contentsMargins().bottom()); // TODO: Unhardcode 100
setMinimumSize(newSize);
setMaximumSize(newSize);
update();
}
void QGitDiffWidget::paintEvent(QPaintEvent *event)
{
QStylePainter painter(this);
int fileIndex = 0, hunkIndex = 0, lineIndex = 0;
const auto &files = m_private->files;
for(const auto &file: files)
{
if (!event->region().intersected(file.rect).isEmpty())
{
painter.fillRect(file.rect, QColor(220,220,220));
if (m_hoverLine == -1)
{
hunkIndex = 0;
for(const auto &hunk: file.hunks)
{
if ((fileIndex == m_hoverFile)&&(hunkIndex == m_hoverHunk))
{
QStyleOptionFocusRect option;
option.initFrom(this);
option.rect = hunk.rect.adjusted(0, 0, 0, -1);
painter.setBrush(Qt::NoBrush);
painter.fillRect(option.rect, QColor(192,192,192));
}
hunkIndex++;
}
}
painter.setPen(Qt::black);
painter.drawText(file.rect.left() + 10, file.rect.top() + 20, file.new_file.path());
hunkIndex = 0;
for(const auto &hunk: file.hunks)
{
if (!event->region().intersected(hunk.rect).isEmpty())
{
lineIndex = 0;
for(const auto &line: hunk.lines)
{
if (!event->region().intersected(line.rect).isEmpty())
{
QString old_lineNo, new_lineNo;
if (line.old_lineno >= 0) old_lineNo = QString::number(line.old_lineno);
if (line.new_lineno >= 0) new_lineNo = QString::number(line.new_lineno);
int yFont = line.rect.top() + m_fontAscent;
if (line.origin == '-')
{
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(QColor(235, 204, 204)));
painter.drawRect(line.rect);
painter.setPen(Qt::darkRed);
} else if (line.origin == '+') {
painter.setPen(Qt::NoPen);
painter.setBrush(QBrush(QColor(204, 230, 194)));
painter.drawRect(line.rect);
painter.setPen(Qt::darkGreen);
} else {
painter.setPen(Qt::SolidLine);
painter.setBrush(QBrush(QColor(Qt::black)));
}
painter.drawText(10, yFont, old_lineNo); // TODO: Unhardcode 10
painter.drawText(40, yFont, new_lineNo); // TODO: Unhardcode 40
painter.drawText(100, yFont, line.content); // TODO: Unhardcode 100
if ((fileIndex == m_hoverFile)&&(hunkIndex == m_hoverHunk)&&(lineIndex == m_hoverLine))
{
QStyleOptionFocusRect option;
option.initFrom(this);
option.rect = line.rect.adjusted(100 - contentsMargins().left(), 0, -1, -1);
painter.setPen(Qt::SolidLine);
painter.setBrush(Qt::NoBrush);
painter.drawRect(option.rect);
//painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
}
}
lineIndex++;
}
if ((fileIndex == m_hoverFile)&&(hunkIndex == m_hoverHunk)&&(m_hoverLine == -1))
{
QStyleOptionFocusRect option;
option.initFrom(this);
option.rect = hunk.rect.adjusted(0, 0, -1, -1);
painter.setPen(Qt::SolidLine);
painter.setBrush(Qt::NoBrush);
painter.drawRect(option.rect);
//painter.drawPrimitive(QStyle::PE_FrameFocusRect, option);
}
}
hunkIndex++;
}
}
fileIndex++;
}
}
void QGitDiffWidget::mousePressEvent(QMouseEvent *event)
{
QVector<QGitDiffWidgetLine> lines;
if ((m_readonly)||(event->buttons() != Qt::LeftButton))
{
return;
}
if ((m_hoverFile < 0)||(m_hoverHunk < 0))
{
return;
}
if (m_hoverFile >= m_private->files.count())
{
return;
}
const auto &file = m_private->files.at(m_hoverFile);
if (m_hoverHunk >= file.hunks.count())
{
return;
}
const auto &hunk = file.hunks.at(m_hoverHunk);
if (m_hoverLine < 0)
{
for(const auto &line: hunk.lines)
{
QGitDiffWidgetLine newLine;
newLine.content = line.content;
newLine.new_lineno = line.new_lineno;
newLine.old_lineno = line.old_lineno;
newLine.origin = line.origin;
lines.push_back(newLine);
}
}
else
{
int old_line_no = 0;
for(int c = 0; c <= m_hoverLine; c++)
{
const auto &line = hunk.lines.at(c);
if ((c < m_hoverLine)&&((line.origin == ' ')||(line.origin == '-')))
{
old_line_no = line.old_lineno;
}
if (c == m_hoverLine)
{
QGitDiffWidgetLine newLine;
if (line.origin == '+')
{
newLine.content = line.content;
newLine.new_lineno = old_line_no + 1;
newLine.old_lineno = -1;
newLine.origin = line.origin;
lines.push_back(newLine);
}
else if (line.origin == '-')
{
newLine.content = line.content;
newLine.new_lineno = -1;
newLine.old_lineno = line.old_lineno;
newLine.origin = line.origin;
lines.push_back(newLine);
}
}
}
}
if (!lines.isEmpty())
{
emit select(file.new_file.path(), lines);
}
}
void QGitDiffWidget::mouseMoveEvent(QMouseEvent *event)
{
QWidget::mouseMoveEvent(event);
updatePosition();
}
void QGitDiffWidget::moveEvent(QMoveEvent *event)
{
updatePosition();
}
void QGitDiffWidget::updatePosition()
{
int l_hoverFile = -1;
int l_hoverHunk = -1;
int l_hoverLine = -1;
if (m_readonly)
return;
QPoint point = this->mapFromGlobal(QCursor::pos());
int file_index = 0;
const auto &files = m_private->files;
for(const auto &file : files)
{
if (file.rect.contains(point))
{
l_hoverFile = file_index;
int hunk_index = 0;
for(const auto &hunk : file.hunks)
{
if (hunk.rect.contains(point))
{
l_hoverHunk = hunk_index;
if (point.x() >= 100) // TODO Unhardcode magic number 100
{
int line_index = 0;
for(const auto &line : hunk.lines)
{
if (line.rect.contains(point))
{
if ((line.origin == '-')||(line.origin == '+'))
{
l_hoverLine = line_index;
}
else
{
l_hoverFile = -1;
l_hoverHunk = -1;
l_hoverLine = -1;
}
break;
}
line_index++;
}
}
break;
}
hunk_index++;
}
break;
}
file_index++;
}
if ((m_hoverFile != l_hoverFile)||(m_hoverHunk != l_hoverHunk)||(m_hoverLine != l_hoverLine))
{
m_hoverFile = l_hoverFile;
m_hoverHunk = l_hoverHunk;
m_hoverLine = l_hoverLine;
if (m_hoverHunk >= 0) {
setCursor(Qt::PointingHandCursor);
} else {
setCursor(Qt::ArrowCursor);
}
update();
}
}