ProjectExplorer: Introduce a new BaseSelectionAspect

To handle an (exclusive) choice within a set of possible options,
visualized by a set of QRadioButtons.

Use in QdbMakeDefaultAppStep.

Change-Id: Icc62a45e8c5986cd9400f90449f92154ed854a22
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2019-06-13 18:25:17 +02:00
parent 77e8e1707c
commit 67b33a0662
4 changed files with 119 additions and 86 deletions

View File

@@ -27,56 +27,27 @@
#include "qdbmakedefaultappservice.h" #include "qdbmakedefaultappservice.h"
#include <QRadioButton> #include <projectexplorer/runconfigurationaspects.h>
#include <QVBoxLayout>
using namespace ProjectExplorer;
namespace Qdb { namespace Qdb {
namespace Internal { namespace Internal {
class QdbConfigWidget : public ProjectExplorer::BuildStepConfigWidget QdbMakeDefaultAppStep::QdbMakeDefaultAppStep(BuildStepList *bsl)
{
public:
QdbConfigWidget(QdbMakeDefaultAppStep *step)
: BuildStepConfigWidget(step)
{
QVBoxLayout * const mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
m_makeDefaultBtn.setText(
QdbMakeDefaultAppStep::tr("Set this application to start by default"));
m_resetDefaultBtn.setText(
QdbMakeDefaultAppStep::tr("Reset default application"));
if (step->makeDefault())
m_makeDefaultBtn.setChecked(true);
else
m_resetDefaultBtn.setChecked(true);
mainLayout->addWidget(&m_makeDefaultBtn);
mainLayout->addWidget(&m_resetDefaultBtn);
connect(&m_makeDefaultBtn, &QRadioButton::clicked, this, [step] {
step->setMakeDefault(true);
});
connect(&m_resetDefaultBtn, &QRadioButton::clicked, this, [step] {
step->setMakeDefault(false);
});
}
private:
QRadioButton m_makeDefaultBtn;
QRadioButton m_resetDefaultBtn;
};
QdbMakeDefaultAppStep::QdbMakeDefaultAppStep(ProjectExplorer::BuildStepList *bsl)
: AbstractRemoteLinuxDeployStep(bsl, stepId()) : AbstractRemoteLinuxDeployStep(bsl, stepId())
{ {
setDefaultDisplayName(stepDisplayName()); setDefaultDisplayName(stepDisplayName());
auto service = createDeployService<QdbMakeDefaultAppService>(); auto service = createDeployService<QdbMakeDefaultAppService>();
setInternalInitializer([this, service] { auto selection = addAspect<BaseSelectionAspect>();
service->setMakeDefault(m_makeDefault); selection->setSettingsKey("QdbMakeDefaultDeployStep.MakeDefault");
selection->addOption(tr("Set this application to start by default"));
selection->addOption(tr("Reset default application"));
setInternalInitializer([service, selection] {
service->setMakeDefault(selection->value() == 0);
return service->isDeploymentPossible(); return service->isDeploymentPossible();
}); });
} }
@@ -86,45 +57,10 @@ Core::Id QdbMakeDefaultAppStep::stepId()
return "Qdb.MakeDefaultAppStep"; return "Qdb.MakeDefaultAppStep";
} }
ProjectExplorer::BuildStepConfigWidget *QdbMakeDefaultAppStep::createConfigWidget()
{
return new QdbConfigWidget(this);
}
QString QdbMakeDefaultAppStep::stepDisplayName() QString QdbMakeDefaultAppStep::stepDisplayName()
{ {
return QStringLiteral("Change default application"); return QStringLiteral("Change default application");
} }
void QdbMakeDefaultAppStep::setMakeDefault(bool makeDefault)
{
m_makeDefault = makeDefault;
}
bool QdbMakeDefaultAppStep::makeDefault() const
{
return m_makeDefault;
}
static QString makeDefaultKey()
{
return QLatin1String("QdbMakeDefaultDeployStep.MakeDefault");
}
bool QdbMakeDefaultAppStep::fromMap(const QVariantMap &map)
{
if (!AbstractRemoteLinuxDeployStep::fromMap(map))
return false;
m_makeDefault = map.value(makeDefaultKey()).toBool();
return true;
}
QVariantMap QdbMakeDefaultAppStep::toMap() const
{
QVariantMap map = AbstractRemoteLinuxDeployStep::toMap();
map.insert(makeDefaultKey(), m_makeDefault);
return map;
}
} // namespace Internal } // namespace Internal
} // namespace Qdb } // namespace Qdb

View File

@@ -39,17 +39,6 @@ public:
static Core::Id stepId(); static Core::Id stepId();
static QString stepDisplayName(); static QString stepDisplayName();
void setMakeDefault(bool makeDefault);
bool makeDefault() const;
protected:
ProjectExplorer::BuildStepConfigWidget *createConfigWidget() override;
bool fromMap(const QVariantMap &map) override;
QVariantMap toMap() const override;
private:
bool m_makeDefault = false;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -44,6 +44,8 @@
#include <QSpinBox> #include <QSpinBox>
#include <QToolButton> #include <QToolButton>
#include <QTextEdit> #include <QTextEdit>
#include <QRadioButton>
#include <QButtonGroup>
using namespace Utils; using namespace Utils;
@@ -60,6 +62,17 @@ public:
QPointer<QCheckBox> m_checkBox; // Owned by configuration widget QPointer<QCheckBox> m_checkBox; // Owned by configuration widget
}; };
class BaseSelectionAspectPrivate
{
public:
int m_value = 0;
int m_defaultValue = 0;
struct Option { QString displayName; QString tooltip; };
QVector<Option> m_options;
QList<QPointer<QRadioButton>> m_buttons; // Owned by configuration widget
QPointer<QButtonGroup> m_buttonGroup;
};
class BaseStringAspectPrivate class BaseStringAspectPrivate
{ {
public: public:
@@ -396,6 +409,75 @@ void BaseBoolAspect::setToolTip(const QString &tooltip)
d->m_tooltip = tooltip; d->m_tooltip = tooltip;
} }
/*!
\class ProjectExplorer::BaseSelectionAspect
*/
BaseSelectionAspect::BaseSelectionAspect()
: d(new Internal::BaseSelectionAspectPrivate)
{}
BaseSelectionAspect::~BaseSelectionAspect() = default;
void BaseSelectionAspect::addToConfigurationLayout(QFormLayout *layout)
{
QTC_CHECK(d->m_buttonGroup == nullptr);
d->m_buttonGroup = new QButtonGroup;
d->m_buttonGroup->setExclusive(true);
QTC_ASSERT(d->m_buttons.isEmpty(), d->m_buttons.clear());
for (int i = 0, n = d->m_options.size(); i < n; ++i) {
const Internal::BaseSelectionAspectPrivate::Option &option = d->m_options.at(i);
auto button = new QRadioButton(option.displayName, layout->parentWidget());
button->setChecked(i == d->m_value);
button->setToolTip(option.tooltip);
layout->addRow(QString(), button);
d->m_buttons.append(button);
d->m_buttonGroup->addButton(button);
connect(button, &QAbstractButton::clicked, this, [this, i] {
d->m_value = i;
emit changed();
});
}
}
void BaseSelectionAspect::fromMap(const QVariantMap &map)
{
d->m_value = map.value(settingsKey(), d->m_defaultValue).toInt();
}
void BaseSelectionAspect::toMap(QVariantMap &data) const
{
data.insert(settingsKey(), d->m_value);
}
int BaseSelectionAspect::defaultValue() const
{
return d->m_defaultValue;
}
void BaseSelectionAspect::setDefaultValue(int defaultValue)
{
d->m_defaultValue = defaultValue;
}
int BaseSelectionAspect::value() const
{
return d->m_value;
}
void BaseSelectionAspect::setValue(int value)
{
d->m_value = value;
if (d->m_buttonGroup && 0 <= value && value < d->m_buttons.size())
d->m_buttons.at(value)->setChecked(true);
}
void BaseSelectionAspect::addOption(const QString &displayName, const QString &toolTip)
{
d->m_options.append({displayName, toolTip});
}
/*! /*!
\class ProjectExplorer::BaseIntegerAspect \class ProjectExplorer::BaseIntegerAspect
*/ */

View File

@@ -39,6 +39,7 @@ namespace Internal {
class BaseBoolAspectPrivate; class BaseBoolAspectPrivate;
class BaseStringAspectPrivate; class BaseStringAspectPrivate;
class BaseIntegerAspectPrivate; class BaseIntegerAspectPrivate;
class BaseSelectionAspectPrivate;
} // Internal } // Internal
class PROJECTEXPLORER_EXPORT BaseBoolAspect : public ProjectConfigurationAspect class PROJECTEXPLORER_EXPORT BaseBoolAspect : public ProjectConfigurationAspect
@@ -67,6 +68,31 @@ private:
std::unique_ptr<Internal::BaseBoolAspectPrivate> d; std::unique_ptr<Internal::BaseBoolAspectPrivate> d;
}; };
class PROJECTEXPLORER_EXPORT BaseSelectionAspect : public ProjectConfigurationAspect
{
Q_OBJECT
public:
BaseSelectionAspect();
~BaseSelectionAspect() override;
void addToConfigurationLayout(QFormLayout *layout) override;
int value() const;
void setValue(int val);
int defaultValue() const;
void setDefaultValue(int defaultValue);
void addOption(const QString &displayName, const QString &toolTip = {});
void fromMap(const QVariantMap &map) override;
void toMap(QVariantMap &map) const override;
private:
std::unique_ptr<Internal::BaseSelectionAspectPrivate> d;
};
class PROJECTEXPLORER_EXPORT BaseStringAspect : public ProjectConfigurationAspect class PROJECTEXPLORER_EXPORT BaseStringAspect : public ProjectConfigurationAspect
{ {
Q_OBJECT Q_OBJECT