1#include "PrChangeListItem.h"
2
3#include <DiffHelper.h>
4#include <FileDiffView.h>
5#include <LineNumberArea.h>
6#include <AddCodeReviewDialog.h>
7
8#include <QGridLayout>
9#include <QLabel>
10
11using namespace DiffHelper;
12
13PrChangeListItem::PrChangeListItem(DiffChange change, QWidget *parent)
14 : QFrame(parent)
15 , mNewFileStartingLine(change.newFileStartLine)
16 , mOldFileName(change.oldFileName)
17 , mNewFileName(change.newFileName)
18{
19 setObjectName("PrChangeListItem");
20
21 DiffHelper::processDiff(change.content, change.newData, change.oldData);
22
23 const auto fileName = change.oldFileName == change.newFileName
24 ? change.newFileName
25 : QString("%1 -> %2").arg(change.oldFileName, change.newFileName);
26
27 const auto fileNameLabel = new QLabel(fileName);
28 fileNameLabel->setObjectName("ChangeFileName");
29
30 const auto headerLayout = new QVBoxLayout();
31 headerLayout->setContentsMargins(QMargins());
32 headerLayout->addWidget(fileNameLabel);
33
34 const auto headerFrame = new QFrame();
35 headerFrame->setObjectName("ChangeHeaderFrame");
36 headerFrame->setLayout(headerLayout);
37
38 const auto mainLayout = new QVBoxLayout(this);
39 mainLayout->setContentsMargins(QMargins());
40 mainLayout->setSpacing(0);
41 mainLayout->addWidget(headerFrame);
42
43 if (!change.header.isEmpty())
44 {
45 const auto headerLabel = new QLabel(change.header);
46 headerLabel->setObjectName("ChangeHeader");
47 headerLayout->addWidget(headerLabel);
48
49 mNewFileEndingLine = mNewFileStartingLine + change.newData.first.count();
50
51 const auto oldFile = new FileDiffView();
52 const auto numberArea = new LineNumberArea(oldFile, true);
53 numberArea->setObjectName("LineNumberArea");
54 numberArea->setEditor(oldFile);
55 connect(numberArea, &LineNumberArea::addComment, this, &PrChangeListItem::openReviewDialog);
56
57 oldFile->setStartingLine(change.oldFileStartLine - 1);
58 oldFile->loadDiff(change.oldData.first.join("\n").trimmed(), change.oldData.second);
59 oldFile->addNumberArea(numberArea);
60 oldFile->setMinimumWidth(590);
61 oldFile->show();
62 oldFile->setMinimumHeight(oldFile->getHeight());
63
64 mNewFileDiff = new FileDiffView();
65 mNewNumberArea = new LineNumberArea(mNewFileDiff, true);
66 mNewNumberArea->setObjectName("LineNumberArea");
67 mNewNumberArea->setEditor(mNewFileDiff);
68 connect(mNewNumberArea, &LineNumberArea::addComment, this, &PrChangeListItem::openReviewDialog);
69
70 mNewFileDiff->setStartingLine(change.newFileStartLine - 1);
71 mNewFileDiff->loadDiff(change.newData.first.join("\n").trimmed(), change.newData.second);
72 mNewFileDiff->addNumberArea(mNewNumberArea);
73 mNewFileDiff->setMinimumWidth(590);
74 mNewFileDiff->show();
75 mNewFileDiff->setMinimumHeight(mNewFileDiff->getHeight());
76
77 const auto diffLayout = new QHBoxLayout();
78 diffLayout->setContentsMargins(QMargins());
79 diffLayout->setSpacing(5);
80 diffLayout->addWidget(oldFile);
81 diffLayout->addWidget(mNewFileDiff);
82 mainLayout->addLayout(diffLayout);
83
84 connect(mNewNumberArea, &LineNumberArea::gotoReview, this, &PrChangeListItem::gotoReview);
85 }
86}
87
88void PrChangeListItem::setBookmarks(const QMap<int, int> &bookmarks)
89{
90 mNewNumberArea->setCommentBookmarks(bookmarks);
91}
92
93void PrChangeListItem::openReviewDialog(int line)
94{
95 const auto path = qobject_cast<LineNumberArea *>(sender()) == mNewNumberArea ? mNewFileName : mOldFileName;
96
97 const auto dlg = new AddCodeReviewDialog(ReviewMode::Comment);
98 dlg->setWindowFlag(Qt::FramelessWindowHint);
99 dlg->setWindowFlag(Qt::Tool);
100 dlg->setWindowModality(Qt::ApplicationModal);
101 dlg->setModal(true);
102
103 auto pos = QCursor::pos();
104 pos.setY(pos.y() + mNewFileDiff->getLineHeigth());
105
106 dlg->move(pos);
107
108 connect(dlg, &AddCodeReviewDialog::accepted, this,
109 [this, line, path, dlg]() { emit addCodeReview(line, path, dlg->getText().trimmed()); });
110
111 dlg->exec();
112}
113