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

Freeciv-3.3

Loading...
Searching...
No Matches
tab_good.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 "improvement.h"
34
35// ruledit
36#include "req_edit.h"
37#include "ruledit.h"
38#include "ruledit_qt.h"
39#include "validity.h"
40
41#include "tab_good.h"
42
43/**********************************************************************/
46tab_good::tab_good(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *good_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *effects_button;
52 QPushButton *add_button;
53 QPushButton *delete_button;
54 QPushButton *reqs_button;
55
56 ui = ui_in;
57 selected = 0;
58
59 good_list = new QListWidget(this);
60
61 connect(good_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_good()));
62 main_layout->addWidget(good_list);
63
64 good_layout->setSizeConstraint(QLayout::SetMaximumSize);
65
66 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
67 label->setParent(this);
68 rname = new QLineEdit(this);
69 rname->setText(R__("None"));
70 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
71 good_layout->addWidget(label, 0, 0);
72 good_layout->addWidget(rname, 0, 2);
73
74 label = new QLabel(QString::fromUtf8(R__("Name")));
75 label->setParent(this);
76 same_name = new QCheckBox();
77 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
78 name = new QLineEdit(this);
79 name->setText(R__("None"));
80 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
81 good_layout->addWidget(label, 1, 0);
82 good_layout->addWidget(same_name, 1, 1);
83 good_layout->addWidget(name, 1, 2);
84
85 reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
86 connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
87 good_layout->addWidget(reqs_button, 2, 2);
88
89 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
90 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
91 good_layout->addWidget(effects_button, 3, 2);
92
93 add_button = new QPushButton(QString::fromUtf8(R__("Add Good")), this);
94 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
95 good_layout->addWidget(add_button, 4, 0);
96 show_experimental(add_button);
97
98 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Good")), this);
99 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
100 good_layout->addWidget(delete_button, 4, 2);
101 show_experimental(delete_button);
102
103 refresh();
104 update_good_info(nullptr);
105
106 main_layout->addLayout(good_layout);
107
108 setLayout(main_layout);
109}
110
111/**********************************************************************/
114 void tab_good::refresh()
115{
116 good_list->clear();
117
118 goods_type_re_active_iterate(pgood) {
119 QListWidgetItem *item
120 = new QListWidgetItem(QString::fromUtf8(goods_rule_name(pgood)));
121
122 good_list->insertItem(goods_index(pgood), item);
123 } goods_type_re_active_iterate_end;
124}
125
126/**********************************************************************/
129 void tab_good::update_good_info(struct goods_type *pgood)
130{
131 selected = pgood;
132
133 if (selected != 0) {
134 QString dispn = QString::fromUtf8(untranslated_name(&(pgood->name)));
135 QString rulen = QString::fromUtf8(goods_rule_name(pgood));
136
137 name->setText(dispn);
138 rname->setText(rulen);
139 if (dispn == rulen) {
140 name->setEnabled(false);
141 same_name->setChecked(true);
142 } else {
143 same_name->setChecked(false);
144 name->setEnabled(true);
145 }
146 } else {
147 name->setText(R__("None"));
148 rname->setText(R__("None"));
149 same_name->setChecked(true);
150 name->setEnabled(false);
151 }
152}
153
154/**********************************************************************/
157 void tab_good::select_good()
158{
159 QList<QListWidgetItem *> select_list = good_list->selectedItems();
160
161 if (!select_list.isEmpty()) {
162 QByteArray gn_bytes;
163
164 gn_bytes = select_list.at(0)->text().toUtf8();
165 update_good_info(goods_by_rule_name(gn_bytes.data()));
166 }
167}
168
169/**********************************************************************/
172 void tab_good::name_given()
173{
174 if (selected != nullptr) {
175 QByteArray name_bytes;
176 QByteArray rname_bytes;
177
178 goods_type_iterate(pgood) {
179 if (pgood != selected && !pgood->ruledit_disabled) {
180 rname_bytes = rname->text().toUtf8();
181 if (!strcmp(goods_rule_name(pgood), rname_bytes.data())) {
182 ui->display_msg(R__("A good with that rule name already exists!"));
183 return;
184 }
185 }
186 } goods_type_iterate_end;
187
188 if (same_name->isChecked()) {
189 name->setText(rname->text());
190 }
191
192 name_bytes = name->text().toUtf8();
193 rname_bytes = rname->text().toUtf8();
194 names_set(&(selected->name), 0,
195 name_bytes.data(),
196 rname_bytes.data());
197 refresh();
198 }
199}
200
201/**********************************************************************/
204 void tab_good::delete_now()
205{
206 if (selected != 0) {
207 requirers_dlg *requirers;
208
209 requirers = ui->create_requirers(goods_rule_name(selected));
210 if (is_good_needed(selected, &ruledit_qt_display_requirers, requirers)) {
211 return;
212 }
213
214 selected->ruledit_disabled = true;
215
216 refresh();
217 update_good_info(nullptr);
218 }
219}
220
221/**********************************************************************/
224 bool tab_good::initialize_new_good(struct goods_type *pgood)
225{
226 if (goods_by_rule_name("New Good") != nullptr) {
227 return false;
228 }
229
230 name_set(&(pgood->name), 0, "New Good");
231 BV_CLR_ALL(pgood->flags);
232
233 return true;
234}
235
236/**********************************************************************/
239 void tab_good::add_now()
240{
241 struct goods_type *new_good;
242
243 // Try to reuse freed good slot
244 goods_type_iterate(pgood) {
245 if (pgood->ruledit_disabled) {
246 if (initialize_new_good(pgood)) {
247 pgood->ruledit_disabled = false;
248 update_good_info(pgood);
249 refresh();
250 }
251 return;
252 }
253 } goods_type_iterate_end;
254
255 // Try to add completely new good
256 if (game.control.num_goods_types >= MAX_GOODS_TYPES) {
257 return;
258 }
259
260 // num_good_types must be big enough to hold new good or
261 // good_by_number() fails.
262 game.control.num_goods_types++;
263 new_good = goods_by_number(game.control.num_goods_types - 1);
264 if (initialize_new_good(new_good)) {
265 update_good_info(new_good);
266
267 refresh();
268 } else {
269 game.control.num_goods_types--; // Restore
270 }
271}
272
273/**********************************************************************/
276 void tab_good::same_name_toggle(bool checked)
277{
278 name->setEnabled(!checked);
279 if (checked) {
280 name->setText(rname->text());
281 }
282}
283
284/**********************************************************************/
287 void tab_good::edit_reqs()
288{
289 if (selected != nullptr) {
290 req_edit *redit = new req_edit(ui, QString::fromUtf8(goods_rule_name(selected)),
291 &selected->reqs);
292
293 redit->show();
294 }
295}
296
297/**********************************************************************/
300 void tab_good::edit_effects()
301{
302 if (selected != nullptr) {
303 struct universal uni;
304
305 uni.value.good = selected;
306 uni.kind = VUT_GOOD;
307
308 ui->open_effect_edit(QString::fromUtf8(goods_rule_name(selected)),
309 &uni, EFMC_NORMAL);
310 }
311}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
req_edit
Definition req_edit.h:36
requirers_dlg
Definition requirers_dlg.h:28
ruledit_gui
Definition ruledit_qt.h:97
ruledit_gui::display_msg
void display_msg(const char *msg)
Definition ruledit_qt.cpp:293
ruledit_gui::create_requirers
requirers_dlg * create_requirers(const char *title)
Definition ruledit_qt.cpp:301
ruledit_gui::open_effect_edit
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
Definition ruledit_qt.cpp:404
tab_good::good_list
QListWidget * good_list
Definition tab_good.h:45
tab_good::edit_effects
void edit_effects()
Definition tab_good.cpp:300
tab_good::selected
struct goods_type * selected
Definition tab_good.h:48
tab_good::initialize_new_good
bool initialize_new_good(struct goods_type *pgood)
Definition tab_good.cpp:224
tab_good::name
QLineEdit * name
Definition tab_good.h:43
tab_good::tab_good
tab_good(ruledit_gui *ui_in)
Definition tab_good.cpp:46
tab_good::refresh
void refresh()
Definition tab_good.cpp:114
tab_good::same_name
QCheckBox * same_name
Definition tab_good.h:46
tab_good::select_good
void select_good()
Definition tab_good.cpp:157
tab_good::rname
QLineEdit * rname
Definition tab_good.h:44
tab_good::delete_now
void delete_now()
Definition tab_good.cpp:204
tab_good::name_given
void name_given()
Definition tab_good.cpp:172
tab_good::edit_reqs
void edit_reqs()
Definition tab_good.cpp:287
tab_good::update_good_info
void update_good_info(struct goods_type *pgood)
Definition tab_good.cpp:129
tab_good::add_now
void add_now()
Definition tab_good.cpp:239
tab_good::ui
ruledit_gui * ui
Definition tab_good.h:39
tab_good::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_good.cpp:276
incite_cost
char * incite_cost
Definition comments.c:74
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:34
MAX_GOODS_TYPES
#define MAX_GOODS_TYPES
Definition fc_types.h:51
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:62
game.h
improvement.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
req_edit.h
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:237
ruledit.h
ruledit_qt_display_requirers
void ruledit_qt_display_requirers(const char *msg, void *data)
Definition ruledit_qt.cpp:97
ruledit_qt.h
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
goods_type
Definition traderoutes.h:187
goods_type::reqs
struct requirement_vector reqs
Definition traderoutes.h:192
goods_type::ruledit_disabled
bool ruledit_disabled
Definition traderoutes.h:190
goods_type::name
struct name_translation name
Definition traderoutes.h:189
item
Definition climisc.h:80
packet_ruleset_control::num_goods_types
int num_goods_types
Definition packets_gen.h:1467
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
tab_good.h
goods_by_rule_name
struct goods_type * goods_by_rule_name(const char *name)
Definition traderoutes.c:642
goods_rule_name
const char * goods_rule_name(struct goods_type *pgood)
Definition traderoutes.c:633
goods_index
Goods_type_id goods_index(const struct goods_type *pgood)
Definition traderoutes.c:605
goods_by_number
struct goods_type * goods_by_number(Goods_type_id id)
Definition traderoutes.c:615
goods_type_iterate_end
#define goods_type_iterate_end
Definition traderoutes.h:236
goods_type_re_active_iterate_end
#define goods_type_re_active_iterate_end
Definition traderoutes.h:244
goods_type_iterate
#define goods_type_iterate(_p)
Definition traderoutes.h:230
goods_type_re_active_iterate
#define goods_type_re_active_iterate(_p)
Definition traderoutes.h:240
universals_u::good
struct goods_type * good
Definition fc_types.h:734
is_good_needed
bool is_good_needed(struct goods_type *pgood, requirers_cb cb, void *data)
Definition validity.c:287
validity.h