Skip to content

Commit

Permalink
Merge pull request #13979 from ronso0/controller-sett-clickable-check…
Browse files Browse the repository at this point in the history
…box-label

Controller settings: have clickable checkbox label (like QCheckBox)
  • Loading branch information
acolombier authored Dec 5, 2024
2 parents 16a9940 + 4d4f173 commit 80fd83d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/controllers/legacycontrollersettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QCheckBox>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QEvent>
#include <QLabel>
#include <QLayout>
#include <QSpinBox>
Expand Down Expand Up @@ -150,9 +151,15 @@ QWidget* LegacyControllerBooleanSetting::buildInputWidget(QWidget* pParent) {
emit changed();
});

// We want to format the checkbox label with html styles. This is not possible
// so we attach a separate QLabel and, in order to get a clickable label like
// with QCheckBox, we associate the label with the checkbox (buddy).
// When the label is clicked we toggle the checkbox in eventFilter().
auto pLabelWidget = make_parented<QLabel>(pWidget);
pLabelWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
pLabelWidget->setText(label());
pLabelWidget->setBuddy(pCheckBox);
pLabelWidget->installEventFilter(this);

QBoxLayout* pLayout = new QHBoxLayout();

Expand All @@ -174,6 +181,17 @@ bool LegacyControllerBooleanSetting::match(const QDomElement& element) {
Qt::CaseInsensitive) == 0;
}

bool LegacyControllerBooleanSetting::eventFilter(QObject* pObj, QEvent* pEvent) {
QLabel* pLabel = qobject_cast<QLabel*>(pObj);
if (pLabel && pEvent->type() == QEvent::MouseButtonPress) {
QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(pLabel->buddy());
if (pCheckBox) {
pCheckBox->toggle();
}
}
return QObject::eventFilter(pObj, pEvent);
}

template<class SettingType,
Serializer<SettingType> ValueSerializer,
Deserializer<SettingType> ValueDeserializer,
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/legacycontrollersettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class LegacyControllerBooleanSetting
LegacyControllerSettingsLayoutContainer::HORIZONTAL)
override;

bool eventFilter(QObject* pObj, QEvent* pEvent) override;

QJSValue value() const override {
return QJSValue(m_savedValue);
}
Expand Down

0 comments on commit 80fd83d

Please sign in to comment.