Freeciv21
Develop your civilization from humble roots to a global empire
tab_building.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 "improvement.h"
28 
29 // ruledit
30 #include "ruledit.h"
31 #include "ruledit_qt.h"
32 #include "validity.h"
33 
34 #include "tab_building.h"
35 
40 {
41  QVBoxLayout *main_layout = new QVBoxLayout(this);
42  QGridLayout *bldg_layout = new QGridLayout();
43  QLabel *label;
44  QPushButton *add_button;
45  QPushButton *delete_button;
46  QPushButton *reqs_button;
47  QPushButton *effects_button;
48 
49  ui = ui_in;
50  selected = 0;
51 
52  bldg_list = new QListWidget(this);
53 
54  connect(bldg_list, &QListWidget::itemSelectionChanged, this,
56  main_layout->addWidget(bldg_list);
57 
58  bldg_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_building::name_given);
65  bldg_layout->addWidget(label, 0, 0);
66  bldg_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_building::name_given);
76  bldg_layout->addWidget(label, 1, 0);
77  bldg_layout->addWidget(same_name, 1, 1);
78  bldg_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  bldg_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  bldg_layout->addWidget(effects_button, 3, 2);
90 
91  add_button = new QPushButton(QString::fromUtf8(R__("Add Building")), this);
92  connect(add_button, &QAbstractButton::pressed, this,
94  bldg_layout->addWidget(add_button, 4, 0);
95  show_experimental(add_button);
96 
97  delete_button =
98  new QPushButton(QString::fromUtf8(R__("Remove this Building")), this);
99  connect(delete_button, &QAbstractButton::pressed, this,
101  bldg_layout->addWidget(delete_button, 4, 2);
102  show_experimental(delete_button);
103 
104  refresh();
105 
106  main_layout->addLayout(bldg_layout);
107 
108  setLayout(main_layout);
109 }
110 
115 {
116  bldg_list->clear();
117 
118  improvement_iterate(pimpr)
119  {
120  if (!pimpr->ruledit_disabled) {
121  QListWidgetItem *item =
122  new QListWidgetItem(improvement_rule_name(pimpr));
123 
124  bldg_list->insertItem(improvement_index(pimpr), item);
125  }
126  }
128 }
129 
134 {
135  selected = pimpr;
136 
137  if (selected != 0) {
138  QString dispn = QString::fromUtf8(untranslated_name(&(pimpr->name)));
139  QString rulen = QString::fromUtf8(improvement_rule_name(pimpr));
140 
141  name->setText(dispn);
142  rname->setText(rulen);
143  if (dispn == rulen) {
144  name->setEnabled(false);
145  same_name->setChecked(true);
146  } else {
147  same_name->setChecked(false);
148  name->setEnabled(true);
149  }
150  } else {
151  name->setText(QStringLiteral("None"));
152  rname->setText(QStringLiteral("None"));
153  same_name->setChecked(true);
154  name->setEnabled(false);
155  }
156 }
157 
162 {
163  QList<QListWidgetItem *> select_list = bldg_list->selectedItems();
164 
165  if (!select_list.isEmpty()) {
166  QByteArray bn_bytes;
167 
168  bn_bytes = select_list.at(0)->text().toUtf8();
169  update_bldg_info(improvement_by_rule_name(bn_bytes.data()));
170  }
171 }
172 
177 {
178  if (selected != nullptr) {
179  QByteArray name_bytes;
180  QByteArray rname_bytes;
181 
182  improvement_iterate(pimpr)
183  {
184  if (pimpr != selected && !pimpr->ruledit_disabled) {
185  rname_bytes = rname->text().toUtf8();
186  if (!strcmp(improvement_rule_name(pimpr), rname_bytes.data())) {
187  ui->display_msg(R__("A building with that rule name already "
188  "exists!"));
189  return;
190  }
191  }
192  }
194 
195  if (same_name->isChecked()) {
196  name->setText(rname->text());
197  }
198 
199  name_bytes = name->text().toUtf8();
200  rname_bytes = rname->text().toUtf8();
201  names_set(&(selected->name), 0, name_bytes.data(), rname_bytes.data());
202  refresh();
203  }
204 }
205 
210 {
211  if (selected != 0) {
212  requirers_dlg *requirers;
213 
216  requirers)) {
217  return;
218  }
219 
220  selected->ruledit_disabled = true;
221 
222  refresh();
223  update_bldg_info(nullptr);
224  }
225 }
226 
231 {
232  if (improvement_by_rule_name("New Building") != nullptr) {
233  return false;
234  }
235 
236  name_set(&(pimpr->name), 0, "New Building");
237  return true;
238 }
239 
244 {
245  struct impr_type *new_bldg;
246 
247  // Try to reuse freed building slot
248  improvement_iterate(pimpr)
249  {
250  if (pimpr->ruledit_disabled) {
251  if (initialize_new_bldg(pimpr)) {
252  pimpr->ruledit_disabled = false;
253  update_bldg_info(pimpr);
254  refresh();
255  }
256  return;
257  }
258  }
260 
261  // Try to add completely new building
262  if (game.control.num_impr_types >= B_LAST) {
263  return;
264  }
265 
266  // num_impr_types must be big enough to hold new building or
267  // improvement_by_number() fails.
268  game.control.num_impr_types++;
269  new_bldg = improvement_by_number(game.control.num_impr_types - 1);
270  if (initialize_new_bldg(new_bldg)) {
271  update_bldg_info(new_bldg);
272 
273  refresh();
274  } else {
275  game.control.num_impr_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(improvement_rule_name(selected)),
297  &selected->reqs);
298  }
299 }
300 
305 {
306  if (selected != nullptr) {
307  struct universal uni;
308 
309  uni.value.building = selected;
310  uni.kind = VUT_IMPROVEMENT;
311 
312  ui->open_effect_edit(QString::fromUtf8(improvement_rule_name(selected)),
313  &uni, 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
struct impr_type * selected
Definition: tab_building.h:45
void add_now2()
User requested new building.
void refresh()
Refresh the information.
void edit_reqs()
User wants to edit reqs.
bool initialize_new_bldg(struct impr_type *pimpr)
Initialize new tech for use.
ruledit_gui * ui
Definition: tab_building.h:36
void name_given()
User entered name for the building.
void update_bldg_info(struct impr_type *pimpr)
Update info of the building.
void select_bldg()
User selected building from the list.
tab_building(ruledit_gui *ui_in)
Setup tab_building object.
QListWidget * bldg_list
Definition: tab_building.h:42
void edit_effects()
User wants to edit effects.
QLineEdit * rname
Definition: tab_building.h:41
QRadioButton * same_name
Definition: tab_building.h:43
void delete_now()
User requested building deletion.
void same_name_toggle(bool checked)
Toggled whether rule_name and name should be kept identical.
QLineEdit * name
Definition: tab_building.h:40
@ EFMC_NORMAL
Definition: effect_edit.h:32
#define R__(String)
Definition: fcintl.h:58
struct civ_game game
Definition: game.cpp:47
const char * improvement_rule_name(const struct impr_type *pimprove)
Return the (untranslated) rule name of the improvement.
struct impr_type * improvement_by_rule_name(const char *name)
Does a linear search of improvement_types[].name.vernacular Returns nullptr when none match.
struct impr_type * improvement_by_number(const Impr_type_id id)
Returns the improvement type for the given index/ID.
Impr_type_id improvement_index(const struct impr_type *pimprove)
Return the improvement index.
#define improvement_iterate_end
Definition: improvement.h:199
#define improvement_iterate(_p)
Definition: improvement.h:193
#define B_LAST
Definition: improvement.h:33
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: improvement.h:62
struct requirement_vector reqs
Definition: improvement.h:66
struct name_translation name
Definition: improvement.h:61
Definition: climisc.h:66
enum universals_n kind
Definition: fc_types.h:740
universals_u value
Definition: fc_types.h:739
const struct impr_type * building
Definition: fc_types.h:579
bool is_building_needed(struct impr_type *pimpr, requirers_cb cb, void *data)
Check if anything in ruleset needs building.
Definition: validity.cpp:215