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

Freeciv-3.1

Loading...
Searching...
No Matches
tab_achievement.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 "achievements.h"
33#include "game.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_achievement.h"
42
43/**********************************************************************/
46tab_achievement::tab_achievement(ruledit_gui *ui_in) : QWidget()
47{
48 QVBoxLayout *main_layout = new QVBoxLayout(this);
49 QGridLayout *ach_layout = new QGridLayout();
50 QLabel *label;
51 QPushButton *effects_button;
52 QPushButton *add_button;
53 QPushButton *delete_button;
54
55 ui = ui_in;
56 selected = nullptr;
57
58 ach_list = new QListWidget(this);
59
60 connect(ach_list,
61 SIGNAL(itemSelectionChanged()), this, SLOT(select_achievement()));
62 main_layout->addWidget(ach_list);
63
64 ach_layout->setSizeConstraint(QLayout::SetMaximumSize);
65
66 label = new QLabel(QString::fromUtf8(R__("Rule Name")));
67 label->setParent(this);
68 rname = new QLineEdit(this);
69 rname->setText(R__("None"));
70 connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given()));
71 ach_layout->addWidget(label, 0, 0);
72 ach_layout->addWidget(rname, 0, 2);
73
74 label = new QLabel(QString::fromUtf8(R__("Name")));
75 label->setParent(this);
76 same_name = new QCheckBox();
77 connect(same_name, SIGNAL(toggled(bool)), this, SLOT(same_name_toggle(bool)));
78 name = new QLineEdit(this);
79 name->setText(R__("None"));
80 connect(name, SIGNAL(returnPressed()), this, SLOT(name_given()));
81 ach_layout->addWidget(label, 1, 0);
82 ach_layout->addWidget(same_name, 1, 1);
83 ach_layout->addWidget(name, 1, 2);
84
85 effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this);
86 connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects()));
87 ach_layout->addWidget(effects_button, 3, 2);
88
89 add_button = new QPushButton(QString::fromUtf8(R__("Add Achievement")), this);
90 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
91 ach_layout->addWidget(add_button, 4, 0);
92 show_experimental(add_button);
93
94 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Achievement")),
95 this);
96 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
97 ach_layout->addWidget(delete_button, 4, 2);
98 show_experimental(delete_button);
99
100 refresh();
101 update_achievement_info(nullptr);
102
103 main_layout->addLayout(ach_layout);
104
105 setLayout(main_layout);
106}
107
108/**********************************************************************/
111 void tab_achievement::refresh()
112{
113 ach_list->clear();
114
115 achievements_re_active_iterate(pach) {
116 QListWidgetItem *item
117 = new QListWidgetItem(QString::fromUtf8(achievement_rule_name(pach)));
118
119 ach_list->insertItem(achievement_index(pach), item);
120 } achievements_re_active_iterate_end;
121}
122
123/**********************************************************************/
126 void tab_achievement::update_achievement_info(struct achievement *pach)
127{
128 selected = pach;
129
130 if (selected != 0) {
131 QString dispn = QString::fromUtf8(untranslated_name(&(pach->name)));
132 QString rulen = QString::fromUtf8(achievement_rule_name(pach));
133
134 name->setText(dispn);
135 rname->setText(rulen);
136 if (dispn == rulen) {
137 name->setEnabled(false);
138 same_name->setChecked(true);
139 } else {
140 same_name->setChecked(false);
141 name->setEnabled(true);
142 }
143 } else {
144 name->setText(R__("None"));
145 rname->setText(R__("None"));
146 same_name->setChecked(true);
147 name->setEnabled(false);
148 }
149}
150
151/**********************************************************************/
154 void tab_achievement::select_achievement()
155{
156 QList<QListWidgetItem *> select_list = ach_list->selectedItems();
157
158 if (!select_list.isEmpty()) {
159 QByteArray gn_bytes;
160
161 gn_bytes = select_list.at(0)->text().toUtf8();
162 update_achievement_info(achievement_by_rule_name(gn_bytes.data()));
163 }
164}
165
166/**********************************************************************/
169 void tab_achievement::name_given()
170{
171 if (selected != nullptr) {
172 QByteArray name_bytes;
173 QByteArray rname_bytes;
174
175 achievements_iterate(pach) {
176 if (pach != selected && !pach->ruledit_disabled) {
177 rname_bytes = rname->text().toUtf8();
178 if (!strcmp(achievement_rule_name(pach), rname_bytes.data())) {
179 ui->display_msg(R__("An achievement with that rule name already exists!"));
180 return;
181 }
182 }
183 } achievements_iterate_end;
184
185 if (same_name->isChecked()) {
186 name->setText(rname->text());
187 }
188
189 name_bytes = name->text().toUtf8();
190 rname_bytes = rname->text().toUtf8();
191 names_set(&(selected->name), 0,
192 name_bytes.data(),
193 rname_bytes.data());
194 refresh();
195 }
196}
197
198/**********************************************************************/
201 void tab_achievement::delete_now()
202{
203 if (selected != 0) {
204 requirers_dlg *requirers;
205
206 requirers = ui->create_requirers(achievement_rule_name(selected));
207 if (is_achievement_needed(selected, &ruledit_qt_display_requirers,
208 requirers)) {
209 return;
210 }
211
212 selected->ruledit_disabled = true;
213
214 refresh();
215 update_achievement_info(nullptr);
216 }
217}
218
219/**********************************************************************/
222 bool tab_achievement::initialize_new_achievement(struct achievement *pach)
223{
224 if (achievement_by_rule_name("New Achievement") != nullptr) {
225 return false;
226 }
227
228 name_set(&(pach->name), 0, "New Achievement");
229
230 return true;
231}
232
233/**********************************************************************/
236 void tab_achievement::add_now()
237{
238 struct achievement *new_ach;
239
240 // Try to reuse freed achievement slot
241 achievements_iterate(pach) {
242 if (pach->ruledit_disabled) {
243 if (initialize_new_achievement(pach)) {
244 pach->ruledit_disabled = false;
245 update_achievement_info(pach);
246 refresh();
247 }
248 return;
249 }
250 } achievements_iterate_end;
251
252 // Try to add completely new achievement
253 if (game.control.num_achievement_types >= MAX_ACHIEVEMENT_TYPES) {
254 return;
255 }
256
257 // num_achievement_types must be big enough to hold new achievement or
258 // achievement_by_number() fails.
259 game.control.num_achievement_types++;
260 new_ach = achievement_by_number(game.control.num_achievement_types - 1);
261 if (initialize_new_achievement(new_ach)) {
262 update_achievement_info(new_ach);
263
264 refresh();
265 } else {
266 game.control.num_achievement_types--; // Restore
267 }
268}
269
270/**********************************************************************/
273 void tab_achievement::same_name_toggle(bool checked)
274{
275 name->setEnabled(!checked);
276 if (checked) {
277 name->setText(rname->text());
278 }
279}
280
281/**********************************************************************/
284 void tab_achievement::edit_effects()
285{
286 if (selected != nullptr) {
287 struct universal uni;
288
289 uni.value.achievement = selected;
290 uni.kind = VUT_ACHIEVEMENT;
291
292 ui->open_effect_edit(QString::fromUtf8(achievement_rule_name(selected)),
293 &uni, EFMC_NORMAL);
294 }
295}
achievement_index
int achievement_index(const struct achievement *pach)
Definition achievements.c:86
achievement_by_number
struct achievement * achievement_by_number(int id)
Definition achievements.c:96
achievement_rule_name
const char * achievement_rule_name(struct achievement *pach)
Definition achievements.c:114
achievement_by_rule_name
struct achievement * achievement_by_rule_name(const char *name)
Definition achievements.c:123
achievements.h
achievements_iterate_end
#define achievements_iterate_end
Definition achievements.h:70
achievements_iterate
#define achievements_iterate(_ach_)
Definition achievements.h:64
achievements_re_active_iterate
#define achievements_re_active_iterate(_p)
Definition achievements.h:74
achievements_re_active_iterate_end
#define achievements_re_active_iterate_end
Definition achievements.h:78
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
ruledit_gui::open_effect_edit
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
Definition ruledit_qt.cpp:400
tab_achievement::name
QLineEdit * name
Definition tab_achievement.h:43
tab_achievement::initialize_new_achievement
bool initialize_new_achievement(struct achievement *pach)
Definition tab_achievement.cpp:222
tab_achievement::update_achievement_info
void update_achievement_info(struct achievement *pach)
Definition tab_achievement.cpp:126
tab_achievement::same_name_toggle
void same_name_toggle(bool checked)
Definition tab_achievement.cpp:273
tab_achievement::delete_now
void delete_now()
Definition tab_achievement.cpp:201
tab_achievement::same_name
QCheckBox * same_name
Definition tab_achievement.h:46
tab_achievement::add_now
void add_now()
Definition tab_achievement.cpp:236
tab_achievement::tab_achievement
tab_achievement(ruledit_gui *ui_in)
Definition tab_achievement.cpp:46
tab_achievement::ui
ruledit_gui * ui
Definition tab_achievement.h:39
tab_achievement::select_achievement
void select_achievement()
Definition tab_achievement.cpp:154
tab_achievement::edit_effects
void edit_effects()
Definition tab_achievement.cpp:284
tab_achievement::ach_list
QListWidget * ach_list
Definition tab_achievement.h:45
tab_achievement::selected
struct achievement * selected
Definition tab_achievement.h:48
tab_achievement::name_given
void name_given()
Definition tab_achievement.cpp:169
tab_achievement::rname
QLineEdit * rname
Definition tab_achievement.h:44
tab_achievement::refresh
void refresh()
Definition tab_achievement.cpp:111
EFMC_NORMAL
@ EFMC_NORMAL
Definition effect_edit.h:33
MAX_ACHIEVEMENT_TYPES
#define MAX_ACHIEVEMENT_TYPES
Definition fc_types.h:53
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:57
game.h
log.h
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
achievement
Definition achievements.h:26
achievement::name
struct name_translation name
Definition achievements.h:28
achievement::ruledit_disabled
bool ruledit_disabled
Definition achievements.h:29
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
item
Definition climisc.h:80
packet_ruleset_control::num_achievement_types
int num_achievement_types
Definition packets_gen.h:1466
universal
Definition fc_types.h:756
universal::kind
enum universals_n kind
Definition fc_types.h:758
universal::value
universals_u value
Definition fc_types.h:757
tab_achievement.h
universals_u::achievement
struct achievement * achievement
Definition fc_types.h:606
is_achievement_needed
bool is_achievement_needed(struct achievement *pach, requirers_cb cb, void *data)
Definition validity.c:263
validity.h