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

Freeciv-3.3

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 *button;
52 int but_row = 0;
53
54 ui = ui_in;
55 selected = 0;
56
57 unit_list = new QListWidget(this);
58
59 connect(unit_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_unit()));
60 main_layout->addWidget(unit_list);
61
62 unit_layout->setSizeConstraint(QLayout::SetMaximumSize);
63
64 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
65 label->setParent(this);
66 rname = new QLineEdit(this);
67 rname->setText(R__("None"));
68 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
69 unit_layout->addWidget(label, but_row, 0);
70 unit_layout->addWidget(rname, but_row++, 2);
71
72 label = new QLabel(QString::fromUtf8(R__("Name")));
73 label->setParent(this);
74 same_name = new QCheckBox();
75 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
76 name = new QLineEdit(this);
77 name->setText(R__("None"));
78 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
79 unit_layout->addWidget(label, but_row, 0);
80 unit_layout->addWidget(same_name, but_row, 1);
81 unit_layout->addWidget(name, but_row++, 2);
82
83 button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this);
84 connect(button, SIGNAL(pressed()), this, SLOT(edit_now()));
85 unit_layout->addWidget(button, but_row++, 2);
86
87 button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
88 connect(button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
89 unit_layout->addWidget(button, but_row++, 2);
90
91 button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
92 connect(button, SIGNAL(pressed()), this, SLOT(edit_effects()));
93 unit_layout->addWidget(button, but_row++, 2);
94
95 button = new QPushButton(QString::fromUtf8(R__("Add Unit")), this);
96 connect(button, SIGNAL(pressed()), this, SLOT(add_now()));
97 unit_layout->addWidget(button, but_row, 0);
98 show_experimental(button);
99
100 button = new QPushButton(QString::fromUtf8(R__("Remove this Unit")), this);
101 connect(button, SIGNAL(pressed()), this, SLOT(delete_now()));
102 unit_layout->addWidget(button, but_row++, 2);
103 show_experimental(button);
104
105 refresh();
106 update_utype_info(nullptr);
107
108 main_layout->addLayout(unit_layout);
109
110 setLayout(main_layout);
111}
112
113/**********************************************************************/
116 void tab_unit::refresh()
117{
118 unit_list->clear();
119
120 unit_type_re_active_iterate(ptype) {
121 QListWidgetItem *item = new QListWidgetItem(utype_rule_name(ptype));
122
123 unit_list->insertItem(utype_index(ptype), item);
124 } unit_type_re_active_iterate_end;
125}
126
127/**********************************************************************/
130 void tab_unit::update_utype_info(struct unit_type *ptype)
131{
132 selected = ptype;
133
134 if (selected != nullptr) {
135 QString dispn = QString::fromUtf8(untranslated_name(&(ptype->name)));
136 QString rulen = QString::fromUtf8(utype_rule_name(ptype));
137
138 name->setText(dispn);
139 rname->setText(rulen);
140 if (dispn == rulen) {
141 name->setEnabled(false);
142 same_name->setChecked(true);
143 } else {
144 same_name->setChecked(false);
145 name->setEnabled(true);
146 }
147 } else {
148 name->setText(R__("None"));
149 rname->setText(R__("None"));
150 same_name->setChecked(true);
151 name->setEnabled(false);
152 }
153}
154
155/**********************************************************************/
158 void tab_unit::select_unit()
159{
160 QList<QListWidgetItem *> select_list = unit_list->selectedItems();
161
162 if (!select_list.isEmpty()) {
163 QByteArray un_bytes;
164
165 un_bytes = select_list.at(0)->text().toUtf8();
166 update_utype_info(unit_type_by_rule_name(un_bytes.data()));
167 }
168}
169
170/**********************************************************************/
173 void tab_unit::name_given()
174{
175 if (selected != nullptr) {
176 QByteArray name_bytes;
177 QByteArray rname_bytes;
178
179 unit_type_iterate(ptype) {
180 if (ptype != selected && !ptype->ruledit_disabled) {
181 rname_bytes = rname->text().toUtf8();
182 if (!strcmp(utype_rule_name(ptype), rname_bytes.data())) {
183 ui->display_msg(R__("A unit type with that rule name already "
184 "exists!"));
185 return;
186 }
187 }
188 } unit_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_unit::delete_now()
207{
208 if (selected != nullptr) {
209 requirers_dlg *requirers;
210
211 requirers = ui->create_requirers(utype_rule_name(selected));
212 if (is_utype_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_utype *)selected->ruledit_dlg)->done(0);
220 }
221
222 refresh();
223 update_utype_info(nullptr);
224 }
225}
226
227/**********************************************************************/
230 void tab_unit::edit_now()
231{
232 if (selected != nullptr) {
233 if (selected->ruledit_dlg == nullptr) {
234 edit_utype *edit = new edit_utype(ui, selected);
235
236 edit->show();
237 selected->ruledit_dlg = edit;
238 } else {
239 ((edit_utype *)selected->ruledit_dlg)->raise();
240 }
241 }
242}
243
244/**********************************************************************/
247 bool tab_unit::initialize_new_utype(struct unit_type *ptype)
248{
249 if (unit_type_by_rule_name("New Unit") != nullptr) {
250 return false;
251 }
252
253 name_set(&(ptype->name), 0, "New Unit");
254 BV_CLR_ALL(ptype->flags);
255
256 return true;
257}
258
259/**********************************************************************/
262 void tab_unit::add_now()
263{
264 struct unit_type *new_utype;
265
266 // Try to reuse freed utype slot
267 unit_type_iterate(ptype) {
268 if (ptype->ruledit_disabled) {
269 if (initialize_new_utype(ptype)) {
270 ptype->ruledit_disabled = false;
271 update_utype_info(ptype);
272 refresh();
273 }
274 return;
275 }
276 } unit_type_iterate_end;
277
278 // Try to add completely new unit type
279 if (game.control.num_unit_types >= U_LAST) {
280 return;
281 }
282
283 // num_unit_types must be big enough to hold new unit or
284 // utype_by_number() fails.
285 game.control.num_unit_types++;
286 new_utype = utype_by_number(game.control.num_unit_types - 1);
287 if (initialize_new_utype(new_utype)) {
288 update_utype_info(new_utype);
289
290 refresh();
291 } else {
292 game.control.num_unit_types--; // Restore
293 }
294}
295
296/**********************************************************************/
299 void tab_unit::same_name_toggle(bool checked)
300{
301 name->setEnabled(!checked);
302 if (checked) {
303 name->setText(rname->text());
304 }
305}
306
307/**********************************************************************/
310 void tab_unit::edit_reqs()
311{
312 if (selected != nullptr) {
313 ui->open_req_edit(QString::fromUtf8(utype_rule_name(selected)),
314 &selected->build_reqs);
315 }
316}
317
318/**********************************************************************/
321 void tab_unit::edit_effects()
322{
323 if (selected != nullptr) {
324 struct universal uni;
325
326 uni.value.utype = selected;
327 uni.kind = VUT_UTYPE;
328
329 ui->open_effect_edit(QString::fromUtf8(utype_rule_name(selected)),
330 &uni, EFMC_NORMAL);
331 }
332}
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
edit_utype
Definition edit_utype.h:32
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_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:116
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:262
tab_unit::edit_effects
void edit_effects()
Definition tab_unit.cpp:321
tab_unit::initialize_new_utype
bool initialize_new_utype(struct unit_type *ptype)
Definition tab_unit.cpp:247
tab_unit::edit_now
void edit_now()
Definition tab_unit.cpp:230
tab_unit::select_unit
void select_unit()
Definition tab_unit.cpp:158
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:299
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:173
tab_unit::update_utype_info
void update_utype_info(struct unit_type *ptype)
Definition tab_unit.cpp:130
tab_unit::edit_reqs
void edit_reqs()
Definition tab_unit.cpp:310
tab_unit::delete_now
void delete_now()
Definition tab_unit.cpp:206
incite_cost
char * incite_cost
Definition comments.c:74
edit_utype.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
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::num_unit_types
int num_unit_types
Definition packets_gen.h:1459
unit_type
Definition unittype.h:507
unit_type::build_reqs
struct requirement_vector build_reqs
Definition unittype.h:527
unit_type::name
struct name_translation name
Definition unittype.h:509
unit_type::ruledit_disabled
bool ruledit_disabled
Definition unittype.h:510
unit_type::ruledit_dlg
void * ruledit_dlg
Definition unittype.h:511
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_unit.h
universals_u::utype
const struct unit_type * utype
Definition fc_types.h:728
unit_type_by_rule_name
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1767
utype_rule_name
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1578
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:871
unit_type_iterate
#define unit_type_iterate(_p)
Definition unittype.h:859
U_LAST
#define U_LAST
Definition unittype.h:40
unit_type_iterate_end
#define unit_type_iterate_end
Definition unittype.h:866
unit_type_re_active_iterate_end
#define unit_type_re_active_iterate_end
Definition unittype.h:875
is_utype_needed
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Definition validity.c:252
validity.h