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/**********************************************************************/
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_behavior current;
119 type = new QComboBox(this);
120
121 label = new QLabel(QString::fromUtf8(R__("Behavior")));
122 label->setParent(this);
123
124 for (current = counter_behavior_begin(); current > counter_behavior_end();
125 current = counter_behavior_next(current)) {
126 QVariant value(current);
127
128 type->addItem(counter_behavior_name(current), value);
129 };
130
131 connect(type, SIGNAL(activated(int)), this, SLOT(counter_behavior_selected(int)));
133 }
134
135 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
136 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
138
139 add_button = new QPushButton(QString::fromUtf8(R__("Add Counter")), this);
140 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
141
142 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Counter")), this);
143 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
144
147 show_experimental(delete_button);
148
149 refresh();
150 update_counter_info(nullptr);
151
152 main_layout->addLayout(counter_layout);
153
154 setLayout(main_layout);
155#undef empty_widget
156#undef widgets_row
157}
158
159/**********************************************************************/
163{
165
166 if (nullptr == selected) {
167
168 return;
169 }
170 item_data = type->currentData();
171 selected->type = (enum counter_behavior) item_data.toInt();
172
174 refresh();
175}
176
177/**********************************************************************/
190
191/**********************************************************************/
195{
197
198 if (selected != nullptr) {
199 QString dispn = QString::fromUtf8(untranslated_name(&(counter->name)));
200 QString rulen = QString::fromUtf8(rule_name_get(&(counter->name)));
201
202 name->setText(dispn);
203 rname->setText(rulen);
204
205 if (dispn == rulen) {
206 name->setEnabled(false);
207 } else {
208 name->setEnabled(true);
209 }
210
211 checkpoint->setValue(selected->checkpoint);
212 def->setValue(selected->def);
213 } else {
214 name->setText(R__("None"));
215 rname->setText(R__("None"));
216 name->setEnabled(false);
217 }
218}
219
220/**********************************************************************/
224{
225 char *cname;
226
228
229 if (!select_list.isEmpty()) {
231
232 tn_bytes = select_list.at(0)->text().toUtf8();
233
234 cname = tn_bytes.data();
236 }
237}
238
239
240/**********************************************************************/
244{
245 if (nullptr != selected) {
246
247 selected->checkpoint = val;
248 }
249}
250
251/**********************************************************************/
255{
256 if (nullptr != selected) {
257
258 selected->def = val;
259 }
260}
261
262/**********************************************************************/
266{
267 if (selected != nullptr) {
270
272 if (pcounter != selected) {
273 rname_bytes = rname->text().toUtf8();
275 ui->display_msg(R__("A counter with that rule name already exists!"));
276 return;
277 }
278 }
280
281 name_bytes = name->text().toUtf8();
282 rname_bytes = rname->text().toUtf8();
283 names_set(&(selected->name), 0,
284 name_bytes.data(),
285 rname_bytes.data());
286 refresh();
287 }
288}
289
290/**********************************************************************/
294{
295
296 if (nullptr != selected) {
298
301 return;
302 }
303
305
306 refresh();
307 update_counter_info(nullptr);
308 }
309}
310
311/**********************************************************************/
315{
316 counter->checkpoint = 0;
317 counter->def = 0;
322
323 name_set(&counter->name, 0, "New counter");
324 if (counter->helptext != nullptr) {
326 }
327
328 return true;
329}
330
331/**********************************************************************/
335{
336 struct counter *new_counter;
337
338 // Try to reuse freed counter slot
340 if (pcount->type == COUNTER_BEHAVIOR_LAST
341 || pcount->ruledit_disabled) {
344 refresh();
345 }
346 return;
347 }
349
350 // Try to add completely new counter
352 return;
353 }
354
355 // num_counter_types must be big enough to hold new counter or
356 // counter_by_number() fails.
362 refresh();
363 }
364}
365
366/**********************************************************************/
370{
371 name->setEnabled(!checked);
372 if (checked) {
373 name->setText(rname->text());
374 }
375}
376
377/**********************************************************************/
381{
382 if (selected != nullptr) {
383 struct universal uni;
384
385 uni.value.counter = selected;
386 uni.kind = VUT_COUNTER;
387
388 ui->open_effect_edit(QString::fromUtf8(counter_rule_name(selected)),
389 &uni, EFMC_NORMAL);
390 }
391}
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)
void counter_behavior_selected(int item)
struct counter * selected
QPushButton * effects_button
ruledit_gui * ui
QComboBox * type
void select_counter()
QCheckBox * same_name
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:76
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:61
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
enum counter_behavior type
Definition counters.h:31
struct strvec * helptext
Definition counters.h:29
Definition climisc.h:82
enum universals_n kind
Definition fc_types.h:608
universals_u value
Definition fc_types.h:607
#define FALSE
Definition support.h:47
#define empty_widget
#define widgets_row(...)
struct counter * counter
Definition fc_types.h:544
bool is_counter_needed(struct counter *pcount, requirers_cb cb, void *data)
Definition validity.c:189