1 | #include "gitlogdialog.h" |
2 | #include "ui_gitlogdialog.h" |
3 | #include "gitmanager.h" |
4 | #include "gitresetdialog.h" |
5 | |
6 | #include <QMenu> |
7 | |
8 | //this is not thread safe, but it's the only solution i can find |
9 | static GitLogModel::CommitInfoCacheManager GitLogModel_CacheManager; |
10 | |
11 | |
12 | GitLogDialog::GitLogDialog(const QString& folder, QWidget *parent) : |
13 | QDialog(parent), |
14 | ui(new Ui::GitLogDialog), |
15 | mModel(folder) |
16 | { |
17 | ui->setupUi(this); |
18 | ui->tblLogs->setModel(&mModel); |
19 | ui->tblLogs->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu); |
20 | connect(ui->tblLogs,&QTableView::customContextMenuRequested, |
21 | this, &GitLogDialog::onLogsContextMenu); |
22 | } |
23 | |
24 | GitLogDialog::~GitLogDialog() |
25 | { |
26 | delete ui; |
27 | } |
28 | |
29 | GitLogModel::GitLogModel(const QString &folder, QObject *parent): |
30 | QAbstractTableModel(parent),mFolder(folder) |
31 | { |
32 | GitManager manager; |
33 | mCount = manager.logCounts(folder); |
34 | PCommitInfoCache infoCache = std::make_shared<CommitInfoCache>(); |
35 | GitLogModel_CacheManager.insert(this,infoCache); |
36 | } |
37 | |
38 | GitLogModel::~GitLogModel() |
39 | { |
40 | GitLogModel_CacheManager.remove(this); |
41 | } |
42 | |
43 | int GitLogModel::rowCount(const QModelIndex &/*parent*/) const |
44 | { |
45 | return mCount; |
46 | } |
47 | |
48 | int GitLogModel::columnCount(const QModelIndex &/*parent*/) const |
49 | { |
50 | return 3; |
51 | } |
52 | |
53 | QVariant GitLogModel::data(const QModelIndex &index, int role) const |
54 | { |
55 | if (!index.isValid()) |
56 | return QVariant(); |
57 | if (role == Qt::DisplayRole) { |
58 | PGitCommitInfo info = commitInfo(index); |
59 | |
60 | switch(index.column()) { |
61 | case 0: |
62 | return info->authorDate; |
63 | case 1: |
64 | return info->author; |
65 | case 2: |
66 | return info->title; |
67 | } |
68 | } |
69 | return QVariant(); |
70 | } |
71 | |
72 | QVariant GitLogModel::(int section, Qt::Orientation orientation, int role) const |
73 | { |
74 | if (orientation==Qt::Horizontal && role == Qt::DisplayRole) { |
75 | switch(section) { |
76 | case 0: |
77 | return tr("Date" ); |
78 | case 1: |
79 | return tr("Author" ); |
80 | case 2: |
81 | return tr("Title" ); |
82 | } |
83 | } |
84 | return QVariant(); |
85 | } |
86 | |
87 | PGitCommitInfo GitLogModel::commitInfo(const QModelIndex &index) const |
88 | { |
89 | if (!index.isValid()) |
90 | return PGitCommitInfo(); |
91 | int row = index.row(); |
92 | PCommitInfoCache infoCache = GitLogModel_CacheManager.value(this); |
93 | PGitCommitInfo commitInfo; |
94 | if (!infoCache->contains(row)) { |
95 | GitManager manager; |
96 | QList<PGitCommitInfo> listCommitInfos = |
97 | manager.log(mFolder,row,50); |
98 | if (listCommitInfos.isEmpty()) { |
99 | return PGitCommitInfo(); |
100 | } |
101 | for (int i=0;i<listCommitInfos.count();i++) { |
102 | infoCache->insert(row+i,listCommitInfos[i]); |
103 | } |
104 | commitInfo = listCommitInfos[0]; |
105 | } else |
106 | commitInfo = (*infoCache)[row]; |
107 | |
108 | return commitInfo; |
109 | } |
110 | |
111 | const QString &GitLogModel::folder() const |
112 | { |
113 | return mFolder; |
114 | } |
115 | |
116 | void GitLogDialog::on_btnClose_clicked() |
117 | { |
118 | reject(); |
119 | } |
120 | |
121 | void GitLogDialog::(const QPoint &pos) |
122 | { |
123 | QMenu (this); |
124 | QString branch; |
125 | GitManager manager; |
126 | if (!manager.hasRepository(mModel.folder(), branch)) |
127 | return; |
128 | // menu.addAction(ui->actionRevert); |
129 | menu.addAction(ui->actionReset); |
130 | // menu.addAction(ui->actionBranch); |
131 | // menu.addAction(ui->actionTag); |
132 | ui->actionReset->setText(tr("Reset \"%1\" to this..." ).arg(branch)); |
133 | ui->actionRevert->setText(tr("Revert \"%1\" to this..." ).arg(branch)); |
134 | ui->actionBranch->setText(tr("Create Branch at this version..." )); |
135 | ui->actionTag->setText(tr("Create Tag at this version..." )); |
136 | menu.exec(ui->tblLogs->mapToGlobal(pos)); |
137 | } |
138 | |
139 | |
140 | void GitLogDialog::on_actionReset_triggered() |
141 | { |
142 | QModelIndex index = ui->tblLogs->currentIndex(); |
143 | if (!index.isValid()) |
144 | return; |
145 | PGitCommitInfo commitInfo = mModel.commitInfo(index); |
146 | GitResetDialog resetDialog(mModel.folder()); |
147 | if (resetDialog.resetToCommit(commitInfo->commitHash)==QDialog::Accepted) |
148 | accept(); |
149 | } |
150 | |
151 | |