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

Freeciv-3.3

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#include "multipliers.h"
32
33// ruledit
34#include "ruledit.h"
35#include "ruledit_qt.h"
36#include "validity.h"
37
38#include "effect_edit.h"
39
40#define NO_MULTIPLIER_NAME "No Multiplier"
41
42/**********************************************************************/
45effect_edit::effect_edit(ruledit_gui *ui_in, QString target,
46 struct universal *filter_in,
47 enum effect_filter_main_class efmc_in) : QDialog()
48{
49 QVBoxLayout *main_layout = new QVBoxLayout(this);
50 QGridLayout *effect_edit_layout = new QGridLayout();
51 QHBoxLayout *active_layout = new QHBoxLayout();
52 QPushButton *close_button;
53 QPushButton *button;
54 QMenu *menu;
55 QLabel *lbl;
56 enum effect_type eff;
57 int row;
58
59 ui = ui_in;
60 selected = nullptr;
61 if (filter_in == nullptr) {
62 fc_assert(efmc_in != EFMC_NORMAL);
63 filter.kind = VUT_NONE;
64 } else {
65 fc_assert(efmc_in == EFMC_NORMAL);
66 filter = *filter_in;
67 }
68 name = target;
69
70 efmc = efmc_in;
71
72 list_widget = new QListWidget(this);
73 effects = effect_list_new();
74
75 connect(list_widget, SIGNAL(itemSelectionChanged()), this, SLOT(select_effect()));
76 main_layout->addWidget(list_widget);
77
78 lbl = new QLabel(R__("Type:"));
79 active_layout->addWidget(lbl, 0);
80 edit_type_button = new QToolButton(this);
81 menu = new QMenu();
82 edit_type_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
83 edit_type_button->setPopupMode(QToolButton::MenuButtonPopup);
84 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(effect_type_menu(QAction *)));
85 edit_type_button->setMenu(menu);
86 for (eff = (enum effect_type)0; eff < EFT_COUNT;
87 eff = (enum effect_type)(eff + 1)) {
88 menu->addAction(effect_type_name(eff));
89 }
90 active_layout->addWidget(edit_type_button, 1);
91
92 lbl = new QLabel(R__("Value:"));
93 active_layout->addWidget(lbl, 2);
94 value_box = new QSpinBox(this);
95 value_box->setRange(-1000, 1000);
96 active_layout->addWidget(value_box, 3);
97 connect(value_box, SIGNAL(valueChanged(int)), this, SLOT(set_value(int)));
98
99 main_layout->addLayout(active_layout);
100 row = 0;
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, 1, 2);
114
115 lbl = new QLabel(QString::fromUtf8(R__("Comment")));
116 effect_edit_layout->addWidget(lbl, row, 0);
117 comment = new QLineEdit(this);
118 connect(comment, SIGNAL(returnPressed()), this, SLOT(comment_given()));
119 effect_edit_layout->addWidget(comment, row++, 1);
120
121 button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
122 connect(button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
123 effect_edit_layout->addWidget(button, row++, 0, 1, 2);
124
125 button = new QPushButton(QString::fromUtf8(R__("Add Effect")), this);
126 connect(button, SIGNAL(pressed()), this, SLOT(add_now()));
127 effect_edit_layout->addWidget(button, row++, 0, 1, 2);
128
129 button = new QPushButton(QString::fromUtf8(R__("Delete Effect")), this);
130 connect(button, SIGNAL(pressed()), this, SLOT(delete_now()));
131 effect_edit_layout->addWidget(button, row++, 0, 1, 2);
132
133 close_button = new QPushButton(QString::fromUtf8(R__("Close")), this);
134 connect(close_button, SIGNAL(pressed()), this, SLOT(close_now()));
135 effect_edit_layout->addWidget(close_button, row++, 0, 1, 2);
136
137 refresh();
138
139 main_layout->addLayout(effect_edit_layout);
140
141 setLayout(main_layout);
142 setWindowTitle(target);
143}
144
145/**********************************************************************/
148effect_edit::~effect_edit()
149{
150 effect_list_destroy(effects);
151}
152
153/**********************************************************************/
156 static bool effect_list_fill_cb(struct effect *peffect, void *data)
157{
158 struct effect_list_fill_data *cbdata = (struct effect_list_fill_data *)data;
159
160 if (cbdata->filter->kind == VUT_NONE) {
161 if (cbdata->efmc == EFMC_NONE) {
162 // Look for empty req lists.
163 if (requirement_vector_size(&peffect->reqs) == 0) {
164 cbdata->edit->add_effect_to_list(peffect, cbdata);
165 }
166 } else {
167 fc_assert(cbdata->efmc == EFMC_ALL);
168 cbdata->edit->add_effect_to_list(peffect, cbdata);
169 }
170 } else if (universal_is_mentioned_by_requirements(&peffect->reqs,
171 cbdata->filter)) {
172 cbdata->edit->add_effect_to_list(peffect, cbdata);
173 }
174
175 return true;
176}
177
178/**********************************************************************/
181 void effect_edit::refresh()
182{
183 struct effect_list_fill_data cb_data;
184
185 list_widget->clear();
186 effect_list_clear(effects);
187 cb_data.filter = &filter;
188 cb_data.efmc = efmc;
189 cb_data.edit = this;
190 cb_data.num = 0;
191
192 iterate_effect_cache(effect_list_fill_cb, &cb_data);
193
194 fill_active();
195}
196
197/**********************************************************************/
200 void effect_edit::add_effect_to_list(struct effect *peffect,
201 struct effect_list_fill_data *fill_data)
202{
203 char buf[512];
204 QListWidgetItem *item;
205
206 fc_snprintf(buf, sizeof(buf), R__("Effect #%d: %s"),
207 fill_data->num + 1, effect_type_name(peffect->type));
208
209 item = new QListWidgetItem(QString::fromUtf8(buf));
210 list_widget->insertItem(fill_data->num++, item);
211 effect_list_append(effects, peffect);
212 if (selected == peffect) {
213 item->setSelected(true);
214 }
215}
216
217/**********************************************************************/
220 struct universal *effect_edit::filter_get()
221{
222 return &filter;
223}
224
225/**********************************************************************/
228 void effect_edit::close_now()
229{
230 ui->unregister_effect_edit(this);
231 done(0);
232}
233
234/**********************************************************************/
237 void effect_edit::select_effect()
238{
239 int i = 0;
240
241 effect_list_iterate(effects, peffect) {
242 QListWidgetItem *item = list_widget->item(i++);
243
244 if (item != nullptr && item->isSelected()) {
245 selected = peffect;
246 selected_nbr = i;
247 fill_active();
248 return;
249 }
250 } effect_list_iterate_end;
251}
252
253/**********************************************************************/
256 void effect_edit::fill_active()
257{
258 if (selected != nullptr) {
259 edit_type_button->setText(effect_type_name(selected->type));
260 value_box->setValue(selected->value);
261 if (selected->multiplier != nullptr) {
262 mp_button->setText(multiplier_rule_name(selected->multiplier));
263 } else {
264 mp_button->setText(NO_MULTIPLIER_NAME);
265 }
266
267 if (selected->rulesave.comment == nullptr) {
268 comment->setText("");
269 } else {
270 comment->setText(selected->rulesave.comment);
271 }
272 } else {
273 mp_button->setText(NO_MULTIPLIER_NAME);
274 }
275}
276
277/**********************************************************************/
280 void effect_edit::effect_type_menu(QAction *action)
281{
282 QByteArray en_bytes = action->text().toUtf8();
283 enum effect_type type = effect_type_by_name(en_bytes.data(),
284 fc_strcasecmp);
285
286 if (selected != nullptr) {
287 selected->type = type;
288 }
289
290 ui->refresh_effect_edits();
291}
292
293/**********************************************************************/
296 void effect_edit::set_value(int value)
297{
298 if (selected != nullptr) {
299 selected->value = value;
300 }
301
302 ui->refresh_effect_edits();
303}
304
305/**********************************************************************/
308 void effect_edit::edit_reqs()
309{
310 if (selected != nullptr) {
311 char buf[128];
312 QByteArray en_bytes;
313
314 en_bytes = name.toUtf8();
315 fc_snprintf(buf, sizeof(buf), R__("%s effect #%d"), en_bytes.data(),
316 selected_nbr);
317
318 ui->open_req_edit(QString::fromUtf8(buf), &selected->reqs);
319 }
320}
321
322/**********************************************************************/
325 void effect_edit::closeEvent(QCloseEvent *event)
326{
327 ui->unregister_effect_edit(this);
328}
329
330/**********************************************************************/
333 void effect_edit::add_now()
334{
335 struct effect *peffect = effect_new((enum effect_type)0, 0, nullptr);
336
337 if (filter.kind != VUT_NONE) {
338 struct requirement req;
339
340 req = req_from_str(universal_type_rule_name(&filter),
341 nullptr, false, true, false,
342 universal_rule_name(&filter));
343 effect_req_append(peffect, req);
344 }
345
346 refresh();
347}
348
349/**********************************************************************/
352 void effect_edit::delete_now()
353{
354 if (selected != nullptr) {
355 effect_remove(selected);
356
357 selected = nullptr;
358
359 refresh();
360 }
361}
362
363/**********************************************************************/
366 void effect_edit::multiplier_menu(QAction *action)
367{
368 QByteArray an_bytes = action->text().toUtf8();
369
370 if (selected == nullptr) {
371 return;
372 }
373
374 if (!fc_strcasecmp(NO_MULTIPLIER_NAME, an_bytes)) {
375 selected->multiplier = nullptr;
376 } else {
377 selected->multiplier = multiplier_by_rule_name(an_bytes);
378 }
379
380 refresh();
381}
382
383/**********************************************************************/
386 void effect_edit::comment_given()
387{
388 if (selected != nullptr) {
389 if (selected->rulesave.comment != nullptr) {
390 free(selected->rulesave.comment);
391 }
392
393 if (!comment->text().isEmpty()) {
394 selected->rulesave.comment = fc_strdup(comment->text().toUtf8());
395 } else {
396 selected->rulesave.comment = nullptr;
397 }
398
399 fill_active();
400 }
401}
effect_edit::effects
struct effect_list * effects
Definition effect_edit.h:70
effect_edit::multiplier_menu
void multiplier_menu(QAction *action)
Definition effect_edit.cpp:366
effect_edit::edit_type_button
QToolButton * edit_type_button
Definition effect_edit.h:75
effect_edit::close_now
void close_now()
Definition effect_edit.cpp:228
effect_edit::name
QString name
Definition effect_edit.h:67
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:200
effect_edit::fill_active
void fill_active()
Definition effect_edit.cpp:256
effect_edit::edit_reqs
void edit_reqs()
Definition effect_edit.cpp:308
effect_edit::~effect_edit
~effect_edit()
Definition effect_edit.cpp:148
effect_edit::efmc
enum effect_filter_main_class efmc
Definition effect_edit.h:62
effect_edit::set_value
void set_value(int value)
Definition effect_edit.cpp:296
effect_edit::mp_button
QToolButton * mp_button
Definition effect_edit.h:78
effect_edit::delete_now
void delete_now()
Definition effect_edit.cpp:352
effect_edit::comment_given
void comment_given()
Definition effect_edit.cpp:386
effect_edit::selected_nbr
int selected_nbr
Definition effect_edit.h:73
effect_edit::closeEvent
void closeEvent(QCloseEvent *event)
Definition effect_edit.cpp:325
effect_edit::filter
struct universal filter
Definition effect_edit.h:69
effect_edit::list_widget
QListWidget * list_widget
Definition effect_edit.h:68
effect_edit::filter_get
struct universal * filter_get()
Definition effect_edit.cpp:220
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:45
effect_edit::value_box
QSpinBox * value_box
Definition effect_edit.h:76
effect_edit::refresh
void refresh()
Definition effect_edit.cpp:181
effect_edit::ui
ruledit_gui * ui
Definition effect_edit.h:65
effect_edit::effect_type_menu
void effect_type_menu(QAction *action)
Definition effect_edit.cpp:280
effect_edit::comment
QLineEdit * comment
Definition effect_edit.h:79
effect_edit::add_now
void add_now()
Definition effect_edit.cpp:333
effect_edit::selected
struct effect * selected
Definition effect_edit.h:72
effect_edit::select_effect
void select_effect()
Definition effect_edit.cpp:237
ruledit_gui
Definition ruledit_qt.h:97
ruledit_gui::unregister_effect_edit
void unregister_effect_edit(class effect_edit *e_edit)
Definition ruledit_qt.cpp:433
ruledit_gui::refresh_effect_edits
void refresh_effect_edits()
Definition ruledit_qt.cpp:441
ruledit_gui::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:335
incite_cost
char * incite_cost
Definition comments.c:74
effect_list_fill_cb
static bool effect_list_fill_cb(struct effect *peffect, void *data)
Definition effect_edit.cpp:156
NO_MULTIPLIER_NAME
#define NO_MULTIPLIER_NAME
Definition effect_edit.cpp:40
effect_edit.h
effect_filter_main_class
effect_filter_main_class
Definition effect_edit.h:34
EFMC_ALL
@ EFMC_ALL
Definition effect_edit.h:36
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:34
EFMC_NONE
@ EFMC_NONE
Definition effect_edit.h:35
effect_remove
void effect_remove(struct effect *peffect)
Definition effects.c:230
iterate_effect_cache
bool iterate_effect_cache(iec_cb cb, void *data)
Definition effects.c:1323
effect_new
struct effect * effect_new(enum effect_type type, int value, struct multiplier *pmul)
Definition effects.c:186
effect_req_append
void effect_req_append(struct effect *peffect, struct requirement req)
Definition effects.c:266
effects.h
effect_list_iterate_end
#define effect_list_iterate_end
Definition effects.h:81
effect_list_iterate
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:79
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:1313
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
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.h
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:1048
universal_rule_name
const char * universal_rule_name(const struct universal *psource)
Definition requirements.c:6800
universal_type_rule_name
const char * universal_type_rule_name(const struct universal *psource)
Definition requirements.c:7313
universal_is_mentioned_by_requirements
bool universal_is_mentioned_by_requirements(const struct requirement_vector *reqs, const struct universal *psource)
Definition requirements.c:7360
ruledit.h
ruledit_qt.h
action
Definition actions.h:106
effect_list_fill_data
Definition effect_edit.h:40
effect_list_fill_data::num
int num
Definition effect_edit.h:44
effect_list_fill_data::edit
class effect_edit * edit
Definition effect_edit.h:43
effect
Definition effects.h:40
effect::value
int value
Definition effects.h:49
effect::comment
char * comment
Definition effects.h:62
effect::multiplier
struct multiplier * multiplier
Definition effects.h:44
effect::rulesave
struct effect::@23 rulesave
effect::reqs
struct requirement_vector reqs
Definition effects.h:53
effect::type
enum effect_type type
Definition effects.h:41
item
Definition climisc.h:80
item::item
struct universal item
Definition climisc.h:81
requirement
Definition requirements.h:81
universal
Definition fc_types.h:908
universal::kind
enum universals_n kind
Definition fc_types.h:910
universal::value
universals_u value
Definition fc_types.h:909
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
validity.h