From 94d646a206a8a0bcd5e50c42ac633994e42ea88b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint <Friedemann.Kleint@nokia.com> Date: Fri, 9 Jan 2009 16:27:42 +0100 Subject: [PATCH] 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. --- src/plugins/git/branchdialog.cpp | 95 ++++++++++++++++++++++++++++++-- src/plugins/git/branchdialog.h | 6 +- src/plugins/git/branchmodel.cpp | 29 +++------- src/plugins/git/branchmodel.h | 1 + 4 files changed, 102 insertions(+), 29 deletions(-) diff --git a/src/plugins/git/branchdialog.cpp b/src/plugins/git/branchdialog.cpp index 3eeee2347b6..5a1aa5d00d6 100644 --- a/src/plugins/git/branchdialog.cpp +++ b/src/plugins/git/branchdialog.cpp @@ -7,6 +7,10 @@ #include <QtGui/QPushButton> #include <QtGui/QMessageBox> +#include <QtCore/QDebug> + +enum { debug = 0 }; + // Single selection helper static inline int selectedRow(const QAbstractItemView *listView) { @@ -16,6 +20,13 @@ static inline int selectedRow(const QAbstractItemView *listView) 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 Internal { @@ -40,6 +51,8 @@ BranchDialog::BranchDialog(QWidget *parent) : connect(m_ui->localBranchListView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotLocalBranchActivated())); + connect(m_ui->remoteBranchListView, SIGNAL(doubleClicked(QModelIndex)), this, + SLOT(slotRemoteBranchActivated(QModelIndex))); } BranchDialog::~BranchDialog() @@ -59,7 +72,7 @@ bool BranchDialog::init(GitClient *client, const QString &workingDirectory, QStr m_ui->repositoryFieldLabel->setText(m_repoDirectory); 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); if (!m_localModel->refresh(workingDirectory, errorMessage) || !m_remoteModel->refresh(workingDirectory, errorMessage)) @@ -101,14 +114,12 @@ void BranchDialog::slotEnableButtons() m_deleteButton->setEnabled(currentIsNotSelected); } -void BranchDialog::slotNewLocalBranchCreated(const QString &b) +void BranchDialog::selectLocalBranch(const QString &b) { // Select the newly created branch const int row = m_localModel->findBranchByName(b); - if (row != -1) { - const QModelIndex index = m_localModel->index(row); - m_ui->localBranchListView->selectionModel()->select(index, QItemSelectionModel::Select); - } + if (row != -1) + selectListRow(m_ui->localBranchListView, row); } 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); } +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() { if (m_checkoutButton->isEnabled()) @@ -173,6 +204,58 @@ void BranchDialog::slotCheckoutSelectedBranch() 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) { switch (e->type()) { diff --git a/src/plugins/git/branchdialog.h b/src/plugins/git/branchdialog.h index c803b57e833..5656c9e5c67 100644 --- a/src/plugins/git/branchdialog.h +++ b/src/plugins/git/branchdialog.h @@ -3,8 +3,10 @@ #include <QtGui/QDialog> + QT_BEGIN_NAMESPACE class QPushButton; +class QModelIndex; QT_END_NAMESPACE namespace Git { @@ -40,10 +42,12 @@ namespace Git { void slotCheckoutSelectedBranch(); void slotDeleteSelectedBranch(); void slotLocalBranchActivated(); - void slotNewLocalBranchCreated(const QString &); + void slotRemoteBranchActivated(const QModelIndex &); + void slotCreateLocalBranch(const QString &branchName); private: bool ask(const QString &title, const QString &what, bool defaultButton); + void selectLocalBranch(const QString &b); int selectedLocalBranchIndex() const; int selectedRemoteBranchIndex() const; diff --git a/src/plugins/git/branchmodel.cpp b/src/plugins/git/branchmodel.cpp index bb8ccf19510..9c778555d9d 100644 --- a/src/plugins/git/branchmodel.cpp +++ b/src/plugins/git/branchmodel.cpp @@ -218,33 +218,18 @@ bool LocalBranchModel::setData(const QModelIndex &index, const QVariant &value, if (role != Qt::EditRole || index.row() < branchCount()) return false; const QString branchName = value.toString(); - const bool ok = checkNewBranchName(branchName); - if (debug) - qDebug() << Q_FUNC_INFO << branchName << ok; - if (!ok) - return false; - // 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())); + // Delay the signal as we don't ourselves to be reset while + // in setData(). + if (checkNewBranchName(branchName)) { + m_newBranch = branchName; + QTimer::singleShot(0, this, SLOT(slotNewBranchDelayedRefresh())); + } return true; } void LocalBranchModel::slotNewBranchDelayedRefresh() { - if (debug) - qDebug() << Q_FUNC_INFO; - - QString errorMessage; - if (!refresh(workingDirectory(), &errorMessage)) { - qWarning("%s", qPrintable(errorMessage)); - return; - } - emit newBranchCreated(m_newBranch); + emit newBranchEntered(m_newBranch); } } diff --git a/src/plugins/git/branchmodel.h b/src/plugins/git/branchmodel.h index 91b1ca84dc0..54c4c698a77 100644 --- a/src/plugins/git/branchmodel.h +++ b/src/plugins/git/branchmodel.h @@ -87,6 +87,7 @@ public: signals: void newBranchCreated(const QString &); + void newBranchEntered(const QString &); private slots: void slotNewBranchDelayedRefresh(); -- GitLab