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 <orgads@gmail.com>
This commit is contained in:
hjk
2023-05-08 15:25:08 +02:00
parent 5a318df0ee
commit 72093746ac
4 changed files with 77 additions and 83 deletions

View File

@@ -7,6 +7,8 @@
#include "../gittr.h" #include "../gittr.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/layoutbuilder.h>
#include <utils/pathchooser.h> #include <utils/pathchooser.h>
#include <vcsbase/vcsbaseconstants.h> #include <vcsbase/vcsbaseconstants.h>
@@ -21,69 +23,56 @@ namespace Gerrit::Internal {
class GerritOptionsWidget : public Core::IOptionsPageWidget class GerritOptionsWidget : public Core::IOptionsPageWidget
{ {
public: public:
GerritOptionsWidget(GerritOptionsPage *page, const QSharedPointer<GerritParameters> &p) GerritOptionsWidget(const QSharedPointer<GerritParameters> &p,
: m_page(page) const std::function<void()> &onChanged)
, m_hostLineEdit(new QLineEdit(this)) : m_parameters(p)
, 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)
{ {
m_hostLineEdit->setText(p->server.host); auto hostLineEdit = new QLineEdit(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 formLayout = new QFormLayout(this); auto userLineEdit = new QLineEdit(p->server.user.userName);
formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
formLayout->addRow(Git::Tr::tr("&Host:"), m_hostLineEdit); auto sshChooser = new Utils::PathChooser;
formLayout->addRow(Git::Tr::tr("&User:"), m_userLineEdit); sshChooser->setFilePath(p->ssh);
m_sshChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); sshChooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
m_sshChooser->setCommandVersionArguments({"-V"}); sshChooser->setCommandVersionArguments({"-V"});
m_sshChooser->setHistoryCompleter("Git.SshCommand.History"); sshChooser->setHistoryCompleter("Git.SshCommand.History");
formLayout->addRow(Git::Tr::tr("&ssh:"), m_sshChooser);
m_curlChooser->setExpectedKind(Utils::PathChooser::ExistingCommand); auto curlChooser = new Utils::PathChooser;
m_curlChooser->setCommandVersionArguments({"-V"}); curlChooser->setFilePath(p->curl);
formLayout->addRow(Git::Tr::tr("cur&l:"), m_curlChooser); curlChooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
m_portSpinBox->setMinimum(1); curlChooser->setCommandVersionArguments({"-V"});
m_portSpinBox->setMaximum(65535);
formLayout->addRow(Git::Tr::tr("SSH &Port:"), m_portSpinBox); auto portSpinBox = new QSpinBox(this);
formLayout->addRow(Git::Tr::tr("P&rotocol:"), m_httpsCheckBox); portSpinBox->setValue(p->server.port);
m_httpsCheckBox->setToolTip(Git::Tr::tr( 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" "Determines the protocol used to form a URL in case\n"
"\"canonicalWebUrl\" is not configured in the file\n" "\"canonicalWebUrl\" is not configured in the file\n"
"\"gerrit.config\".")); "\"gerrit.config\"."));
setTabOrder(m_sshChooser, m_curlChooser);
setTabOrder(m_curlChooser, m_portSpinBox);
}
private: using namespace Layouting;
void apply() final; 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);
GerritOptionsPage *m_page; setOnApply([this, hostLineEdit, userLineEdit, sshChooser,
QLineEdit *m_hostLineEdit; curlChooser, portSpinBox, httpsCheckBox, onChanged] {
QLineEdit *m_userLineEdit;
Utils::PathChooser *m_sshChooser;
Utils::PathChooser *m_curlChooser;
QSpinBox *m_portSpinBox;
QCheckBox *m_httpsCheckBox;
const QSharedPointer<GerritParameters> &m_parameters;
};
void GerritOptionsWidget::apply()
{
GerritParameters newParameters; GerritParameters newParameters;
newParameters.server = GerritServer(m_hostLineEdit->text().trimmed(), newParameters.server = GerritServer(hostLineEdit->text().trimmed(),
static_cast<unsigned short>(m_portSpinBox->value()), static_cast<unsigned short>(portSpinBox->value()),
m_userLineEdit->text().trimmed(), userLineEdit->text().trimmed(),
GerritServer::Ssh); GerritServer::Ssh);
newParameters.ssh = m_sshChooser->filePath(); newParameters.ssh = sshChooser->filePath();
newParameters.curl = m_curlChooser->filePath(); newParameters.curl = curlChooser->filePath();
newParameters.https = m_httpsCheckBox->isChecked(); newParameters.https = httpsCheckBox->isChecked();
if (newParameters != *m_parameters) { if (newParameters != *m_parameters) {
if (m_parameters->ssh == newParameters.ssh) if (m_parameters->ssh == newParameters.ssh)
@@ -92,19 +81,24 @@ void GerritOptionsWidget::apply()
newParameters.setPortFlagBySshType(); newParameters.setPortFlagBySshType();
*m_parameters = newParameters; *m_parameters = newParameters;
m_parameters->toSettings(Core::ICore::settings()); m_parameters->toSettings(Core::ICore::settings());
emit m_page->settingsChanged(); emit onChanged();
} }
} });
}
private:
const QSharedPointer<GerritParameters> &m_parameters;
};
// GerritOptionsPage // GerritOptionsPage
GerritOptionsPage::GerritOptionsPage(const QSharedPointer<GerritParameters> &p, QObject *parent) GerritOptionsPage::GerritOptionsPage(const QSharedPointer<GerritParameters> &p,
: Core::IOptionsPage(parent) const std::function<void()> &onChanged)
{ {
setId("Gerrit"); setId("Gerrit");
setDisplayName(Git::Tr::tr("Gerrit")); setDisplayName(Git::Tr::tr("Gerrit"));
setCategory(VcsBase::Constants::VCS_SETTINGS_CATEGORY); setCategory(VcsBase::Constants::VCS_SETTINGS_CATEGORY);
setWidgetCreator([this, p] { return new GerritOptionsWidget(this, p); }); setWidgetCreator([p, onChanged] { return new GerritOptionsWidget(p, onChanged); });
} }
} // Gerrit::Internal } // Gerrit::Internal

View File

@@ -13,13 +13,9 @@ class GerritParameters;
class GerritOptionsPage : public Core::IOptionsPage class GerritOptionsPage : public Core::IOptionsPage
{ {
Q_OBJECT
public: public:
GerritOptionsPage(const QSharedPointer<GerritParameters> &p, QObject *parent = nullptr); GerritOptionsPage(const QSharedPointer<GerritParameters> &p,
const std::function<void()> &onChanged);
signals:
void settingsChanged();
}; };
} // Gerrit::Internal } // Gerrit::Internal

View File

@@ -156,13 +156,22 @@ GerritPlugin::GerritPlugin()
: m_parameters(new GerritParameters) : m_parameters(new GerritParameters)
, m_server(new GerritServer) , 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) void GerritPlugin::addToMenu(ActionContainer *ac)
{ {
m_parameters->fromSettings(ICore::settings());
QAction *openViewAction = new QAction(Git::Tr::tr("Gerrit..."), this); QAction *openViewAction = new QAction(Git::Tr::tr("Gerrit..."), this);
@@ -177,13 +186,6 @@ void GerritPlugin::addToMenu(ActionContainer *ac)
ActionManager::registerAction(pushAction, Constants::GERRIT_PUSH); ActionManager::registerAction(pushAction, Constants::GERRIT_PUSH);
connect(pushAction, &QAction::triggered, this, [this] { push(); }); connect(pushAction, &QAction::triggered, this, [this] { push(); });
ac->addAction(m_pushToGerritCommand); 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) void GerritPlugin::updateActions(const VcsBase::VcsBasePluginState &state)

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
#include <utils/fileutils.h> #include <utils/filepath.h>
#include <QObject> #include <QObject>
#include <QPointer> #include <QPointer>
@@ -25,6 +25,7 @@ class GerritChange;
class GerritDialog; class GerritDialog;
class GerritParameters; class GerritParameters;
class GerritServer; class GerritServer;
class GerritOptionsPage;
class GerritPlugin : public QObject class GerritPlugin : public QObject
{ {
@@ -60,6 +61,7 @@ private:
Core::Command *m_gerritCommand = nullptr; Core::Command *m_gerritCommand = nullptr;
Core::Command *m_pushToGerritCommand = nullptr; Core::Command *m_pushToGerritCommand = nullptr;
QString m_reviewers; QString m_reviewers;
GerritOptionsPage *m_gerritOptionsPage = nullptr;
}; };
} // namespace Internal } // namespace Internal