Freeciv-3.1: /fast/freeciv/nightly/S3_1/tools/ruledit/effect_edit.cpp Source File

Freeciv-3.1

Loading...
Searching...
No Matches
effect_edit.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 <QGridLayout>
20#include <QLabel>
21#include <QLineEdit>
22#include <QMenu>
23#include <QPushButton>
24#include <QSpinBox>
25
26// utility
27#include "fcintl.h"
28
29// common
30#include "effects.h"
31
32// ruledit
33#include "ruledit.h"
34#include "ruledit_qt.h"
35#include "validity.h"
36
37#include "effect_edit.h"
38
39#define NO_MULTIPLIER_NAME "No Multiplier"
40
41/**********************************************************************/
44effect_edit::effect_edit(ruledit_gui *ui_in, QString target,
45 struct universal *filter_in,
46 enum effect_filter_main_class efmc_in) : QDialog()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *effect_edit_layout = new QGridLayout();
50 QHBoxLayout *active_layout = new QHBoxLayout();
51 QPushButton *close_button;
52 QPushButton *button;
53 QMenu *menu;
54 QLabel *lbl;
55 enum effect_type eff;
56 int row;
57
58 ui = ui_in;
59 selected = nullptr;
60 if (filter_in == nullptr) {
61 fc_assert(efmc_in != EFMC_NORMAL);
62 filter.kind = VUT_NONE;
63 } else {
64 fc_assert(efmc_in == EFMC_NORMAL);
65 filter = *filter_in;
66 }
67 name = target;
68
69 efmc = efmc_in;
70
71 list_widget = new QListWidget(this);
72 effects = effect_list_new();
73
74 connect(list_widget, SIGNAL(itemSelectionChanged()), this, SLOT(select_effect()));
75 main_layout->addWidget(list_widget);
76
77 lbl = new QLabel(R__("Type:"));
78 active_layout->addWidget(lbl, 0);
79 edit_type_button = new QToolButton(this);
80 menu = new QMenu();
81 edit_type_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
82 edit_type_button->setPopupMode(QToolButton::MenuButtonPopup);
83 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(effect_type_menu(QAction *)));
84 edit_type_button->setMenu(menu);
85 for (eff = (enum effect_type)0; eff < EFT_COUNT;
86 eff = (enum effect_type)(eff + 1)) {
87 menu->addAction(effect_type_name(eff));
88 }
89 active_layout->addWidget(edit_type_button, 1);
90
91 lbl = new QLabel(R__("Value:"));
92 active_layout->addWidget(lbl, 2);
93 value_box = new QSpinBox(this);
94 value_box->setRange(-1000, 1000);
95 active_layout->addWidget(value_box, 3);
96 connect(value_box, SIGNAL(valueChanged(int)), this, SLOT(set_value(int)));
97
98 main_layout->addLayout(active_layout);
99 row = 0;
100
101
102 mp_button = new QToolButton();
103 mp_button->setParent(this);
104 mp_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
105 mp_button->setPopupMode(QToolButton::MenuButtonPopup);
106 menu = new QMenu();
107 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(multiplier_menu(QAction *)));
108 mp_button->setMenu(menu);
109 menu->addAction(NO_MULTIPLIER_NAME); // FIXME: Can't translate, as we compare to "rule_name"
110 multipliers_re_active_iterate(pmul) {
111 menu->addAction(multiplier_rule_name(pmul));
112 } multipliers_re_active_iterate_end;
113 effect_edit_layout->addWidget(mp_button, row++, 0);
114
115 button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
116 connect(button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
117 effect_edit_layout->addWidget(button, row++, 0);
118
119 button = new QPushButton(QString::fromUtf8(R__("Add Effect")), this);
120 connect(button, SIGNAL(pressed()), this, SLOT(add_now()));
121 effect_edit_layout->addWidget(button, row++, 0);
122
123 button = new QPushButton(QString::fromUtf8(R__("Delete Effect")), this);
124 connect(button, SIGNAL(pressed()), this, SLOT(delete_now()));
125 effect_edit_layout->addWidget(button, row++, 0);
126
127 close_button = new QPushButton(QString::fromUtf8(R__("Close")), this);
128 connect(close_button, SIGNAL(pressed()), this, SLOT(close_now()));
129 effect_edit_layout->addWidget(close_button, row++, 0);
130
131 refresh();
132
133 main_layout->addLayout(effect_edit_layout);
134
135 setLayout(main_layout);
136 setWindowTitle(target);
137}
138
139/**********************************************************************/
142effect_edit::~effect_edit()
143{
144 effect_list_destroy(effects);
145}
146
147/**********************************************************************/
150 static bool effect_list_fill_cb(struct effect *peffect, void *data)
151{
152 struct effect_list_fill_data *cbdata = (struct effect_list_fill_data *)data;
153
154 if (cbdata->filter->kind == VUT_NONE) {
155 if (cbdata->efmc == EFMC_NONE) {
156 // Look for empty req lists.
157 if (requirement_vector_size(&peffect->reqs) == 0) {
158 cbdata->edit->add_effect_to_list(peffect, cbdata);
159 }
160 } else {
161 fc_assert(cbdata->efmc == EFMC_ALL);
162 cbdata->edit->add_effect_to_list(peffect, cbdata);
163 }
164 } else if (universal_is_mentioned_by_requirements(&peffect->reqs,
165 cbdata->filter)) {
166 cbdata->edit->add_effect_to_list(peffect, cbdata);
167 }
168
169 return true;
170}
171
172/**********************************************************************/
175 void effect_edit::refresh()
176{
177 struct effect_list_fill_data cb_data;
178
179 list_widget->clear();
180 effect_list_clear(effects);
181 cb_data.filter = &filter;
182 cb_data.efmc = efmc;
183 cb_data.edit = this;
184 cb_data.num = 0;
185
186 iterate_effect_cache(effect_list_fill_cb, &cb_data);
187
188 fill_active();
189}
190
191/**********************************************************************/
194 void effect_edit::add_effect_to_list(struct effect *peffect,
195 struct effect_list_fill_data *fill_data)
196{
197 char buf[512];
198 QListWidgetItem *item;
199
200 fc_snprintf(buf, sizeof(buf), R__("Effect #%d: %s"),
201 fill_data->num + 1, effect_type_name(peffect->type));
202
203 item = new QListWidgetItem(QString::fromUtf8(buf));
204 list_widget->insertItem(fill_data->num++, item);
205 effect_list_append(effects, peffect);
206 if (selected == peffect) {
207 item->setSelected(true);
208 }
209}
210
211/**********************************************************************/
214 struct universal *effect_edit::filter_get()
215{
216 return &filter;
217}
218
219/**********************************************************************/
222 void effect_edit::close_now()
223{
224 ui->unregister_effect_edit(this);
225 done(0);
226}
227
228/**********************************************************************/
231 void effect_edit::select_effect()
232{
233 int i = 0;
234
235 effect_list_iterate(effects, peffect) {
236 QListWidgetItem *item = list_widget->item(i++);
237
238 if (item != nullptr && item->isSelected()) {
239 selected = peffect;
240 selected_nbr = i;
241 fill_active();
242 return;
243 }
244 } effect_list_iterate_end;
245}
246
247/**********************************************************************/
250 void effect_edit::fill_active()
251{
252 if (selected != nullptr) {
253 edit_type_button->setText(effect_type_name(selected->type));
254 value_box->setValue(selected->value);
255 if (selected->multiplier != NULL) {
256 mp_button->setText(multiplier_rule_name(selected->multiplier));
257 } else {
258 mp_button->setText(NO_MULTIPLIER_NAME);
259 }
260 } else {
261 mp_button->setText(NO_MULTIPLIER_NAME);
262 }
263}
264
265/**********************************************************************/
268 void effect_edit::effect_type_menu(QAction *action)
269{
270 QByteArray en_bytes = action->text().toUtf8();
271 enum effect_type type = effect_type_by_name(en_bytes.data(),
272 fc_strcasecmp);
273
274 if (selected != nullptr) {
275 selected->type = type;
276 }
277
278 ui->refresh_effect_edits();
279}
280
281/**********************************************************************/
284 void effect_edit::set_value(int value)
285{
286 if (selected != nullptr) {
287 selected->value = value;
288 }
289
290 ui->refresh_effect_edits();
291}
292
293/**********************************************************************/
296 void effect_edit::edit_reqs()
297{
298 if (selected != nullptr) {
299 char buf[128];
300 QByteArray en_bytes;
301
302 en_bytes = name.toUtf8();
303 fc_snprintf(buf, sizeof(buf), R__("%s effect #%d"), en_bytes.data(),
304 selected_nbr);
305
306 ui->open_req_edit(QString::fromUtf8(buf), &selected->reqs);
307 }
308}
309
310/**********************************************************************/
313 void effect_edit::closeEvent(QCloseEvent *event)
314{
315 ui->unregister_effect_edit(this);
316}
317
318/**********************************************************************/
321 void effect_edit::add_now()
322{
323 struct effect *peffect = effect_new((enum effect_type)0, 0, NULL);
324
325 if (filter.kind != VUT_NONE) {
326 struct requirement req;
327
328 req = req_from_str(universal_type_rule_name(&filter),
329 nullptr, false, true, false,
330 universal_rule_name(&filter));
331 effect_req_append(peffect, req);
332 }
333
334 refresh();
335}
336
337/**********************************************************************/
340 void effect_edit::delete_now()
341{
342 if (selected != nullptr) {
343 effect_remove(selected);
344
345 selected = nullptr;
346
347 refresh();
348 }
349}
350
351/**********************************************************************/
354 void effect_edit::multiplier_menu(QAction *action)
355{
356 QByteArray an_bytes = action->text().toUtf8();
357
358 if (selected == nullptr) {
359 return;
360 }
361
362 if (!fc_strcasecmp(NO_MULTIPLIER_NAME, an_bytes)) {
363 selected->multiplier = NULL;
364 } else {
365 selected->multiplier = multiplier_by_rule_name(an_bytes);
366 }
367
368 refresh();
369}
effect_edit::effects
struct effect_list * effects
Definition effect_edit.h:69
effect_edit::multiplier_menu
void multiplier_menu(QAction *action)
Definition effect_edit.cpp:354
effect_edit::edit_type_button
QToolButton * edit_type_button
Definition effect_edit.h:74
effect_edit::close_now
void close_now()
Definition effect_edit.cpp:222
effect_edit::name
QString name
Definition effect_edit.h:66
effect_edit::add_effect_to_list
void add_effect_to_list(struct effect *peffect, struct effect_list_fill_data *fill_data)
Definition effect_edit.cpp:194
effect_edit::fill_active
void fill_active()
Definition effect_edit.cpp:250
effect_edit::edit_reqs
void edit_reqs()
Definition effect_edit.cpp:296
effect_edit::~effect_edit
~effect_edit()
Definition effect_edit.cpp:142
effect_edit::efmc
enum effect_filter_main_class efmc
Definition effect_edit.h:61
effect_edit::set_value
void set_value(int value)
Definition effect_edit.cpp:284
effect_edit::mp_button
QToolButton * mp_button
Definition effect_edit.h:77
effect_edit::delete_now
void delete_now()
Definition effect_edit.cpp:340
effect_edit::selected_nbr
int selected_nbr
Definition effect_edit.h:72
effect_edit::closeEvent
void closeEvent(QCloseEvent *event)
Definition effect_edit.cpp:313
effect_edit::filter
struct universal filter
Definition effect_edit.h:68
effect_edit::list_widget
QListWidget * list_widget
Definition effect_edit.h:67
effect_edit::filter_get
struct universal * filter_get()
Definition effect_edit.cpp:214
effect_edit::effect_edit
effect_edit(ruledit_gui *ui_in, QString target, struct universal *filter_in, enum effect_filter_main_class efmc_in)
Definition effect_edit.cpp:44
effect_edit::value_box
QSpinBox * value_box
Definition effect_edit.h:75
effect_edit::refresh
void refresh()
Definition effect_edit.cpp:175
effect_edit::ui
ruledit_gui * ui
Definition effect_edit.h:64
effect_edit::effect_type_menu
void effect_type_menu(QAction *action)
Definition effect_edit.cpp:268
effect_edit::add_now
void add_now()
Definition effect_edit.cpp:321
effect_edit::selected
struct effect * selected
Definition effect_edit.h:71
effect_edit::select_effect
void select_effect()
Definition effect_edit.cpp:231
ruledit_gui
Definition ruledit_qt.h:96
ruledit_gui::unregister_effect_edit
void unregister_effect_edit(class effect_edit *e_edit)
Definition ruledit_qt.cpp:429
ruledit_gui::refresh_effect_edits
void refresh_effect_edits()
Definition ruledit_qt.cpp:437
ruledit_gui::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:331
effect_list_fill_cb
static bool effect_list_fill_cb(struct effect *peffect, void *data)
Definition effect_edit.cpp:150
NO_MULTIPLIER_NAME
#define NO_MULTIPLIER_NAME
Definition effect_edit.cpp:39
effect_edit.h
effect_filter_main_class
effect_filter_main_class
Definition effect_edit.h:33
EFMC_ALL
@ EFMC_ALL
Definition effect_edit.h:35
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:33
EFMC_NONE
@ EFMC_NONE
Definition effect_edit.h:34
effect_remove
void effect_remove(struct effect *peffect)
Definition effects.c:205
iterate_effect_cache
bool iterate_effect_cache(iec_cb cb, void *data)
Definition effects.c:1245
effect_new
struct effect * effect_new(enum effect_type type, int value, struct multiplier *pmul)
Definition effects.c:179
effect_req_append
void effect_req_append(struct effect *peffect, struct requirement req)
Definition effects.c:229
effects.h
effect_list_iterate_end
#define effect_list_iterate_end
Definition effects.h:375
effect_list_iterate
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:373
event
enum event_type event
Definition events.c:81
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
type
GType type
Definition repodlgs.c:1312
fc_assert
#define fc_assert(condition)
Definition log.h:176
multiplier_rule_name
const char * multiplier_rule_name(const struct multiplier *pmul)
Definition multipliers.c:106
multiplier_by_rule_name
struct multiplier * multiplier_by_rule_name(const char *name)
Definition multipliers.c:115
multipliers_re_active_iterate
#define multipliers_re_active_iterate(_mul_)
Definition multipliers.h:71
multipliers_re_active_iterate_end
#define multipliers_re_active_iterate_end
Definition multipliers.h:75
req_from_str
struct requirement req_from_str(const char *type, const char *range, bool survives, bool present, bool quiet, const char *value)
Definition requirements.c:938
universal_rule_name
const char * universal_rule_name(const struct universal *psource)
Definition requirements.c:5392
universal_type_rule_name
const char * universal_type_rule_name(const struct universal *psource)
Definition requirements.c:5811
universal_is_mentioned_by_requirements
bool universal_is_mentioned_by_requirements(const struct requirement_vector *reqs, const struct universal *psource)
Definition requirements.c:5858
ruledit.h
ruledit_qt.h
action
Definition actions.h:379
effect_list_fill_data
Definition effect_edit.h:39
effect_list_fill_data::num
int num
Definition effect_edit.h:43
effect_list_fill_data::filter
struct universal * filter
Definition effect_edit.h:40
effect_list_fill_data::efmc
enum effect_filter_main_class efmc
Definition effect_edit.h:41
effect_list_fill_data::edit
class effect_edit * edit
Definition effect_edit.h:42
effect
Definition effects.h:340
effect::value
int value
Definition effects.h:349
effect::multiplier
struct multiplier * multiplier
Definition effects.h:344
effect::reqs
struct requirement_vector reqs
Definition effects.h:353
effect::type
enum effect_type type
Definition effects.h:341
item
Definition climisc.h:80
item::item
struct universal item
Definition climisc.h:81
requirement
Definition requirements.h:80
universal
Definition fc_types.h:756
universal::kind
enum universals_n kind
Definition fc_types.h:758
universal::value
universals_u value
Definition fc_types.h:757
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
validity.h