From e626324655c78b80f7921f84ccc2cfac13cbeace Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Thu, 13 Jun 2024 03:42:33 +0200 Subject: [PATCH] Governor widget: send updates after 100ms This prevents sending multiple updates when every slider gets modified. --- client/widgets/city/governor_widget.cpp | 18 ++++++++++++++++-- client/widgets/city/governor_widget.h | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/client/widgets/city/governor_widget.cpp b/client/widgets/city/governor_widget.cpp index 7d015f2df8..0248d8fb19 100644 --- a/client/widgets/city/governor_widget.cpp +++ b/client/widgets/city/governor_widget.cpp @@ -12,6 +12,7 @@ // Qt #include +#include namespace freeciv { @@ -49,7 +50,7 @@ governor_widget::governor_widget(QWidget *parent) : QWidget(parent) }; for (auto slider : sliders) { connect(slider, &QSlider::valueChanged, this, - &governor_widget::emit_params_changed); + &governor_widget::queue_params_changed); } const auto checkboxes = { @@ -60,7 +61,7 @@ governor_widget::governor_widget(QWidget *parent) : QWidget(parent) }; for (auto box : checkboxes) { connect(box, &QCheckBox::toggled, this, - &governor_widget::emit_params_changed); + &governor_widget::queue_params_changed); } } @@ -130,6 +131,19 @@ void governor_widget::set_parameters(const cm_parameter ¶ms) void governor_widget::emit_params_changed() { emit parameters_changed(parameters()); + m_dirty = false; +} + +/** + * Queues an update of the parameters. This prevents emitting + * parameters_changed, and thus recalculating the results, too often. + */ +void governor_widget::queue_params_changed() +{ + if (!m_dirty) { + QTimer::singleShot(100, this, &governor_widget::emit_params_changed); + m_dirty = true; + } } } // namespace freeciv diff --git a/client/widgets/city/governor_widget.h b/client/widgets/city/governor_widget.h index 698e59be0e..7700ccabf7 100644 --- a/client/widgets/city/governor_widget.h +++ b/client/widgets/city/governor_widget.h @@ -24,6 +24,9 @@ class governor_widget : public QWidget { private: void emit_params_changed(); + void queue_params_changed(); + + bool m_dirty = false; ///< Whether we need to propagate a params update. }; } // namespace freeciv