Freeciv-3.2
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/**********************************************************************/
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);
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
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()));
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)));
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)));
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)));
132 }
133
134 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
135 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
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
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/**********************************************************************/
162{
164
165 if (nullptr == selected) {
166
167 return;
168 }
169 item_data = type->currentData();
170 selected->type = (enum counter_behaviour) item_data.toInt();
171
173 refresh();
174}
175
176/**********************************************************************/
189
190/**********************************************************************/
194{
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/**********************************************************************/
223{
224 char *cname;
225
227
228 if (!select_list.isEmpty()) {
230
231 tn_bytes = select_list.at(0)->text().toUtf8();
232
233 cname = tn_bytes.data();
235 }
236}
237
238
239/**********************************************************************/
243{
244 if (nullptr != selected) {
245
246 selected->checkpoint = val;
247 }
248}
249
250/**********************************************************************/
254{
255 if (nullptr != selected) {
256
257 selected->def = val;
258 }
259}
260
261/**********************************************************************/
265{
266 if (selected != nullptr) {
269
271 if (pcounter != selected) {
272 rname_bytes = rname->text().toUtf8();
274 ui->display_msg(R__("A counter with that rule name already exists!"));
275 return;
276 }
277 }
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/**********************************************************************/
293{
294
295 if (nullptr != selected) {
297
300 return;
301 }
302
304
305 refresh();
306 update_counter_info(nullptr);
307 }
308}
309
310/**********************************************************************/
314{
315 counter->checkpoint = 0;
316 counter->def = 0;
321
322 name_set(&counter->name, 0, "New counter");
323 if (counter->helptext != nullptr) {
325 }
326
327 return true;
328}
329
330/**********************************************************************/
334{
335 struct counter *new_counter;
336
337 // Try to reuse freed counter slot
339 if (pcount->type == COUNTER_BEHAVIOUR_LAST
340 || pcount->ruledit_disabled) {
343 refresh();
344 }
345 return;
346 }
348
349 // Try to add completely new counter
351 return;
352 }
353
354 // num_counter_types must be big enough to hold new counter or
355 // counter_by_number() fails.
361 refresh();
362 }
363}
364
365/**********************************************************************/
369{
370 name->setEnabled(!checked);
371 if (checked) {
372 name->setText(rname->text());
373 }
374}
375
376/**********************************************************************/
380{
381 if (selected != nullptr) {
382 struct universal uni;
383
384 uni.value.counter = selected;
385 uni.kind = VUT_COUNTER;
386
387 ui->open_effect_edit(QString::fromUtf8(counter_rule_name(selected)),
388 &uni, EFMC_NORMAL);
389 }
390}
void display_msg(const char *msg)
requirers_dlg * create_requirers(const char *title)
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
QLineEdit * rname
void update_counter_info(struct counter *counter)
bool initialize_new_counter(struct counter *padv)
struct counter * selected
QPushButton * effects_button
ruledit_gui * ui
QComboBox * type
void select_counter()
QCheckBox * same_name
void counter_behaviour_selected(int item)
QSpinBox * def
tab_counter(ruledit_gui *ui_in)
QListWidget * counter_list
QSpinBox * checkpoint
void same_name_toggle(bool checked)
void edit_effects()
QLineEdit * name
void default_given(int val)
void checkpoint_given(int val)
char * incite_cost
Definition comments.c:75
int counter_index(const struct counter *pcount)
Definition counters.c:174
struct counter * counter_by_rule_name(const char *name)
Definition counters.c:115
const char * counter_rule_name(struct counter *pcount)
Definition counters.c:165
int counters_get_city_counters_count(void)
Definition counters.c:74
struct counter * counter_by_id(int id)
Definition counters.c:82
void attach_city_counter(struct counter *counter)
Definition counters.c:94
#define city_counters_iterate_end
Definition counters.h:64
#define city_counters_iterate(pcount)
Definition counters.h:57
#define counters_re_iterate_end
Definition counters.h:75
#define counters_re_iterate(pcount)
Definition counters.h:67
@ EFMC_NORMAL
Definition effect_edit.h:34
@ CTGT_CITY
Definition fc_types.h:126
#define MAX_COUNTERS
Definition fc_types.h:106
#define R__(String)
Definition fcintl.h:75
struct civ_game game
Definition game.c:62
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * rule_name_get(const struct name_translation *ptrans)
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)
Definition ruledit.cpp:237
void ruledit_qt_display_requirers(const char *msg, void *data)
void strvec_clear(struct strvec *psv)
struct packet_ruleset_control control
Definition game.h:83
enum counter_target target
Definition counters.h:32
int def
Definition counters.h:34
bool ruledit_disabled
Definition counters.h:30
int checkpoint
Definition counters.h:33
int index
Definition counters.h:37
struct name_translation name
Definition counters.h:28
struct strvec * helptext
Definition counters.h:29
enum counter_behaviour type
Definition counters.h:31
Definition climisc.h:82
enum universals_n kind
Definition fc_types.h:902
universals_u value
Definition fc_types.h:901
#define FALSE
Definition support.h:47
#define empty_widget
#define widgets_row(...)
struct counter * counter
Definition fc_types.h:712
bool is_counter_needed(struct counter *pcount, requirers_cb cb, void *data)
Definition validity.c:189