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/**********************************************************************/
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/**********************************************************************/
115{
116 unit_list->clear();
117
119 QListWidgetItem *item = new QListWidgetItem(utype_rule_name(ptype));
120
121 unit_list->insertItem(utype_index(ptype), item);
123}
124
125/**********************************************************************/
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/**********************************************************************/
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();
165 }
166}
167
168/**********************************************************************/
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 }
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/**********************************************************************/
205{
206 if (selected != nullptr) {
207 requirers_dlg *requirers;
208
211 return;
212 }
213
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/**********************************************************************/
229{
230 if (selected != nullptr) {
231 if (selected->ruledit_dlg == nullptr) {
233
234 edit->show();
236 } else {
237 ((edit_utype *)selected->ruledit_dlg)->raise();
238 }
239 }
240}
241
242/**********************************************************************/
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 if (ptype->helptext != nullptr) {
254 strvec_clear(ptype->helptext);
255 }
256
257 return true;
258}
259
260/**********************************************************************/
264{
265 struct unit_type *new_utype;
266
267 // Try to reuse freed utype slot
268 unit_type_iterate(ptype) {
269 if (ptype->ruledit_disabled) {
270 if (initialize_new_utype(ptype)) {
271 ptype->ruledit_disabled = false;
272 update_utype_info(ptype);
273 refresh();
274 }
275 return;
276 }
278
279 // Try to add completely new unit type
281 return;
282 }
283
284 // num_unit_types must be big enough to hold new unit or
285 // utype_by_number() fails.
288 if (initialize_new_utype(new_utype)) {
289 update_utype_info(new_utype);
290
291 refresh();
292 } else {
293 game.control.num_unit_types--; // Restore
294 }
295}
296
297/**********************************************************************/
301{
302 name->setEnabled(!checked);
303 if (checked) {
304 name->setText(rname->text());
305 }
306}
307
308/**********************************************************************/
312{
313 if (selected != nullptr) {
314 struct universal uni;
315
316 uni.value.utype = selected;
317 uni.kind = VUT_UTYPE;
318
319 ui->open_effect_edit(QString::fromUtf8(utype_rule_name(selected)),
320 &uni, EFMC_NORMAL);
321 }
322}
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
void display_msg(const char *msg)
requirers_dlg * create_requirers(const char *title)
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
QListWidget * unit_list
Definition tab_unit.h:45
QCheckBox * same_name
Definition tab_unit.h:46
ruledit_gui * ui
Definition tab_unit.h:39
void refresh()
Definition tab_unit.cpp:114
struct unit_type * selected
Definition tab_unit.h:48
QLineEdit * rname
Definition tab_unit.h:44
void add_now()
Definition tab_unit.cpp:263
void edit_effects()
Definition tab_unit.cpp:311
bool initialize_new_utype(struct unit_type *ptype)
Definition tab_unit.cpp:245
void edit_now()
Definition tab_unit.cpp:228
void select_unit()
Definition tab_unit.cpp:156
QLineEdit * name
Definition tab_unit.h:43
void same_name_toggle(bool checked)
Definition tab_unit.cpp:300
tab_unit(ruledit_gui *ui_in)
Definition tab_unit.cpp:46
void name_given()
Definition tab_unit.cpp:171
void update_utype_info(struct unit_type *ptype)
Definition tab_unit.cpp:128
void delete_now()
Definition tab_unit.cpp:204
@ EFMC_NORMAL
Definition effect_edit.h:33
#define R__(String)
Definition fcintl.h:75
struct civ_game game
Definition game.c:57
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
static const char * untranslated_name(const struct name_translation *ptrans)
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:216
void ruledit_qt_display_requirers(const char *msg, void *data)
void strvec_clear(struct strvec *psv)
struct packet_ruleset_control control
Definition game.h:83
Definition climisc.h:82
struct strvec * helptext
Definition unittype.h:552
struct name_translation name
Definition unittype.h:483
bv_unit_type_flags flags
Definition unittype.h:515
bool ruledit_disabled
Definition unittype.h:484
void * ruledit_dlg
Definition unittype.h:485
enum universals_n kind
Definition fc_types.h:758
universals_u value
Definition fc_types.h:757
const struct unit_type * utype
Definition fc_types.h:604
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1819
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
struct unit_type * utype_by_number(const Unit_type_id id)
Definition unittype.c:112
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
#define unit_type_re_active_iterate(_p)
Definition unittype.h:853
#define unit_type_iterate(_p)
Definition unittype.h:841
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:848
#define unit_type_re_active_iterate_end
Definition unittype.h:857
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Definition validity.c:242
#define edit(pedit)
Definition widget_edit.h:34