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

Freeciv-3.1

Loading...
Searching...
No Matches
tab_enablers.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 <QGridLayout>
20#include <QLineEdit>
21#include <QListWidget>
22#include <QMenu>
23#include <QMessageBox>
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 "government.h"
34
35// ruledit
36#include "ruledit.h"
37#include "ruledit_qt.h"
38#include "validity.h"
39
40#include "tab_enablers.h"
41
42 class fix_enabler_item : public req_vec_fix_item
43{
44public:
45 explicit fix_enabler_item(struct action_enabler *enabler);
46 virtual ~fix_enabler_item();
47 void close();
48
49 const void *item();
50 void *item_working_copy();
51 const char *name();
52 struct req_vec_problem *find_next_problem(void);
53 void apply_accepted_changes();
54 void undo_accepted_changes();
55 int num_vectors();
56 requirement_vector_namer vector_namer();
57 requirement_vector_by_number vector_getter();
58 bool vector_in_item(const struct requirement_vector *vec);
59
60private:
61 struct action_enabler *current_enabler;
62 struct action_enabler *local_copy;
63 QString my_name;
64};
65
66/**********************************************************************/
71static enum req_vec_problem_seriousness
72enabler_problem_level(struct action_enabler *enabler)
73{
74 struct req_vec_problem *problem = action_enabler_suggest_repair(enabler);
75
76 if (problem != NULL) {
77 req_vec_problem_free(problem);
78 return RVPS_REPAIR;
79 }
80
81 problem = action_enabler_suggest_improvement(enabler);
82 if (problem != NULL) {
83 req_vec_problem_free(problem);
84 return RVPS_IMPROVE;
85 }
86
87 return RVPS_NO_PROBLEM;
88}
89
90/**********************************************************************/
93tab_enabler::tab_enabler(ruledit_gui *ui_in) : QWidget()
94{
95 QVBoxLayout *main_layout = new QVBoxLayout(this);
96 QGridLayout *enabler_layout = new QGridLayout();
97 QLabel *label;
98 QPushButton *add_button;
99
100 ui = ui_in;
101 connect(ui, SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
102 this, SLOT(incoming_req_vec_change(const requirement_vector *)));
103
104 selected = 0;
105
106 enabler_list = new QListWidget(this);
107
108 connect(enabler_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_enabler()));
109 main_layout->addWidget(enabler_list);
110
111 enabler_layout->setSizeConstraint(QLayout::SetMaximumSize);
112
113 label = new QLabel(QString::fromUtf8(R__("Type")));
114 label->setParent(this);
115 enabler_layout->addWidget(label, 0, 0);
116
117 type_button = new QToolButton();
118 type_menu = new QMenu();
119
120 action_iterate(act) {
121 type_menu->addAction(action_id_rule_name(act));
122 } action_iterate_end;
123
124 connect(type_menu, SIGNAL(triggered(QAction *)),
125 this, SLOT(edit_type(QAction *)));
126
127 type_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
128 type_button->setPopupMode(QToolButton::MenuButtonPopup);
129
130 type_button->setMenu(type_menu);
131 type_button->setText(R__("None"));
132
133 type_button->setEnabled(false);
134 enabler_layout->addWidget(type_button, 0, 2);
135
136 act_reqs_button
137 = new QPushButton(QString::fromUtf8(R__("Actor Requirements")), this);
138 connect(act_reqs_button, SIGNAL(pressed()),
139 this, SLOT(edit_actor_reqs()));
140 act_reqs_button->setEnabled(false);
141 enabler_layout->addWidget(act_reqs_button, 1, 2);
142
143 tgt_reqs_button
144 = new QPushButton(QString::fromUtf8(R__("Target Requirements")),
145 this);
146 connect(tgt_reqs_button, SIGNAL(pressed()),
147 this, SLOT(edit_target_reqs()));
148 tgt_reqs_button->setEnabled(false);
149 enabler_layout->addWidget(tgt_reqs_button, 2, 2);
150
151 add_button = new QPushButton(QString::fromUtf8(R__("Add Enabler")), this);
152 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
153 enabler_layout->addWidget(add_button, 3, 0);
154 show_experimental(add_button);
155
156 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Enabler")), this);
157 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
158 delete_button->setEnabled(false);
159 enabler_layout->addWidget(delete_button, 3, 2);
160 show_experimental(delete_button);
161
162 repair_button = new QPushButton(this);
163 connect(repair_button, SIGNAL(pressed()), this, SLOT(repair_now()));
164 repair_button->setEnabled(false);
165 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
166 enabler_layout->addWidget(repair_button, 3, 1);
167
168 refresh();
169
170 main_layout->addLayout(enabler_layout);
171
172 setLayout(main_layout);
173}
174
175/**********************************************************************/
178 void tab_enabler::refresh()
179{
180 int n = 0;
181
182 enabler_list->clear();
183
184 action_enablers_iterate(enabler) {
185 if (!enabler->ruledit_disabled) {
186 char buffer[512];
187 QListWidgetItem *item;
188
189 fc_snprintf(buffer, sizeof(buffer), "#%d: %s", n,
190 action_rule_name(enabler_get_action(enabler)));
191
192 item = new QListWidgetItem(QString::fromUtf8(buffer));
193
194 enabler_list->insertItem(n++, item);
195
196 mark_item(item, enabler_problem_level(enabler));
197
198 item->setSelected(enabler == selected);
199 }
200 } action_enablers_iterate_end;
201}
202
203/**********************************************************************/
206 void tab_enabler::update_enabler_info(struct action_enabler *enabler)
207{
208 int i = 0;
209
210 selected = enabler;
211
212 if (selected != nullptr) {
213 QString dispn = QString::fromUtf8(action_rule_name(enabler_get_action(enabler)));
214
215 type_button->setText(dispn);
216
217 type_button->setEnabled(true);
218 act_reqs_button->setEnabled(true);
219 tgt_reqs_button->setEnabled(true);
220
221 delete_button->setEnabled(true);
222
223 switch (enabler_problem_level(selected)) {
224 case RVPS_REPAIR:
225 /* Offer to repair the enabler if it has a problem. */
226 /* TRANS: Fix an error in an action enabler. */
227 repair_button->setText(QString::fromUtf8(R__("Repair Enabler")));
228 repair_button->setEnabled(TRUE);
229 break;
230 case RVPS_IMPROVE:
231 /* TRANS: Fix a non error issue in an action enabler. */
232 repair_button->setText(QString::fromUtf8(R__("Improve Enabler")));
233 repair_button->setEnabled(true);
234 break;
235 case RVPS_NO_PROBLEM:
236 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
237 repair_button->setEnabled(false);
238 break;
239 }
240 } else {
241 type_button->setText(R__("None"));
242 type_button->setEnabled(false);
243
244 act_reqs_button->setEnabled(false);
245 tgt_reqs_button->setEnabled(false);
246
247 repair_button->setEnabled(false);
248 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
249
250 delete_button->setEnabled(false);
251 }
252
253 /* The enabler may have gotten (rid of) a problem. */
254 action_enablers_iterate(other_enabler) {
255 QListWidgetItem *item = enabler_list->item(i++);
256
257 if (item == nullptr) {
258 continue;
259 }
260
261 mark_item(item, enabler_problem_level(other_enabler));
262 } action_enablers_iterate_end;
263}
264
265/**********************************************************************/
268 void tab_enabler::select_enabler()
269{
270 int i = 0;
271
272 action_enablers_iterate(enabler) {
273 QListWidgetItem *item = enabler_list->item(i++);
274
275 if (item != nullptr && item->isSelected()) {
276 update_enabler_info(enabler);
277 }
278 } action_enablers_iterate_end;
279}
280
281/**********************************************************************/
284 void tab_enabler::delete_now()
285{
286 if (selected != nullptr) {
287 selected->ruledit_disabled = true;
288
289 refresh();
290 update_enabler_info(nullptr);
291 }
292}
293
294/**********************************************************************/
297 bool tab_enabler::initialize_new_enabler(struct action_enabler *enabler)
298{
299 return true;
300}
301
302/**********************************************************************/
305 void tab_enabler::add_now()
306{
307 struct action_enabler *new_enabler;
308
309 // Try to reuse freed enabler slot
310 action_enablers_iterate(enabler) {
311 if (enabler->ruledit_disabled) {
312 if (initialize_new_enabler(enabler)) {
313 enabler->ruledit_disabled = false;
314 update_enabler_info(enabler);
315 refresh();
316 }
317 return;
318 }
319 } action_enablers_iterate_end;
320
321 // Try to add completely new enabler
322 new_enabler = action_enabler_new();
323
324 fc_assert_ret(NUM_ACTIONS > 0);
325 fc_assert_ret(action_id_exists(NUM_ACTIONS - 1));
326 new_enabler->action = (NUM_ACTIONS - 1);
327
328 action_enabler_add(new_enabler);
329
330 update_enabler_info(new_enabler);
331 refresh();
332}
333
334/**********************************************************************/
337 void tab_enabler::repair_now()
338{
339 if (selected == nullptr) {
340 /* Nothing to repair */
341 return;
342 }
343
344 ui->open_req_vec_fix(new fix_enabler_item(selected));
345}
346
347/**********************************************************************/
351 void tab_enabler::incoming_req_vec_change(const requirement_vector *vec)
352{
353 action_enablers_iterate(enabler) {
354 if (&enabler->actor_reqs == vec || &enabler->target_reqs == vec) {
355 update_enabler_info(enabler);
356 }
357 } action_enablers_iterate_end;
358}
359
360/**********************************************************************/
363 void tab_enabler::edit_type(QAction *action)
364{
365 struct action *paction;
366 QByteArray an_bytes;
367
368 an_bytes = action->text().toUtf8();
369 paction = action_by_rule_name(an_bytes.data());
370
371 if (selected != nullptr && paction != nullptr) {
372 /* Must remove and add back because enablers are stored by action. */
373 action_enabler_remove(selected);
374 selected->action = paction->id;
375 action_enabler_add(selected);
376
377 /* Show the changes. */
378 update_enabler_info(selected);
379 refresh();
380 }
381}
382
383/**********************************************************************/
386 void tab_enabler::edit_target_reqs()
387{
388 if (selected != nullptr) {
389 ui->open_req_edit(QString::fromUtf8(R__("Enabler (target)")),
390 &selected->target_reqs);
391 }
392}
393
394/**********************************************************************/
397 void tab_enabler::edit_actor_reqs()
398{
399 if (selected != nullptr) {
400 ui->open_req_edit(QString::fromUtf8(R__("Enabler (actor)")),
401 &selected->actor_reqs);
402 }
403}
404
405/**********************************************************************/
409fix_enabler_item::fix_enabler_item(struct action_enabler *enabler)
410{
411 char buf[MAX_LEN_NAME * 2];
412 struct action *paction = action_by_number(enabler->action);
413
414 fc_assert_ret(paction);
415
416 /* Can't use QString::asprintf() as msys libintl.h defines asprintf()
417 * as a macro */
418 fc_snprintf(buf, sizeof(buf),
419 R__("action enabler for %s"), action_rule_name(paction));
420
421 /* Don't modify the original until the user accepts */
422 local_copy = action_enabler_copy(enabler);
423 current_enabler = enabler;
424
425 /* As precise a title as possible */
426 my_name = QString(buf);
427}
428
429/**********************************************************************/
432fix_enabler_item::~fix_enabler_item()
433{
434 action_enabler_free(local_copy);
435}
436
437/********************************************************************/
440 void fix_enabler_item::close()
441{
442 delete this;
443}
444
445/********************************************************************/
449 const void *fix_enabler_item::item()
450{
451 return current_enabler;
452}
453
454/********************************************************************/
458 void *fix_enabler_item::item_working_copy()
459{
460 return local_copy;
461}
462
463/**********************************************************************/
469 const char *fix_enabler_item::name()
470{
471 return my_name.toUtf8().data();
472}
473
474/**********************************************************************/
480 struct req_vec_problem *fix_enabler_item::find_next_problem(void)
481{
482 struct req_vec_problem *out = action_enabler_suggest_repair(local_copy);
483
484 if (out != NULL) {
485 return out;
486 }
487
488 return action_enabler_suggest_improvement(local_copy);
489}
490
491/**********************************************************************/
497 void fix_enabler_item::apply_accepted_changes()
498{
499 /* The user has approved the solution */
500 current_enabler->action = local_copy->action;
501 requirement_vector_copy(&current_enabler->actor_reqs,
502 &local_copy->actor_reqs);
503 requirement_vector_copy(&current_enabler->target_reqs,
504 &local_copy->target_reqs);
505}
506
507/**********************************************************************/
512 void fix_enabler_item::undo_accepted_changes()
513{
514 /* The user has rejected all solutions */
515 local_copy->action = current_enabler->action;
516 requirement_vector_copy(&local_copy->actor_reqs,
517 &current_enabler->actor_reqs);
518 requirement_vector_copy(&local_copy->target_reqs,
519 &current_enabler->target_reqs);
520}
521
522/********************************************************************/
526 int fix_enabler_item::num_vectors()
527{
528 return 2;
529}
530
531/********************************************************************/
537 requirement_vector_namer fix_enabler_item::vector_namer()
538{
539 return action_enabler_vector_by_number_name;
540}
541
542/********************************************************************/
548 requirement_vector_by_number fix_enabler_item::vector_getter()
549{
550 return action_enabler_vector_by_number;
551}
552
553/**********************************************************************/
558 bool fix_enabler_item::vector_in_item(const struct requirement_vector *vec)
559{
560 return (&current_enabler->actor_reqs == vec
561 || &current_enabler->target_reqs == vec);
562}
action_enabler_suggest_repair
struct req_vec_problem * action_enabler_suggest_repair(const struct action_enabler *enabler)
Definition actions.c:2838
action_enabler_suggest_improvement
struct req_vec_problem * action_enabler_suggest_improvement(const struct action_enabler *enabler)
Definition actions.c:2904
action_enabler_vector_by_number_name
const char * action_enabler_vector_by_number_name(req_vec_num_in_item vec)
Definition actions.c:3015
action_by_rule_name
struct action * action_by_rule_name(const char *name)
Definition actions.c:1708
action_rule_name
const char * action_rule_name(const struct action *action)
Definition actions.c:1876
action_id_rule_name
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1899
action_enabler_free
void action_enabler_free(struct action_enabler *enabler)
Definition actions.c:2415
action_enabler_add
void action_enabler_add(struct action_enabler *enabler)
Definition actions.c:2442
action_enabler_new
struct action_enabler * action_enabler_new(void)
Definition actions.c:2396
action_id_exists
bool action_id_exists(const action_id act_id)
Definition actions.c:1697
action_enabler_remove
bool action_enabler_remove(struct action_enabler *enabler)
Definition actions.c:2459
action_enabler_copy
struct action_enabler * action_enabler_copy(const struct action_enabler *original)
Definition actions.c:2427
action_enabler_vector_by_number
struct requirement_vector * action_enabler_vector_by_number(const void *enabler, req_vec_num_in_item number)
Definition actions.c:2991
action_enablers_iterate_end
#define action_enablers_iterate_end
Definition actions.h:526
enabler_get_action
#define enabler_get_action(_enabler_)
Definition actions.h:450
action_by_number
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
NUM_ACTIONS
#define NUM_ACTIONS
Definition actions.h:297
action_iterate_end
#define action_iterate_end
Definition actions.h:472
action_enablers_iterate
#define action_enablers_iterate(_enabler_)
Definition actions.h:520
action_iterate
#define action_iterate(_act_)
Definition actions.h:467
n
#define n
Definition astring.c:77
fix_enabler_item
Definition tab_enablers.cpp:43
fix_enabler_item::fix_enabler_item
fix_enabler_item(struct action_enabler *enabler)
Definition tab_enablers.cpp:409
fix_enabler_item::apply_accepted_changes
void apply_accepted_changes()
Definition tab_enablers.cpp:497
fix_enabler_item::num_vectors
int num_vectors()
Definition tab_enablers.cpp:526
fix_enabler_item::find_next_problem
struct req_vec_problem * find_next_problem(void)
Definition tab_enablers.cpp:480
fix_enabler_item::vector_in_item
bool vector_in_item(const struct requirement_vector *vec)
Definition tab_enablers.cpp:558
fix_enabler_item::my_name
QString my_name
Definition tab_enablers.cpp:63
fix_enabler_item::close
void close()
Definition tab_enablers.cpp:440
fix_enabler_item::vector_namer
requirement_vector_namer vector_namer()
Definition tab_enablers.cpp:537
fix_enabler_item::~fix_enabler_item
virtual ~fix_enabler_item()
Definition tab_enablers.cpp:432
fix_enabler_item::local_copy
struct action_enabler * local_copy
Definition tab_enablers.cpp:62
fix_enabler_item::item
const void * item()
Definition tab_enablers.cpp:449
fix_enabler_item::undo_accepted_changes
void undo_accepted_changes()
Definition tab_enablers.cpp:512
fix_enabler_item::current_enabler
struct action_enabler * current_enabler
Definition tab_enablers.cpp:61
fix_enabler_item::vector_getter
requirement_vector_by_number vector_getter()
Definition tab_enablers.cpp:548
fix_enabler_item::item_working_copy
void * item_working_copy()
Definition tab_enablers.cpp:458
fix_enabler_item::name
const char * name()
Definition tab_enablers.cpp:469
req_vec_fix_item
Definition req_vec_fix.h:49
ruledit_gui
Definition ruledit_qt.h:96
ruledit_gui::open_req_vec_fix
void open_req_vec_fix(req_vec_fix_item *item_info)
Definition ruledit_qt.cpp:364
ruledit_gui::open_req_edit
void open_req_edit(QString target, struct requirement_vector *preqs)
Definition ruledit_qt.cpp:331
tab_enabler::ui
ruledit_gui * ui
Definition tab_enablers.h:45
tab_enabler::incoming_req_vec_change
void incoming_req_vec_change(const requirement_vector *vec)
Definition tab_enablers.cpp:351
tab_enabler::delete_now
void delete_now()
Definition tab_enablers.cpp:284
tab_enabler::refresh
void refresh()
Definition tab_enablers.cpp:178
tab_enabler::edit_type
void edit_type(QAction *action)
Definition tab_enablers.cpp:363
tab_enabler::selected
struct action_enabler * selected
Definition tab_enablers.h:57
tab_enabler::act_reqs_button
QPushButton * act_reqs_button
Definition tab_enablers.h:51
tab_enabler::type_button
QToolButton * type_button
Definition tab_enablers.h:49
tab_enabler::edit_actor_reqs
void edit_actor_reqs()
Definition tab_enablers.cpp:397
tab_enabler::tab_enabler
tab_enabler(ruledit_gui *ui_in)
Definition tab_enablers.cpp:93
tab_enabler::tgt_reqs_button
QPushButton * tgt_reqs_button
Definition tab_enablers.h:52
tab_enabler::type_menu
QMenu * type_menu
Definition tab_enablers.h:50
tab_enabler::repair_now
void repair_now()
Definition tab_enablers.cpp:337
tab_enabler::edit_target_reqs
void edit_target_reqs()
Definition tab_enablers.cpp:386
tab_enabler::select_enabler
void select_enabler()
Definition tab_enablers.cpp:268
tab_enabler::enabler_list
QListWidget * enabler_list
Definition tab_enablers.h:55
tab_enabler::delete_button
QPushButton * delete_button
Definition tab_enablers.h:53
tab_enabler::initialize_new_enabler
bool initialize_new_enabler(struct action_enabler *enabler)
Definition tab_enablers.cpp:297
tab_enabler::repair_button
QPushButton * repair_button
Definition tab_enablers.h:54
tab_enabler::update_enabler_info
void update_enabler_info(struct action_enabler *enabler)
Definition tab_enablers.cpp:206
tab_enabler::add_now
void add_now()
Definition tab_enablers.cpp:305
MAX_LEN_NAME
#define MAX_LEN_NAME
Definition fc_types.h:66
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
game.h
government.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
mark_item
void mark_item(QListWidgetItem *item, enum req_vec_problem_seriousness problem_level)
Definition req_vec_fix.cpp:36
req_vec_problem_seriousness
req_vec_problem_seriousness
Definition req_vec_fix.h:35
RVPS_NO_PROBLEM
@ RVPS_NO_PROBLEM
Definition req_vec_fix.h:36
RVPS_REPAIR
@ RVPS_REPAIR
Definition req_vec_fix.h:38
RVPS_IMPROVE
@ RVPS_IMPROVE
Definition req_vec_fix.h:37
req_vec_problem_free
void req_vec_problem_free(struct req_vec_problem *issue)
Definition requirements.c:4994
requirement_vector_namer
const char *(* requirement_vector_namer)(req_vec_num_in_item number)
Definition requirements.h:262
requirement_vector_by_number
struct requirement_vector *(* requirement_vector_by_number)(const void *parent_item, req_vec_num_in_item number)
Definition requirements.h:252
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:216
ruledit.h
ruledit_qt.h
action_enabler
Definition actions.h:438
action_enabler::ruledit_disabled
bool ruledit_disabled
Definition actions.h:445
action_enabler::action
action_id action
Definition actions.h:439
action_enabler::actor_reqs
struct requirement_vector actor_reqs
Definition actions.h:440
action_enabler::target_reqs
struct requirement_vector target_reqs
Definition actions.h:441
action
Definition actions.h:379
action::id
action_id id
Definition actions.h:380
item
Definition climisc.h:80
item::item
struct universal item
Definition climisc.h:81
req_vec_problem
Definition requirements.h:287
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
TRUE
#define TRUE
Definition support.h:46
enabler_problem_level
static enum req_vec_problem_seriousness enabler_problem_level(struct action_enabler *enabler)
Definition tab_enablers.cpp:72
tab_enablers.h
validity.h