Project Aspects: Provide alternative display style for selection aspect

Typically, a combo box is preferred to radio buttons.

Change-Id: I685d9c1773e4188252139fb7c784fd7b61e62c8f
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2019-11-25 12:46:08 +01:00
parent d94636f8f9
commit 9811f95aa7
2 changed files with 47 additions and 16 deletions

View File

@@ -38,6 +38,7 @@
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <QCheckBox> #include <QCheckBox>
#include <QComboBox>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QFormLayout> #include <QFormLayout>
@@ -67,9 +68,14 @@ class BaseSelectionAspectPrivate
public: public:
int m_value = 0; int m_value = 0;
int m_defaultValue = 0; int m_defaultValue = 0;
BaseSelectionAspect::DisplayStyle m_displayStyle
= BaseSelectionAspect::DisplayStyle::RadioButtons;
struct Option { QString displayName; QString tooltip; }; struct Option { QString displayName; QString tooltip; };
QVector<Option> m_options; QVector<Option> m_options;
QList<QPointer<QRadioButton>> m_buttons; // Owned by configuration widget
// These are all owned by the configuration widget.
QList<QPointer<QRadioButton>> m_buttons;
QPointer<QComboBox> m_comboBox;
QPointer<QButtonGroup> m_buttonGroup; QPointer<QButtonGroup> m_buttonGroup;
}; };
@@ -445,10 +451,13 @@ BaseSelectionAspect::~BaseSelectionAspect() = default;
void BaseSelectionAspect::addToLayout(LayoutBuilder &builder) void BaseSelectionAspect::addToLayout(LayoutBuilder &builder)
{ {
QTC_CHECK(d->m_buttonGroup == nullptr); QTC_CHECK(d->m_buttonGroup == nullptr);
QTC_CHECK(!d->m_comboBox);
QTC_ASSERT(d->m_buttons.isEmpty(), d->m_buttons.clear());
switch (d->m_displayStyle) {
case DisplayStyle::RadioButtons:
d->m_buttonGroup = new QButtonGroup; d->m_buttonGroup = new QButtonGroup;
d->m_buttonGroup->setExclusive(true); 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) { for (int i = 0, n = d->m_options.size(); i < n; ++i) {
const Internal::BaseSelectionAspectPrivate::Option &option = d->m_options.at(i); const Internal::BaseSelectionAspectPrivate::Option &option = d->m_options.at(i);
auto button = new QRadioButton(option.displayName); auto button = new QRadioButton(option.displayName);
@@ -462,6 +471,17 @@ void BaseSelectionAspect::addToLayout(LayoutBuilder &builder)
emit changed(); emit changed();
}); });
} }
break;
case DisplayStyle::ComboBox:
d->m_comboBox = new QComboBox;
for (int i = 0, n = d->m_options.size(); i < n; ++i)
d->m_comboBox->addItem(d->m_options.at(i).displayName);
connect(d->m_comboBox.data(), QOverload<int>::of(&QComboBox::activated), this,
[this](int index) { d->m_value = index; emit changed(); });
d->m_comboBox->setCurrentIndex(d->m_value);
builder.addItems(new QLabel(displayName()), d->m_comboBox.data());
break;
}
} }
void BaseSelectionAspect::fromMap(const QVariantMap &map) void BaseSelectionAspect::fromMap(const QVariantMap &map)
@@ -484,6 +504,11 @@ void BaseSelectionAspect::setDefaultValue(int defaultValue)
d->m_defaultValue = defaultValue; d->m_defaultValue = defaultValue;
} }
void BaseSelectionAspect::setDisplayStyle(BaseSelectionAspect::DisplayStyle style)
{
d->m_displayStyle = style;
}
int BaseSelectionAspect::value() const int BaseSelectionAspect::value() const
{ {
return d->m_value; return d->m_value;
@@ -494,6 +519,9 @@ void BaseSelectionAspect::setValue(int value)
d->m_value = value; d->m_value = value;
if (d->m_buttonGroup && 0 <= value && value < d->m_buttons.size()) if (d->m_buttonGroup && 0 <= value && value < d->m_buttons.size())
d->m_buttons.at(value)->setChecked(true); d->m_buttons.at(value)->setChecked(true);
else if (d->m_comboBox) {
d->m_comboBox->setCurrentIndex(value);
}
} }
void BaseSelectionAspect::addOption(const QString &displayName, const QString &toolTip) void BaseSelectionAspect::addOption(const QString &displayName, const QString &toolTip)

View File

@@ -91,6 +91,9 @@ public:
int defaultValue() const; int defaultValue() const;
void setDefaultValue(int defaultValue); void setDefaultValue(int defaultValue);
enum class DisplayStyle { RadioButtons, ComboBox };
void setDisplayStyle(DisplayStyle style);
void addOption(const QString &displayName, const QString &toolTip = {}); void addOption(const QString &displayName, const QString &toolTip = {});
void fromMap(const QVariantMap &map) override; void fromMap(const QVariantMap &map) override;