Skip to content

Commit

Permalink
add WSettingsCheckBoxLabel to replace the click eventFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Feb 17, 2025
1 parent 88aa50e commit e43a0ca
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,7 @@ add_library(
src/widget/wscrollable.cpp
src/widget/wsearchlineedit.cpp
src/widget/wsearchrelatedtracksmenu.cpp
src/widget/wsettingscheckboxlabel.cpp
src/widget/wsingletoncontainer.cpp
src/widget/wsizeawarestack.cpp
src/widget/wskincolor.cpp
Expand Down
32 changes: 5 additions & 27 deletions src/controllers/legacycontrollersettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QColorDialog>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QEvent>
#include <QFileDialog>
#include <QLabel>
#include <QLayout>
Expand All @@ -18,6 +17,7 @@
#include "moc_legacycontrollersettings.cpp"
#include "util/assert.h"
#include "util/parented_ptr.h"
#include "widget/wsettingscheckboxlabel.h"

namespace {

Expand Down Expand Up @@ -163,18 +163,13 @@ QWidget* LegacyControllerBooleanSetting::buildInputWidget(QWidget* pParent) {
});

// 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 the function
// eventFilter() of helper class ToggleCheckboxEventFilter.
auto pLabelWidget = make_parented<QLabel>(pWidget);
// so we attach a separate label. In order to get a clickable label like
// with QCheckBox, we use a custom QLabel that toggles its buddy QCheckBox
// (on left-click, like QCheckBox) and sets focus on it.
auto pLabelWidget = make_parented<WSettingsCheckBoxLabel>(pWidget);
pLabelWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
pLabelWidget->setText(label());
pLabelWidget->setBuddy(pCheckBox);
if (!m_pToggleCheckboxEventFilter) {
m_pToggleCheckboxEventFilter = make_parented<ToggleCheckboxEventFilter>(this);
}
pLabelWidget->installEventFilter(m_pToggleCheckboxEventFilter.get());

QBoxLayout* pLayout = new QHBoxLayout();

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

bool LegacyControllerBooleanSetting::ToggleCheckboxEventFilter::eventFilter(
QObject* pObj, QEvent* pEvent) {
// eventFilter was a method of LegacyControllerBooleanSetting, but this
// doesn't work, as LegacyControllerBooleanSetting is created from a
// different thread, and Qt cannot filter events for objects in a
// different thread.

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
11 changes: 0 additions & 11 deletions src/controllers/legacycontrollersettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,6 @@ class LegacyControllerBooleanSetting

QWidget* buildInputWidget(QWidget* parent) override;

private:
class ToggleCheckboxEventFilter : public QObject {
public:
ToggleCheckboxEventFilter(QObject* pParent)
: QObject(pParent) {
}
bool eventFilter(QObject* pObj, QEvent* pEvent) override;
};

parented_ptr<ToggleCheckboxEventFilter> m_pToggleCheckboxEventFilter;

FRIEND_TEST(LegacyControllerMappingSettingsTest, booleanSettingEditing);
};

Expand Down
17 changes: 17 additions & 0 deletions src/widget/wsettingscheckboxlabel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "widget/wsettingscheckboxlabel.h"

#include <QCheckBox>
#include <QMouseEvent>

#include "moc_wsettingscheckboxlabel.cpp"

void WSettingsCheckBoxLabel::mousePressEvent(QMouseEvent* pEvent) {
if (pEvent->buttons().testFlag(Qt::LeftButton)) {
QCheckBox* pCB = qobject_cast<QCheckBox*>(buddy());
if (pCB) {
pCB->toggle();
pCB->setFocus(Qt::MouseFocusReason);
}
}
QLabel::mousePressEvent(pEvent);
}
24 changes: 24 additions & 0 deletions src/widget/wsettingscheckboxlabel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <QLabel>

/// This is a QLabel for use in controller settings.
/// It is expected to have a QCheckBox buddy assigned. If so, left-click on the
/// label will toggle the checkbox and set focus on it.
class WSettingsCheckBoxLabel : public QLabel {
Q_OBJECT
public:
explicit WSettingsCheckBoxLabel(QWidget* pParent = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags())
: QLabel(pParent, flags) {

};
explicit WSettingsCheckBoxLabel(const QString& text,
QWidget* pParent = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags())
: QLabel(text, pParent, flags) {
};

protected:
void mousePressEvent(QMouseEvent* pEvent) override;
};

0 comments on commit e43a0ca

Please sign in to comment.