Freeciv21
Develop your civilization from humble roots to a global empire
tab_extras.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 "extras.h"
27 #include "game.h"
28 
29 // ruledit
30 #include "ruledit.h"
31 #include "ruledit_qt.h"
32 #include "validity.h"
33 
34 #include "tab_extras.h"
35 
40 {
41  QVBoxLayout *main_layout = new QVBoxLayout(this);
42  QGridLayout *extra_layout = new QGridLayout();
43  QLabel *label;
44  QPushButton *effects_button;
45  QPushButton *add_button;
46  QPushButton *delete_button;
47  QPushButton *reqs_button;
48 
49  ui = ui_in;
50  selected = 0;
51 
52  extra_list = new QListWidget(this);
53 
54  connect(extra_list, &QListWidget::itemSelectionChanged, this,
56  main_layout->addWidget(extra_list);
57 
58  extra_layout->setSizeConstraint(QLayout::SetMaximumSize);
59 
60  label = new QLabel(QString::fromUtf8(R__("Rule Name")));
61  label->setParent(this);
62  rname = new QLineEdit(this);
63  rname->setText(QStringLiteral("None"));
64  connect(rname, &QLineEdit::returnPressed, this, &tab_extras::name_given);
65  extra_layout->addWidget(label, 0, 0);
66  extra_layout->addWidget(rname, 0, 2);
67 
68  label = new QLabel(QString::fromUtf8(R__("Name")));
69  label->setParent(this);
70  same_name = new QRadioButton();
71  connect(same_name, &QAbstractButton::toggled, this,
73  name = new QLineEdit(this);
74  name->setText(QStringLiteral("None"));
75  connect(name, &QLineEdit::returnPressed, this, &tab_extras::name_given);
76  extra_layout->addWidget(label, 1, 0);
77  extra_layout->addWidget(same_name, 1, 1);
78  extra_layout->addWidget(name, 1, 2);
79 
80  reqs_button =
81  new QPushButton(QString::fromUtf8(R__("Requirements")), this);
82  connect(reqs_button, &QAbstractButton::pressed, this,
84  extra_layout->addWidget(reqs_button, 2, 2);
85 
86  effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
87  connect(effects_button, &QAbstractButton::pressed, this,
89  extra_layout->addWidget(effects_button, 3, 2);
90 
91  add_button = new QPushButton(QString::fromUtf8(R__("Add Extra")), this);
92  connect(add_button, &QAbstractButton::pressed, this, &tab_extras::add_now);
93  extra_layout->addWidget(add_button, 4, 0);
94  show_experimental(add_button);
95 
96  delete_button =
97  new QPushButton(QString::fromUtf8(R__("Remove this Extra")), this);
98  connect(delete_button, &QAbstractButton::pressed, this,
100  extra_layout->addWidget(delete_button, 4, 2);
101  show_experimental(delete_button);
102 
103  refresh();
104 
105  main_layout->addLayout(extra_layout);
106 
107  setLayout(main_layout);
108 }
109 
114 {
115  extra_list->clear();
116 
117  extra_type_iterate(pextra)
118  {
119  if (!pextra->ruledit_disabled) {
120  QListWidgetItem *item =
121  new QListWidgetItem(QString::fromUtf8(extra_rule_name(pextra)));
122 
123  extra_list->insertItem(extra_index(pextra), item);
124  }
125  }
127 }
128 
133 {
134  selected = pextra;
135 
136  if (selected != nullptr) {
137  QString dispn = QString::fromUtf8(untranslated_name(&(pextra->name)));
138  QString rulen = QString::fromUtf8(extra_rule_name(pextra));
139 
140  name->setText(dispn);
141  rname->setText(rulen);
142  if (dispn == rulen) {
143  name->setEnabled(false);
144  same_name->setChecked(true);
145  } else {
146  same_name->setChecked(false);
147  name->setEnabled(true);
148  }
149  } else {
150  name->setText(QStringLiteral("None"));
151  rname->setText(QStringLiteral("None"));
152  same_name->setChecked(true);
153  name->setEnabled(false);
154  }
155 }
156 
161 {
162  QList<QListWidgetItem *> select_list = extra_list->selectedItems();
163 
164  if (!select_list.isEmpty()) {
165  QByteArray en_bytes;
166 
167  en_bytes = select_list.at(0)->text().toUtf8();
168  update_extra_info(extra_type_by_rule_name(en_bytes.data()));
169  }
170 }
171 
176 {
177  if (selected != nullptr) {
178  QByteArray name_bytes;
179  QByteArray rname_bytes;
180 
181  extra_type_iterate(pextra)
182  {
183  if (pextra != selected && !pextra->ruledit_disabled) {
184  rname_bytes = rname->text().toUtf8();
185  if (!strcmp(extra_rule_name(pextra), rname_bytes.data())) {
186  ui->display_msg(
187  R__("An extra with that rule name already exists!"));
188  return;
189  }
190  }
191  }
193 
194  if (same_name->isChecked()) {
195  name->setText(rname->text());
196  }
197 
198  name_bytes = name->text().toUtf8();
199  rname_bytes = rname->text().toUtf8();
200  names_set(&(selected->name), 0, name_bytes.data(), rname_bytes.data());
201  refresh();
202  }
203 }
204 
209 {
210  if (selected != nullptr) {
211  requirers_dlg *requirers;
212 
215  requirers)) {
216  return;
217  }
218 
219  selected->ruledit_disabled = true;
220 
221  refresh();
222  update_extra_info(nullptr);
223  }
224 }
225 
230 {
231  if (extra_type_by_rule_name("New Extra") != nullptr) {
232  return false;
233  }
234 
235  name_set(&(pextra->name), 0, "New Extra");
236 
237  return true;
238 }
239 
244 {
245  struct extra_type *new_extra;
246 
247  // Try to reuse freed extra slot
248  extra_type_iterate(pextra)
249  {
250  if (pextra->ruledit_disabled) {
251  if (initialize_new_extra(pextra)) {
252  pextra->ruledit_disabled = false;
253  update_extra_info(pextra);
254  refresh();
255  }
256  return;
257  }
258  }
260 
261  // Try to add completely new extra
262  if (game.control.num_extra_types >= MAX_EXTRA_TYPES) {
263  return;
264  }
265 
266  // num_extra_types must be big enough to hold new extra or
267  // extra_by_number() fails.
268  game.control.num_extra_types++;
269  new_extra = extra_by_number(game.control.num_extra_types - 1);
270  if (initialize_new_extra(new_extra)) {
271  update_extra_info(new_extra);
272 
273  refresh();
274  } else {
275  game.control.num_extra_types--; // Restore
276  }
277 }
278 
283 {
284  name->setEnabled(!checked);
285  if (checked) {
286  name->setText(rname->text());
287  }
288 }
289 
294 {
295  if (selected != nullptr) {
296  ui->open_req_edit(QString::fromUtf8(extra_rule_name(selected)),
297  &selected->reqs);
298  }
299 }
300 
305 {
306  if (selected != nullptr) {
307  struct universal uni;
308 
309  uni.value.extra = selected;
310  uni.kind = VUT_EXTRA;
311 
312  ui->open_effect_edit(QString::fromUtf8(extra_rule_name(selected)), &uni,
313  EFMC_NORMAL);
314  }
315 }
void display_msg(const char *msg)
Display status message.
Definition: ruledit_qt.cpp:240
void open_req_edit(const QString &target, struct requirement_vector *preqs)
Open req_edit dialog.
Definition: ruledit_qt.cpp:278
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
void same_name_toggle(bool checked)
Toggled whether rule_name and name should be kept identical.
Definition: tab_extras.cpp:282
void add_now()
User requested new extra.
Definition: tab_extras.cpp:243
void update_extra_info(struct extra_type *pextra)
Update info of the extra.
Definition: tab_extras.cpp:132
QListWidget * extra_list
Definition: tab_extras.h:42
void name_given()
User entered name for the extra.
Definition: tab_extras.cpp:175
tab_extras(ruledit_gui *ui_in)
Setup tab_extras object.
Definition: tab_extras.cpp:39
QLineEdit * name
Definition: tab_extras.h:40
QRadioButton * same_name
Definition: tab_extras.h:43
QLineEdit * rname
Definition: tab_extras.h:41
void refresh()
Refresh the information.
Definition: tab_extras.cpp:113
void delete_now()
User requested extra deletion.
Definition: tab_extras.cpp:208
struct extra_type * selected
Definition: tab_extras.h:45
void edit_effects()
User wants to edit effects.
Definition: tab_extras.cpp:304
ruledit_gui * ui
Definition: tab_extras.h:36
void edit_reqs()
User wants to edit reqs.
Definition: tab_extras.cpp:293
bool initialize_new_extra(struct extra_type *pextra)
Initialize new extra for use.
Definition: tab_extras.cpp:229
void select_extra()
User selected extra from the list.
Definition: tab_extras.cpp:160
@ EFMC_NORMAL
Definition: effect_edit.h:32
struct extra_type * extra_type_by_rule_name(const char *name)
Returns extra type matching rule name or nullptr if there is no extra type with such name.
Definition: extras.cpp:183
struct extra_type * extra_by_number(int id)
Return extras type of given id.
Definition: extras.cpp:154
const char * extra_rule_name(const struct extra_type *pextra)
Return the (untranslated) rule name of the extra type.
Definition: extras.cpp:174
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
#define extra_index(_e_)
Definition: extras.h:163
#define MAX_EXTRA_TYPES
Definition: fc_types.h:42
#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
bool ruledit_disabled
Definition: extras.h:77
struct requirement_vector reqs
Definition: extras.h:90
struct name_translation name
Definition: extras.h:76
Definition: climisc.h:66
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
struct extra_type * extra
Definition: fc_types.h:586
bool is_extra_needed(struct extra_type *pextra, requirers_cb cb, void *data)
Check if anything in ruleset needs extra type.
Definition: validity.cpp:255