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/**********************************************************************/
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
50 QLabel *label;
51 QPushButton *button;
52 int row = 0;
53
54 ui = ui_in;
55 selected = 0;
56
57 unit_list = new QListWidget(this);
58
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, row, 0);
70 unit_layout->addWidget(rname, 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, row, 0);
80 unit_layout->addWidget(same_name, row, 1);
81 unit_layout->addWidget(name, 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, 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, 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, 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, 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, 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/**********************************************************************/
126
127/**********************************************************************/
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/**********************************************************************/
159{
161
162 if (!select_list.isEmpty()) {
164
165 un_bytes = select_list.at(0)->text().toUtf8();
167 }
168}
169
170/**********************************************************************/
174{
175 if (selected != nullptr) {
178
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 }
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/**********************************************************************/
207{
208 if (selected != nullptr) {
210
213 return;
214 }
215
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/**********************************************************************/
231{
232 if (selected != nullptr) {
233 if (selected->ruledit_dlg == nullptr) {
235
236 edit->show();
238 } else {
239 ((edit_utype *)selected->ruledit_dlg)->raise();
240 }
241 }
242}
243
244/**********************************************************************/
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 if (ptype->helptext != nullptr) {
256 strvec_clear(ptype->helptext);
257 }
258
259 return true;
260}
261
262/**********************************************************************/
266{
267 struct unit_type *new_utype;
268
269 // Try to reuse freed utype slot
271 if (ptype->ruledit_disabled) {
273 ptype->ruledit_disabled = false;
275 refresh();
276 }
277 return;
278 }
280
281 // Try to add completely new unit type
283 return;
284 }
285
286 // num_unit_types must be big enough to hold new unit or
287 // utype_by_number() fails.
292
293 refresh();
294 } else {
295 game.control.num_unit_types--; // Restore
296 }
297}
298
299/**********************************************************************/
303{
304 name->setEnabled(!checked);
305 if (checked) {
306 name->setText(rname->text());
307 }
308}
309
310/**********************************************************************/
314{
315 if (selected != nullptr) {
316 ui->open_req_edit(QString::fromUtf8(utype_rule_name(selected)),
318 }
319}
320
321/**********************************************************************/
325{
326 if (selected != nullptr) {
327 struct universal uni;
328
329 uni.value.utype = selected;
330 uni.kind = VUT_UTYPE;
331
332 ui->open_effect_edit(QString::fromUtf8(utype_rule_name(selected)),
333 &uni, EFMC_NORMAL);
334 }
335}
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
void display_msg(const char *msg)
void open_req_edit(QString target, struct requirement_vector *preqs)
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:116
struct unit_type * selected
Definition tab_unit.h:48
QLineEdit * rname
Definition tab_unit.h:44
void add_now()
Definition tab_unit.cpp:265
void edit_effects()
Definition tab_unit.cpp:324
bool initialize_new_utype(struct unit_type *ptype)
Definition tab_unit.cpp:247
void edit_now()
Definition tab_unit.cpp:230
void select_unit()
Definition tab_unit.cpp:158
QLineEdit * name
Definition tab_unit.h:43
void same_name_toggle(bool checked)
Definition tab_unit.cpp:302
tab_unit(ruledit_gui *ui_in)
Definition tab_unit.cpp:46
void name_given()
Definition tab_unit.cpp:173
void update_utype_info(struct unit_type *ptype)
Definition tab_unit.cpp:130
void edit_reqs()
Definition tab_unit.cpp:313
void delete_now()
Definition tab_unit.cpp:206
char * incite_cost
Definition comments.c:76
@ EFMC_NORMAL
Definition effect_edit.h:34
#define R__(String)
Definition fcintl.h:75
struct civ_game game
Definition game.c:61
#define edit(pedit)
Definition widget_edit.h:34
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:237
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 requirement_vector build_reqs
Definition unittype.h:527
struct name_translation name
Definition unittype.h:509
bool ruledit_disabled
Definition unittype.h:510
void * ruledit_dlg
Definition unittype.h:511
enum universals_n kind
Definition fc_types.h:608
universals_u value
Definition fc_types.h:607
const struct unit_type * utype
Definition fc_types.h:553
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1773
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1584
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:874
#define unit_type_iterate(_p)
Definition unittype.h:862
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:869
#define unit_type_re_active_iterate_end
Definition unittype.h:878
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Definition validity.c:252