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/**********************************************************************/
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"
111 menu->addAction(multiplier_rule_name(pmul));
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/**********************************************************************/
143{
144 effect_list_destroy(effects);
145}
146
147/**********************************************************************/
150static 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/**********************************************************************/
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
187
188 fill_active();
189}
190
191/**********************************************************************/
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/**********************************************************************/
215{
216 return &filter;
217}
218
219/**********************************************************************/
223{
225 done(0);
226}
227
228/**********************************************************************/
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 }
245}
246
247/**********************************************************************/
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) {
257 } else {
259 }
260 } else {
262 }
263}
264
265/**********************************************************************/
269{
270 QByteArray en_bytes = action->text().toUtf8();
271 enum effect_type type = effect_type_by_name(en_bytes.data(),
273
274 if (selected != nullptr) {
275 selected->type = type;
276 }
277
279}
280
281/**********************************************************************/
285{
286 if (selected != nullptr) {
287 selected->value = value;
288 }
289
291}
292
293/**********************************************************************/
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(),
305
306 ui->open_req_edit(QString::fromUtf8(buf), &selected->reqs);
307 }
308}
309
310/**********************************************************************/
314{
316}
317
318/**********************************************************************/
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
329 nullptr, false, true, false,
331 effect_req_append(peffect, req);
332 }
333
334 refresh();
335}
336
337/**********************************************************************/
341{
342 if (selected != nullptr) {
344
345 selected = nullptr;
346
347 refresh();
348 }
349}
350
351/**********************************************************************/
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 {
366 }
367
368 refresh();
369}
struct effect_list * effects
Definition effect_edit.h:69
void multiplier_menu(QAction *action)
QToolButton * edit_type_button
Definition effect_edit.h:74
QString name
Definition effect_edit.h:66
void add_effect_to_list(struct effect *peffect, struct effect_list_fill_data *fill_data)
void fill_active()
enum effect_filter_main_class efmc
Definition effect_edit.h:61
void set_value(int value)
QToolButton * mp_button
Definition effect_edit.h:77
void delete_now()
void closeEvent(QCloseEvent *event)
struct universal filter
Definition effect_edit.h:68
QListWidget * list_widget
Definition effect_edit.h:67
struct universal * filter_get()
effect_edit(ruledit_gui *ui_in, QString target, struct universal *filter_in, enum effect_filter_main_class efmc_in)
QSpinBox * value_box
Definition effect_edit.h:75
ruledit_gui * ui
Definition effect_edit.h:64
void effect_type_menu(QAction *action)
struct effect * selected
Definition effect_edit.h:71
void select_effect()
void unregister_effect_edit(class effect_edit *e_edit)
void refresh_effect_edits()
void open_req_edit(QString target, struct requirement_vector *preqs)
static bool effect_list_fill_cb(struct effect *peffect, void *data)
#define NO_MULTIPLIER_NAME
effect_filter_main_class
Definition effect_edit.h:33
@ EFMC_ALL
Definition effect_edit.h:35
@ EFMC_NORMAL
Definition effect_edit.h:33
@ EFMC_NONE
Definition effect_edit.h:34
void effect_remove(struct effect *peffect)
Definition effects.c:205
bool iterate_effect_cache(iec_cb cb, void *data)
Definition effects.c:1245
struct effect * effect_new(enum effect_type type, int value, struct multiplier *pmul)
Definition effects.c:179
void effect_req_append(struct effect *peffect, struct requirement req)
Definition effects.c:229
#define effect_list_iterate_end
Definition effects.h:375
#define effect_list_iterate(effect_list, peffect)
Definition effects.h:373
enum event_type event
Definition events.c:81
#define R__(String)
Definition fcintl.h:75
GType type
Definition repodlgs.c:1312
#define fc_assert(condition)
Definition log.h:176
const char * multiplier_rule_name(const struct multiplier *pmul)
struct multiplier * multiplier_by_rule_name(const char *name)
#define multipliers_re_active_iterate(_mul_)
Definition multipliers.h:71
#define multipliers_re_active_iterate_end
Definition multipliers.h:75
struct requirement req_from_str(const char *type, const char *range, bool survives, bool present, bool quiet, const char *value)
const char * universal_rule_name(const struct universal *psource)
const char * universal_type_rule_name(const struct universal *psource)
bool universal_is_mentioned_by_requirements(const struct requirement_vector *reqs, const struct universal *psource)
struct universal * filter
Definition effect_edit.h:40
enum effect_filter_main_class efmc
Definition effect_edit.h:41
class effect_edit * edit
Definition effect_edit.h:42
int value
Definition effects.h:349
struct multiplier * multiplier
Definition effects.h:344
struct requirement_vector reqs
Definition effects.h:353
enum effect_type type
Definition effects.h:341
Definition climisc.h:82
struct universal item
Definition climisc.h:83
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189