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

Freeciv-3.2

Loading...
Searching...
No Matches
tab_terrains.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 "terrain.h"
34
35// ruledit
36#include "edit_terrain.h"
37#include "req_edit.h"
38#include "ruledit.h"
39#include "ruledit_qt.h"
40#include "validity.h"
41
42#include "tab_terrains.h"
43
44/**********************************************************************/
47tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget()
48{
49 QVBoxLayout *main_layout = new QVBoxLayout(this);
50 QGridLayout *terrains_layout = new QGridLayout();
51 QLabel *label;
52 QPushButton *effects_button;
53 QPushButton *add_button;
54 QPushButton *delete_button;
55 QPushButton *edit_button;
56 int row = 0;
57
58 ui = ui_in;
59 selected = 0;
60
61 terrain_list = new QListWidget(this);
62
63 connect(terrain_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_terrain()));
64 main_layout->addWidget(terrain_list);
65
66 terrains_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 terrains_layout->addWidget(label, row, 0);
74 terrains_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 terrains_layout->addWidget(label, row, 0);
84 terrains_layout->addWidget(same_name, row, 1);
85 terrains_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 terrains_layout->addWidget(edit_button, row++, 2);
90
91 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
92 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
93 terrains_layout->addWidget(effects_button, row++, 2);
94
95 add_button = new QPushButton(QString::fromUtf8(R__("Add Terrain")), this);
96 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
97 terrains_layout->addWidget(add_button, row, 0);
98 show_experimental(add_button);
99
100 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Terrain")), this);
101 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
102 terrains_layout->addWidget(delete_button, row++, 2);
103 show_experimental(delete_button);
104
105 refresh();
106 update_terrain_info(nullptr);
107
108 main_layout->addLayout(terrains_layout);
109
110 setLayout(main_layout);
111}
112
113/**********************************************************************/
116 void tab_terrains::refresh()
117{
118 terrain_list->clear();
119
120 terrain_re_active_iterate(pterr) {
121 QListWidgetItem *item
122 = new QListWidgetItem(QString::fromUtf8(terrain_rule_name(pterr)));
123
124 terrain_list->insertItem(terrain_index(pterr), item);
125 } terrain_re_active_iterate_end;
126}
127
128/**********************************************************************/
131 void tab_terrains::update_terrain_info(struct terrain *pterr)
132{
133 selected = pterr;
134
135 if (selected != nullptr) {
136 QString dispn = QString::fromUtf8(untranslated_name(&(pterr->name)));
137 QString rulen = QString::fromUtf8(terrain_rule_name(pterr));
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_terrains::select_terrain()
160{
161 QList<QListWidgetItem *> select_list = terrain_list->selectedItems();
162
163 if (!select_list.isEmpty()) {
164 QByteArray tn_bytes;
165
166 tn_bytes = select_list.at(0)->text().toUtf8();
167 update_terrain_info(terrain_by_rule_name(tn_bytes.data()));
168 }
169}
170
171/**********************************************************************/
174 void tab_terrains::name_given()
175{
176 if (selected != nullptr) {
177 QByteArray name_bytes;
178 QByteArray rname_bytes;
179
180 terrain_type_iterate(pterr) {
181 if (pterr != selected && !pterr->ruledit_disabled) {
182 rname_bytes = rname->text().toUtf8();
183 if (!strcmp(terrain_rule_name(pterr), rname_bytes.data())) {
184 ui->display_msg(R__("A terrain with that rule name already exists!"));
185 return;
186 }
187 }
188 } terrain_type_iterate_end;
189
190 if (same_name->isChecked()) {
191 name->setText(rname->text());
192 }
193
194 name_bytes = name->text().toUtf8();
195 rname_bytes = rname->text().toUtf8();
196 names_set(&(selected->name), 0,
197 name_bytes.data(),
198 rname_bytes.data());
199 refresh();
200 }
201}
202
203/**********************************************************************/
206 void tab_terrains::delete_now()
207{
208 if (selected != nullptr) {
209 requirers_dlg *requirers;
210
211 requirers = ui->create_requirers(terrain_rule_name(selected));
212 if (is_terrain_needed(selected, &ruledit_qt_display_requirers, requirers)) {
213 return;
214 }
215
216 selected->ruledit_disabled = true;
217
218 if (selected->ruledit_dlg != nullptr) {
219 ((edit_terrain *)selected->ruledit_dlg)->done(0);
220 }
221
222 refresh();
223 update_terrain_info(nullptr);
224 }
225}
226
227/**********************************************************************/
230 bool tab_terrains::initialize_new_terrain(struct terrain *pterr)
231{
232 if (terrain_by_rule_name("New Terrain") != nullptr) {
233 return false;
234 }
235
236 name_set(&(pterr->name), 0, "New Terrain");
237 BV_CLR_ALL(pterr->flags);
238
239 return true;
240}
241
242/**********************************************************************/
245 void tab_terrains::add_now()
246{
247 struct terrain *new_terr;
248
249 // Try to reuse freed terrain slot
250 terrain_type_iterate(pterr) {
251 if (pterr->ruledit_disabled) {
252 if (initialize_new_terrain(pterr)) {
253 pterr->ruledit_disabled = false;
254 update_terrain_info(pterr);
255 refresh();
256 }
257 return;
258 }
259 } terrain_type_iterate_end;
260
261 // Try to add completely new terrain
262 if (game.control.terrain_count >= MAX_NUM_TERRAINS) {
263 return;
264 }
265
266 // terrain_count must be big enough to hold new extra or
267 // terrain_by_number() fails.
268 game.control.terrain_count++;
269 new_terr = terrain_by_number(game.control.terrain_count - 1);
270 if (initialize_new_terrain(new_terr)) {
271 update_terrain_info(new_terr);
272
273 refresh();
274 } else {
275 game.control.terrain_count--; // Restore
276 }
277}
278
279/**********************************************************************/
282 void tab_terrains::same_name_toggle(bool checked)
283{
284 name->setEnabled(!checked);
285 if (checked) {
286 name->setText(rname->text());
287 }
288}
289
290/**********************************************************************/
293 void tab_terrains::edit_effects()
294{
295 if (selected != nullptr) {
296 struct universal uni;
297
298 uni.value.terrain = selected;
299 uni.kind = VUT_TERRAIN;
300
301 ui->open_effect_edit(QString::fromUtf8(terrain_rule_name(selected)),
302 &uni, EFMC_NORMAL);
303 }
304}
305
306/**********************************************************************/
309 void tab_terrains::edit_now()
310{
311 if (selected != nullptr) {
312 if (selected->ruledit_dlg == nullptr) {
313 edit_terrain *edit = new edit_terrain(ui, selected);
314
315 edit->show();
316 selected->ruledit_dlg = edit;
317 } else {
318 ((edit_terrain *)selected->ruledit_dlg)->raise();
319 }
320 }
321}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
edit_terrain
Definition edit_terrain.h:31
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_terrains::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_terrains.cpp:282
tab_terrains::rname
QLineEdit * rname
Definition tab_terrains.h:44
tab_terrains::add_now
void add_now()
Definition tab_terrains.cpp:245
tab_terrains::edit_effects
void edit_effects()
Definition tab_terrains.cpp:293
tab_terrains::tab_terrains
tab_terrains(ruledit_gui *ui_in)
Definition tab_terrains.cpp:47
tab_terrains::initialize_new_terrain
bool initialize_new_terrain(struct terrain *pterr)
Definition tab_terrains.cpp:230
tab_terrains::name
QLineEdit * name
Definition tab_terrains.h:43
tab_terrains::select_terrain
void select_terrain()
Definition tab_terrains.cpp:159
tab_terrains::selected
struct terrain * selected
Definition tab_terrains.h:48
tab_terrains::refresh
void refresh()
Definition tab_terrains.cpp:116
tab_terrains::update_terrain_info
void update_terrain_info(struct terrain *pterr)
Definition tab_terrains.cpp:131
tab_terrains::same_name
QCheckBox * same_name
Definition tab_terrains.h:46
tab_terrains::edit_now
void edit_now()
Definition tab_terrains.cpp:309
tab_terrains::ui
ruledit_gui * ui
Definition tab_terrains.h:39
tab_terrains::delete_now
void delete_now()
Definition tab_terrains.cpp:206
tab_terrains::terrain_list
QListWidget * terrain_list
Definition tab_terrains.h:45
tab_terrains::name_given
void name_given()
Definition tab_terrains.cpp:174
incite_cost
char * incite_cost
Definition comments.c:74
edit_terrain.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
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
item
Definition climisc.h:80
packet_ruleset_control::terrain_count
int terrain_count
Definition packets_gen.h:1467
terrain
Definition terrain.h:182
terrain::name
struct name_translation name
Definition terrain.h:184
terrain::ruledit_dlg
void * ruledit_dlg
Definition terrain.h:186
terrain::ruledit_disabled
bool ruledit_disabled
Definition terrain.h:185
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_terrains.h
terrain_by_rule_name
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:186
terrain_index
Terrain_type_id terrain_index(const struct terrain *pterrain)
Definition terrain.c:138
terrain_by_number
struct terrain * terrain_by_number(const Terrain_type_id type)
Definition terrain.c:156
terrain_rule_name
const char * terrain_rule_name(const struct terrain *pterrain)
Definition terrain.c:247
terrain.h
terrain_type_iterate
#define terrain_type_iterate(_p)
Definition terrain.h:373
terrain_re_active_iterate_end
#define terrain_re_active_iterate_end
Definition terrain.h:388
terrain_re_active_iterate
#define terrain_re_active_iterate(_p)
Definition terrain.h:384
terrain_type_iterate_end
#define terrain_type_iterate_end
Definition terrain.h:379
MAX_NUM_TERRAINS
#define MAX_NUM_TERRAINS
Definition terrain.h:64
universals_u::terrain
struct terrain * terrain
Definition fc_types.h:722
is_terrain_needed
bool is_terrain_needed(struct terrain *pterr, requirers_cb cb, void *data)
Definition validity.c:331
validity.h