From 72093746ac6134551c7f7de8cc9570e1940b1e20 Mon Sep 17 00:00:00 2001 From: hjk Date: Mon, 8 May 2023 15:25:08 +0200 Subject: [PATCH] Git: Use LayoutBuilder in Gerrit options page Move towards using aspects and away from IOptionPage's QObject base. Change-Id: I07850a6b8e9fa7d2591efc6f76a23f249dfc164f Reviewed-by: Orgad Shaneh --- src/plugins/git/gerrit/gerritoptionspage.cpp | 128 +++++++++---------- src/plugins/git/gerrit/gerritoptionspage.h | 8 +- src/plugins/git/gerrit/gerritplugin.cpp | 20 +-- src/plugins/git/gerrit/gerritplugin.h | 4 +- 4 files changed, 77 insertions(+), 83 deletions(-) diff --git a/src/plugins/git/gerrit/gerritoptionspage.cpp b/src/plugins/git/gerrit/gerritoptionspage.cpp index 3b96d160bd0..3f225d82eb1 100644 --- a/src/plugins/git/gerrit/gerritoptionspage.cpp +++ b/src/plugins/git/gerrit/gerritoptionspage.cpp @@ -7,6 +7,8 @@ #include "../gittr.h" #include + +#include #include #include @@ -21,90 +23,82 @@ namespace Gerrit::Internal { class GerritOptionsWidget : public Core::IOptionsPageWidget { public: - GerritOptionsWidget(GerritOptionsPage *page, const QSharedPointer &p) - : m_page(page) - , m_hostLineEdit(new QLineEdit(this)) - , m_userLineEdit(new QLineEdit(this)) - , m_sshChooser(new Utils::PathChooser) - , m_curlChooser(new Utils::PathChooser) - , m_portSpinBox(new QSpinBox(this)) - , m_httpsCheckBox(new QCheckBox(Git::Tr::tr("HTTPS"))) - , m_parameters(p) + GerritOptionsWidget(const QSharedPointer &p, + const std::function &onChanged) + : m_parameters(p) { - m_hostLineEdit->setText(p->server.host); - m_userLineEdit->setText(p->server.user.userName); - m_sshChooser->setFilePath(p->ssh); - m_curlChooser->setFilePath(p->curl); - m_portSpinBox->setValue(p->server.port); - m_httpsCheckBox->setChecked(p->https); + auto hostLineEdit = new QLineEdit(p->server.host); - auto formLayout = new QFormLayout(this); - formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow); - formLayout->addRow(Git::Tr::tr("&Host:"), m_hostLineEdit); - formLayout->addRow(Git::Tr::tr("&User:"), m_userLineEdit); - m_sshChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); - m_sshChooser->setCommandVersionArguments({"-V"}); - m_sshChooser->setHistoryCompleter("Git.SshCommand.History"); - formLayout->addRow(Git::Tr::tr("&ssh:"), m_sshChooser); - m_curlChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); - m_curlChooser->setCommandVersionArguments({"-V"}); - formLayout->addRow(Git::Tr::tr("cur&l:"), m_curlChooser); - m_portSpinBox->setMinimum(1); - m_portSpinBox->setMaximum(65535); - formLayout->addRow(Git::Tr::tr("SSH &Port:"), m_portSpinBox); - formLayout->addRow(Git::Tr::tr("P&rotocol:"), m_httpsCheckBox); - m_httpsCheckBox->setToolTip(Git::Tr::tr( + auto userLineEdit = new QLineEdit(p->server.user.userName); + + auto sshChooser = new Utils::PathChooser; + sshChooser->setFilePath(p->ssh); + sshChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); + sshChooser->setCommandVersionArguments({"-V"}); + sshChooser->setHistoryCompleter("Git.SshCommand.History"); + + auto curlChooser = new Utils::PathChooser; + curlChooser->setFilePath(p->curl); + curlChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); + curlChooser->setCommandVersionArguments({"-V"}); + + auto portSpinBox = new QSpinBox(this); + portSpinBox->setValue(p->server.port); + portSpinBox->setRange(1, 65535); + + auto httpsCheckBox = new QCheckBox(Git::Tr::tr("HTTPS")); + httpsCheckBox->setChecked(p->https); + httpsCheckBox->setToolTip(Git::Tr::tr( "Determines the protocol used to form a URL in case\n" "\"canonicalWebUrl\" is not configured in the file\n" "\"gerrit.config\".")); - setTabOrder(m_sshChooser, m_curlChooser); - setTabOrder(m_curlChooser, m_portSpinBox); + + using namespace Layouting; + Form { + Git::Tr::tr("&Host:"), hostLineEdit, br, + Git::Tr::tr("&User:"), userLineEdit, br, + Git::Tr::tr("&ssh:"), sshChooser, br, + Git::Tr::tr("cur&l:"), curlChooser, br, + Git::Tr::tr("SSH &Port:"), portSpinBox, br, + Git::Tr::tr("P&rotocol:"), httpsCheckBox + }.attachTo(this); + + setOnApply([this, hostLineEdit, userLineEdit, sshChooser, + curlChooser, portSpinBox, httpsCheckBox, onChanged] { + GerritParameters newParameters; + newParameters.server = GerritServer(hostLineEdit->text().trimmed(), + static_cast(portSpinBox->value()), + userLineEdit->text().trimmed(), + GerritServer::Ssh); + newParameters.ssh = sshChooser->filePath(); + newParameters.curl = curlChooser->filePath(); + newParameters.https = httpsCheckBox->isChecked(); + + if (newParameters != *m_parameters) { + if (m_parameters->ssh == newParameters.ssh) + newParameters.portFlag = m_parameters->portFlag; + else + newParameters.setPortFlagBySshType(); + *m_parameters = newParameters; + m_parameters->toSettings(Core::ICore::settings()); + emit onChanged(); + } + }); } private: - void apply() final; - - GerritOptionsPage *m_page; - QLineEdit *m_hostLineEdit; - QLineEdit *m_userLineEdit; - Utils::PathChooser *m_sshChooser; - Utils::PathChooser *m_curlChooser; - QSpinBox *m_portSpinBox; - QCheckBox *m_httpsCheckBox; const QSharedPointer &m_parameters; }; -void GerritOptionsWidget::apply() -{ - GerritParameters newParameters; - newParameters.server = GerritServer(m_hostLineEdit->text().trimmed(), - static_cast(m_portSpinBox->value()), - m_userLineEdit->text().trimmed(), - GerritServer::Ssh); - newParameters.ssh = m_sshChooser->filePath(); - newParameters.curl = m_curlChooser->filePath(); - newParameters.https = m_httpsCheckBox->isChecked(); - - if (newParameters != *m_parameters) { - if (m_parameters->ssh == newParameters.ssh) - newParameters.portFlag = m_parameters->portFlag; - else - newParameters.setPortFlagBySshType(); - *m_parameters = newParameters; - m_parameters->toSettings(Core::ICore::settings()); - emit m_page->settingsChanged(); - } -} - // GerritOptionsPage -GerritOptionsPage::GerritOptionsPage(const QSharedPointer &p, QObject *parent) - : Core::IOptionsPage(parent) +GerritOptionsPage::GerritOptionsPage(const QSharedPointer &p, + const std::function &onChanged) { setId("Gerrit"); setDisplayName(Git::Tr::tr("Gerrit")); setCategory(VcsBase::Constants::VCS_SETTINGS_CATEGORY); - setWidgetCreator([this, p] { return new GerritOptionsWidget(this, p); }); + setWidgetCreator([p, onChanged] { return new GerritOptionsWidget(p, onChanged); }); } } // Gerrit::Internal diff --git a/src/plugins/git/gerrit/gerritoptionspage.h b/src/plugins/git/gerrit/gerritoptionspage.h index 6e6695ae367..96caab06092 100644 --- a/src/plugins/git/gerrit/gerritoptionspage.h +++ b/src/plugins/git/gerrit/gerritoptionspage.h @@ -13,13 +13,9 @@ class GerritParameters; class GerritOptionsPage : public Core::IOptionsPage { - Q_OBJECT - public: - GerritOptionsPage(const QSharedPointer &p, QObject *parent = nullptr); - -signals: - void settingsChanged(); + GerritOptionsPage(const QSharedPointer &p, + const std::function &onChanged); }; } // Gerrit::Internal diff --git a/src/plugins/git/gerrit/gerritplugin.cpp b/src/plugins/git/gerrit/gerritplugin.cpp index 3b46022e771..87ce5bfdaca 100644 --- a/src/plugins/git/gerrit/gerritplugin.cpp +++ b/src/plugins/git/gerrit/gerritplugin.cpp @@ -156,13 +156,22 @@ GerritPlugin::GerritPlugin() : m_parameters(new GerritParameters) , m_server(new GerritServer) { + m_parameters->fromSettings(ICore::settings()); + + m_gerritOptionsPage = new GerritOptionsPage(m_parameters, + [this] { + if (m_dialog) + m_dialog->scheduleUpdateRemotes(); + }); } -GerritPlugin::~GerritPlugin() = default; +GerritPlugin::~GerritPlugin() +{ + delete m_gerritOptionsPage; +} void GerritPlugin::addToMenu(ActionContainer *ac) { - m_parameters->fromSettings(ICore::settings()); QAction *openViewAction = new QAction(Git::Tr::tr("Gerrit..."), this); @@ -177,13 +186,6 @@ void GerritPlugin::addToMenu(ActionContainer *ac) ActionManager::registerAction(pushAction, Constants::GERRIT_PUSH); connect(pushAction, &QAction::triggered, this, [this] { push(); }); ac->addAction(m_pushToGerritCommand); - - auto options = new GerritOptionsPage(m_parameters, this); - connect(options, &GerritOptionsPage::settingsChanged, - this, [this] { - if (m_dialog) - m_dialog->scheduleUpdateRemotes(); - }); } void GerritPlugin::updateActions(const VcsBase::VcsBasePluginState &state) diff --git a/src/plugins/git/gerrit/gerritplugin.h b/src/plugins/git/gerrit/gerritplugin.h index 8de02570827..b7e0de29d81 100644 --- a/src/plugins/git/gerrit/gerritplugin.h +++ b/src/plugins/git/gerrit/gerritplugin.h @@ -3,7 +3,7 @@ #pragma once -#include +#include #include #include @@ -25,6 +25,7 @@ class GerritChange; class GerritDialog; class GerritParameters; class GerritServer; +class GerritOptionsPage; class GerritPlugin : public QObject { @@ -60,6 +61,7 @@ private: Core::Command *m_gerritCommand = nullptr; Core::Command *m_pushToGerritCommand = nullptr; QString m_reviewers; + GerritOptionsPage *m_gerritOptionsPage = nullptr; }; } // namespace Internal