Freeciv-3.2: /fast/freeciv/nightly/S3_2/tools/ruledit/tab_building.cpp Source File

Freeciv-3.2

Loading...
Searching...
No Matches
tab_building.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 "edit_impr.h"
37#include "ruledit.h"
38#include "ruledit_qt.h"
39#include "validity.h"
40
41#include "tab_building.h"
42
43/**********************************************************************/
46tab_building::tab_building(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *bldg_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *add_button;
52 QPushButton *delete_button;
53 QPushButton *reqs_button;
54 QPushButton *effects_button;
55 QPushButton *edit_button;
56
57 ui = ui_in;
58 selected = 0;
59
60 bldg_list = new QListWidget(this);
61
62 connect(bldg_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_bldg()));
63 main_layout->addWidget(bldg_list);
64
65 bldg_layout->setSizeConstraint(QLayout::SetMaximumSize);
66
67 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
68 label->setParent(this);
69 rname = new QLineEdit(this);
70 rname->setText(R__("None"));
71 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
72 bldg_layout->addWidget(label, 0, 0);
73 bldg_layout->addWidget(rname, 0, 2);
74
75 label = new QLabel(QString::fromUtf8(R__("Name")));
76 label->setParent(this);
77 same_name = new QCheckBox();
78 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
79 name = new QLineEdit(this);
80 name->setText(R__("None"));
81 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
82 bldg_layout->addWidget(label, 1, 0);
83 bldg_layout->addWidget(same_name, 1, 1);
84 bldg_layout->addWidget(name, 1, 2);
85
86 edit_button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
87 connect(edit_button, SIGNAL(pressed()), this, SLOT(edit_now()));
88 bldg_layout->addWidget(edit_button, 2, 2);
89
90 reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
91 connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
92 bldg_layout->addWidget(reqs_button, 3, 2);
93
94 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
95 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
96 bldg_layout->addWidget(effects_button, 4, 2);
97
98 add_button = new QPushButton(QString::fromUtf8(R__("Add Building")), this);
99 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now2()));
100 bldg_layout->addWidget(add_button, 5, 0);
101
102 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Building")), this);
103 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
104 bldg_layout->addWidget(delete_button, 5, 2);
105
106 refresh();
107 update_bldg_info(nullptr);
108
109 main_layout->addLayout(bldg_layout);
110
111 setLayout(main_layout);
112}
113
114/**********************************************************************/
117 void tab_building::refresh()
118{
119 bldg_list->clear();
120
121 improvement_re_active_iterate(pimpr) {
122 QListWidgetItem *item = new QListWidgetItem(improvement_rule_name(pimpr));
123
124 bldg_list->insertItem(improvement_index(pimpr), item);
125 } improvement_re_active_iterate_end;
126}
127
128/**********************************************************************/
131 void tab_building::update_bldg_info(struct impr_type *pimpr)
132{
133 selected = pimpr;
134
135 if (selected != 0) {
136 QString dispn = QString::fromUtf8(untranslated_name(&(pimpr->name)));
137 QString rulen = QString::fromUtf8(improvement_rule_name(pimpr));
138
139 name->setText(dispn);
140 rname->setText(rulen);
141 if (dispn == rulen) {
142 name->setEnabled(false);
143 same_name->setChecked(true);
144 } else {
145 same_name->setChecked(false);
146 name->setEnabled(true);
147 }
148 } else {
149 name->setText(R__("None"));
150 rname->setText(R__("None"));
151 same_name->setChecked(true);
152 name->setEnabled(false);
153 }
154}
155
156/**********************************************************************/
159 void tab_building::select_bldg()
160{
161 QList<QListWidgetItem *> select_list = bldg_list->selectedItems();
162
163 if (!select_list.isEmpty()) {
164 QByteArray bn_bytes;
165
166 bn_bytes = select_list.at(0)->text().toUtf8();
167 update_bldg_info(improvement_by_rule_name(bn_bytes.data()));
168 }
169}
170
171/**********************************************************************/
174 void tab_building::name_given()
175{
176 if (selected != nullptr) {
177 QByteArray name_bytes;
178 QByteArray rname_bytes;
179
180 improvement_iterate(pimpr) {
181 if (pimpr != selected && !pimpr->ruledit_disabled) {
182 rname_bytes = rname->text().toUtf8();
183 if (!strcmp(improvement_rule_name(pimpr), rname_bytes.data())) {
184 ui->display_msg(R__("A building with that rule name already "
185 "exists!"));
186 return;
187 }
188 }
189 } improvement_iterate_end;
190
191 if (same_name->isChecked()) {
192 name->setText(rname->text());
193 }
194
195 name_bytes = name->text().toUtf8();
196 rname_bytes = rname->text().toUtf8();
197 names_set(&(selected->name), 0,
198 name_bytes.data(),
199 rname_bytes.data());
200 refresh();
201 }
202}
203
204/**********************************************************************/
207 void tab_building::delete_now()
208{
209 if (selected != nullptr) {
210 requirers_dlg *requirers;
211
212 requirers = ui->create_requirers(improvement_rule_name(selected));
213 if (is_building_needed(selected, &ruledit_qt_display_requirers, requirers)) {
214 return;
215 }
216
217 selected->ruledit_disabled = true;
218
219 if (selected->ruledit_dlg != nullptr) {
220 ((edit_impr *)selected->ruledit_dlg)->done(0);
221 }
222
223 refresh();
224 update_bldg_info(nullptr);
225 }
226}
227
228/**********************************************************************/
231 bool tab_building::initialize_new_bldg(struct impr_type *pimpr)
232{
233 if (improvement_by_rule_name("New Building") != nullptr) {
234 return false;
235 }
236
237 name_set(&(pimpr->name), 0, "New Building");
238 BV_CLR_ALL(pimpr->flags);
239
240 return true;
241}
242
243/**********************************************************************/
250 void tab_building::add_now2()
251{
252 struct impr_type *new_bldg;
253
254 // Try to reuse freed building slot
255 improvement_iterate(pimpr) {
256 if (pimpr->ruledit_disabled) {
257 if (initialize_new_bldg(pimpr)) {
258 pimpr->ruledit_disabled = false;
259 update_bldg_info(pimpr);
260 refresh();
261 }
262 return;
263 }
264 } improvement_iterate_end;
265
266 // Try to add completely new building
267 if (game.control.num_impr_types >= B_LAST) {
268 return;
269 }
270
271 // num_impr_types must be big enough to hold new building or
272 // improvement_by_number() fails.
273 game.control.num_impr_types++;
274 new_bldg = improvement_by_number(game.control.num_impr_types - 1);
275 if (initialize_new_bldg(new_bldg)) {
276 update_bldg_info(new_bldg);
277
278 refresh();
279 } else {
280 game.control.num_impr_types--; // Restore
281 }
282}
283
284/**********************************************************************/
287 void tab_building::same_name_toggle(bool checked)
288{
289 name->setEnabled(!checked);
290 if (checked) {
291 name->setText(rname->text());
292 }
293}
294
295/**********************************************************************/
298 void tab_building::edit_reqs()
299{
300 if (selected != nullptr) {
301 ui->open_req_edit(QString::fromUtf8(improvement_rule_name(selected)),
302 &selected->reqs);
303 }
304}
305
306/**********************************************************************/
309 void tab_building::edit_effects()
310{
311 if (selected != nullptr) {
312 struct universal uni;
313
314 uni.value.building = selected;
315 uni.kind = VUT_IMPROVEMENT;
316
317 ui->open_effect_edit(QString::fromUtf8(improvement_rule_name(selected)),
318 &uni, EFMC_NORMAL);
319 }
320}
321
322/**********************************************************************/
325 void tab_building::edit_now()
326{
327 if (selected != nullptr) {
328 if (selected->ruledit_dlg == nullptr) {
329 edit_impr *edit = new edit_impr(ui, selected);
330
331 edit->show();
332 selected->ruledit_dlg = edit;
333 } else {
334 ((edit_impr *)selected->ruledit_dlg)->raise();
335 }
336 }
337}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
edit_impr
Definition edit_impr.h:35
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::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:335
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_building::selected
struct impr_type * selected
Definition tab_building.h:48
tab_building::add_now2
void add_now2()
Definition tab_building.cpp:250
tab_building::refresh
void refresh()
Definition tab_building.cpp:117
tab_building::same_name
QCheckBox * same_name
Definition tab_building.h:46
tab_building::edit_reqs
void edit_reqs()
Definition tab_building.cpp:298
tab_building::initialize_new_bldg
bool initialize_new_bldg(struct impr_type *pimpr)
Definition tab_building.cpp:231
tab_building::ui
ruledit_gui * ui
Definition tab_building.h:39
tab_building::name_given
void name_given()
Definition tab_building.cpp:174
tab_building::update_bldg_info
void update_bldg_info(struct impr_type *pimpr)
Definition tab_building.cpp:131
tab_building::select_bldg
void select_bldg()
Definition tab_building.cpp:159
tab_building::tab_building
tab_building(ruledit_gui *ui_in)
Definition tab_building.cpp:46
tab_building::bldg_list
QListWidget * bldg_list
Definition tab_building.h:45
tab_building::edit_effects
void edit_effects()
Definition tab_building.cpp:309
tab_building::rname
QLineEdit * rname
Definition tab_building.h:44
tab_building::delete_now
void delete_now()
Definition tab_building.cpp:207
tab_building::edit_now
void edit_now()
Definition tab_building.cpp:325
tab_building::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_building.cpp:287
tab_building::name
QLineEdit * name
Definition tab_building.h:43
incite_cost
char * incite_cost
Definition comments.c:74
edit_impr.h
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:34
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:62
game.h
edit
#define edit(pedit)
Definition widget_edit.h:34
improvement_by_number
struct impr_type * improvement_by_number(const Impr_type_id id)
Definition improvement.c:194
improvement_rule_name
const char * improvement_rule_name(const struct impr_type *pimprove)
Definition improvement.c:252
improvement_index
Impr_type_id improvement_index(const struct impr_type *pimprove)
Definition improvement.c:175
improvement_by_rule_name
struct impr_type * improvement_by_rule_name(const char *name)
Definition improvement.c:367
improvement.h
improvement_re_active_iterate_end
#define improvement_re_active_iterate_end
Definition improvement.h:214
improvement_iterate_end
#define improvement_iterate_end
Definition improvement.h:205
improvement_re_active_iterate
#define improvement_re_active_iterate(_p)
Definition improvement.h:210
improvement_iterate
#define improvement_iterate(_p)
Definition improvement.h:199
B_LAST
#define B_LAST
Definition improvement.h:42
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
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
impr_type
Definition improvement.h:50
impr_type::ruledit_disabled
bool ruledit_disabled
Definition improvement.h:53
impr_type::reqs
struct requirement_vector reqs
Definition improvement.h:58
impr_type::name
struct name_translation name
Definition improvement.h:52
impr_type::ruledit_dlg
void * ruledit_dlg
Definition improvement.h:54
item
Definition climisc.h:80
packet_ruleset_control::num_impr_types
int num_impr_types
Definition packets_gen.h:1451
universal
Definition fc_types.h:901
universal::kind
enum universals_n kind
Definition fc_types.h:903
universal::value
universals_u value
Definition fc_types.h:902
tab_building.h
universals_u::building
const struct impr_type * building
Definition fc_types.h:717
is_building_needed
bool is_building_needed(struct impr_type *pimpr, requirers_cb cb, void *data)
Definition validity.c:238
validity.h