forked from qt-creator/qt-creator
Fixes: Added dbl-click handling for remote branches
Task: RevBy: AutoTest: Details: Moved code to create a local branch from model to dialog which is slightly better since all the modifying app code is there.
This commit is contained in:
@@ -7,6 +7,10 @@
|
|||||||
#include <QtGui/QPushButton>
|
#include <QtGui/QPushButton>
|
||||||
#include <QtGui/QMessageBox>
|
#include <QtGui/QMessageBox>
|
||||||
|
|
||||||
|
#include <QtCore/QDebug>
|
||||||
|
|
||||||
|
enum { debug = 0 };
|
||||||
|
|
||||||
// Single selection helper
|
// Single selection helper
|
||||||
static inline int selectedRow(const QAbstractItemView *listView)
|
static inline int selectedRow(const QAbstractItemView *listView)
|
||||||
{
|
{
|
||||||
@@ -16,6 +20,13 @@ static inline int selectedRow(const QAbstractItemView *listView)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper to select a row. No sooner said then done
|
||||||
|
static inline void selectListRow(QAbstractItemView *iv, int row)
|
||||||
|
{
|
||||||
|
const QModelIndex index = iv->model()->index(row, 0);
|
||||||
|
iv->selectionModel()->select(index, QItemSelectionModel::Select);
|
||||||
|
}
|
||||||
|
|
||||||
namespace Git {
|
namespace Git {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
@@ -40,6 +51,8 @@ BranchDialog::BranchDialog(QWidget *parent) :
|
|||||||
|
|
||||||
connect(m_ui->localBranchListView, SIGNAL(doubleClicked(QModelIndex)), this,
|
connect(m_ui->localBranchListView, SIGNAL(doubleClicked(QModelIndex)), this,
|
||||||
SLOT(slotLocalBranchActivated()));
|
SLOT(slotLocalBranchActivated()));
|
||||||
|
connect(m_ui->remoteBranchListView, SIGNAL(doubleClicked(QModelIndex)), this,
|
||||||
|
SLOT(slotRemoteBranchActivated(QModelIndex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
BranchDialog::~BranchDialog()
|
BranchDialog::~BranchDialog()
|
||||||
@@ -59,7 +72,7 @@ bool BranchDialog::init(GitClient *client, const QString &workingDirectory, QStr
|
|||||||
m_ui->repositoryFieldLabel->setText(m_repoDirectory);
|
m_ui->repositoryFieldLabel->setText(m_repoDirectory);
|
||||||
|
|
||||||
m_localModel = new LocalBranchModel(client, this);
|
m_localModel = new LocalBranchModel(client, this);
|
||||||
connect(m_localModel, SIGNAL(newBranchCreated(QString)), this, SLOT(slotNewLocalBranchCreated(QString)));
|
connect(m_localModel, SIGNAL(newBranchEntered(QString)), this, SLOT(slotCreateLocalBranch(QString)));
|
||||||
m_remoteModel = new RemoteBranchModel(client, this);
|
m_remoteModel = new RemoteBranchModel(client, this);
|
||||||
if (!m_localModel->refresh(workingDirectory, errorMessage)
|
if (!m_localModel->refresh(workingDirectory, errorMessage)
|
||||||
|| !m_remoteModel->refresh(workingDirectory, errorMessage))
|
|| !m_remoteModel->refresh(workingDirectory, errorMessage))
|
||||||
@@ -101,14 +114,12 @@ void BranchDialog::slotEnableButtons()
|
|||||||
m_deleteButton->setEnabled(currentIsNotSelected);
|
m_deleteButton->setEnabled(currentIsNotSelected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BranchDialog::slotNewLocalBranchCreated(const QString &b)
|
void BranchDialog::selectLocalBranch(const QString &b)
|
||||||
{
|
{
|
||||||
// Select the newly created branch
|
// Select the newly created branch
|
||||||
const int row = m_localModel->findBranchByName(b);
|
const int row = m_localModel->findBranchByName(b);
|
||||||
if (row != -1) {
|
if (row != -1)
|
||||||
const QModelIndex index = m_localModel->index(row);
|
selectListRow(m_ui->localBranchListView, row);
|
||||||
m_ui->localBranchListView->selectionModel()->select(index, QItemSelectionModel::Select);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BranchDialog::ask(const QString &title, const QString &what, bool defaultButton)
|
bool BranchDialog::ask(const QString &title, const QString &what, bool defaultButton)
|
||||||
@@ -143,6 +154,26 @@ void BranchDialog::slotDeleteSelectedBranch()
|
|||||||
QMessageBox::warning(this, tr("Failed to delete branch"), errorMessage);
|
QMessageBox::warning(this, tr("Failed to delete branch"), errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BranchDialog::slotCreateLocalBranch(const QString &branchName)
|
||||||
|
{
|
||||||
|
// Create
|
||||||
|
QString output;
|
||||||
|
QString errorMessage;
|
||||||
|
bool ok = false;
|
||||||
|
do {
|
||||||
|
if (!m_client->synchronousBranchCmd(m_repoDirectory, QStringList(branchName), &output, &errorMessage))
|
||||||
|
break;
|
||||||
|
if (!m_localModel->refresh(m_repoDirectory, &errorMessage))
|
||||||
|
break;
|
||||||
|
ok = true;
|
||||||
|
} while (false);
|
||||||
|
if (!ok) {
|
||||||
|
QMessageBox::warning(this, tr("Failed to create branch"), errorMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
selectLocalBranch(branchName);
|
||||||
|
}
|
||||||
|
|
||||||
void BranchDialog::slotLocalBranchActivated()
|
void BranchDialog::slotLocalBranchActivated()
|
||||||
{
|
{
|
||||||
if (m_checkoutButton->isEnabled())
|
if (m_checkoutButton->isEnabled())
|
||||||
@@ -173,6 +204,58 @@ void BranchDialog::slotCheckoutSelectedBranch()
|
|||||||
m_client->checkoutBranch(m_repoDirectory, name);
|
m_client->checkoutBranch(m_repoDirectory, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BranchDialog::slotRemoteBranchActivated(const QModelIndex &i)
|
||||||
|
{
|
||||||
|
// Double click on a remote branch (origin/foo): Switch to matching
|
||||||
|
// local (foo) one or offer to create a tracking branch.
|
||||||
|
const QString remoteName = m_remoteModel->branchName(i.row());
|
||||||
|
// build the name of the corresponding local branch
|
||||||
|
// and look for it in the local model.
|
||||||
|
const int slashPos = remoteName.indexOf(QLatin1Char('/'));
|
||||||
|
if (slashPos == -1)
|
||||||
|
return;
|
||||||
|
const QString localBranch = remoteName.mid(slashPos + 1);
|
||||||
|
if (localBranch == QLatin1String("HEAD") || localBranch == QLatin1String("master"))
|
||||||
|
return;
|
||||||
|
const int localIndex = m_localModel->findBranchByName(localBranch);
|
||||||
|
if (debug)
|
||||||
|
qDebug() << Q_FUNC_INFO << remoteName << localBranch << localIndex;
|
||||||
|
// There is a matching a local one!
|
||||||
|
if (localIndex != -1) {
|
||||||
|
// Is it the current one? Just close.
|
||||||
|
if (m_localModel->currentBranch() == localIndex) {
|
||||||
|
accept();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Nope, select and trigger checkout
|
||||||
|
selectListRow(m_ui->localBranchListView, localIndex);
|
||||||
|
slotLocalBranchActivated();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Does not exist yet. Ask to create.
|
||||||
|
const QString msg = tr("Would you like to create a local branch '%1' tracking the remote branch '%2'?").arg(localBranch, remoteName);
|
||||||
|
if (!ask(tr("Create branch"), msg, true))
|
||||||
|
return;
|
||||||
|
QStringList args(QLatin1String("--track"));
|
||||||
|
args << localBranch << remoteName;
|
||||||
|
QString errorMessage;
|
||||||
|
bool ok = false;
|
||||||
|
do {
|
||||||
|
QString output;
|
||||||
|
if (!m_client->synchronousBranchCmd(m_repoDirectory, args, &output, &errorMessage))
|
||||||
|
break;
|
||||||
|
if (!m_localModel->refresh(m_repoDirectory, &errorMessage))
|
||||||
|
break;
|
||||||
|
ok = true;
|
||||||
|
} while (false);
|
||||||
|
if (!ok) {
|
||||||
|
QMessageBox::warning(this, tr("Failed to create a tracking branch"), errorMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Select it
|
||||||
|
selectLocalBranch(localBranch);
|
||||||
|
}
|
||||||
|
|
||||||
void BranchDialog::changeEvent(QEvent *e)
|
void BranchDialog::changeEvent(QEvent *e)
|
||||||
{
|
{
|
||||||
switch (e->type()) {
|
switch (e->type()) {
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
|
|
||||||
#include <QtGui/QDialog>
|
#include <QtGui/QDialog>
|
||||||
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QPushButton;
|
class QPushButton;
|
||||||
|
class QModelIndex;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace Git {
|
namespace Git {
|
||||||
@@ -40,10 +42,12 @@ namespace Git {
|
|||||||
void slotCheckoutSelectedBranch();
|
void slotCheckoutSelectedBranch();
|
||||||
void slotDeleteSelectedBranch();
|
void slotDeleteSelectedBranch();
|
||||||
void slotLocalBranchActivated();
|
void slotLocalBranchActivated();
|
||||||
void slotNewLocalBranchCreated(const QString &);
|
void slotRemoteBranchActivated(const QModelIndex &);
|
||||||
|
void slotCreateLocalBranch(const QString &branchName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool ask(const QString &title, const QString &what, bool defaultButton);
|
bool ask(const QString &title, const QString &what, bool defaultButton);
|
||||||
|
void selectLocalBranch(const QString &b);
|
||||||
|
|
||||||
int selectedLocalBranchIndex() const;
|
int selectedLocalBranchIndex() const;
|
||||||
int selectedRemoteBranchIndex() const;
|
int selectedRemoteBranchIndex() const;
|
||||||
|
|||||||
@@ -218,33 +218,18 @@ bool LocalBranchModel::setData(const QModelIndex &index, const QVariant &value,
|
|||||||
if (role != Qt::EditRole || index.row() < branchCount())
|
if (role != Qt::EditRole || index.row() < branchCount())
|
||||||
return false;
|
return false;
|
||||||
const QString branchName = value.toString();
|
const QString branchName = value.toString();
|
||||||
const bool ok = checkNewBranchName(branchName);
|
// Delay the signal as we don't ourselves to be reset while
|
||||||
if (debug)
|
// in setData().
|
||||||
qDebug() << Q_FUNC_INFO << branchName << ok;
|
if (checkNewBranchName(branchName)) {
|
||||||
if (!ok)
|
m_newBranch = branchName;
|
||||||
return false;
|
QTimer::singleShot(0, this, SLOT(slotNewBranchDelayedRefresh()));
|
||||||
// Create
|
}
|
||||||
QString output;
|
|
||||||
QString errorMessage;
|
|
||||||
if (!runGitBranchCommand(workingDirectory(), QStringList(branchName), &output, &errorMessage))
|
|
||||||
return false;
|
|
||||||
m_newBranch = branchName;
|
|
||||||
// Start a delayed complete refresh and return true for now.
|
|
||||||
QTimer::singleShot(0, this, SLOT(slotNewBranchDelayedRefresh()));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocalBranchModel::slotNewBranchDelayedRefresh()
|
void LocalBranchModel::slotNewBranchDelayedRefresh()
|
||||||
{
|
{
|
||||||
if (debug)
|
emit newBranchEntered(m_newBranch);
|
||||||
qDebug() << Q_FUNC_INFO;
|
|
||||||
|
|
||||||
QString errorMessage;
|
|
||||||
if (!refresh(workingDirectory(), &errorMessage)) {
|
|
||||||
qWarning("%s", qPrintable(errorMessage));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
emit newBranchCreated(m_newBranch);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ public:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newBranchCreated(const QString &);
|
void newBranchCreated(const QString &);
|
||||||
|
void newBranchEntered(const QString &);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void slotNewBranchDelayedRefresh();
|
void slotNewBranchDelayedRefresh();
|
||||||
|
|||||||
Reference in New Issue
Block a user