Freeciv21
Develop your civilization from humble roots to a global empire
governor_widget.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPLv3-or-later
2 // SPDX-FileCopyrightText: Louis Moureaux <m_louis30@yahoo.com>
3 
5 
6 // client
7 #include "fc_types.h"
8 #include "governor.h"
9 
10 // common
11 #include "cm.h"
12 
13 // Qt
14 #include <QSlider>
15 #include <QTimer>
16 
17 namespace freeciv {
18 
27 governor_widget::governor_widget(QWidget *parent) : QWidget(parent)
28 {
29  ui.setupUi(this);
30 
31  const auto labels = {
32  ui.food_surplus_label, ui.food_priority_label,
33  ui.production_surplus_label, ui.production_priority_label,
34  ui.trade_surplus_label, ui.trade_priority_label,
35  ui.gold_surplus_label, ui.gold_priority_label,
36  ui.luxury_goods_surplus_label, ui.luxury_goods_priority_label,
37  ui.science_surplus_label, ui.science_priority_label,
38  ui.celebrate_priority_label,
39  };
40  for (auto label : labels) {
41  label->setNum(0);
42  }
43 
44  const auto sliders = {
45  ui.food_surplus, ui.food_priority, ui.production_surplus,
46  ui.production_priority, ui.trade_surplus, ui.trade_priority,
47  ui.gold_surplus, ui.gold_priority, ui.luxury_goods_surplus,
48  ui.luxury_goods_priority, ui.science_surplus, ui.science_priority,
49  ui.celebrate_priority,
50  };
51  for (auto slider : sliders) {
52  connect(slider, &QSlider::valueChanged, this,
54  }
55 
56  const auto checkboxes = {
57  ui.celebrate_surplus,
58  ui.optimize_growth,
59  ui.allow_disorder,
60  ui.allow_specialists,
61  };
62  for (auto box : checkboxes) {
63  connect(box, &QCheckBox::toggled, this,
65  }
66 }
67 
72 {
73  auto params = cm_parameter();
74 
75 #define get_output(name, O_TYPE) \
76  params.minimal_surplus[O_TYPE] = ui.name##_surplus->value(); \
77  params.factor[O_TYPE] = ui.name##_priority->value();
78  get_output(food, O_FOOD);
79  get_output(production, O_SHIELD);
80  get_output(trade, O_TRADE);
81  get_output(gold, O_GOLD);
82  get_output(luxury_goods, O_LUXURY);
83  get_output(science, O_SCIENCE);
84 #undef get_output
85 
86  params.require_happy = ui.celebrate_surplus->isChecked();
87  params.happy_factor = ui.celebrate_priority->value();
88 
89  params.max_growth = ui.optimize_growth->isChecked();
90  params.allow_disorder = ui.allow_disorder->isChecked();
91  params.allow_specialists = ui.allow_specialists->isChecked();
92 
93  return params;
94 }
95 
100 {
101  // Labels are updated automatically through signal connections
102 
103 #define sync_output(name, O_TYPE) \
104  ui.name##_surplus->setValue(params.minimal_surplus[O_TYPE]); \
105  ui.name##_priority->setValue(params.factor[O_TYPE]);
106  sync_output(food, O_FOOD);
107  sync_output(production, O_SHIELD);
108  sync_output(trade, O_TRADE);
109  sync_output(gold, O_GOLD);
110  sync_output(luxury_goods, O_LUXURY);
111  sync_output(science, O_SCIENCE);
112 #undef sync_output
113 
114  ui.celebrate_surplus->setChecked(params.require_happy);
115  ui.celebrate_priority->setValue(params.happy_factor);
116 
117  ui.optimize_growth->setChecked(params.max_growth);
118  ui.allow_disorder->setChecked(params.allow_disorder);
119  ui.allow_specialists->setChecked(params.allow_specialists);
120 }
121 
132 {
134  m_dirty = false;
135 }
136 
142 {
143  if (!m_dirty) {
144  QTimer::singleShot(100, this, &governor_widget::emit_params_changed);
145  m_dirty = true;
146  }
147 }
148 
149 } // namespace freeciv
bool m_dirty
Whether we need to propagate a params update.
cm_parameter parameters() const
Returns the parameters currently shown by the widget.
Ui::governor_widget ui
void queue_params_changed()
Queues an update of the parameters.
void emit_params_changed()
Helper to fill the argument of parameters_changed.
void parameters_changed(const cm_parameter &params)
Signal emitted when the governor settings are changed.
governor_widget(QWidget *parent=nullptr)
Constructor.
void set_parameters(const cm_parameter &params)
Changes the parameters displayed by this widget.
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_TRADE
Definition: fc_types.h:87
@ O_SCIENCE
Definition: fc_types.h:90
@ O_LUXURY
Definition: fc_types.h:89
@ O_GOLD
Definition: fc_types.h:88
#define get_output(name, O_TYPE)
#define sync_output(name, O_TYPE)
Definition: path.cpp:10
bool allow_disorder
Definition: cm.h:20
bool max_growth
Definition: cm.h:18
bool allow_specialists
Definition: cm.h:21
bool require_happy
Definition: cm.h:19
int happy_factor
Definition: cm.h:24