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

Freeciv-3.1

Loading...
Searching...
No Matches
tab_unit.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 "unittype.h"
34
35// ruledit
36#include "edit_utype.h"
37#include "ruledit.h"
38#include "ruledit_qt.h"
39#include "validity.h"
40
41#include "tab_unit.h"
42
43/**********************************************************************/
46tab_unit::tab_unit(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *unit_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *effects_button;
52 QPushButton *add_button;
53 QPushButton *delete_button;
54 QPushButton *edit_button;
55
56 ui = ui_in;
57 selected = 0;
58
59 unit_list = new QListWidget(this);
60
61 connect(unit_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_unit()));
62 main_layout->addWidget(unit_list);
63
64 unit_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 unit_layout->addWidget(label, 0, 0);
72 unit_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 unit_layout->addWidget(label, 1, 0);
82 unit_layout->addWidget(same_name, 1, 1);
83 unit_layout->addWidget(name, 1, 2);
84
85 edit_button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
86 connect(edit_button, SIGNAL(pressed()), this, SLOT(edit_now()));
87 unit_layout->addWidget(edit_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 unit_layout->addWidget(effects_button, 3, 2);
92
93 add_button = new QPushButton(QString::fromUtf8(R__("Add Unit")), this);
94 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
95 unit_layout->addWidget(add_button, 4, 0);
96 show_experimental(add_button);
97
98 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Unit")), this);
99 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
100 unit_layout->addWidget(delete_button, 4, 2);
101 show_experimental(delete_button);
102
103 refresh();
104 update_utype_info(nullptr);
105
106 main_layout->addLayout(unit_layout);
107
108 setLayout(main_layout);
109}
110
111/**********************************************************************/
114 void tab_unit::refresh()
115{
116 unit_list->clear();
117
118 unit_type_re_active_iterate(ptype) {
119 QListWidgetItem *item = new QListWidgetItem(utype_rule_name(ptype));
120
121 unit_list->insertItem(utype_index(ptype), item);
122 } unit_type_re_active_iterate_end;
123}
124
125/**********************************************************************/
128 void tab_unit::update_utype_info(struct unit_type *ptype)
129{
130 selected = ptype;
131
132 if (selected != nullptr) {
133 QString dispn = QString::fromUtf8(untranslated_name(&(ptype->name)));
134 QString rulen = QString::fromUtf8(utype_rule_name(ptype));
135
136 name->setText(dispn);
137 rname->setText(rulen);
138 if (dispn == rulen) {
139 name->setEnabled(false);
140 same_name->setChecked(true);
141 } else {
142 same_name->setChecked(false);
143 name->setEnabled(true);
144 }
145 } else {
146 name->setText(R__("None"));
147 rname->setText(R__("None"));
148 same_name->setChecked(true);
149 name->setEnabled(false);
150 }
151}
152
153/**********************************************************************/
156 void tab_unit::select_unit()
157{
158 QList<QListWidgetItem *> select_list = unit_list->selectedItems();
159
160 if (!select_list.isEmpty()) {
161 QByteArray un_bytes;
162
163 un_bytes = select_list.at(0)->text().toUtf8();
164 update_utype_info(unit_type_by_rule_name(un_bytes.data()));
165 }
166}
167
168/**********************************************************************/
171 void tab_unit::name_given()
172{
173 if (selected != nullptr) {
174 QByteArray name_bytes;
175 QByteArray rname_bytes;
176
177 unit_type_iterate(ptype) {
178 if (ptype != selected && !ptype->ruledit_disabled) {
179 rname_bytes = rname->text().toUtf8();
180 if (!strcmp(utype_rule_name(ptype), rname_bytes.data())) {
181 ui->display_msg(R__("A unit type with that rule name already "
182 "exists!"));
183 return;
184 }
185 }
186 } unit_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_unit::delete_now()
205{
206 if (selected != nullptr) {
207 requirers_dlg *requirers;
208
209 requirers = ui->create_requirers(utype_rule_name(selected));
210 if (is_utype_needed(selected, &ruledit_qt_display_requirers, requirers)) {
211 return;
212 }
213
214 selected->ruledit_disabled = true;
215
216 if (selected->ruledit_dlg != nullptr) {
217 ((edit_utype *)selected->ruledit_dlg)->done(0);
218 }
219
220 refresh();
221 update_utype_info(nullptr);
222 }
223}
224
225/**********************************************************************/
228 void tab_unit::edit_now()
229{
230 if (selected != nullptr) {
231 if (selected->ruledit_dlg == nullptr) {
232 edit_utype *edit = new edit_utype(ui, selected);
233
234 edit->show();
235 selected->ruledit_dlg = edit;
236 } else {
237 ((edit_utype *)selected->ruledit_dlg)->raise();
238 }
239 }
240}
241
242/**********************************************************************/
245 bool tab_unit::initialize_new_utype(struct unit_type *ptype)
246{
247 if (unit_type_by_rule_name("New Unit") != nullptr) {
248 return false;
249 }
250
251 name_set(&(ptype->name), 0, "New Unit");
252 BV_CLR_ALL(ptype->flags);
253
254 return true;
255}
256
257/**********************************************************************/
260 void tab_unit::add_now()
261{
262 struct unit_type *new_utype;
263
264 // Try to reuse freed utype slot
265 unit_type_iterate(ptype) {
266 if (ptype->ruledit_disabled) {
267 if (initialize_new_utype(ptype)) {
268 ptype->ruledit_disabled = false;
269 update_utype_info(ptype);
270 refresh();
271 }
272 return;
273 }
274 } unit_type_iterate_end;
275
276 // Try to add completely new unit type
277 if (game.control.num_unit_types >= U_LAST) {
278 return;
279 }
280
281 // num_unit_types must be big enough to hold new unit or
282 // utype_by_number() fails.
283 game.control.num_unit_types++;
284 new_utype = utype_by_number(game.control.num_unit_types - 1);
285 if (initialize_new_utype(new_utype)) {
286 update_utype_info(new_utype);
287
288 refresh();
289 } else {
290 game.control.num_unit_types--; // Restore
291 }
292}
293
294/**********************************************************************/
297 void tab_unit::same_name_toggle(bool checked)
298{
299 name->setEnabled(!checked);
300 if (checked) {
301 name->setText(rname->text());
302 }
303}
304
305/**********************************************************************/
308 void tab_unit::edit_effects()
309{
310 if (selected != nullptr) {
311 struct universal uni;
312
313 uni.value.utype = selected;
314 uni.kind = VUT_UTYPE;
315
316 ui->open_effect_edit(QString::fromUtf8(utype_rule_name(selected)),
317 &uni, EFMC_NORMAL);
318 }
319}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
edit_utype
Definition edit_utype.h:35
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::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_unit::unit_list
QListWidget * unit_list
Definition tab_unit.h:45
tab_unit::same_name
QCheckBox * same_name
Definition tab_unit.h:46
tab_unit::ui
ruledit_gui * ui
Definition tab_unit.h:39
tab_unit::refresh
void refresh()
Definition tab_unit.cpp:114
tab_unit::selected
struct unit_type * selected
Definition tab_unit.h:48
tab_unit::rname
QLineEdit * rname
Definition tab_unit.h:44
tab_unit::add_now
void add_now()
Definition tab_unit.cpp:260
tab_unit::edit_effects
void edit_effects()
Definition tab_unit.cpp:308
tab_unit::initialize_new_utype
bool initialize_new_utype(struct unit_type *ptype)
Definition tab_unit.cpp:245
tab_unit::edit_now
void edit_now()
Definition tab_unit.cpp:228
tab_unit::select_unit
void select_unit()
Definition tab_unit.cpp:156
tab_unit::name
QLineEdit * name
Definition tab_unit.h:43
tab_unit::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_unit.cpp:297
tab_unit::tab_unit
tab_unit(ruledit_gui *ui_in)
Definition tab_unit.cpp:46
tab_unit::name_given
void name_given()
Definition tab_unit.cpp:171
tab_unit::update_utype_info
void update_utype_info(struct unit_type *ptype)
Definition tab_unit.cpp:128
tab_unit::delete_now
void delete_now()
Definition tab_unit.cpp:204
edit_utype.h
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:33
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
item
Definition climisc.h:80
packet_ruleset_control::num_unit_types
int num_unit_types
Definition packets_gen.h:1456
unit_type
Definition unittype.h:481
unit_type::name
struct name_translation name
Definition unittype.h:483
unit_type::flags
bv_unit_type_flags flags
Definition unittype.h:515
unit_type::ruledit_disabled
bool ruledit_disabled
Definition unittype.h:484
unit_type::ruledit_dlg
void * ruledit_dlg
Definition unittype.h:485
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_unit.h
universals_u::utype
const struct unit_type * utype
Definition fc_types.h:604
unit_type_by_rule_name
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1819
utype_rule_name
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
utype_by_number
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
utype_index
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
unittype.h
unit_type_re_active_iterate
#define unit_type_re_active_iterate(_p)
Definition unittype.h:850
unit_type_iterate
#define unit_type_iterate(_p)
Definition unittype.h:838
U_LAST
#define U_LAST
Definition unittype.h:40
unit_type_iterate_end
#define unit_type_iterate_end
Definition unittype.h:845
unit_type_re_active_iterate_end
#define unit_type_re_active_iterate_end
Definition unittype.h:854
is_utype_needed
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Definition validity.c:242
validity.h
edit
#define edit(pedit)
Definition widget_edit.h:34