Commit 162d8711 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Implement Staging UI options

parent 3ee02c58
......@@ -71,13 +71,13 @@ QVariant GitStatusModel::data(const QModelIndex &index, int role) const
if (index.internalId() == Root) {
if (role == Qt::DisplayRole) {
if (row == Staged) {
return QStringLiteral("Staged");
return QStringLiteral("Staged (%1)").arg(m_nodes[Staged].count());
} else if (row == Untrack) {
return QStringLiteral("Untracked");
return QStringLiteral("Untracked (%1)").arg(m_nodes[Untrack].count());
} else if (row == Conflict) {
return QStringLiteral("Conflict");
return QStringLiteral("Conflict (%1)").arg(m_nodes[Conflict].count());
} else if (row == Changed) {
return QStringLiteral("Modified");
return QStringLiteral("Modified (%1)").arg(m_nodes[Changed].count());
}
} else if (role == Qt::FontRole) {
QFont bold;
......@@ -86,11 +86,10 @@ QVariant GitStatusModel::data(const QModelIndex &index, int role) const
} else if (role == Qt::DecorationRole) {
static const auto branchIcon = QIcon(QStringLiteral(":/kxmlgui5/kateproject/sc-apps-git.svg"));
return branchIcon;
} else if (role == Role::TreeItemType) {
return ItemType::Node;
}
} else {
if (role != Qt::DisplayRole && role != Qt::DecorationRole) {
return {};
}
int rootIndex = index.internalId();
if (rootIndex < 0 || rootIndex > 3) {
return QVariant();
......@@ -100,6 +99,8 @@ QVariant GitStatusModel::data(const QModelIndex &index, int role) const
return m_nodes[rootIndex].at(row).file;
} else if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QMimeDatabase().mimeTypeForFile(m_nodes[rootIndex].at(row).file, QMimeDatabase::MatchExtension).iconName());
} else if (role == Role::TreeItemType) {
return ItemType::File;
}
}
......@@ -128,3 +129,47 @@ QVector<int> GitStatusModel::emptyRows()
}
return empty;
}
bool GitStatusModel::stageAll(const QModelIndex &idx)
{
if (!idx.isValid()) {
return false;
}
if (idx.internalId() != Root) {
return false;
}
// do actual staging, check for return code
// model update
const auto node = idx.row();
const auto srcSize = m_nodes[node].size();
const auto destSize = m_nodes[Staged].size();
beginMoveRows(createIndex(node, 0, Root), 0, srcSize, createIndex(Staged, 0, Root), destSize);
m_nodes[Staged].append(m_nodes[node]);
m_nodes[node].clear();
endMoveRows();
return true;
}
bool GitStatusModel::stageFile(const QModelIndex &idx)
{
if (!idx.isValid()) {
return false;
}
// do actual staging, check for return code
const auto parent = idx.internalId();
const auto destSize = m_nodes[Staged].size();
beginMoveRows(createIndex(parent, 0, Root), idx.row(), idx.row(), createIndex(Staged, 0, Root), destSize);
auto item = m_nodes[parent].at(idx.row());
m_nodes[Staged].append(item);
m_nodes[parent].remove(idx.row());
endMoveRows();
return true;
}
......@@ -10,6 +10,9 @@ class GitStatusModel : public QAbstractItemModel
public:
explicit GitStatusModel(QObject *parent);
enum ItemType { Node, File };
enum Role { TreeItemType = Qt::UserRole };
public:
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &child) const override;
......@@ -23,6 +26,9 @@ public:
const QVector<GitUtils::StatusItem> &untracked);
QVector<int> emptyRows();
bool stageFile(const QModelIndex &idx);
bool stageAll(const QModelIndex &idx);
private:
QVector<GitUtils::StatusItem> m_nodes[4];
};
......
......@@ -2,7 +2,10 @@
#include "gitstatusmodel.h"
#include "kateproject.h"
#include <QContextMenuEvent>
#include <QDebug>
#include <QEvent>
#include <QMenu>
#include <QProcess>
#include <QPushButton>
#include <QStringListModel>
......@@ -10,6 +13,8 @@
#include <QVBoxLayout>
#include <QtConcurrentRun>
#include <KLocalizedString>
GitWidget::GitWidget(KateProject *project, QWidget *parent)
: QWidget(parent)
, m_project(project)
......@@ -30,11 +35,12 @@ GitWidget::GitWidget(KateProject *project, QWidget *parent)
layout->addLayout(btnsLayout);
layout->addWidget(m_treeView);
m_treeView->setHeaderHidden(true);
m_treeView->setRootIsDecorated(false);
m_model = new GitStatusModel(this);
m_treeView->setHeaderHidden(true);
// m_treeView->setRootIsDecorated(false);
m_treeView->setModel(m_model);
m_treeView->installEventFilter(this);
setLayout(layout);
......@@ -156,8 +162,12 @@ GitWidget::GitParsedStatus GitWidget::parseStatus(const QByteArray &raw)
void GitWidget::hideEmptyTreeNodes()
{
const auto emptyRows = m_model->emptyRows();
for (const int row : emptyRows) {
m_treeView->setRowHidden(row, QModelIndex(), true);
for (int i = 0; i < 4; ++i) {
if (emptyRows.contains(i)) {
m_treeView->setRowHidden(i, QModelIndex(), true);
} else {
m_treeView->setRowHidden(i, QModelIndex(), false);
}
}
}
......@@ -168,3 +178,40 @@ void GitWidget::parseStatusReady()
hideEmptyTreeNodes();
}
bool GitWidget::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::ContextMenu) {
if (o != m_treeView)
return QWidget::eventFilter(o, e);
QContextMenuEvent *cme = static_cast<QContextMenuEvent *>(e);
treeViewContextMenuEvent(cme);
}
return QWidget::eventFilter(o, e);
}
void GitWidget::treeViewContextMenuEvent(QContextMenuEvent *e)
{
auto idx = m_model->index(m_treeView->currentIndex().row(), 0, m_treeView->currentIndex().parent());
auto type = idx.data(GitStatusModel::TreeItemType);
if (type == GitStatusModel::Node) {
QMenu menu;
auto stage = menu.addAction(i18n("Stage All"));
auto act = menu.exec(m_treeView->viewport()->mapToGlobal(e->pos()));
if (act == stage) {
m_model->stageAll(m_treeView->currentIndex());
hideEmptyTreeNodes();
}
} else if (type == GitStatusModel::File) {
QMenu menu;
auto stage = menu.addAction(i18n("Stage file"));
auto act = menu.exec(m_treeView->viewport()->mapToGlobal(e->pos()));
if (act == stage) {
m_model->stageFile(m_treeView->currentIndex());
hideEmptyTreeNodes();
}
}
}
......@@ -19,6 +19,8 @@ class GitWidget : public QWidget
public:
explicit GitWidget(KateProject *project, QWidget *parent = nullptr);
bool eventFilter(QObject *o, QEvent *e) override;
private:
struct GitParsedStatus {
QVector<GitUtils::StatusItem> untracked;
......@@ -38,6 +40,7 @@ private:
void getStatus(const QString &repo, bool submodules = false);
GitParsedStatus parseStatus(const QByteArray &raw);
void hideEmptyTreeNodes();
void treeViewContextMenuEvent(QContextMenuEvent *e);
Q_SLOT void gitStatusReady();
Q_SLOT void parseStatusReady();
......
Markdown is supported
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