Skip to content

Commit

Permalink
Revert "options/value: add QObject::connect wrapper"
Browse files Browse the repository at this point in the history
This reverts commit a67e863.
  • Loading branch information
sthalik committed Oct 10, 2022
1 parent 4ad996e commit 6188459
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 124 deletions.
4 changes: 2 additions & 2 deletions filter-kalman/kalman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ dialog_kalman::dialog_kalman()
tie_setting(s.noise_rot_slider_value, ui.noiseRotSlider);
tie_setting(s.noise_pos_slider_value, ui.noisePosSlider);

s.noise_rot_slider_value.connect_to(this, &dialog_kalman::updateLabels);
s.noise_pos_slider_value.connect_to(this, &dialog_kalman::updateLabels);
connect(&s.noise_rot_slider_value, SIGNAL(valueChanged(const slider_value&)), this, SLOT(updateLabels(const slider_value&)));
connect(&s.noise_pos_slider_value, SIGNAL(valueChanged(const slider_value&)), this, SLOT(updateLabels(const slider_value&)));

updateLabels(slider_value());
}
Expand Down
8 changes: 5 additions & 3 deletions gui/mapping-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ void mapping_dialog::load()

if (altp)
{
axis.opts.altp.connect_to(this, [&](bool f) { qfc.setEnabled(f); });
connect(&axis.opts.altp,
value_::value_changed<bool>(),
this, [&](bool f) { qfc.setEnabled(f); });
qfc.setEnabled(axis.opts.altp);
}

Expand Down Expand Up @@ -162,8 +164,8 @@ void mapping_dialog::load()

qfc.set_snap(.5, 1);

axis.opts.clamp_x_.connect_to(&qfc, update_xstep);
axis.opts.clamp_y_.connect_to(&qfc, update_ystep);
connect(&axis.opts.clamp_x_, value_::value_changed<int>(), &qfc, update_xstep);
connect(&axis.opts.clamp_y_, value_::value_changed<int>(), &qfc, update_ystep);

// force signal to avoid duplicating the slot's logic
qfc.set_config(&conf);
Expand Down
9 changes: 7 additions & 2 deletions gui/options-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ options_dialog::options_dialog(std::unique_ptr<ITrackerDialog>& tracker_dialog_,
{
tmp val = val_;
val.label->setText(kopts_to_string(val.opt));
val.opt.keycode.connect_to(val.label, [=] { val.label->setText(kopts_to_string(val.opt)); }, Qt::DirectConnection);
connect(val.button, &QPushButton::clicked, this, [=] { bind_key(val.opt, val.label); });
connect(&val.opt.keycode,
static_cast<void (value_::*)(const QString&) const>(&value_::valueChanged),
val.label,
[=](const QString&) { val.label->setText(kopts_to_string(val.opt)); });
{
connect(val.button, &QPushButton::clicked, this, [=] { bind_key(val.opt, val.label); });
}
}

auto add_module_tab = [this] (auto& place, auto&& dlg, const QString& label) {
Expand Down
17 changes: 13 additions & 4 deletions opentrack/main-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,17 @@ void main_window::init_dylibs()
this, [this](const QString&) { pFilterDialog = nullptr; if (options_widget) options_widget->filter_module_changed(); });
#endif

m.tracker_dll.connect_to(this, &main_window::save_modules, Qt::DirectConnection);
m.protocol_dll.connect_to(this, &main_window::save_modules, Qt::DirectConnection);
m.filter_dll.connect_to(this, &main_window::save_modules, Qt::DirectConnection);
connect(&m.tracker_dll, value_::value_changed<QString>(),
this, &main_window::save_modules,
Qt::DirectConnection);

connect(&m.protocol_dll, value_::value_changed<QString>(),
this, &main_window::save_modules,
Qt::DirectConnection);

connect(&m.filter_dll, value_::value_changed<QString>(),
this, &main_window::save_modules,
Qt::DirectConnection);

{
struct list {
Expand Down Expand Up @@ -231,7 +239,8 @@ void main_window::init_tray_menu()
QObject::connect(&menu_action_exit, &QAction::triggered, this, &main_window::exit);
tray_menu.addAction(&menu_action_exit);

s.tray_enabled.connect_to(this, &main_window::ensure_tray, Qt::DirectConnection);
connect(&s.tray_enabled, value_::value_changed<bool>(),
this, &main_window::ensure_tray);
}

void main_window::init_buttons()
Expand Down
13 changes: 9 additions & 4 deletions options/base-value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "value-traits.hpp"

#include <utility>
#include <type_traits>

#include <QObject>
#include <QString>
Expand All @@ -26,15 +25,21 @@ class OTR_OPTIONS_EXPORT value_ : public QObject
{
Q_OBJECT

protected:
template<typename t> using signal_sig_ = void(value_::*)(detail::cv_qualified<detail::maybe_enum_type_t<t>>) const;
template<typename t> using slot_sig_ = void(value_::*)(detail::cv_qualified<detail::maybe_enum_type_t<t>>);
template<typename t> using cv_qualified = detail::cv_qualified<t>;
template<typename t>
using signal_sig = void(value_::*)(cv_qualified<t>) const;

public:
QString name() const { return self_name; }
value_(bundle const& b, const QString& name) noexcept;
~value_() override;

template<typename t>
static constexpr auto value_changed()
{
return static_cast<signal_sig<t>>(&value_::valueChanged);
}

static const bool TRACE_NOTIFY;

signals:
Expand Down
66 changes: 39 additions & 27 deletions options/tie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "tie.hpp"
#include "compat/run-in-thread.hpp"
#include "compat/macros.h"

#include "value-traits.hpp"

Expand All @@ -19,16 +20,16 @@ void tie_setting(value<int>& v, QComboBox* cb)
{
cb->setCurrentIndex(v);
v = cb->currentIndex();
v.connect_to(cb, &QComboBox::setCurrentIndex);
v.connect_from(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged));
value_::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE);
}

void tie_setting(value<QString>& v, QComboBox* cb)
{
cb->setCurrentText(v);
v = cb->currentText();
v.connect_to(cb, &QComboBox::currentTextChanged);
v.connect_from(cb, &QComboBox::setCurrentText);
value_::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(const QString&)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(const QString&)), cb, SLOT(setCurrentText(const QString&)), v.SAFE_CONNTYPE);
}

void tie_setting(value<QVariant>& v, QComboBox* cb)
Expand All @@ -53,57 +54,60 @@ void tie_setting(value<QVariant>& v, QComboBox* cb)
else
v = {};

v.connect_from(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[cb, &v](int idx) { v = cb->itemData(idx); });
v.connect_to(cb, [fn = std::move(set_idx)](const QVariant& var) { fn(var); });
value_::connect(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
&v, [cb, &v](int idx) { v = cb->itemData(idx); },
v.DIRECT_CONNTYPE);
value_::connect(&v, value_::value_changed<QVariant>(),
cb, [set_idx](const QVariant& var) { set_idx(var); },
v.SAFE_CONNTYPE);
}

void tie_setting(value<bool>& v, QRadioButton* cb)
{
cb->setChecked(v);
v.connect_to(cb, &QRadioButton::setChecked);
v.connect_from(cb, &QRadioButton::toggled);
value_::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE);
}

void tie_setting(value<bool>& v, QCheckBox* cb)
{
cb->setChecked(v);
v.connect_to(cb, &QCheckBox::setChecked);
v.connect_from(cb, &QCheckBox::toggled);
value_::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE);
}

void tie_setting(value<double>& v, QDoubleSpinBox* dsb)
{
dsb->setValue(v);
v.connect_to(dsb, &QDoubleSpinBox::setValue);
v.connect_from(dsb, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged));
value_::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE);
}

void tie_setting(value<int>& v, QSpinBox* sb)
{
sb->setValue(v);
v.connect_to(sb, &QSpinBox::setValue);
v.connect_from(sb, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged));
value_::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE);
}

void tie_setting(value<QString>& v, QLineEdit* le)
{
le->setText(v);
v.connect_to(le, &QLineEdit::setText);
v.connect_from(le, &QLineEdit::textChanged);
value_::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE);
value_::connect(&v, value_::value_changed<QString>(), le, &QLineEdit::setText, v.SAFE_CONNTYPE);
}

void tie_setting(value<QString>& v, QLabel* lb)
{
lb->setText(v);
v.connect_to(lb, &QLabel::setText);
value_::connect(&v, value_::value_changed<QString>(), lb, &QLabel::setText, v.SAFE_CONNTYPE);
}

void tie_setting(value<int>& v, QTabWidget* t)
{
t->setCurrentIndex(v);
v.connect_to(t, &QTabWidget::setCurrentIndex);
v.connect_from(t, &QTabWidget::currentChanged);
value_::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
value_::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE);
}

void tie_setting(value<slider_value>& v, QSlider* w)
Expand All @@ -116,24 +120,32 @@ void tie_setting(value<slider_value>& v, QSlider* w)
v = v().update_from_slider(w->value(), q_min, q_max);
}

v.connect_from(w, &QSlider::valueChanged, [=, &v](int pos) {
run_in_thread_sync(w, [&]() {
value_::connect(w, &QSlider::valueChanged, &v, [=, &v](int pos)
{
run_in_thread_sync(w, [&]()
{
const int q_min = w->minimum();
const int q_max = w->maximum();
v = v().update_from_slider(pos, q_min, q_max);
w->setValue(v().to_slider_pos(q_min, q_max));
});
}, v.DIRECT_CONNTYPE);

v.connect_to(w, [=, &v](double) {
run_in_thread_sync(w, [=, &v]() {
},
v.DIRECT_CONNTYPE);

value_::connect(&v,
value_::value_changed<slider_value>(),
w,
[=, &v](double) {
run_in_thread_sync(w, [=, &v]()
{
const int q_min = w->minimum();
const int q_max = w->maximum();
const int pos = v->to_slider_pos(q_min, q_max);
v = v->update_from_slider(pos, q_min, q_max);
w->setValue(pos);
});
}, v.DIRECT_CONNTYPE);
},
v.DIRECT_CONNTYPE);
}

} // ns options
32 changes: 21 additions & 11 deletions options/tie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ std::enable_if_t<std::is_enum_v<t>> tie_setting(value<t>& v, QComboBox* cb)
cb->setCurrentIndex(cb->findData(int(static_cast<t>(v))));
v = static_cast<t>(cb->currentData().toInt());

v.connect_from(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[&v, cb](int idx) { v = static_cast<t>(cb->itemData(idx).toInt()); });
v.connect_to(cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); });
value_::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
&v, [&v, cb](int idx) { v = static_cast<t>(cb->itemData(idx).toInt()); },
v.DIRECT_CONNTYPE);

value_::connect(&v, value_::value_changed<int>(),
cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); },
v.SAFE_CONNTYPE);
}

template<typename t, typename From, typename To>
Expand All @@ -48,20 +52,23 @@ void tie_setting(value<t>& v, QComboBox* cb, From&& fn_to_index, To&& fn_to_valu
cb->setCurrentIndex(fn_to_index(v));
v = fn_to_value(cb->currentIndex(), cb->currentData());

v.connect_from(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[&v, cb, fn = std::forward<To>(fn_to_value)](int idx) { v = fn(idx, cb->currentData()); });
v.connect_to(cb, [cb, fn = std::forward<From>(fn_to_index)](detail::cv_qualified<t> v) { cb->setCurrentIndex(fn(v)); });
value_::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
&v, [&v, cb, fn_to_value](int idx) { v = fn_to_value(idx, cb->currentData()); },
v.DIRECT_CONNTYPE);
value_::connect(&v, value_::value_changed<t>(),
cb, [cb, fn_to_index](detail::cv_qualified<t>& v) { cb->setCurrentIndex(fn_to_index(v)); },
v.DIRECT_CONNTYPE);
}

template<typename t, typename F>
void tie_setting(value<t>& v, QLabel* lb, F&& fun)
{
auto closure = [lb, fn = std::forward<F>(fun)](detail::cv_qualified<t> v) {
lb->setText(fn(v));
};
auto closure = [lb, fun](detail::cv_qualified<t> v) { lb->setText(fun(v)); };

closure(v());
v.connect_to(lb, std::move(closure));
value_::connect(&v, value_::value_changed<t>(),
lb, closure,
v.SAFE_CONNTYPE);
}

template<typename t, typename F>
Expand All @@ -71,7 +78,10 @@ void tie_setting(value<t>& v, QObject* obj, F&& fun)
abort();

fun(v());
v.connect_to(obj, std::forward<F>(fun));

value_::connect(&v, value_::value_changed<t>(),
obj, fun,
v.DIRECT_CONNTYPE);
}

OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QComboBox* cb);
Expand Down
11 changes: 0 additions & 11 deletions options/value-traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ using cv_qualified =
std::remove_cvref_t<t>,
std::add_lvalue_reference_t<std::add_const_t<std::remove_cvref_t<t>>>>;

template<typename t, typename = void>
struct maybe_enum_type {
using type = t;
};
template<typename t>
struct maybe_enum_type<t, std::enable_if_t<std::is_enum_v<t>>> {
using type = int;
};

template<typename t> using maybe_enum_type_t = typename maybe_enum_type<t>::type;

template<typename t, typename u = t, typename Enable = void>
struct default_value_traits
{
Expand Down
21 changes: 1 addition & 20 deletions options/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ namespace options::detail {
{
t x;
public:
constexpr t const* operator->() const { return &x; }
constexpr t* operator->() { return &x; }
constexpr explicit dereference_wrapper(t&& x) : x(x) {}
};
template<typename t, typename...> /*MSVC workaround*/ static constexpr bool is_enum_v = std::is_enum_v<t>;
} // ns options::detail

namespace options {
Expand Down Expand Up @@ -85,9 +85,6 @@ class value final : public value_
}

public:
using signal_sig = typename value_::signal_sig_<t>;
using slot_sig = typename value_::slot_sig_<t>;

QVariant get_variant() const noexcept override
{
if (QVariant ret{b->get_variant(self_name)}; ret.isValid() && !ret.isNull())
Expand Down Expand Up @@ -197,22 +194,6 @@ class value final : public value_
{
return static_cast<u>(get());
}

template<typename Q, typename F>
QMetaObject::Connection
connect_to(Q* qobject, F&& writer, Qt::ConnectionType conn = Qt::QueuedConnection) {
return QObject::connect(this, static_cast<signal_sig>(&value<t>::valueChanged), qobject, std::forward<F>(writer), conn);
}
template<typename Q, typename F>
QMetaObject::Connection
connect_from(Q* qobject, F&& reader, Qt::ConnectionType conn = Qt::DirectConnection) {
return QObject::connect(qobject, std::forward<F>(reader), this, static_cast<slot_sig>(&value<t>::setValue), conn);
}
template<typename Q, typename F, typename G>
QMetaObject::Connection
connect_from(Q* qobject, F&& reader, G&& fn, Qt::ConnectionType conn = Qt::DirectConnection) {
return QObject::connect(qobject, std::forward<F>(reader), this, std::forward<G>(fn), conn);
}
};

#if !defined OTR_OPTIONS_INST_VALUE
Expand Down
9 changes: 6 additions & 3 deletions spline/spline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,12 @@ void spline::set_bundle(bundle b, const QString& axis_name, Axis axis)
invalidate_settings_();
S = s;

conn_points = s->points.connect_to(&*ctx, [this] { invalidate_settings(); }, Qt::DirectConnection);
conn_maxx = s->opts.clamp_x_.connect_to(&*ctx, [this] { invalidate_settings(); }, Qt::DirectConnection);
conn_maxy = s->opts.clamp_y_.connect_to(&*ctx, [this] { invalidate_settings(); }, Qt::DirectConnection);
conn_points = QObject::connect(&s->points, value_::value_changed<QList<QPointF>>(),
&*ctx, [this] { invalidate_settings(); }, Qt::DirectConnection);
conn_maxx = QObject::connect(&s->opts.clamp_x_, value_::value_changed<int>(),
&*ctx, [this](double) { invalidate_settings(); }, Qt::DirectConnection);
conn_maxy = QObject::connect(&s->opts.clamp_y_, value_::value_changed<int>(),
&*ctx, [this](double) { invalidate_settings(); }, Qt::DirectConnection);
}

emit S->recomputed();
Expand Down

0 comments on commit 6188459

Please sign in to comment.