forked from qt-creator/qt-creator
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:
@@ -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,22 +451,36 @@ 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);
|
||||||
d->m_buttonGroup = new QButtonGroup;
|
QTC_CHECK(!d->m_comboBox);
|
||||||
d->m_buttonGroup->setExclusive(true);
|
|
||||||
|
|
||||||
QTC_ASSERT(d->m_buttons.isEmpty(), d->m_buttons.clear());
|
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);
|
switch (d->m_displayStyle) {
|
||||||
auto button = new QRadioButton(option.displayName);
|
case DisplayStyle::RadioButtons:
|
||||||
button->setChecked(i == d->m_value);
|
d->m_buttonGroup = new QButtonGroup;
|
||||||
button->setToolTip(option.tooltip);
|
d->m_buttonGroup->setExclusive(true);
|
||||||
builder.addItems(QString(), button);
|
for (int i = 0, n = d->m_options.size(); i < n; ++i) {
|
||||||
d->m_buttons.append(button);
|
const Internal::BaseSelectionAspectPrivate::Option &option = d->m_options.at(i);
|
||||||
d->m_buttonGroup->addButton(button);
|
auto button = new QRadioButton(option.displayName);
|
||||||
connect(button, &QAbstractButton::clicked, this, [this, i] {
|
button->setChecked(i == d->m_value);
|
||||||
d->m_value = i;
|
button->setToolTip(option.tooltip);
|
||||||
emit changed();
|
builder.addItems(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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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)
|
||||||
|
@@ -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;
|
||||||
|
Reference in New Issue
Block a user