Freeciv21
Develop your civilization from humble roots to a global empire
tab_unit.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // Qt
15 #include <QGridLayout>
16 #include <QLineEdit>
17 #include <QListWidget>
18 #include <QMenu>
19 #include <QPushButton>
20 #include <QRadioButton>
21 
22 // utility
23 #include "fcintl.h"
24 
25 // common
26 #include "game.h"
27 #include "unittype.h"
28 
29 // ruledit
30 #include "edit_utype.h"
31 #include "ruledit.h"
32 #include "ruledit_qt.h"
33 #include "validity.h"
34 
35 #include "tab_unit.h"
36 
40 tab_unit::tab_unit(ruledit_gui *ui_in) : QWidget()
41 {
42  QVBoxLayout *main_layout = new QVBoxLayout(this);
43  QGridLayout *unit_layout = new QGridLayout();
44  QLabel *label;
45  QPushButton *effects_button;
46  QPushButton *add_button;
47  QPushButton *delete_button;
48  QPushButton *edit_button;
49 
50  ui = ui_in;
51  selected = 0;
52 
53  unit_list = new QListWidget(this);
54 
55  connect(unit_list, &QListWidget::itemSelectionChanged, this,
57  main_layout->addWidget(unit_list);
58 
59  unit_layout->setSizeConstraint(QLayout::SetMaximumSize);
60 
61  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
62  label->setParent(this);
63  rname = new QLineEdit(this);
64  rname->setText(QStringLiteral("None"));
65  connect(rname, &QLineEdit::returnPressed, this, &tab_unit::name_given);
66  unit_layout->addWidget(label, 0, 0);
67  unit_layout->addWidget(rname, 0, 2);
68 
69  label = new QLabel(QString::fromUtf8(R__("Name")));
70  label->setParent(this);
71  same_name = new QRadioButton();
72  connect(same_name, &QAbstractButton::toggled, this,
74  name = new QLineEdit(this);
75  name->setText(QStringLiteral("None"));
76  connect(name, &QLineEdit::returnPressed, this, &tab_unit::name_given);
77  unit_layout->addWidget(label, 1, 0);
78  unit_layout->addWidget(same_name, 1, 1);
79  unit_layout->addWidget(name, 1, 2);
80 
81  edit_button = new QPushButton(QString::fromUtf8(R__("Edit Unit")), this);
82  connect(edit_button, &QAbstractButton::pressed, this, &tab_unit::edit_now);
83  unit_layout->addWidget(edit_button, 2, 2);
84 
85  effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
86  connect(effects_button, &QAbstractButton::pressed, this,
88  unit_layout->addWidget(effects_button, 3, 2);
89 
90  add_button = new QPushButton(QString::fromUtf8(R__("Add Unit")), this);
91  connect(add_button, &QAbstractButton::pressed, this, &tab_unit::add_now);
92  unit_layout->addWidget(add_button, 4, 0);
93  show_experimental(add_button);
94 
95  delete_button =
96  new QPushButton(QString::fromUtf8(R__("Remove this Unit")), this);
97  connect(delete_button, &QAbstractButton::pressed, this,
99  unit_layout->addWidget(delete_button, 4, 2);
100  show_experimental(delete_button);
101 
102  refresh();
103 
104  main_layout->addLayout(unit_layout);
105 
106  setLayout(main_layout);
107 }
108 
113 {
114  unit_list->clear();
115 
116  unit_type_iterate(ptype)
117  {
118  if (!ptype->ruledit_disabled) {
119  QListWidgetItem *item = new QListWidgetItem(utype_rule_name(ptype));
120 
121  unit_list->insertItem(utype_index(ptype), item);
122  }
123  }
125 }
126 
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(QStringLiteral("None"));
149  rname->setText(QStringLiteral("None"));
150  same_name->setChecked(true);
151  name->setEnabled(false);
152  }
153 }
154 
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 
174 {
175  if (selected != nullptr) {
176  QByteArray name_bytes;
177  QByteArray rname_bytes;
178 
179  unit_type_iterate(ptype)
180  {
181  if (ptype != selected && !ptype->ruledit_disabled) {
182  rname_bytes = rname->text().toUtf8();
183  if (!strcmp(utype_rule_name(ptype), rname_bytes.data())) {
184  ui->display_msg(R__("A unit type with that rule name already "
185  "exists!"));
186  return;
187  }
188  }
189  }
191 
192  if (same_name->isChecked()) {
193  name->setText(rname->text());
194  }
195 
196  name_bytes = name->text().toUtf8();
197  rname_bytes = rname->text().toUtf8();
198  names_set(&(selected->name), 0, name_bytes.data(), rname_bytes.data());
199  refresh();
200  }
201 }
202 
207 {
208  if (selected != 0) {
209  requirers_dlg *requirers;
210 
213  requirers)) {
214  return;
215  }
216 
217  selected->ruledit_disabled = true;
218 
219  refresh();
220  update_utype_info(nullptr);
221  }
222 }
223 
228 {
229  if (selected != nullptr) {
230  edit_utype *edit = new edit_utype(ui, selected);
231 
232  edit->show();
233  }
234 }
235 
240 {
241  if (unit_type_by_rule_name("New Unit") != nullptr) {
242  return false;
243  }
244 
245  name_set(&(ptype->name), 0, "New Unit");
246  return true;
247 }
248 
253 {
254  struct unit_type *new_utype;
255 
256  // Try to reuse freed utype slot
257  unit_type_iterate(ptype)
258  {
259  if (ptype->ruledit_disabled) {
260  if (initialize_new_utype(ptype)) {
261  ptype->ruledit_disabled = false;
262  update_utype_info(ptype);
263  refresh();
264  }
265  return;
266  }
267  }
269 
270  // Try to add completely new unit type
271  if (game.control.num_unit_types >= U_LAST) {
272  return;
273  }
274 
275  // num_unit_types must be big enough to hold new unit or
276  // utype_by_number() fails.
277  game.control.num_unit_types++;
278  new_utype = utype_by_number(game.control.num_unit_types - 1);
279  if (initialize_new_utype(new_utype)) {
280  update_utype_info(new_utype);
281 
282  refresh();
283  } else {
284  game.control.num_unit_types--; // Restore
285  }
286 }
287 
291 void tab_unit::same_name_toggle(bool checked)
292 {
293  name->setEnabled(!checked);
294  if (checked) {
295  name->setText(rname->text());
296  }
297 }
298 
303 {
304  if (selected != nullptr) {
305  struct universal uni;
306 
307  uni.value.utype = selected;
308  uni.kind = VUT_UTYPE;
309 
310  ui->open_effect_edit(QString::fromUtf8(utype_rule_name(selected)), &uni,
311  EFMC_NORMAL);
312  }
313 }
void display_msg(const char *msg)
Display status message.
Definition: ruledit_qt.cpp:240
requirers_dlg * create_requirers(const char *title)
Create requirers dlg.
Definition: ruledit_qt.cpp:248
void open_effect_edit(const QString &target, struct universal *uni, enum effect_filter_main_class efmc)
Open effect_edit dialog.
Definition: ruledit_qt.cpp:350
QListWidget * unit_list
Definition: tab_unit.h:42
ruledit_gui * ui
Definition: tab_unit.h:36
void refresh()
Refresh the information.
Definition: tab_unit.cpp:112
struct unit_type * selected
Definition: tab_unit.h:45
QLineEdit * rname
Definition: tab_unit.h:41
QRadioButton * same_name
Definition: tab_unit.h:43
void add_now()
User requested new unit.
Definition: tab_unit.cpp:252
void edit_effects()
User wants to edit effects.
Definition: tab_unit.cpp:302
bool initialize_new_utype(struct unit_type *ptype)
Initialize new tech for use.
Definition: tab_unit.cpp:239
void edit_now()
User requested unit edit dialog.
Definition: tab_unit.cpp:227
void select_unit()
User selected unit from the list.
Definition: tab_unit.cpp:158
QLineEdit * name
Definition: tab_unit.h:40
void same_name_toggle(bool checked)
Toggled whether rule_name and name should be kept identical.
Definition: tab_unit.cpp:291
tab_unit(ruledit_gui *ui_in)
Setup tab_unit object.
Definition: tab_unit.cpp:40
void name_given()
User entered name for the unit.
Definition: tab_unit.cpp:173
void update_utype_info(struct unit_type *ptype)
Update info of the unit.
Definition: tab_unit.cpp:130
void delete_now()
User requested unit deletion.
Definition: tab_unit.cpp:206
@ EFMC_NORMAL
Definition: effect_edit.h:32
#define R__(String)
Definition: fcintl.h:58
struct civ_game game
Definition: game.cpp:47
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)
Show widget if experimental features enabled, hide otherwise.
Definition: ruledit.cpp:163
void ruledit_qt_display_requirers(const char *msg, void *data)
Display requirer list.
Definition: ruledit_qt.cpp:67
struct packet_ruleset_control control
Definition: game.h:74
Definition: climisc.h:66
struct name_translation name
Definition: unittype.h:467
bool ruledit_disabled
Definition: unittype.h:468
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
const struct unit_type * utype
Definition: fc_types.h:585
struct unit_type * utype_by_number(const Unit_type_id id)
Return a pointer for the unit type struct for the given unit type id.
Definition: unittype.cpp:103
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
struct unit_type * unit_type_by_rule_name(const char *name)
Returns the unit type that has the given (untranslated) rule name.
Definition: unittype.cpp:1444
Unit_type_id utype_index(const struct unit_type *punittype)
Return the unit type index.
Definition: unittype.cpp:82
#define unit_type_iterate(_p)
Definition: unittype.h:785
#define U_LAST
Definition: unittype.h:31
#define unit_type_iterate_end
Definition: unittype.h:791
bool is_utype_needed(struct unit_type *ptype, requirers_cb cb, void *data)
Check if anything in ruleset needs unit type.
Definition: validity.cpp:229