Freeciv-3.3: /fast/freeciv/nightly/main/tools/ruledit/tab_counters.cpp Source File

Freeciv-3.3

Loading...
Searching...
No Matches
tab_counters.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 <QPushButton>
23
24// utility
25#include "fcintl.h"
26#include "log.h"
27
28// common
29#include "counters.h"
30#include "fc_types.h"
31#include "game.h"
32
33// ruledit
34#include "ruledit.h"
35#include "ruledit_qt.h"
36#include "validity.h"
37
38#include "tab_counters.h"
39
40/**********************************************************************/
43tab_counter::tab_counter(ruledit_gui *ui_in) : QWidget()
44{
45#define empty_widget (nullptr)
46/* We use const array of pointers and hopes compiler
47 * will unroll loop
48 */
49#define widgets_row(...) { \
50 column = 0; \
51 QWidget * const wid[] = { __VA_ARGS__ }; \
52 for (unsigned int i = 0; \
53 i < sizeof(wid) / sizeof(wid[0]); i++) {\
54 if (wid[i]) { \
55 counter_layout->addWidget(wid[i], \
56 row, \
57 column);\
58 }\
59 column++;\
60 };\
61 row++;\
62 }
63
64 QVBoxLayout *main_layout = new QVBoxLayout(this);
65 QGridLayout *counter_layout = new QGridLayout();
66 QLabel *label;
67 QPushButton *add_button;
68 QPushButton *delete_button;
69 int row = 0;
70 int column = 0;
71
72 ui = ui_in;
73 selected = nullptr;
74
75 counter_list = new QListWidget(this);
76
77 connect(counter_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_counter()));
78 main_layout->addWidget(counter_list);
79
80 counter_layout->setSizeConstraint(QLayout::SetMaximumSize);
81
82 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
83 label->setParent(this);
84 rname = new QLineEdit(this);
85 rname->setText(R__("None"));
86 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
87 widgets_row(label, empty_widget, rname);
88
89 label = new QLabel(QString::fromUtf8(R__("Name")));
90 label->setParent(this);
91
92 name = new QLineEdit(this);
93 name->setText(R__("None"));
94 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
95 same_name = new QCheckBox(this);
96 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
97 same_name->setChecked(true);
98 widgets_row(label, same_name, name);
99
100 label = new QLabel(QString::fromUtf8(R__("Default Value")), this);
101 label->setParent(this);
102 def = new QSpinBox(this);
103 def->setMaximum(INT_MAX);
104 def->setMinimum(INT_MIN);
105 connect(def, SIGNAL(valueChanged(int)), this, SLOT(default_given(int)));
106 widgets_row(label, empty_widget, def);
107
108
109 label = new QLabel(QString::fromUtf8(R__("Checkpoint")), this);
110 label->setParent(this);
111 checkpoint = new QSpinBox(this);
112 checkpoint->setMaximum(INT_MAX);
113 checkpoint->setMinimum(INT_MIN);
114 connect(checkpoint, SIGNAL(valueChanged(int)), this, SLOT(checkpoint_given(int)));
115 widgets_row(label, empty_widget, checkpoint);
116
117 {
118 enum counter_behaviour current;
119 type = new QComboBox(this);
120
121 label = new QLabel(QString::fromUtf8(R__("Behavior")));
122 label->setParent(this);
123
124 for (current = counter_behaviour_begin(); current > counter_behaviour_end(); current = counter_behaviour_next(current)) {
125 QVariant value(current);
126
127 type->addItem(counter_behaviour_name(current), value);
128 };
129
130 connect(type, SIGNAL(activated(int)), this, SLOT(counter_behaviour_selected(int)));
131 widgets_row(label, empty_widget, type);
132 }
133
134 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
135 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
136 widgets_row(empty_widget, empty_widget, effects_button);
137
138 add_button = new QPushButton(QString::fromUtf8(R__("Add Counter")), this);
139 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
140
141 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Counter")), this);
142 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
143
144 widgets_row(empty_widget, empty_widget, add_button, delete_button);
145 show_experimental(add_button);
146 show_experimental(delete_button);
147
148 refresh();
149 update_counter_info(nullptr);
150
151 main_layout->addLayout(counter_layout);
152
153 setLayout(main_layout);
154#undef empty_widget
155#undef widgets_row
156}
157
158/**********************************************************************/
161 void tab_counter::counter_behaviour_selected(int item)
162{
163 QVariant item_data;
164
165 if (nullptr == selected) {
166
167 return;
168 }
169 item_data = type->currentData();
170 selected->type = (enum counter_behaviour) item_data.toInt();
171
172 update_counter_info(selected);
173 refresh();
174}
175
176/**********************************************************************/
179 void tab_counter::refresh()
180{
181 counter_list->clear();
182
183 counters_re_iterate(pcount) {
184 QListWidgetItem *item = new QListWidgetItem(counter_rule_name(pcount));
185
186 counter_list->insertItem(counter_index(pcount), item);
187 } counters_re_iterate_end;
188}
189
190/**********************************************************************/
193 void tab_counter::update_counter_info(struct counter *counter)
194{
195 selected = counter;
196
197 if (selected != nullptr) {
198 QString dispn = QString::fromUtf8(untranslated_name(&(counter->name)));
199 QString rulen = QString::fromUtf8(rule_name_get(&(counter->name)));
200
201 name->setText(dispn);
202 rname->setText(rulen);
203
204 if (dispn == rulen) {
205 name->setEnabled(false);
206 } else {
207 name->setEnabled(true);
208 }
209
210 checkpoint->setValue(selected->checkpoint);
211 def->setValue(selected->def);
212 } else {
213 name->setText(R__("None"));
214 rname->setText(R__("None"));
215 name->setEnabled(false);
216 }
217}
218
219/**********************************************************************/
222 void tab_counter::select_counter()
223{
224 char *cname;
225
226 QList<QListWidgetItem *> select_list = counter_list->selectedItems();
227
228 if (!select_list.isEmpty()) {
229 QByteArray tn_bytes;
230
231 tn_bytes = select_list.at(0)->text().toUtf8();
232
233 cname = tn_bytes.data();
234 update_counter_info(counter_by_rule_name(cname));
235 }
236}
237
238
239/**********************************************************************/
242 void tab_counter::checkpoint_given(int val)
243{
244 if (nullptr != selected) {
245
246 selected->checkpoint = val;
247 }
248}
249
250/**********************************************************************/
253 void tab_counter::default_given(int val)
254{
255 if (nullptr != selected) {
256
257 selected->def = val;
258 }
259}
260
261/**********************************************************************/
264 void tab_counter::name_given()
265{
266 if (selected != nullptr) {
267 QByteArray name_bytes;
268 QByteArray rname_bytes;
269
270 city_counters_iterate(pcounter) {
271 if (pcounter != selected) {
272 rname_bytes = rname->text().toUtf8();
273 if (!strcmp(counter_rule_name(pcounter), rname_bytes.data())) {
274 ui->display_msg(R__("A counter with that rule name already exists!"));
275 return;
276 }
277 }
278 } city_counters_iterate_end;
279
280 name_bytes = name->text().toUtf8();
281 rname_bytes = rname->text().toUtf8();
282 names_set(&(selected->name), 0,
283 name_bytes.data(),
284 rname_bytes.data());
285 refresh();
286 }
287}
288
289/**********************************************************************/
292 void tab_counter::delete_now()
293{
294
295 if (nullptr != selected) {
296 requirers_dlg *requirers;
297
298 requirers = ui->create_requirers(counter_rule_name(selected));
299 if (is_counter_needed(selected, &ruledit_qt_display_requirers, requirers)) {
300 return;
301 }
302
303 selected->ruledit_disabled = true;
304
305 refresh();
306 update_counter_info(nullptr);
307 }
308}
309
310/**********************************************************************/
313 bool tab_counter::initialize_new_counter(struct counter *counter)
314{
315 counter->checkpoint = 0;
316 counter->def = 0;
317 counter->target = CTGT_CITY;
318 counter->type = CB_CITY_OWNED_TURNS;
319 counter->index = counters_get_city_counters_count() - 1;
320 counter->ruledit_disabled = FALSE;
321
322 name_set(&counter->name, 0, "New counter");
323
324 return true;
325}
326
327/**********************************************************************/
330 void tab_counter::add_now()
331{
332 struct counter *new_counter;
333
334 // Try to reuse freed counter slot
335 city_counters_iterate(pcount) {
336 if (pcount->type == COUNTER_BEHAVIOUR_LAST
337 || pcount->ruledit_disabled) {
338 if (initialize_new_counter(pcount)) {
339 update_counter_info(pcount);
340 refresh();
341 }
342 return;
343 }
344 } city_counters_iterate_end;
345
346 // Try to add completely new counter
347 if (counters_get_city_counters_count() >= MAX_COUNTERS) {
348 return;
349 }
350
351 // num_counter_types must be big enough to hold new counter or
352 // counter_by_number() fails.
353 game.control.num_counters++;
354 new_counter = counter_by_id(counters_get_city_counters_count());
355 if (initialize_new_counter(new_counter)) {
356 update_counter_info(new_counter);
357 attach_city_counter(new_counter);
358 refresh();
359 }
360}
361
362/**********************************************************************/
365 void tab_counter::same_name_toggle(bool checked)
366{
367 name->setEnabled(!checked);
368 if (checked) {
369 name->setText(rname->text());
370 }
371}
372
373/**********************************************************************/
376 void tab_counter::edit_effects()
377{
378 if (selected != nullptr) {
379 struct universal uni;
380
381 uni.value.counter = selected;
382 uni.kind = VUT_COUNTER;
383
384 ui->open_effect_edit(QString::fromUtf8(counter_rule_name(selected)),
385 &uni, EFMC_NORMAL);
386 }
387}
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::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_counter::add_now
void add_now()
Definition tab_counters.cpp:330
tab_counter::rname
QLineEdit * rname
Definition tab_counters.h:49
tab_counter::update_counter_info
void update_counter_info(struct counter *counter)
Definition tab_counters.cpp:193
tab_counter::initialize_new_counter
bool initialize_new_counter(struct counter *padv)
Definition tab_counters.cpp:313
tab_counter::selected
struct counter * selected
Definition tab_counters.h:58
tab_counter::effects_button
QPushButton * effects_button
Definition tab_counters.h:54
tab_counter::ui
ruledit_gui * ui
Definition tab_counters.h:46
tab_counter::type
QComboBox * type
Definition tab_counters.h:52
tab_counter::select_counter
void select_counter()
Definition tab_counters.cpp:222
tab_counter::same_name
QCheckBox * same_name
Definition tab_counters.h:53
tab_counter::name_given
void name_given()
Definition tab_counters.cpp:264
tab_counter::counter_behaviour_selected
void counter_behaviour_selected(int item)
Definition tab_counters.cpp:161
tab_counter::def
QSpinBox * def
Definition tab_counters.h:51
tab_counter::tab_counter
tab_counter(ruledit_gui *ui_in)
Definition tab_counters.cpp:43
tab_counter::counter_list
QListWidget * counter_list
Definition tab_counters.h:56
tab_counter::checkpoint
QSpinBox * checkpoint
Definition tab_counters.h:50
tab_counter::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_counters.cpp:365
tab_counter::edit_effects
void edit_effects()
Definition tab_counters.cpp:376
tab_counter::name
QLineEdit * name
Definition tab_counters.h:48
tab_counter::refresh
void refresh()
Definition tab_counters.cpp:179
tab_counter::delete_now
void delete_now()
Definition tab_counters.cpp:292
tab_counter::default_given
void default_given(int val)
Definition tab_counters.cpp:253
tab_counter::checkpoint_given
void checkpoint_given(int val)
Definition tab_counters.cpp:242
incite_cost
char * incite_cost
Definition comments.c:74
counter_index
int counter_index(const struct counter *pcount)
Definition counters.c:174
counter_by_rule_name
struct counter * counter_by_rule_name(const char *name)
Definition counters.c:115
counter_rule_name
const char * counter_rule_name(struct counter *pcount)
Definition counters.c:165
counters_get_city_counters_count
int counters_get_city_counters_count(void)
Definition counters.c:74
counter_by_id
struct counter * counter_by_id(int id)
Definition counters.c:82
attach_city_counter
void attach_city_counter(struct counter *counter)
Definition counters.c:94
counters.h
city_counters_iterate_end
#define city_counters_iterate_end
Definition counters.h:64
city_counters_iterate
#define city_counters_iterate(pcount)
Definition counters.h:57
counters_re_iterate_end
#define counters_re_iterate_end
Definition counters.h:75
counters_re_iterate
#define counters_re_iterate(pcount)
Definition counters.h:67
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:34
fc_types.h
CTGT_CITY
@ CTGT_CITY
Definition fc_types.h:126
MAX_COUNTERS
#define MAX_COUNTERS
Definition fc_types.h:106
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:61
game.h
log.h
name_set
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
Definition name_translation.h:88
rule_name_get
static const char * rule_name_get(const struct name_translation *ptrans)
Definition name_translation.h:112
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
counter
Definition counters.h:27
counter::target
enum counter_target target
Definition counters.h:32
counter::def
int def
Definition counters.h:34
counter::ruledit_disabled
bool ruledit_disabled
Definition counters.h:30
counter::checkpoint
int checkpoint
Definition counters.h:33
counter::index
int index
Definition counters.h:37
counter::name
struct name_translation name
Definition counters.h:28
counter::type
enum counter_behaviour type
Definition counters.h:31
item
Definition climisc.h:80
packet_ruleset_control::num_counters
int num_counters
Definition packets_gen.h:1467
universal
Definition fc_types.h:878
universal::kind
enum universals_n kind
Definition fc_types.h:880
universal::value
universals_u value
Definition fc_types.h:879
FALSE
#define FALSE
Definition support.h:47
empty_widget
#define empty_widget
widgets_row
#define widgets_row(...)
tab_counters.h
universals_u::counter
struct counter * counter
Definition fc_types.h:689
is_counter_needed
bool is_counter_needed(struct counter *pcount, requirers_cb cb, void *data)
Definition validity.c:189
validity.h