Freeciv-3.1: /fast/freeciv/nightly/S3_1/tools/ruledit/tab_multiplier.cpp Source File

Freeciv-3.1

Loading...
Searching...
No Matches
tab_multiplier.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 "multipliers.h"
34
35// ruledit
36#include "req_edit.h"
37#include "ruledit.h"
38#include "ruledit_qt.h"
39#include "validity.h"
40
41#include "tab_multiplier.h"
42
43/**********************************************************************/
46tab_multiplier::tab_multiplier(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *mpr_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *add_button;
52 QPushButton *delete_button;
53 QPushButton *reqs_button;
54
55 ui = ui_in;
56 selected = 0;
57
58 mpr_list = new QListWidget(this);
59
60 connect(mpr_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_multiplier()));
61 main_layout->addWidget(mpr_list);
62
63 mpr_layout->setSizeConstraint(QLayout::SetMaximumSize);
64
65 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
66 label->setParent(this);
67 rname = new QLineEdit(this);
68 rname->setText(R__("None"));
69 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
70 mpr_layout->addWidget(label, 0, 0);
71 mpr_layout->addWidget(rname, 0, 2);
72
73 label = new QLabel(QString::fromUtf8(R__("Name")));
74 label->setParent(this);
75 same_name = new QCheckBox();
76 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
77 name = new QLineEdit(this);
78 name->setText(R__("None"));
79 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
80 mpr_layout->addWidget(label, 1, 0);
81 mpr_layout->addWidget(same_name, 1, 1);
82 mpr_layout->addWidget(name, 1, 2);
83
84 reqs_button = new QPushButton(QString::fromUtf8(R__("Requirements")), this);
85 connect(reqs_button, SIGNAL(pressed()), this, SLOT(edit_reqs()));
86 mpr_layout->addWidget(reqs_button, 2, 2);
87
88 add_button = new QPushButton(QString::fromUtf8(R__("Add Multiplier")), this);
89 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
90 mpr_layout->addWidget(add_button, 4, 0);
91 show_experimental(add_button);
92
93 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Multiplier")), this);
94 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
95 mpr_layout->addWidget(delete_button, 4, 2);
96 show_experimental(delete_button);
97
98 refresh();
99 update_multiplier_info(nullptr);
100
101 main_layout->addLayout(mpr_layout);
102
103 setLayout(main_layout);
104}
105
106/**********************************************************************/
109 void tab_multiplier::refresh()
110{
111 mpr_list->clear();
112
113 multipliers_re_active_iterate(pmul) {
114 QListWidgetItem *item
115 = new QListWidgetItem(QString::fromUtf8(multiplier_rule_name(pmul)));
116
117 mpr_list->insertItem(multiplier_index(pmul), item);
118 } multipliers_re_active_iterate_end;
119}
120
121/**********************************************************************/
124 void tab_multiplier::update_multiplier_info(struct multiplier *pmul)
125{
126 selected = pmul;
127
128 if (selected != 0) {
129 QString dispn = QString::fromUtf8(untranslated_name(&(pmul->name)));
130 QString rulen = QString::fromUtf8(multiplier_rule_name(pmul));
131
132 name->setText(dispn);
133 rname->setText(rulen);
134 if (dispn == rulen) {
135 name->setEnabled(false);
136 same_name->setChecked(true);
137 } else {
138 same_name->setChecked(false);
139 name->setEnabled(true);
140 }
141 } else {
142 name->setText(R__("None"));
143 rname->setText(R__("None"));
144 same_name->setChecked(true);
145 name->setEnabled(false);
146 }
147}
148
149/**********************************************************************/
152 void tab_multiplier::select_multiplier()
153{
154 QList<QListWidgetItem *> select_list = mpr_list->selectedItems();
155
156 if (!select_list.isEmpty()) {
157 QByteArray mn_bytes;
158
159 mn_bytes = select_list.at(0)->text().toUtf8();
160 update_multiplier_info(multiplier_by_rule_name(mn_bytes.data()));
161 }
162}
163
164/**********************************************************************/
167 void tab_multiplier::name_given()
168{
169 if (selected != nullptr) {
170 QByteArray name_bytes;
171 QByteArray rname_bytes;
172
173 multipliers_iterate(pmul) {
174 if (pmul != selected && !pmul->ruledit_disabled) {
175 rname_bytes = rname->text().toUtf8();
176 if (!strcmp(multiplier_rule_name(pmul), rname_bytes.data())) {
177 ui->display_msg(R__("A multiplier with that rule name already exists!"));
178 return;
179 }
180 }
181 } multipliers_iterate_end;
182
183 if (same_name->isChecked()) {
184 name->setText(rname->text());
185 }
186
187 name_bytes = name->text().toUtf8();
188 rname_bytes = rname->text().toUtf8();
189 names_set(&(selected->name), 0,
190 name_bytes.data(),
191 rname_bytes.data());
192 refresh();
193 }
194}
195
196/**********************************************************************/
199 void tab_multiplier::delete_now()
200{
201 if (selected != 0) {
202 requirers_dlg *requirers;
203
204 requirers = ui->create_requirers(multiplier_rule_name(selected));
205 if (is_multiplier_needed(selected, &ruledit_qt_display_requirers, requirers)) {
206 return;
207 }
208
209 selected->ruledit_disabled = true;
210
211 refresh();
212 update_multiplier_info(nullptr);
213 }
214}
215
216/**********************************************************************/
219 bool tab_multiplier::initialize_new_multiplier(struct multiplier *pmul)
220{
221 if (multiplier_by_rule_name("New Multiplier") != nullptr) {
222 return false;
223 }
224
225 name_set(&(pmul->name), 0, "New Multiplier");
226
227 return true;
228}
229
230/**********************************************************************/
233 void tab_multiplier::add_now()
234{
235 struct multiplier *new_multiplier;
236
237 // Try to reuse freed multiplier slot
238 multipliers_iterate(pmul) {
239 if (pmul->ruledit_disabled) {
240 if (initialize_new_multiplier(pmul)) {
241 pmul->ruledit_disabled = false;
242 update_multiplier_info(pmul);
243 refresh();
244 }
245 return;
246 }
247 } multipliers_iterate_end;
248
249 // Try to add completely new multiplier
250 if (game.control.num_multipliers >= MAX_NUM_MULTIPLIERS) {
251 return;
252 }
253
254 // num_multipliers must be big enough to hold new multiplier or
255 // multiplier_by_number() fails.
256 game.control.num_multipliers++;
257 new_multiplier = multiplier_by_number(game.control.num_multipliers - 1);
258 if (initialize_new_multiplier(new_multiplier)) {
259 update_multiplier_info(new_multiplier);
260
261 refresh();
262 } else {
263 game.control.num_multipliers--; // Restore
264 }
265}
266
267/**********************************************************************/
270 void tab_multiplier::same_name_toggle(bool checked)
271{
272 name->setEnabled(!checked);
273 if (checked) {
274 name->setText(rname->text());
275 }
276}
277
278/**********************************************************************/
281 void tab_multiplier::edit_reqs()
282{
283 if (selected != nullptr) {
284 req_edit *redit = new req_edit(ui, QString::fromUtf8(multiplier_rule_name(selected)),
285 &selected->reqs);
286
287 redit->show();
288 }
289}
req_edit
Definition req_edit.h:36
requirers_dlg
Definition requirers_dlg.h:28
ruledit_gui
Definition ruledit_qt.h:96
ruledit_gui::display_msg
void display_msg(const char *msg)
Definition ruledit_qt.cpp:289
ruledit_gui::create_requirers
requirers_dlg * create_requirers(const char *title)
Definition ruledit_qt.cpp:297
tab_multiplier::ui
ruledit_gui * ui
Definition tab_multiplier.h:39
tab_multiplier::tab_multiplier
tab_multiplier(ruledit_gui *ui_in)
Definition tab_multiplier.cpp:46
tab_multiplier::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_multiplier.cpp:270
tab_multiplier::edit_reqs
void edit_reqs()
Definition tab_multiplier.cpp:281
tab_multiplier::delete_now
void delete_now()
Definition tab_multiplier.cpp:199
tab_multiplier::name
QLineEdit * name
Definition tab_multiplier.h:43
tab_multiplier::same_name
QCheckBox * same_name
Definition tab_multiplier.h:46
tab_multiplier::refresh
void refresh()
Definition tab_multiplier.cpp:109
tab_multiplier::select_multiplier
void select_multiplier()
Definition tab_multiplier.cpp:152
tab_multiplier::update_multiplier_info
void update_multiplier_info(struct multiplier *pmul)
Definition tab_multiplier.cpp:124
tab_multiplier::mpr_list
QListWidget * mpr_list
Definition tab_multiplier.h:45
tab_multiplier::add_now
void add_now()
Definition tab_multiplier.cpp:233
tab_multiplier::name_given
void name_given()
Definition tab_multiplier.cpp:167
tab_multiplier::selected
struct multiplier * selected
Definition tab_multiplier.h:48
tab_multiplier::initialize_new_multiplier
bool initialize_new_multiplier(struct multiplier *pmul)
Definition tab_multiplier.cpp:219
tab_multiplier::rname
QLineEdit * rname
Definition tab_multiplier.h:44
MAX_NUM_MULTIPLIERS
#define MAX_NUM_MULTIPLIERS
Definition fc_types.h:55
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:57
game.h
log.h
multiplier_rule_name
const char * multiplier_rule_name(const struct multiplier *pmul)
Definition multipliers.c:106
multiplier_by_rule_name
struct multiplier * multiplier_by_rule_name(const char *name)
Definition multipliers.c:115
multiplier_by_number
struct multiplier * multiplier_by_number(Multiplier_type_id id)
Definition multipliers.c:57
multiplier_index
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
multipliers.h
multipliers_re_active_iterate
#define multipliers_re_active_iterate(_mul_)
Definition multipliers.h:71
multipliers_iterate
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
multipliers_iterate_end
#define multipliers_iterate_end
Definition multipliers.h:67
multipliers_re_active_iterate_end
#define multipliers_re_active_iterate_end
Definition multipliers.h:75
name_set
static void name_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name)
Definition name_translation.h:88
untranslated_name
static const char * untranslated_name(const struct name_translation *ptrans)
Definition name_translation.h:104
names_set
static void names_set(struct name_translation *ptrans, const char *domain, const char *vernacular_name, const char *rule_name)
Definition name_translation.h:60
req_edit.h
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:216
ruledit.h
ruledit_qt_display_requirers
void ruledit_qt_display_requirers(const char *msg, void *data)
Definition ruledit_qt.cpp:96
ruledit_qt.h
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
item
Definition climisc.h:80
multiplier
Definition multipliers.h:31
multiplier::reqs
struct requirement_vector reqs
Definition multipliers.h:42
multiplier::ruledit_disabled
bool ruledit_disabled
Definition multipliers.h:34
multiplier::name
struct name_translation name
Definition multipliers.h:33
packet_ruleset_control::num_multipliers
int num_multipliers
Definition packets_gen.h:1467
tab_multiplier.h
is_multiplier_needed
bool is_multiplier_needed(struct multiplier *pmul, requirers_cb cb, void *data)
Definition validity.c:386
validity.h