1#include "FileListWidget.h"
2
3#include <FileContextMenu.h>
4#include <FileListDelegate.h>
5#include <GitBase.h>
6#include <GitCache.h>
7#include <GitHistory.h>
8#include <GitQlientStyles.h>
9#include <RevisionFiles.h>
10
11#include <QApplication>
12#include <QDrag>
13#include <QItemDelegate>
14#include <QMenu>
15#include <QMimeData>
16#include <QMouseEvent>
17#include <QPainter>
18#include <QPalette>
19
20FileListWidget::FileListWidget(const QSharedPointer<GitBase> &git, QSharedPointer<GitCache> cache, QWidget *p)
21 : QListWidget(p)
22 , mGit(git)
23 , mCache(std::move(cache))
24{
25 setContextMenuPolicy(Qt::CustomContextMenu);
26 setItemDelegate(mFileDelegate = new FileListDelegate(this));
27 setAttribute(Qt::WA_DeleteOnClose);
28
29 connect(this, &FileListWidget::customContextMenuRequested, this, &FileListWidget::showContextMenu);
30}
31
32FileListWidget::~FileListWidget()
33{
34 delete mFileDelegate;
35}
36
37void FileListWidget::addItem(const QString &label, const QColor &clr)
38{
39 const auto item = new QListWidgetItem(label, this);
40 item->setForeground(clr);
41 item->setToolTip(label);
42}
43
44void FileListWidget::showContextMenu(const QPoint &pos)
45{
46 const auto item = itemAt(pos);
47
48 if (item)
49 {
50 const auto fileName = item->data(Qt::DisplayRole).toString();
51 const auto menu = new FileContextMenu(fileName, mCurrentSha == CommitInfo::ZERO_SHA, this);
52 connect(menu, &FileContextMenu::signalShowFileHistory, this,
53 [this, fileName]() { emit signalShowFileHistory(fileName); });
54 connect(menu, &FileContextMenu::signalOpenFileDiff, this,
55 [this, item] { emit QListWidget::itemDoubleClicked(item); });
56 connect(menu, &FileContextMenu::signalEditFile, this,
57 [this, fileName]() { emit signalEditFile(mGit->getWorkingDir() + "/" + fileName, 0, 0); });
58 menu->exec(viewport()->mapToGlobal(pos));
59 }
60}
61
62void FileListWidget::insertFiles(const QString &currentSha, const QString &compareToSha)
63{
64 clear();
65
66 mCurrentSha = currentSha;
67
68 auto files = mCache->revisionFile(mCurrentSha, compareToSha);
69
70 if (!files)
71 {
72 QScopedPointer<GitHistory> git(new GitHistory(mGit));
73 const auto ret = git->getDiffFiles(mCurrentSha, compareToSha);
74
75 if (ret.success)
76 {
77 files = RevisionFiles(ret.output);
78 mCache->insertRevisionFiles(mCurrentSha, compareToSha, files.value());
79 }
80 }
81
82 if (files->count() != 0)
83 {
84 setUpdatesEnabled(false);
85
86 for (auto i = 0; i < files->count(); ++i)
87 {
88 if (!files->statusCmp(i, RevisionFiles::UNKNOWN))
89 {
90 QColor clr;
91 QString fileName;
92
93 if (files->statusCmp(i, RevisionFiles::NEW))
94 {
95 const auto fileRename = files->extendedStatus(i);
96
97 clr = fileRename.isEmpty() ? GitQlientStyles::getGreen() : GitQlientStyles::getBlue();
98 fileName = fileRename.isEmpty() ? files->getFile(i) : fileRename;
99 }
100 else
101 {
102 clr = files->statusCmp(i, RevisionFiles::DELETED) ? GitQlientStyles::getRed()
103 : GitQlientStyles::getTextColor();
104 fileName = files->getFile(i);
105 }
106
107 addItem(fileName, clr);
108 }
109 }
110
111 setUpdatesEnabled(true);
112 }
113}
114