Skip to content

Commit

Permalink
Governor widget: send updates after 100ms
Browse files Browse the repository at this point in the history
This prevents sending multiple updates when every slider gets modified.
  • Loading branch information
lmoureaux committed Jun 30, 2024
1 parent cccb608 commit e626324
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions client/widgets/city/governor_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// Qt
#include <QSlider>
#include <QTimer>

namespace freeciv {

Expand Down Expand Up @@ -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 = {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -130,6 +131,19 @@ void governor_widget::set_parameters(const cm_parameter &params)
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
3 changes: 3 additions & 0 deletions client/widgets/city/governor_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e626324

Please sign in to comment.