Freeciv21
Develop your civilization from humble roots to a global empire
tab_gov.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // Qt
15 #include <QGridLayout>
16 #include <QLineEdit>
17 #include <QListWidget>
18 #include <QMenu>
19 #include <QPushButton>
20 #include <QRadioButton>
21 
22 // utility
23 #include "fcintl.h"
24 
25 // common
26 #include "game.h"
27 #include "government.h"
28 
29 // ruledit
30 #include "ruledit.h"
31 #include "ruledit_qt.h"
32 #include "validity.h"
33 
34 #include "tab_gov.h"
35 
39 tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
40 {
41  QVBoxLayout *main_layout = new QVBoxLayout(this);
42  QGridLayout *gov_layout = new QGridLayout();
43  QLabel *label;
44  QPushButton *effects_button;
45  QPushButton *add_button;
46  QPushButton *delete_button;
47  QPushButton *reqs_button;
48 
49  ui = ui_in;
50  selected = 0;
51 
52  gov_list = new QListWidget(this);
53 
54  connect(gov_list, &QListWidget::itemSelectionChanged, this,
56  main_layout->addWidget(gov_list);
57 
58  gov_layout->setSizeConstraint(QLayout::SetMaximumSize);
59 
60  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
61  label->setParent(this);
62  rname = new QLineEdit(this);
63  rname->setText(QStringLiteral("None"));
64  connect(rname, &QLineEdit::returnPressed, this, &tab_gov::name_given);
65  gov_layout->addWidget(label, 0, 0);
66  gov_layout->addWidget(rname, 0, 2);
67 
68  label = new QLabel(QString::fromUtf8(R__("Name")));
69  label->setParent(this);
70  same_name = new QRadioButton();
71  connect(same_name, &QAbstractButton::toggled, this,
73  name = new QLineEdit(this);
74  name->setText(QStringLiteral("None"));
75  connect(name, &QLineEdit::returnPressed, this, &tab_gov::name_given);
76  gov_layout->addWidget(label, 1, 0);
77  gov_layout->addWidget(same_name, 1, 1);
78  gov_layout->addWidget(name, 1, 2);
79 
80  reqs_button =
81  new QPushButton(QString::fromUtf8(R__("Requirements")), this);
82  connect(reqs_button, &QAbstractButton::pressed, this, &tab_gov::edit_reqs);
83  gov_layout->addWidget(reqs_button, 2, 2);
84 
85  effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
86  connect(effects_button, &QAbstractButton::pressed, this,
88  gov_layout->addWidget(effects_button, 3, 2);
89 
90  add_button =
91  new QPushButton(QString::fromUtf8(R__("Add Government")), this);
92  connect(add_button, &QAbstractButton::pressed, this, &tab_gov::add_now);
93  gov_layout->addWidget(add_button, 4, 0);
94  show_experimental(add_button);
95 
96  delete_button = new QPushButton(
97  QString::fromUtf8(R__("Remove this Government")), this);
98  connect(delete_button, &QAbstractButton::pressed, this,
100  gov_layout->addWidget(delete_button, 4, 2);
101  show_experimental(delete_button);
102 
103  refresh();
104 
105  main_layout->addLayout(gov_layout);
106 
107  setLayout(main_layout);
108 }
109 
114 {
115  gov_list->clear();
116 
117  for (auto &pgov : governments) {
118  if (!pgov.ruledit_disabled) {
119  QListWidgetItem *item = new QListWidgetItem(
120  QString::fromUtf8(government_rule_name(&pgov)));
121 
122  gov_list->insertItem(government_index(&pgov), item);
123  }
124  };
125 }
126 
131 {
132  selected = pgov;
133 
134  if (selected != 0) {
135  QString dispn = QString::fromUtf8(untranslated_name(&(pgov->name)));
136  QString rulen = QString::fromUtf8(government_rule_name(pgov));
137 
138  name->setText(dispn);
139  rname->setText(rulen);
140  if (dispn == rulen) {
141  name->setEnabled(false);
142  same_name->setChecked(true);
143  } else {
144  same_name->setChecked(false);
145  name->setEnabled(true);
146  }
147  } else {
148  name->setText(QStringLiteral("None"));
149  rname->setText(QStringLiteral("None"));
150  same_name->setChecked(true);
151  name->setEnabled(false);
152  }
153 }
154 
159 {
160  QList<QListWidgetItem *> select_list = gov_list->selectedItems();
161 
162  if (!select_list.isEmpty()) {
163  QByteArray gn_bytes;
164 
165  gn_bytes = select_list.at(0)->text().toUtf8();
166  update_gov_info(government_by_rule_name(gn_bytes.data()));
167  }
168 }
169 
174 {
175  if (selected != nullptr) {
176  QByteArray name_bytes;
177  QByteArray rname_bytes;
178 
179  for (const auto &pgov : governments) {
180  if (&pgov != selected && !pgov.ruledit_disabled) {
181  rname_bytes = rname->text().toUtf8();
182  if (!strcmp(government_rule_name(&pgov), rname_bytes.data())) {
183  ui->display_msg(R__("A government with that rule name already "
184  "exists!"));
185  return;
186  }
187  }
188  }
189 
190  if (same_name->isChecked()) {
191  name->setText(rname->text());
192  }
193 
194  name_bytes = name->text().toUtf8();
195  rname_bytes = rname->text().toUtf8();
196  names_set(&(selected->name), 0, name_bytes.data(), rname_bytes.data());
197  refresh();
198  }
199 }
200 
205 {
206  if (selected != 0) {
207  requirers_dlg *requirers;
208 
211  requirers)) {
212  return;
213  }
214 
215  selected->ruledit_disabled = true;
216 
217  refresh();
218  update_gov_info(nullptr);
219  }
220 }
221 
226 {
227  if (government_by_rule_name("New Government") != nullptr) {
228  return false;
229  }
230 
231  name_set(&(pgov->name), 0, "New Government");
232 
233  return true;
234 }
235 
240 {
241  struct government *new_gov;
242 
243  // Try to reuse freed government slot
244  for (auto &pgov : governments) {
245  if (pgov.ruledit_disabled) {
246  if (initialize_new_gov(&pgov)) {
247  pgov.ruledit_disabled = false;
248  update_gov_info(&pgov);
249  refresh();
250  }
251  return;
252  }
253  }
254 
255  // Try to add completely new government
256  if (game.control.num_goods_types >= MAX_GOODS_TYPES) {
257  return;
258  }
259 
260  // government_count must be big enough to hold new government or
261  // government_by_number() fails.
262  game.control.government_count++;
263  new_gov = government_by_number(game.control.government_count - 1);
264  if (initialize_new_gov(new_gov)) {
265  update_gov_info(new_gov);
266 
267  refresh();
268  } else {
269  game.control.government_count--; // Restore
270  }
271 }
272 
276 void tab_gov::same_name_toggle(bool checked)
277 {
278  name->setEnabled(!checked);
279  if (checked) {
280  name->setText(rname->text());
281  }
282 }
283 
288 {
289  if (selected != nullptr) {
290  ui->open_req_edit(QString::fromUtf8(government_rule_name(selected)),
291  &selected->reqs);
292  }
293 }
294 
299 {
300  if (selected != nullptr) {
301  struct universal uni;
302 
303  uni.value.govern = selected;
304  uni.kind = VUT_GOVERNMENT;
305 
306  ui->open_effect_edit(QString::fromUtf8(government_rule_name(selected)),
307  &uni, EFMC_NORMAL);
308  }
309 }
void display_msg(const char *msg)
Display status message.
Definition: ruledit_qt.cpp:240
void open_req_edit(const QString &target, struct requirement_vector *preqs)
Open req_edit dialog.
Definition: ruledit_qt.cpp:278
requirers_dlg * create_requirers(const char *title)
Create requirers dlg.
Definition: ruledit_qt.cpp:248
void open_effect_edit(const QString &target, struct universal *uni, enum effect_filter_main_class efmc)
Open effect_edit dialog.
Definition: ruledit_qt.cpp:350
void delete_now()
User requested government deletion.
Definition: tab_gov.cpp:204
tab_gov(ruledit_gui *ui_in)
Setup tab_gov object.
Definition: tab_gov.cpp:39
QRadioButton * same_name
Definition: tab_gov.h:43
QLineEdit * rname
Definition: tab_gov.h:41
void edit_effects()
User wants to edit effects.
Definition: tab_gov.cpp:298
QLineEdit * name
Definition: tab_gov.h:40
void add_now()
User requested new government.
Definition: tab_gov.cpp:239
bool initialize_new_gov(struct government *pgov)
Initialize new government for use.
Definition: tab_gov.cpp:225
void update_gov_info(struct government *pgov)
Update info of the government.
Definition: tab_gov.cpp:130
void edit_reqs()
User wants to edit reqs.
Definition: tab_gov.cpp:287
struct government * selected
Definition: tab_gov.h:45
void refresh()
Refresh the information.
Definition: tab_gov.cpp:113
void same_name_toggle(bool checked)
Toggled whether rule_name and name should be kept identical.
Definition: tab_gov.cpp:276
void name_given()
User entered name for the government.
Definition: tab_gov.cpp:173
QListWidget * gov_list
Definition: tab_gov.h:42
ruledit_gui * ui
Definition: tab_gov.h:36
void select_gov()
User selected government from the list.
Definition: tab_gov.cpp:158
@ EFMC_NORMAL
Definition: effect_edit.h:32
#define MAX_GOODS_TYPES
Definition: fc_types.h:45
#define R__(String)
Definition: fcintl.h:58
struct civ_game game
Definition: game.cpp:47
struct government * government_by_number(const Government_type_id gov)
Return the government with the given index.
Definition: government.cpp:96
const char * government_rule_name(const struct government *pgovern)
Return the (untranslated) rule name of the government.
Definition: government.cpp:126
struct government * government_by_rule_name(const char *name)
Returns the government that has the given (untranslated) rule name.
Definition: government.cpp:48
std::vector< government > governments
Definition: government.cpp:28
Government_type_id government_index(const struct government *pgovern)
Return the government index.
Definition: government.cpp:75
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * untranslated_name(const struct name_translation *ptrans)
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
void show_experimental(QWidget *wdg)
Show widget if experimental features enabled, hide otherwise.
Definition: ruledit.cpp:163
void ruledit_qt_display_requirers(const char *msg, void *data)
Display requirer list.
Definition: ruledit_qt.cpp:67
struct packet_ruleset_control control
Definition: game.h:74
struct requirement_vector reqs
Definition: government.h:41
bool ruledit_disabled
Definition: government.h:38
struct name_translation name
Definition: government.h:37
Definition: climisc.h:66
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct government * govern
Definition: fc_types.h:578
bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)
Check if anything in ruleset needs government.
Definition: validity.cpp:299