From 09b1cf78fd0c96c929176d360902ed13787e33be Mon Sep 17 00:00:00 2001 From: Petar Perisin Date: Thu, 21 Mar 2013 00:35:30 +0100 Subject: [PATCH] Git: refactoring ChangeSelectionDialog > Dialog now combines show, cherry-pick and revert > has fixed path, and no way to change it > not created on stack Change-Id: I7cee0b2e775a80941b51a4ca023064baf0d6575c Reviewed-by: Orgad Shaneh Reviewed-by: Petar Perisin Reviewed-by: Tobias Hunger --- src/plugins/git/changeselectiondialog.cpp | 130 ++++++++++++++-------- src/plugins/git/changeselectiondialog.h | 40 +++++-- src/plugins/git/changeselectiondialog.ui | 104 ----------------- src/plugins/git/git.pro | 3 +- src/plugins/git/git.qbs | 1 - src/plugins/git/gitplugin.cpp | 97 +++++++--------- src/plugins/git/gitplugin.h | 8 +- 7 files changed, 152 insertions(+), 231 deletions(-) delete mode 100644 src/plugins/git/changeselectiondialog.ui diff --git a/src/plugins/git/changeselectiondialog.cpp b/src/plugins/git/changeselectiondialog.cpp index 49e01e25d59..8a750cd4db8 100644 --- a/src/plugins/git/changeselectiondialog.cpp +++ b/src/plugins/git/changeselectiondialog.cpp @@ -31,9 +31,15 @@ #include "gitplugin.h" #include "gitclient.h" -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include namespace Git { namespace Internal { @@ -41,27 +47,53 @@ namespace Internal { ChangeSelectionDialog::ChangeSelectionDialog(const QString &workingDirectory, QWidget *parent) : QDialog(parent) , m_process(0) + , m_workingDirectoryLabel(new QLabel(workingDirectory, this)) + , m_changeNumberEdit(new QLineEdit(this)) + , m_detailsText(new QPlainTextEdit(this)) + , m_showButton(new QPushButton(tr("Show"), this)) + , m_cherryPickButton(new QPushButton(tr("Cherry Pick"), this)) + , m_revertButton(new QPushButton(tr("Revert"), this)) + , m_cancelButton(new QPushButton(tr("Cancel"), this)) + , m_command(NoCommand) { - m_ui.setupUi(this); - if (!workingDirectory.isEmpty()) { - setWorkingDirectory(workingDirectory); - m_ui.workingDirectoryButton->setEnabled(false); - m_ui.workingDirectoryEdit->setEnabled(false); - } - setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); - connect(m_ui.workingDirectoryButton, SIGNAL(clicked()), this, SLOT(selectWorkingDirectory())); - setWindowTitle(tr("Select a Git Commit")); - bool ok; m_gitBinaryPath = GitPlugin::instance()->gitClient()->gitBinaryPath(&ok); if (!ok) return; + QGridLayout* layout = new QGridLayout(this); + layout->addWidget(new QLabel(tr("Working Directory:"), this), 0, 0 , 1, 1); + layout->addWidget(m_workingDirectoryLabel, 0, 1, 1, 1); + layout->addWidget(new QLabel(tr("Change:"), this),1, 0, 1, 1); + layout->addWidget(m_changeNumberEdit, 1, 1, 1, 1); + layout->addWidget(m_detailsText, 2, 0, 1, 2); + + QHBoxLayout* buttonsLine = new QHBoxLayout(); + buttonsLine->addWidget(m_cancelButton); + buttonsLine->addStretch(); + buttonsLine->addWidget(m_revertButton); + buttonsLine->addWidget(m_cherryPickButton); + buttonsLine->addWidget(m_showButton); + + layout->addLayout(buttonsLine, 3, 0 ,1 ,2); + + m_changeNumberEdit->setFocus(Qt::ActiveWindowFocusReason); + m_changeNumberEdit->setText(QLatin1String("HEAD")); + m_changeNumberEdit->selectAll(); + + setWindowTitle(tr("Select a Git Commit")); + setGeometry(0, 0, 550, 350); + adjustPosition(parent); + m_gitEnvironment = GitPlugin::instance()->gitClient()->processEnvironment(); - connect(m_ui.changeNumberEdit, SIGNAL(textChanged(QString)), + connect(m_changeNumberEdit, SIGNAL(textChanged(QString)), this, SLOT(recalculateDetails(QString))); - connect(m_ui.workingDirectoryEdit, SIGNAL(textChanged(QString)), this, SLOT(refresh())); - refresh(); + connect(m_showButton, SIGNAL(clicked()), this, SLOT(acceptShow())); + connect(m_cherryPickButton, SIGNAL(clicked()), this, SLOT(acceptCherryPick())); + connect(m_revertButton, SIGNAL(clicked()), this, SLOT(acceptRevert())); + connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject())); + + recalculateDetails(m_changeNumberEdit->text()); } ChangeSelectionDialog::~ChangeSelectionDialog() @@ -71,49 +103,53 @@ ChangeSelectionDialog::~ChangeSelectionDialog() QString ChangeSelectionDialog::change() const { - return m_ui.changeNumberEdit->text(); + return m_changeNumberEdit->text(); } QString ChangeSelectionDialog::workingDirectory() const { - return m_ui.workingDirectoryEdit->text(); + return m_workingDirectoryLabel->text(); } -void ChangeSelectionDialog::setWorkingDirectory(const QString &s) +ChangeCommand ChangeSelectionDialog::command() const { - if (s.isEmpty()) - return; - m_ui.workingDirectoryEdit->setText(QDir::toNativeSeparators(s)); - m_ui.changeNumberEdit->setFocus(Qt::ActiveWindowFocusReason); - m_ui.changeNumberEdit->setText(QLatin1String("HEAD")); - m_ui.changeNumberEdit->selectAll(); + return m_command; } -void ChangeSelectionDialog::selectWorkingDirectory() +void ChangeSelectionDialog::acceptCherryPick() { - QString location = QFileDialog::getExistingDirectory(this, tr("Select Working Directory"), - m_ui.workingDirectoryEdit->text()); - if (location.isEmpty()) - return; + m_command = CherryPick; + accept(); +} - // Verify that the location is a repository - // We allow specifying a directory, which is not the head directory of the repository. - // This is useful for git show commit:./file - QString topLevel = GitPlugin::instance()->gitClient()->findRepositoryForDirectory(location); - if (!topLevel.isEmpty()) - m_ui.workingDirectoryEdit->setText(location); - else // Did not find a repo - QMessageBox::critical(this, tr("Error"), - tr("Selected directory is not a Git repository.")); +void ChangeSelectionDialog::acceptRevert() +{ + m_command = Revert; + accept(); +} + +void ChangeSelectionDialog::acceptShow() +{ + m_command = Show; + accept(); } //! Set commit message in details void ChangeSelectionDialog::setDetails(int exitCode) { - if (exitCode == 0) - m_ui.detailsText->setPlainText(QString::fromUtf8(m_process->readAllStandardOutput())); - else - m_ui.detailsText->setPlainText(tr("Error: Unknown reference")); + if (exitCode == 0) { + m_detailsText->setPlainText(QString::fromUtf8(m_process->readAllStandardOutput())); + enableButtons(true); + } else { + m_detailsText->setPlainText(tr("Error: Unknown reference")); + } +} + +void ChangeSelectionDialog::enableButtons(bool b) +{ + m_showButton->setEnabled(b); + m_cherryPickButton->setEnabled(b); + m_revertButton->setEnabled(b); } void ChangeSelectionDialog::recalculateDetails(const QString &ref) @@ -123,6 +159,7 @@ void ChangeSelectionDialog::recalculateDetails(const QString &ref) m_process->waitForFinished(); delete m_process; } + enableButtons(false); QStringList args; args << QLatin1String("log") << QLatin1String("-n1") << ref; @@ -136,14 +173,9 @@ void ChangeSelectionDialog::recalculateDetails(const QString &ref) m_process->start(m_gitBinaryPath, args); m_process->closeWriteChannel(); if (!m_process->waitForStarted()) - m_ui.detailsText->setPlainText(tr("Error: Could not start Git.")); + m_detailsText->setPlainText(tr("Error: Could not start Git.")); else - m_ui.detailsText->setPlainText(tr("Fetching commit data...")); -} - -void ChangeSelectionDialog::refresh() -{ - recalculateDetails(m_ui.changeNumberEdit->text()); + m_detailsText->setPlainText(tr("Fetching commit data...")); } } // Internal diff --git a/src/plugins/git/changeselectiondialog.h b/src/plugins/git/changeselectiondialog.h index 230c4966c7c..0f2e77e6ea9 100644 --- a/src/plugins/git/changeselectiondialog.h +++ b/src/plugins/git/changeselectiondialog.h @@ -32,38 +32,60 @@ #include #include -QT_FORWARD_DECLARE_CLASS(QProcess) -#include "ui_changeselectiondialog.h" +QT_BEGIN_NAMESPACE +class QPushButton; +class QLabel; +class QLineEdit; +class QPlainTextEdit; +class QProcess; +QT_END_NAMESPACE namespace Git { namespace Internal { +enum ChangeCommand { + NoCommand, + CherryPick, + Revert, + Show +}; + class ChangeSelectionDialog : public QDialog { Q_OBJECT public: - ChangeSelectionDialog(const QString &workingDirectory = QString(), QWidget *parent = 0); + ChangeSelectionDialog(const QString &workingDirectory, QWidget *parent); ~ChangeSelectionDialog(); QString change() const; QString workingDirectory() const; - void setWorkingDirectory(const QString &s); - -public slots: - void selectWorkingDirectory(); + ChangeCommand command() const; private slots: void setDetails(int exitCode); void recalculateDetails(const QString &ref); - void refresh(); + void acceptCherryPick(); + void acceptRevert(); + void acceptShow(); private: - Ui_ChangeSelectionDialog m_ui; + void enableButtons(bool b); + QProcess* m_process; QString m_gitBinaryPath; QProcessEnvironment m_gitEnvironment; + + QLabel* m_workingDirectoryLabel; + QLineEdit* m_changeNumberEdit; + QPlainTextEdit* m_detailsText; + QPushButton* m_showButton; + QPushButton* m_cherryPickButton; + QPushButton* m_revertButton; + QPushButton* m_cancelButton; + + ChangeCommand m_command; }; } // namespace Internal diff --git a/src/plugins/git/changeselectiondialog.ui b/src/plugins/git/changeselectiondialog.ui deleted file mode 100644 index d4b56fa7cba..00000000000 --- a/src/plugins/git/changeselectiondialog.ui +++ /dev/null @@ -1,104 +0,0 @@ - - - Git::Internal::ChangeSelectionDialog - - - - 0 - 0 - 595 - 396 - - - - - QLayout::SetDefaultConstraint - - - - - Working directory: - - - - - - - - - - Select - - - - - - - Change: - - - - - - - - - - false - - - QPlainTextEdit::NoWrap - - - true - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - buttonBox - accepted() - Git::Internal::ChangeSelectionDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - Git::Internal::ChangeSelectionDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/src/plugins/git/git.pro b/src/plugins/git/git.pro index d5dca2d4fe2..06634276437 100644 --- a/src/plugins/git/git.pro +++ b/src/plugins/git/git.pro @@ -48,8 +48,7 @@ SOURCES += gitplugin.cpp \ mergetool.cpp \ branchcheckoutdialog.cpp -FORMS += changeselectiondialog.ui \ - settingspage.ui \ +FORMS += settingspage.ui \ gitsubmitpanel.ui \ branchdialog.ui \ stashdialog.ui \ diff --git a/src/plugins/git/git.qbs b/src/plugins/git/git.qbs index 2b7cdd4d1d9..2f92f3154ba 100644 --- a/src/plugins/git/git.qbs +++ b/src/plugins/git/git.qbs @@ -28,7 +28,6 @@ QtcPlugin { "branchmodel.h", "changeselectiondialog.cpp", "changeselectiondialog.h", - "changeselectiondialog.ui", "clonewizard.cpp", "clonewizard.h", "clonewizardpage.cpp", diff --git a/src/plugins/git/gitplugin.cpp b/src/plugins/git/gitplugin.cpp index 314b31d0793..63061d8cc82 100644 --- a/src/plugins/git/gitplugin.cpp +++ b/src/plugins/git/gitplugin.cpp @@ -130,7 +130,6 @@ GitPlugin *GitPlugin::m_instance = 0; GitPlugin::GitPlugin() : VcsBase::VcsBasePlugin(Git::Constants::GITSUBMITEDITOR_ID), m_commandLocator(0), - m_showAction(0), m_submitCurrentAction(0), m_diffSelectedFilesAction(0), m_undoAction(0), @@ -138,7 +137,6 @@ GitPlugin::GitPlugin() : m_menuAction(0), m_applyCurrentFilePatchAction(0), m_gitClient(0), - m_changeSelectionDialog(0), m_submitActionTriggered(false) { m_instance = this; @@ -440,12 +438,8 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage) globalcontext, true, SLOT(startRebase())); createRepositoryAction(localRepositoryMenu, - tr("Revert Single Commit..."), Core::Id("Git.Revert"), - globalcontext, true, SLOT(startRevertCommit())); - - createRepositoryAction(localRepositoryMenu, - tr("Cherry-Pick Commit..."), Core::Id("Git.CherryPick"), - globalcontext, true, SLOT(startCherryPickCommit())); + tr("Change-related Actions..."), Core::Id("Git.ChangeRelatedActions"), + globalcontext, true, SLOT(startChangeRelatedAction())); // -------------- localRepositoryMenu->addSeparator(globalcontext); @@ -589,11 +583,6 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage) // -------------- gitContainer->addSeparator(globalcontext); - m_showAction - = createRepositoryAction(gitContainer, - tr("Show..."), Core::Id("Git.ShowCommit"), - globalcontext, true, SLOT(showCommit())).first; - m_createRepositoryAction = new QAction(tr("Create Repository..."), this); Core::Command *createRepositoryCommand = Core::ActionManager::registerAction(m_createRepositoryAction, "Git.CreateRepository", globalcontext); connect(m_createRepositoryAction, SIGNAL(triggered()), this, SLOT(createRepository())); @@ -741,40 +730,51 @@ void GitPlugin::startRebase() m_gitClient->interactiveRebase(workingDirectory, change); } -void GitPlugin::startRevertCommit() +void GitPlugin::startChangeRelatedAction() { const VcsBase::VcsBasePluginState state = currentState(); - QString workingDirectory = state.currentDirectoryOrTopLevel(); + const QString workingDirectory = state.currentDirectoryOrTopLevel(); if (workingDirectory.isEmpty()) return; - GitClient::StashGuard stashGuard(workingDirectory, QLatin1String("Revert")); + + QPointer dialog = new ChangeSelectionDialog + (workingDirectory, Core::ICore::mainWindow()); + + int result = dialog->exec(); + + if (dialog.isNull() || (result == QDialog::Rejected) || dialog->change().isEmpty()) + return; + + const QString change = dialog->change(); + + if (dialog->command() == Show) { + m_gitClient->show(workingDirectory, change); + return; + } + + QString command; + bool (GitClient::*commandFunction)(const QString&, const QString&); + switch (dialog->command()) { + case CherryPick: + command = QLatin1String("Cherry-pick"); + commandFunction = &GitClient::cherryPickCommit; + break; + case Revert: + command = QLatin1String("Revert"); + commandFunction = &GitClient::revertCommit; + break; + default: + return; + } + + GitClient::StashGuard stashGuard(workingDirectory, command); if (stashGuard.stashingFailed(true)) return; - ChangeSelectionDialog changeSelectionDialog(workingDirectory); - if (changeSelectionDialog.exec() != QDialog::Accepted) - return; - const QString change = changeSelectionDialog.change(); - if (!change.isEmpty() && !m_gitClient->revertCommit(workingDirectory, change)) + if (!(m_gitClient->*commandFunction)(workingDirectory, change)) stashGuard.preventPop(); -} -void GitPlugin::startCherryPickCommit() -{ - const VcsBase::VcsBasePluginState state = currentState(); - QString workingDirectory = state.currentDirectoryOrTopLevel(); - if (workingDirectory.isEmpty()) - return; - GitClient::StashGuard stashGuard(state.topLevel(), QLatin1String("Cherry-pick")); - if (stashGuard.stashingFailed(true)) - return; - ChangeSelectionDialog changeSelectionDialog(workingDirectory); - - if (changeSelectionDialog.exec() != QDialog::Accepted) - return; - const QString change = changeSelectionDialog.change(); - if (!change.isEmpty() && !m_gitClient->cherryPickCommit(workingDirectory, change)) - stashGuard.preventPop(); + delete dialog; } void GitPlugin::stageFile() @@ -1228,9 +1228,6 @@ void GitPlugin::updateActions(VcsBase::VcsBasePlugin::ActionState as) foreach (QAction *repositoryAction, m_repositoryActions) repositoryAction->setEnabled(repositoryEnabled); updateRepositoryBrowserAction(); - - // Prompts for repo. - m_showAction->setEnabled(true); } void GitPlugin::updateRepositoryBrowserAction() @@ -1240,24 +1237,6 @@ void GitPlugin::updateRepositoryBrowserAction() m_repositoryBrowserAction->setEnabled(repositoryEnabled && hasRepositoryBrowserCmd); } -void GitPlugin::showCommit() -{ - const VcsBase::VcsBasePluginState state = currentState(); - - if (!m_changeSelectionDialog) - m_changeSelectionDialog = new ChangeSelectionDialog(); - - m_changeSelectionDialog->setWorkingDirectory(state.currentDirectoryOrTopLevel()); - - if (m_changeSelectionDialog->exec() != QDialog::Accepted) - return; - const QString change = m_changeSelectionDialog->change(); - if (change.isEmpty()) - return; - - m_gitClient->show(m_changeSelectionDialog->workingDirectory(), change); -} - const GitSettings &GitPlugin::settings() const { return m_settings; diff --git a/src/plugins/git/gitplugin.h b/src/plugins/git/gitplugin.h index e0ffc8a45ab..1517531cbfb 100644 --- a/src/plugins/git/gitplugin.h +++ b/src/plugins/git/gitplugin.h @@ -68,7 +68,6 @@ namespace Internal { class GitVersionControl; class GitClient; -class ChangeSelectionDialog; class GitSubmitEditor; class CommitData; class StashDialog; @@ -117,8 +116,7 @@ private slots: void undoUnstagedFileChanges(); void resetRepository(); void startRebase(); - void startRevertCommit(); - void startCherryPickCommit(); + void startChangeRelatedAction(); void stageFile(); void unstageFile(); void gitkForCurrentFile(); @@ -129,7 +127,6 @@ private slots: void promptApplyPatch(); void gitClientMemberFuncRepositoryAction(); - void showCommit(); void startAmendCommit(); void stash(); void stashSnapshot(); @@ -201,8 +198,6 @@ private: Locator::CommandLocator *m_commandLocator; QAction *m_createRepositoryAction; - QAction *m_showAction; - QAction *m_submitCurrentAction; QAction *m_diffSelectedFilesAction; QAction *m_undoAction; @@ -216,7 +211,6 @@ private: Utils::ParameterAction *m_applyCurrentFilePatchAction; GitClient *m_gitClient; - ChangeSelectionDialog *m_changeSelectionDialog; QPointer m_stashDialog; QPointer m_branchDialog; QPointer m_remoteDialog;