RedPanda-CPP/RedPandaIDE/vcs/gitlogdialog.cpp

151 lines
3.9 KiB
C++
Raw Normal View History

2022-02-21 23:35:28 +08:00
#include "gitlogdialog.h"
#include "ui_gitlogdialog.h"
#include "gitmanager.h"
2022-02-22 17:12:54 +08:00
#include "gitresetdialog.h"
2022-02-21 23:35:28 +08:00
#include <QMenu>
2022-02-23 19:25:34 +08:00
//this is not thread safe, but it's the only solution i can find
static GitLogModel::CommitInfoCacheManager GitLogModel_CacheManager;
2022-02-21 23:35:28 +08:00
GitLogDialog::GitLogDialog(const QString& folder, QWidget *parent) :
QDialog(parent),
ui(new Ui::GitLogDialog),
mModel(folder)
{
ui->setupUi(this);
ui->tblLogs->setModel(&mModel);
ui->tblLogs->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(ui->tblLogs,&QTableView::customContextMenuRequested,
this, &GitLogDialog::onLogsContextMenu);
}
GitLogDialog::~GitLogDialog()
{
delete ui;
}
GitLogModel::GitLogModel(const QString &folder, QObject *parent):
QAbstractTableModel(parent),mFolder(folder)
{
GitManager manager;
mCount = manager.logCounts(folder);
2022-02-23 19:25:34 +08:00
PCommitInfoCache infoCache = std::make_shared<CommitInfoCache>();
GitLogModel_CacheManager.insert(this,infoCache);
}
GitLogModel::~GitLogModel()
{
GitLogModel_CacheManager.remove(this);
2022-02-21 23:35:28 +08:00
}
2022-07-04 11:39:06 +08:00
int GitLogModel::rowCount(const QModelIndex &/*parent*/) const
2022-02-21 23:35:28 +08:00
{
return mCount;
}
2022-07-04 11:39:06 +08:00
int GitLogModel::columnCount(const QModelIndex &/*parent*/) const
2022-02-21 23:35:28 +08:00
{
return 3;
}
QVariant GitLogModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
2022-02-23 19:25:34 +08:00
PGitCommitInfo info = commitInfo(index);
2022-02-21 23:35:28 +08:00
switch(index.column()) {
case 0:
2022-02-23 19:25:34 +08:00
return info->authorDate;
2022-02-21 23:35:28 +08:00
case 1:
2022-02-23 19:25:34 +08:00
return info->author;
2022-02-21 23:35:28 +08:00
case 2:
2022-02-23 19:25:34 +08:00
return info->title;
2022-02-21 23:35:28 +08:00
}
}
return QVariant();
}
QVariant GitLogModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation==Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case 0:
return tr("Date");
case 1:
return tr("Author");
case 2:
return tr("Title");
}
}
return QVariant();
}
2022-02-23 19:25:34 +08:00
PGitCommitInfo GitLogModel::commitInfo(const QModelIndex &index) const
2022-02-22 17:12:54 +08:00
{
if (!index.isValid())
return PGitCommitInfo();
int row = index.row();
2022-02-23 19:25:34 +08:00
PCommitInfoCache infoCache = GitLogModel_CacheManager.value(this);
PGitCommitInfo commitInfo;
if (!infoCache->contains(row)) {
GitManager manager;
QList<PGitCommitInfo> listCommitInfos =
manager.log(mFolder,row,50);
if (listCommitInfos.isEmpty()) {
return PGitCommitInfo();
}
for (int i=0;i<listCommitInfos.count();i++) {
infoCache->insert(row+i,listCommitInfos[i]);
}
commitInfo = listCommitInfos[0];
} else
commitInfo = (*infoCache)[row];
return commitInfo;
2022-02-22 17:12:54 +08:00
}
2022-02-21 23:35:28 +08:00
const QString &GitLogModel::folder() const
{
return mFolder;
}
void GitLogDialog::on_btnClose_clicked()
{
reject();
}
void GitLogDialog::onLogsContextMenu(const QPoint &pos)
{
QMenu menu(this);
QString branch;
GitManager manager;
if (!manager.hasRepository(mModel.folder(), branch))
return;
2022-02-22 17:12:54 +08:00
// menu.addAction(ui->actionRevert);
2022-02-21 23:35:28 +08:00
menu.addAction(ui->actionReset);
2022-02-22 17:12:54 +08:00
// menu.addAction(ui->actionBranch);
// menu.addAction(ui->actionTag);
2022-02-21 23:35:28 +08:00
ui->actionReset->setText(tr("Reset \"%1\" to this...").arg(branch));
ui->actionRevert->setText(tr("Revert \"%1\" to this...").arg(branch));
ui->actionBranch->setText(tr("Create Branch at this version..."));
ui->actionTag->setText(tr("Create Tag at this version..."));
menu.exec(ui->tblLogs->mapToGlobal(pos));
}
2022-02-22 17:12:54 +08:00
void GitLogDialog::on_actionReset_triggered()
{
QModelIndex index = ui->tblLogs->currentIndex();
if (!index.isValid())
return;
PGitCommitInfo commitInfo = mModel.commitInfo(index);
GitResetDialog resetDialog(mModel.folder());
if (resetDialog.resetToCommit(commitInfo->commitHash)==QDialog::Accepted)
accept();
}