Git: Enable renaming local branches by direct editing

Change-Id: I7edab94d949a6e48642f280af52fd156c803cafb
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Orgad Shaneh
2018-11-04 05:03:46 +02:00
committed by Orgad Shaneh
parent 1f1149bb58
commit a82dd10518
4 changed files with 37 additions and 1 deletions

View File

@@ -24,9 +24,11 @@
****************************************************************************/ ****************************************************************************/
#include "branchadddialog.h" #include "branchadddialog.h"
#include "branchmodel.h"
#include "ui_branchadddialog.h" #include "ui_branchadddialog.h"
#include "gitplugin.h" #include "gitplugin.h"
#include <utils/fancylineedit.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <QPushButton> #include <QPushButton>
@@ -87,6 +89,21 @@ private:
QStringList m_localBranches; QStringList m_localBranches;
}; };
BranchValidationDelegate::BranchValidationDelegate(QWidget *parent, BranchModel *model)
: QItemDelegate(parent)
, m_model(model)
{
}
QWidget *BranchValidationDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & /*option*/,
const QModelIndex & /*index*/) const
{
auto lineEdit = new Utils::FancyLineEdit(parent);
BranchNameValidator *validator = new BranchNameValidator(m_model->localBranchNames(), lineEdit);
lineEdit->setValidator(validator);
return lineEdit;
}
BranchAddDialog::BranchAddDialog(const QStringList &localBranches, bool addBranch, QWidget *parent) : BranchAddDialog::BranchAddDialog(const QStringList &localBranches, bool addBranch, QWidget *parent) :
QDialog(parent), QDialog(parent),

View File

@@ -26,13 +26,26 @@
#pragma once #pragma once
#include <QDialog> #include <QDialog>
#include <QItemDelegate>
namespace Git { namespace Git {
namespace Internal { namespace Internal {
class BranchModel;
namespace Ui { class BranchAddDialog; } namespace Ui { class BranchAddDialog; }
class BranchValidationDelegate : public QItemDelegate
{
public:
BranchValidationDelegate(QWidget *parent, BranchModel *model);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
private:
BranchModel *m_model;
};
class BranchAddDialog : public QDialog class BranchAddDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT

View File

@@ -317,7 +317,10 @@ Qt::ItemFlags BranchModel::flags(const QModelIndex &index) const
BranchNode *node = indexToNode(index); BranchNode *node = indexToNode(index);
if (!node) if (!node)
return Qt::NoItemFlags; return Qt::NoItemFlags;
return Qt::ItemIsSelectable | Qt::ItemIsEnabled; Qt::ItemFlags res = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
if (node->isLeaf() && node->isLocal())
res |= Qt::ItemIsEditable;
return res;
} }
void BranchModel::clear() void BranchModel::clear()

View File

@@ -126,6 +126,9 @@ BranchView::BranchView() :
this, &BranchView::BranchView::setIncludeTags); this, &BranchView::BranchView::setIncludeTags);
m_branchView->setContextMenuPolicy(Qt::CustomContextMenu); m_branchView->setContextMenuPolicy(Qt::CustomContextMenu);
m_branchView->setEditTriggers(QAbstractItemView::SelectedClicked
| QAbstractItemView::EditKeyPressed);
m_branchView->setItemDelegate(new BranchValidationDelegate(this, m_model));
connect(m_branchView, &QAbstractItemView::doubleClicked, connect(m_branchView, &QAbstractItemView::doubleClicked,
this, [this](const QModelIndex &idx) { log(m_filterModel->mapToSource(idx)); }); this, [this](const QModelIndex &idx) { log(m_filterModel->mapToSource(idx)); });
connect(m_branchView, &QWidget::customContextMenuRequested, connect(m_branchView, &QWidget::customContextMenuRequested,