Commit 2cd842db authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Make FileHistoryWidget standalone

parent 33b11fb2
......@@ -5,17 +5,27 @@
*/
#include "filehistorywidget.h"
#include "diffparams.h"
#include "ktexteditor_utils.h"
#include <gitprocess.h>
#include <QDate>
#include <QDebug>
#include <QFileInfo>
#include <QListView>
#include <QPainter>
#include <QPointer>
#include <QProcess>
#include <QPushButton>
#include <QStyledItemDelegate>
#include <QVBoxLayout>
#include <optional>
#include <QWidget>
#include <KIconLoader>
#include <KLocalizedString>
#include <KTextEditor/Application>
#include <KTextEditor/Editor>
#include <KTextEditor/MainWindow>
struct Commit {
QByteArray hash;
......@@ -209,9 +219,35 @@ public:
}
};
FileHistoryWidget::FileHistoryWidget(const QString &gitDir, const QString &file, QWidget *parent)
class FileHistoryWidget : public QWidget
{
Q_OBJECT
public:
void itemClicked(const QModelIndex &idx);
void getFileHistory(const QString &file);
explicit FileHistoryWidget(const QString &gitDir, const QString &file, KTextEditor::MainWindow *mw, QWidget *parent = nullptr);
~FileHistoryWidget() override;
QPushButton m_backBtn;
QListView *m_listView;
QProcess m_git;
const QString m_file;
const QString m_gitDir;
const QPointer<QWidget> m_toolView;
const QPointer<KTextEditor::MainWindow> m_mainWindow;
Q_SIGNALS:
void backClicked();
void errorMessage(const QString &msg, bool warn);
};
FileHistoryWidget::FileHistoryWidget(const QString &gitDir, const QString &file, KTextEditor::MainWindow *mw, QWidget *parent)
: QWidget(parent)
, m_file(file)
, m_gitDir(gitDir)
, m_toolView(parent)
, m_mainWindow(mw)
{
auto model = new CommitListModel(this);
m_listView = new QListView;
......@@ -220,11 +256,15 @@ FileHistoryWidget::FileHistoryWidget(const QString &gitDir, const QString &file,
setLayout(new QVBoxLayout);
m_backBtn.setText(i18n("Back"));
m_backBtn.setIcon(QIcon::fromTheme(QStringLiteral("go-previous")));
connect(&m_backBtn, &QPushButton::clicked, this, &FileHistoryWidget::backClicked);
m_backBtn.setText(i18n("Close"));
m_backBtn.setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
connect(&m_backBtn, &QPushButton::clicked, this, [this] {
deleteLater();
m_mainWindow->hideToolView(m_toolView);
m_toolView->deleteLater();
});
connect(m_listView, &QListView::clicked, this, &FileHistoryWidget::itemClicked);
m_listView->setItemDelegate(new CommitDelegate(this));
layout()->addWidget(&m_backBtn);
......@@ -284,9 +324,38 @@ void FileHistoryWidget::itemClicked(const QModelIndex &idx)
if (git.exitStatus() != QProcess::NormalExit || git.exitCode() != 0) {
return;
}
QByteArray contents(git.readAllStandardOutput());
// we send this signal to the parent, which will pass it on to
// the GitWidget from where a temporary file is opened
Q_EMIT commitClicked(contents, QString::fromUtf8(commit.hash.mid(0, 7)));
const QByteArray contents(git.readAllStandardOutput());
auto mw = m_mainWindow->window();
DiffParams d;
const QString shortCommit = QString::fromUtf8(commit.hash.mid(0, 7));
d.tabTitle = QStringLiteral("%1[%2]").arg(Utils::fileNameFromPath(m_file), shortCommit);
QMetaObject::invokeMethod(mw, "showDiff", Q_ARG(QByteArray, contents), Q_ARG(DiffParams, d));
}
}
void FileHistory::showFileHistory(const QString &file, KTextEditor::MainWindow *mainWindow)
{
QFileInfo fi(file);
if (!fi.exists()) {
qWarning() << "Unexpected non-existent file: " << file;
return;
}
const auto repoBase = getRepoBasePath(fi.absolutePath());
if (!repoBase.has_value()) {
// TODO: show message;
return;
}
if (!mainWindow) {
mainWindow = KTextEditor::Editor::instance()->application()->activeMainWindow();
}
const auto gitIcon = KDE::icon(QStringLiteral(":/icons/icons/sc-apps-git.svg"));
auto toolView = mainWindow->createToolView(nullptr, QStringLiteral("git_file_history"), KTextEditor::MainWindow::Left, gitIcon, i18n("File History"));
new FileHistoryWidget(repoBase.value(), file, mainWindow, toolView);
mainWindow->showToolView(toolView);
}
#include "filehistorywidget.moc"
......@@ -6,33 +6,22 @@
#ifndef FILEHISTORYWIDGET_H
#define FILEHISTORYWIDGET_H
#include <QListView>
#include <QProcess>
#include <QPushButton>
#include <QWidget>
class QString;
namespace KTextEditor
{
class MainWindow;
}
class FileHistoryWidget : public QWidget
class FileHistory
{
Q_OBJECT
public:
explicit FileHistoryWidget(const QString &gitDir, const QString &file, QWidget *parent = nullptr);
~FileHistoryWidget() override;
private Q_SLOTS:
void itemClicked(const QModelIndex &idx);
private:
void getFileHistory(const QString &file);
QPushButton m_backBtn;
QListView *m_listView;
QProcess m_git;
QString m_gitDir;
Q_SIGNALS:
void backClicked();
void commitClicked(const QByteArray &contents, const QString &commit);
void errorMessage(const QString &msg, bool warn);
/**
* @brief shows git file history of @p file
* @param file the file whose history you want to see
* @param mainWindow the mainWindow where the toolview with history will open. If null, active mainWindow
* will be used
*/
static void showFileHistory(const QString &file, KTextEditor::MainWindow *mainWindow = nullptr);
};
#endif // FILEHISTORYWIDGET_H
......@@ -7,14 +7,11 @@
#include "kateprojectview.h"
#include "branchcheckoutdialog.h"
#include "diffparams.h"
#include "filehistorywidget.h"
#include "git/gitutils.h"
#include "gitprocess.h"
#include "gitwidget.h"
#include "kateprojectfiltermodel.h"
#include "kateprojectpluginview.h"
#include "ktexteditor_utils.h"
#include <KTextEditor/Document>
#include <KTextEditor/Editor>
......@@ -150,31 +147,7 @@ void KateProjectView::setTreeViewAsCurrent()
void KateProjectView::showFileGitHistory(const QString &file)
{
// create on demand and on switch back delete
const auto dotGitPath = getRepoBasePath(QFileInfo(file).absolutePath());
if (!dotGitPath.has_value()) {
// TODO: Show message in output
return;
}
auto fhs = new FileHistoryWidget(dotGitPath.value(), file);
connect(fhs, &FileHistoryWidget::backClicked, this, &KateProjectView::setTreeViewAsCurrent);
connect(fhs, &FileHistoryWidget::commitClicked, this, [this, file](const QByteArray &diff, const QString &commit) {
auto mw = m_pluginView->mainWindow()->window();
DiffParams d;
d.tabTitle = QStringLiteral("%1[%2]").arg(Utils::fileNameFromPath(file), commit);
QMetaObject::invokeMethod(mw, "showDiff", Q_ARG(QByteArray, diff), Q_ARG(DiffParams, d));
});
connect(fhs, &FileHistoryWidget::errorMessage, m_pluginView, [this](const QString &s, bool warn) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), warn ? QStringLiteral("Error") : QStringLiteral("Info"));
genericMessage.insert(QStringLiteral("category"), i18n("Git"));
genericMessage.insert(QStringLiteral("categoryIcon"), KDE::icon(QStringLiteral(":/icons/icons/sc-apps-git.svg")));
genericMessage.insert(QStringLiteral("text"), s);
Q_EMIT m_pluginView->message(genericMessage);
});
m_stackWidget->addWidget(fhs);
m_stackWidget->setCurrentWidget(fhs);
FileHistory::showFileHistory(file);
}
void KateProjectView::checkAndRefreshGit()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment