Commit bad9c052 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Add a model and improve display of the branches dialog

parent 9d0615f0
......@@ -59,6 +59,7 @@ target_sources(
kateprojectconfigpage.cpp
kateprojectcodeanalysistool.cpp
branchesdialog.cpp
branchesdialogmodel.cpp
tools/kateprojectcodeanalysistoolcppcheck.cpp
tools/kateprojectcodeanalysistoolflake8.cpp
......
......@@ -4,6 +4,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "branchesdialog.h"
#include "branchesdialogmodel.h"
#include "gitutils.h"
#include <QAction>
......@@ -59,7 +60,7 @@ protected:
int score = 0;
const auto idx = sourceModel()->index(sourceRow, 0, sourceParent);
const QString string = idx.data().toString();
const QString string = idx.data(BranchesDialogModel::DisplayName).toString();
const bool res = kfts::fuzzy_match(m_pattern, string, score);
sourceModel()->setData(idx, score, WeightRole);
return res;
......@@ -85,12 +86,26 @@ public:
QTextDocument doc;
auto str = index.data().toString();
auto name = index.data(BranchesDialogModel::DisplayName).toString();
const QString nameColor = option.palette.color(QPalette::Link).name();
kfts::to_scored_fuzzy_matched_display_string(m_filterString, str, QStringLiteral("<b style=\"color:%1;\">").arg(nameColor), QStringLiteral("</b>"));
kfts::to_scored_fuzzy_matched_display_string(m_filterString, name, QStringLiteral("<b style=\"color:%1;\">").arg(nameColor), QStringLiteral("</b>"));
auto type = (GitUtils::RefType)index.data(BranchesDialogModel::RefType).toInt();
auto commit = index.data(BranchesDialogModel::Commit).toString();
using RefType = GitUtils::RefType;
const auto fontSz = option.font.pointSize();
if (type == RefType::Head) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">at %2</span>").arg(fontSz).arg(commit));
} else if (type == RefType::Remote) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">remote at %2</span>").arg(fontSz).arg(commit));
} else if (type == RefType::Tag) {
name.append(QStringLiteral(" &nbsp;<span style=\"color:gray; font-size:%1pt;\">tag at %2</span>").arg(fontSz).arg(commit));
} else {
Q_ASSERT(false);
}
doc.setHtml(str);
doc.setHtml(name);
doc.setDocumentMargin(2);
painter->save();
......@@ -146,7 +161,7 @@ BranchesDialog::BranchesDialog(QWidget *parent, KTextEditor::MainWindow *mainWin
m_treeView->setTextElideMode(Qt::ElideLeft);
m_treeView->setUniformRowHeights(true);
m_model = new QStandardItemModel(this);
m_model = new BranchesDialogModel(this);
StyleDelegate *delegate = new StyleDelegate(this);
m_treeView->setItemDelegateForColumn(0, delegate);
......@@ -182,16 +197,9 @@ BranchesDialog::BranchesDialog(QWidget *parent, KTextEditor::MainWindow *mainWin
void BranchesDialog::openDialog()
{
const QVector<GitUtils::Branch> branches = GitUtils::getAllBranches(m_projectPath);
m_model->clear();
static const QIcon branchIcon = QIcon(QStringLiteral(":/kxmlgui5/kateproject/sc-apps-git.svg"));
for (const auto &branch : branches) {
m_model->appendRow(new QStandardItem(branchIcon, branch.name));
}
m_model->refresh(branches);
reselectFirst();
updateViewGeometry();
show();
setFocus();
......@@ -238,7 +246,7 @@ bool BranchesDialog::eventFilter(QObject *obj, QEvent *event)
void BranchesDialog::slotReturnPressed()
{
const auto branch = m_proxyModel->data(m_treeView->currentIndex()).toString();
const auto branch = m_proxyModel->data(m_treeView->currentIndex(), BranchesDialogModel::CheckoutName).toString();
int res = GitUtils::checkoutBranch(m_projectPath, branch);
auto msgType = KTextEditor::Message::Positive;
......
......@@ -8,7 +8,7 @@
class QTreeView;
class QLineEdit;
class QStandardItemModel;
class BranchesDialogModel;
class QAction;
class BranchFilterModel;
class KActionCollection;
......@@ -40,7 +40,7 @@ private Q_SLOTS:
private:
QTreeView *m_treeView;
QLineEdit *m_lineEdit;
QStandardItemModel *m_model;
BranchesDialogModel *m_model;
BranchFilterModel *m_proxyModel;
KTextEditor::MainWindow *m_mainWindow;
QString m_projectPath;
......
/* SPDX-License-Identifier: LGPL-2.0-or-later
SPDX-FileCopyrightText: 2018 Tomaz Canabrava <tcanabrava@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "branchesdialogmodel.h"
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
#include <QIcon>
BranchesDialogModel::BranchesDialogModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int BranchesDialogModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return m_modelEntries.size();
}
int BranchesDialogModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant BranchesDialogModel::data(const QModelIndex &idx, int role) const
{
if (!idx.isValid()) {
return {};
}
const GitUtils::Branch &branch = m_modelEntries.at(idx.row());
if (role == Role::DisplayName) {
return branch.name;
} else if (role == Role::Score) {
return branch.score;
} else if (role == Qt::DecorationRole) {
static const auto branchIcon = QIcon(QStringLiteral(":/kxmlgui5/kateproject/sc-apps-git.svg"));
return branchIcon;
} else if (role == Role::Commit) {
return branch.commit.mid(0, 7);
} else if (role == Role::CheckoutName) {
return branch.type == GitUtils::RefType::Remote ? branch.name.mid(branch.remote.size() + 1) : branch.name;
} else if (role == Role::RefType) {
return branch.type;
}
return {};
}
void BranchesDialogModel::refresh(QVector<GitUtils::Branch> branches)
{
beginResetModel();
m_modelEntries = std::move(branches);
endResetModel();
}
/* SPDX-License-Identifier: LGPL-2.0-or-later
SPDX-FileCopyrightText: 2018 Tomaz Canabrava <tcanabrava@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KATEQUICKOPENMODEL_H
#define KATEQUICKOPENMODEL_H
#include <QAbstractTableModel>
#include <QVariant>
#include <QVector>
#include "gitutils.h"
class BranchesDialogModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Role { Score = Qt::UserRole + 1, DisplayName, Commit, CheckoutName, RefType };
explicit BranchesDialogModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &idx, int role) const override;
void refresh(QVector<GitUtils::Branch> branches);
bool setData(const QModelIndex &index, const QVariant &value, int role) override
{
if (!index.isValid()) {
return false;
}
if (role == Role::Score) {
auto row = index.row();
m_modelEntries[row].score = value.toInt();
}
return QAbstractTableModel::setData(index, value, role);
}
private:
QVector<GitUtils::Branch> m_modelEntries;
};
#endif
......@@ -60,17 +60,20 @@ QVector<GitUtils::Branch> GitUtils::getAllBranches(const QString &repo, RefType
branches.append({m.captured(1),
QString(), // no remote
m.captured(2),
RefType::Head});
RefType::Head,
-1});
} else if (ref & Remote && (m = remoteRe.match(o)).hasMatch()) {
branches.append({m.captured(1).append(QLatin1Char('/') + m.captured(2)),
m.captured(2),
m.captured(1),
m.captured(3),
RefType::Remote});
RefType::Remote,
-1});
} else if (ref & Tag && (m = tagRe.match(o)).hasMatch()) {
branches.append({m.captured(1),
QString(), // no remote
m.captured(3).isEmpty() ? QString() : m.captured(2),
RefType::Tag});
RefType::Tag,
-1});
}
}
// clang-format on
......
......@@ -23,6 +23,7 @@ struct Branch {
QString remote;
QString commit;
RefType type;
int score; // used for scoring when filtering
};
bool isGitRepo(const QString &repo);
......
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