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

Freeciv-3.1

Loading...
Searching...
No Matches
tab_extras.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 <QListWidget>
23#include <QMenu>
24#include <QPushButton>
25#include <QToolButton>
26
27// utility
28#include "fcintl.h"
29#include "log.h"
30
31// common
32#include "game.h"
33#include "extras.h"
34
35// ruledit
36#include "edit_extra.h"
37#include "ruledit.h"
38#include "ruledit_qt.h"
39#include "validity.h"
40
41#include "tab_extras.h"
42
43/**********************************************************************/
46tab_extras::tab_extras(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *extra_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *effects_button;
52 QPushButton *add_button;
53 QPushButton *delete_button;
54 QPushButton *reqs_button;
55 QPushButton *edit_button;
56 int row = 0;
57
58 ui = ui_in;
59 selected = 0;
60
61 extra_list = new QListWidget(this);
62
63 connect(extra_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_extra()));
64 main_layout->addWidget(extra_list);
65
66 extra_layout->setSizeConstraint(QLayout::SetMaximumSize);
67
68 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
69 label->setParent(this);
70 rname = new QLineEdit(this);
71 rname->setText(R__("None"));
72 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
73 extra_layout->addWidget(label, row, 0);
74 extra_layout->addWidget(rname, row++, 2);
75
76 label = new QLabel(QString::fromUtf8(R__("Name")));
77 label->setParent(this);
78 same_name = new QCheckBox();
79 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
80 name = new QLineEdit(this);
81 name->setText(R__("None"));
82 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
83 extra_layout->addWidget(label, row, 0);
84 extra_layout->addWidget(same_name, row, 1);
85 extra_layout->addWidget(name, row++, 2);
86
87 edit_button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
88 connect(edit_button, SIGNAL(pressed()), this, SLOT(edit_now()));
89 extra_layout->addWidget(edit_button, row++, 2);
90
91 reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
92 connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
93 extra_layout->addWidget(reqs_button, row++, 2);
94
95 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
96 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
97 extra_layout->addWidget(effects_button, row++, 2);
98
99 add_button = new QPushButton(QString::fromUtf8(R__("Add Extra")), this);
100 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
101 extra_layout->addWidget(add_button, row, 0);
102 show_experimental(add_button);
103
104 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Extra")), this);
105 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
106 extra_layout->addWidget(delete_button, row++, 2);
107 show_experimental(delete_button);
108
109 refresh();
110 update_extra_info(nullptr);
111
112 main_layout->addLayout(extra_layout);
113
114 setLayout(main_layout);
115}
116
117/**********************************************************************/
120 void tab_extras::refresh()
121{
122 extra_list->clear();
123
124 extra_type_re_active_iterate(pextra) {
125 QListWidgetItem *item
126 = new QListWidgetItem(QString::fromUtf8(extra_rule_name(pextra)));
127
128 extra_list->insertItem(extra_index(pextra), item);
129 } extra_type_re_active_iterate_end;
130}
131
132/**********************************************************************/
135 void tab_extras::update_extra_info(struct extra_type *pextra)
136{
137 selected = pextra;
138
139 if (selected != nullptr) {
140 QString dispn = QString::fromUtf8(untranslated_name(&(pextra->name)));
141 QString rulen = QString::fromUtf8(extra_rule_name(pextra));
142
143 name->setText(dispn);
144 rname->setText(rulen);
145 if (dispn == rulen) {
146 name->setEnabled(false);
147 same_name->setChecked(true);
148 } else {
149 same_name->setChecked(false);
150 name->setEnabled(true);
151 }
152 } else {
153 name->setText(R__("None"));
154 rname->setText(R__("None"));
155 same_name->setChecked(true);
156 name->setEnabled(false);
157 }
158}
159
160/**********************************************************************/
163 void tab_extras::select_extra()
164{
165 QList<QListWidgetItem *> select_list = extra_list->selectedItems();
166
167 if (!select_list.isEmpty()) {
168 QByteArray en_bytes;
169
170 en_bytes = select_list.at(0)->text().toUtf8();
171 update_extra_info(extra_type_by_rule_name(en_bytes.data()));
172 }
173}
174
175/**********************************************************************/
178 void tab_extras::name_given()
179{
180 if (selected != nullptr) {
181 QByteArray name_bytes;
182 QByteArray rname_bytes;
183
184 extra_type_iterate(pextra) {
185 if (pextra != selected && !pextra->ruledit_disabled) {
186 rname_bytes = rname->text().toUtf8();
187 if (!strcmp(extra_rule_name(pextra), rname_bytes.data())) {
188 ui->display_msg(R__("An extra with that rule name already exists!"));
189 return;
190 }
191 }
192 } extra_type_iterate_end;
193
194 if (same_name->isChecked()) {
195 name->setText(rname->text());
196 }
197
198 name_bytes = name->text().toUtf8();
199 rname_bytes = rname->text().toUtf8();
200 names_set(&(selected->name), 0,
201 name_bytes.data(),
202 rname_bytes.data());
203 refresh();
204 }
205}
206
207/**********************************************************************/
210 void tab_extras::delete_now()
211{
212 if (selected != nullptr) {
213 requirers_dlg *requirers;
214
215 requirers = ui->create_requirers(extra_rule_name(selected));
216 if (is_extra_needed(selected, &ruledit_qt_display_requirers, requirers)) {
217 return;
218 }
219
220 selected->ruledit_disabled = true;
221
222 if (selected->ruledit_dlg != nullptr) {
223 ((edit_extra *)selected->ruledit_dlg)->done(0);
224 }
225
226 refresh();
227 update_extra_info(nullptr);
228 }
229}
230
231/**********************************************************************/
234 bool tab_extras::initialize_new_extra(struct extra_type *pextra)
235{
236 if (extra_type_by_rule_name("New Extra") != nullptr) {
237 return false;
238 }
239
240 name_set(&(pextra->name), 0, "New Extra");
241 BV_CLR_ALL(pextra->flags);
242
243 return true;
244}
245
246/**********************************************************************/
249 void tab_extras::add_now()
250{
251 struct extra_type *new_extra;
252
253 // Try to reuse freed extra slot
254 extra_type_iterate(pextra) {
255 if (pextra->ruledit_disabled) {
256 if (initialize_new_extra(pextra)) {
257 pextra->ruledit_disabled = false;
258 update_extra_info(pextra);
259 refresh();
260 }
261 return;
262 }
263 } extra_type_iterate_end;
264
265 // Try to add completely new extra
266 if (game.control.num_extra_types >= MAX_EXTRA_TYPES) {
267 return;
268 }
269
270 // num_extra_types must be big enough to hold new extra or
271 // extra_by_number() fails.
272 game.control.num_extra_types++;
273 new_extra = extra_by_number(game.control.num_extra_types - 1);
274 if (initialize_new_extra(new_extra)) {
275 update_extra_info(new_extra);
276
277 refresh();
278 } else {
279 game.control.num_extra_types--; // Restore
280 }
281}
282
283/**********************************************************************/
286 void tab_extras::same_name_toggle(bool checked)
287{
288 name->setEnabled(!checked);
289 if (checked) {
290 name->setText(rname->text());
291 }
292}
293
294/**********************************************************************/
297 void tab_extras::edit_reqs()
298{
299 if (selected != nullptr) {
300 ui->open_req_edit(QString::fromUtf8(extra_rule_name(selected)),
301 &selected->reqs);
302 }
303}
304
305/**********************************************************************/
308 void tab_extras::edit_effects()
309{
310 if (selected != nullptr) {
311 struct universal uni;
312
313 uni.value.extra = selected;
314 uni.kind = VUT_EXTRA;
315
316 ui->open_effect_edit(QString::fromUtf8(extra_rule_name(selected)),
317 &uni, EFMC_NORMAL);
318 }
319}
320
321/**********************************************************************/
324 void tab_extras::edit_now()
325{
326 if (selected != nullptr) {
327 if (selected->ruledit_dlg == nullptr) {
328 edit_extra *edit = new edit_extra(ui, selected);
329
330 edit->show();
331 selected->ruledit_dlg = edit;
332 } else {
333 ((edit_extra *)selected->ruledit_dlg)->raise();
334 }
335 }
336}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
edit_extra
Definition edit_extra.h:31
requirers_dlg
Definition requirers_dlg.h:28
ruledit_gui
Definition ruledit_qt.h:96
ruledit_gui::display_msg
void display_msg(const char *msg)
Definition ruledit_qt.cpp:289
ruledit_gui::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:331
ruledit_gui::create_requirers
requirers_dlg * create_requirers(const char *title)
Definition ruledit_qt.cpp:297
ruledit_gui::open_effect_edit
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
Definition ruledit_qt.cpp:400
tab_extras::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_extras.cpp:286
tab_extras::add_now
void add_now()
Definition tab_extras.cpp:249
tab_extras::update_extra_info
void update_extra_info(struct extra_type *pextra)
Definition tab_extras.cpp:135
tab_extras::extra_list
QListWidget * extra_list
Definition tab_extras.h:45
tab_extras::same_name
QCheckBox * same_name
Definition tab_extras.h:46
tab_extras::name_given
void name_given()
Definition tab_extras.cpp:178
tab_extras::tab_extras
tab_extras(ruledit_gui *ui_in)
Definition tab_extras.cpp:46
tab_extras::name
QLineEdit * name
Definition tab_extras.h:43
tab_extras::edit_now
void edit_now()
Definition tab_extras.cpp:324
tab_extras::rname
QLineEdit * rname
Definition tab_extras.h:44
tab_extras::refresh
void refresh()
Definition tab_extras.cpp:120
tab_extras::delete_now
void delete_now()
Definition tab_extras.cpp:210
tab_extras::selected
struct extra_type * selected
Definition tab_extras.h:48
tab_extras::edit_effects
void edit_effects()
Definition tab_extras.cpp:308
tab_extras::ui
ruledit_gui * ui
Definition tab_extras.h:39
tab_extras::edit_reqs
void edit_reqs()
Definition tab_extras.cpp:297
tab_extras::initialize_new_extra
bool initialize_new_extra(struct extra_type *pextra)
Definition tab_extras.cpp:234
tab_extras::select_extra
void select_extra()
Definition tab_extras.cpp:163
edit_extra.h
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:33
extra_type_by_rule_name
struct extra_type * extra_type_by_rule_name(const char *name)
Definition extras.c:204
extra_by_number
struct extra_type * extra_by_number(int id)
Definition extras.c:175
extra_rule_name
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:195
extras.h
extra_type_iterate
#define extra_type_iterate(_p)
Definition extras.h:291
extra_type_iterate_end
#define extra_type_iterate_end
Definition extras.h:297
extra_index
#define extra_index(_e_)
Definition extras.h:177
extra_type_re_active_iterate_end
#define extra_type_re_active_iterate_end
Definition extras.h:305
extra_type_re_active_iterate
#define extra_type_re_active_iterate(_p)
Definition extras.h:301
MAX_EXTRA_TYPES
#define MAX_EXTRA_TYPES
Definition fc_types.h:50
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:57
game.h
log.h
name_set
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
Definition name_translation.h:88
untranslated_name
static const char * untranslated_name(const struct name_translation *ptrans)
Definition name_translation.h:104
names_set
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
Definition name_translation.h:60
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:216
ruledit.h
ruledit_qt_display_requirers
void ruledit_qt_display_requirers(const char *msg, void *data)
Definition ruledit_qt.cpp:96
ruledit_qt.h
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
extra_type
Definition extras.h:85
extra_type::flags
bv_extra_flags flags
Definition extras.h:128
extra_type::ruledit_disabled
bool ruledit_disabled
Definition extras.h:88
extra_type::ruledit_dlg
void * ruledit_dlg
Definition extras.h:89
extra_type::reqs
struct requirement_vector reqs
Definition extras.h:102
extra_type::name
struct name_translation name
Definition extras.h:87
item
Definition climisc.h:80
packet_ruleset_control::num_extra_types
int num_extra_types
Definition packets_gen.h:1460
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
tab_extras.h
universals_u::extra
struct extra_type * extra
Definition fc_types.h:605
is_extra_needed
bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data)
Definition validity.c:291
validity.h
edit
#define edit(pedit)
Definition widget_edit.h:34