Freeciv-3.2: /fast/freeciv/nightly/S3_2/tools/ruledit/tab_gov.cpp Source File

Freeciv-3.2

Loading...
Searching...
No Matches
tab_gov.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QCheckBox>
20#include <QGridLayout>
21#include <QLineEdit>
22#include <QListWidget>
23#include <QMenu>
24#include <QPushButton>
25#include <QToolButton>
26
27// utility
28#include "fcintl.h"
29#include "log.h"
30
31// common
32#include "game.h"
33#include "government.h"
34
35// ruledit
36#include "ruledit.h"
37#include "ruledit_qt.h"
38#include "validity.h"
39
40#include "tab_gov.h"
41
42/**********************************************************************/
45tab_gov::tab_gov(ruledit_gui *ui_in) : QWidget()
46{
47 QVBoxLayout *main_layout = new QVBoxLayout(this);
48 QGridLayout *gov_layout = new QGridLayout();
49 QLabel *label;
50 QPushButton *effects_button;
51 QPushButton *add_button;
52 QPushButton *delete_button;
53 QPushButton *reqs_button;
54
55 ui = ui_in;
56 selected = 0;
57
58 gov_list = new QListWidget(this);
59
60 connect(gov_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_gov()));
61 main_layout->addWidget(gov_list);
62
63 gov_layout->setSizeConstraint(QLayout::SetMaximumSize);
64
65 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
66 label->setParent(this);
67 rname = new QLineEdit(this);
68 rname->setText(R__("None"));
69 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
70 gov_layout->addWidget(label, 0, 0);
71 gov_layout->addWidget(rname, 0, 2);
72
73 label = new QLabel(QString::fromUtf8(R__("Name")));
74 label->setParent(this);
75 same_name = new QCheckBox();
76 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
77 name = new QLineEdit(this);
78 name->setText(R__("None"));
79 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
80 gov_layout->addWidget(label, 1, 0);
81 gov_layout->addWidget(same_name, 1, 1);
82 gov_layout->addWidget(name, 1, 2);
83
84 reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
85 connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
86 gov_layout->addWidget(reqs_button, 2, 2);
87
88 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
89 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
90 gov_layout->addWidget(effects_button, 3, 2);
91
92 add_button = new QPushButton(QString::fromUtf8(R__("Add Government")), this);
93 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
94 gov_layout->addWidget(add_button, 4, 0);
95 show_experimental(add_button);
96
97 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Government")), this);
98 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
99 gov_layout->addWidget(delete_button, 4, 2);
100 show_experimental(delete_button);
101
102 refresh();
103 update_gov_info(nullptr);
104
105 main_layout->addLayout(gov_layout);
106
107 setLayout(main_layout);
108}
109
110/**********************************************************************/
113 void tab_gov::refresh()
114{
115 gov_list->clear();
116
117 governments_re_active_iterate(pgov) {
118 QListWidgetItem *item
119 = new QListWidgetItem(QString::fromUtf8(government_rule_name(pgov)));
120
121 gov_list->insertItem(government_index(pgov), item);
122 } governments_re_active_iterate_end;
123}
124
125/**********************************************************************/
128 void tab_gov::update_gov_info(struct government *pgov)
129{
130 selected = pgov;
131
132 if (selected != 0) {
133 QString dispn = QString::fromUtf8(untranslated_name(&(pgov->name)));
134 QString rulen = QString::fromUtf8(government_rule_name(pgov));
135
136 name->setText(dispn);
137 rname->setText(rulen);
138 if (dispn == rulen) {
139 name->setEnabled(false);
140 same_name->setChecked(true);
141 } else {
142 same_name->setChecked(false);
143 name->setEnabled(true);
144 }
145 } else {
146 name->setText(R__("None"));
147 rname->setText(R__("None"));
148 same_name->setChecked(true);
149 name->setEnabled(false);
150 }
151}
152
153/**********************************************************************/
156 void tab_gov::select_gov()
157{
158 QList<QListWidgetItem *> select_list = gov_list->selectedItems();
159
160 if (!select_list.isEmpty()) {
161 QByteArray gn_bytes;
162
163 gn_bytes = select_list.at(0)->text().toUtf8();
164 update_gov_info(government_by_rule_name(gn_bytes.data()));
165 }
166}
167
168/**********************************************************************/
171 void tab_gov::name_given()
172{
173 if (selected != nullptr) {
174 QByteArray name_bytes;
175 QByteArray rname_bytes;
176
177 governments_iterate(pgov) {
178 if (pgov != selected && !pgov->ruledit_disabled) {
179 rname_bytes = rname->text().toUtf8();
180 if (!strcmp(government_rule_name(pgov), rname_bytes.data())) {
181 ui->display_msg(R__("A government with that rule name already "
182 "exists!"));
183 return;
184 }
185 }
186 } governments_iterate_end;
187
188 if (same_name->isChecked()) {
189 name->setText(rname->text());
190 }
191
192 name_bytes = name->text().toUtf8();
193 rname_bytes = rname->text().toUtf8();
194 names_set(&(selected->name), 0,
195 name_bytes.data(),
196 rname_bytes.data());
197 refresh();
198 }
199}
200
201/**********************************************************************/
204 void tab_gov::delete_now()
205{
206 if (selected != 0) {
207 requirers_dlg *requirers;
208
209 requirers = ui->create_requirers(government_rule_name(selected));
210 if (is_government_needed(selected, &ruledit_qt_display_requirers, requirers)) {
211 return;
212 }
213
214 selected->ruledit_disabled = true;
215
216 refresh();
217 update_gov_info(nullptr);
218 }
219}
220
221/**********************************************************************/
224 bool tab_gov::initialize_new_gov(struct government *pgov)
225{
226 if (government_by_rule_name("New Government") != nullptr) {
227 return false;
228 }
229
230 name_set(&(pgov->name), 0, "New Government");
231
232 return true;
233}
234
235/**********************************************************************/
238 void tab_gov::add_now()
239{
240 struct government *new_gov;
241
242 // Try to reuse freed government slot
243 governments_iterate(pgov) {
244 if (pgov->ruledit_disabled) {
245 if (initialize_new_gov(pgov)) {
246 pgov->ruledit_disabled = false;
247 update_gov_info(pgov);
248 refresh();
249 }
250 return;
251 }
252 } governments_iterate_end;
253
254 // Try to add completely new government
255 if (game.control.num_goods_types >= MAX_GOODS_TYPES) {
256 return;
257 }
258
259 // government_count must be big enough to hold new government or
260 // government_by_number() fails.
261 game.control.government_count++;
262 new_gov = government_by_number(game.control.government_count - 1);
263 if (initialize_new_gov(new_gov)) {
264 update_gov_info(new_gov);
265
266 refresh();
267 } else {
268 game.control.government_count--; // Restore
269 }
270}
271
272/**********************************************************************/
275 void tab_gov::same_name_toggle(bool checked)
276{
277 name->setEnabled(!checked);
278 if (checked) {
279 name->setText(rname->text());
280 }
281}
282
283/**********************************************************************/
286 void tab_gov::edit_reqs()
287{
288 if (selected != nullptr) {
289 ui->open_req_edit(QString::fromUtf8(government_rule_name(selected)),
290 &selected->reqs);
291 }
292}
293
294/**********************************************************************/
297 void tab_gov::edit_effects()
298{
299 if (selected != nullptr) {
300 struct universal uni;
301
302 uni.value.govern = selected;
303 uni.kind = VUT_GOVERNMENT;
304
305 ui->open_effect_edit(QString::fromUtf8(government_rule_name(selected)),
306 &uni, EFMC_NORMAL);
307 }
308}
requirers_dlg
Definition requirers_dlg.h:28
ruledit_gui
Definition ruledit_qt.h:97
ruledit_gui::display_msg
void display_msg(const char *msg)
Definition ruledit_qt.cpp:293
ruledit_gui::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:335
ruledit_gui::create_requirers
requirers_dlg * create_requirers(const char *title)
Definition ruledit_qt.cpp:301
ruledit_gui::open_effect_edit
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
Definition ruledit_qt.cpp:404
tab_gov::delete_now
void delete_now()
Definition tab_gov.cpp:204
tab_gov::tab_gov
tab_gov(ruledit_gui *ui_in)
Definition tab_gov.cpp:45
tab_gov::rname
QLineEdit * rname
Definition tab_gov.h:44
tab_gov::edit_effects
void edit_effects()
Definition tab_gov.cpp:297
tab_gov::name
QLineEdit * name
Definition tab_gov.h:43
tab_gov::add_now
void add_now()
Definition tab_gov.cpp:238
tab_gov::initialize_new_gov
bool initialize_new_gov(struct government *pgov)
Definition tab_gov.cpp:224
tab_gov::update_gov_info
void update_gov_info(struct government *pgov)
Definition tab_gov.cpp:128
tab_gov::edit_reqs
void edit_reqs()
Definition tab_gov.cpp:286
tab_gov::selected
struct government * selected
Definition tab_gov.h:48
tab_gov::refresh
void refresh()
Definition tab_gov.cpp:113
tab_gov::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_gov.cpp:275
tab_gov::name_given
void name_given()
Definition tab_gov.cpp:171
tab_gov::gov_list
QListWidget * gov_list
Definition tab_gov.h:45
tab_gov::ui
ruledit_gui * ui
Definition tab_gov.h:39
tab_gov::select_gov
void select_gov()
Definition tab_gov.cpp:156
tab_gov::same_name
QCheckBox * same_name
Definition tab_gov.h:46
incite_cost
char * incite_cost
Definition comments.c:74
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:34
MAX_GOODS_TYPES
#define MAX_GOODS_TYPES
Definition fc_types.h:51
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:62
game.h
government_by_number
struct government * government_by_number(const Government_type_id gov)
Definition government.c:103
government_index
Government_type_id government_index(const struct government *pgovern)
Definition government.c:82
government_rule_name
const char * government_rule_name(const struct government *pgovern)
Definition government.c:133
government_by_rule_name
struct government * government_by_rule_name(const char *name)
Definition government.c:55
government.h
governments_iterate
#define governments_iterate(NAME_pgov)
Definition government.h:120
governments_re_active_iterate
#define governments_re_active_iterate(_p)
Definition government.h:125
governments_re_active_iterate_end
#define governments_re_active_iterate_end
Definition government.h:129
governments_iterate_end
#define governments_iterate_end
Definition government.h:123
log.h
name_set
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
Definition name_translation.h:88
untranslated_name
static const char * untranslated_name(const struct name_translation *ptrans)
Definition name_translation.h:104
names_set
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
Definition name_translation.h:60
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:237
ruledit.h
ruledit_qt_display_requirers
void ruledit_qt_display_requirers(const char *msg, void *data)
Definition ruledit_qt.cpp:97
ruledit_qt.h
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
government
Definition government.h:52
government::reqs
struct requirement_vector reqs
Definition government.h:58
government::ruledit_disabled
bool ruledit_disabled
Definition government.h:55
government::name
struct name_translation name
Definition government.h:54
item
Definition climisc.h:80
packet_ruleset_control::num_goods_types
int num_goods_types
Definition packets_gen.h:1458
packet_ruleset_control::government_count
int government_count
Definition packets_gen.h:1464
universal
Definition fc_types.h:901
universal::kind
enum universals_n kind
Definition fc_types.h:903
universal::value
universals_u value
Definition fc_types.h:902
tab_gov.h
universals_u::govern
struct government * govern
Definition fc_types.h:716
is_government_needed
bool is_government_needed(struct government *pgov, requirers_cb cb, void *data)
Definition validity.c:358
validity.h