Freeciv-3.3
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
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();
55 int num_vectors();
58 bool vector_in_item(const struct requirement_vector *vec);
59
60private:
64};
65
66/**********************************************************************/
89
90/**********************************************************************/
94{
95 QVBoxLayout *main_layout = new QVBoxLayout(this);
97 QLabel *label;
98 QPushButton *add_button;
99 QLabel *lbl;
100 int row = 0;
101
102 ui = ui_in;
103 connect(ui, SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
105
106 selected = 0;
107
108 enabler_list = new QListWidget(this);
109
111 main_layout->addWidget(enabler_list);
112
113 enabler_layout->setSizeConstraint(QLayout::SetMaximumSize);
114
115 label = new QLabel(QString::fromUtf8(R__("Type")));
116 label->setParent(this);
117 enabler_layout->addWidget(label, row, 0);
118
119 type_button = new QToolButton();
120 type_menu = new QMenu();
121
122 action_iterate(act) {
123 type_menu->addAction(action_id_rule_name(act));
125
126 connect(type_menu, SIGNAL(triggered(QAction *)),
127 this, SLOT(edit_type(QAction *)));
128
129 type_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
130 type_button->setPopupMode(QToolButton::MenuButtonPopup);
131
132 type_button->setMenu(type_menu);
133 type_button->setText(R__("None"));
134
135 type_button->setEnabled(false);
136 enabler_layout->addWidget(type_button, row++, 1);
137
139 = new QPushButton(QString::fromUtf8(R__("Actor Requirements")), this);
140 connect(act_reqs_button, SIGNAL(pressed()),
141 this, SLOT(edit_actor_reqs()));
142 act_reqs_button->setEnabled(false);
143 enabler_layout->addWidget(act_reqs_button, row++, 1);
144
146 = new QPushButton(QString::fromUtf8(R__("Target Requirements")),
147 this);
148 connect(tgt_reqs_button, SIGNAL(pressed()),
149 this, SLOT(edit_target_reqs()));
150 tgt_reqs_button->setEnabled(false);
151 enabler_layout->addWidget(tgt_reqs_button, row++, 1);
152
153 lbl = new QLabel(QString::fromUtf8(R__("Comment")));
154 enabler_layout->addWidget(lbl, row, 0);
155 comment = new QLineEdit(this);
156 connect(comment, SIGNAL(returnPressed()), this, SLOT(comment_given()));
157 enabler_layout->addWidget(comment, row++, 1);
158
159 add_button = new QPushButton(QString::fromUtf8(R__("Add Enabler")), this);
160 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
161 enabler_layout->addWidget(add_button, row, 0);
162
163 delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Enabler")), this);
164 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
165 delete_button->setEnabled(false);
166 enabler_layout->addWidget(delete_button, row++, 1);
167
168 repair_button = new QPushButton(this);
169 connect(repair_button, SIGNAL(pressed()), this, SLOT(repair_now()));
170 repair_button->setEnabled(false);
171 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
172 enabler_layout->addWidget(repair_button, row++, 1);
173
174 refresh();
175
176 main_layout->addLayout(enabler_layout);
177
178 setLayout(main_layout);
179}
180
181/**********************************************************************/
185{
186 int n = 0;
187
188 enabler_list->clear();
189
191 if (!enabler->rulesave.ruledit_disabled) {
192 char buffer[512];
194
195 fc_snprintf(buffer, sizeof(buffer), "#%d: %s", n,
197
198 item = new QListWidgetItem(QString::fromUtf8(buffer));
199
200 enabler_list->insertItem(n++, item);
201
203
204 item->setSelected(enabler == selected);
205 }
207}
208
209/**********************************************************************/
213{
214 int i = 0;
215
217
218 if (selected != nullptr) {
220
221 type_button->setText(dispn);
222
223 type_button->setEnabled(true);
224 act_reqs_button->setEnabled(true);
225 tgt_reqs_button->setEnabled(true);
226
227 if (selected->rulesave.comment != nullptr) {
228 comment->setText(selected->rulesave.comment);
229 } else {
230 comment->setText("");
231 }
232
233 delete_button->setEnabled(true);
234
236 case RVPS_REPAIR:
237 // Offer to repair the enabler if it has a problem.
238 // TRANS: Fix an error in an action enabler.
239 repair_button->setText(QString::fromUtf8(R__("Repair Enabler")));
240 repair_button->setEnabled(TRUE);
241 break;
242 case RVPS_IMPROVE:
243 // TRANS: Fix a non error issue in an action enabler.
244 repair_button->setText(QString::fromUtf8(R__("Improve Enabler")));
245 repair_button->setEnabled(true);
246 break;
247 case RVPS_NO_PROBLEM:
248 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
249 repair_button->setEnabled(false);
250 break;
251 }
252 } else {
253 type_button->setText(R__("None"));
254 type_button->setEnabled(false);
255
256 act_reqs_button->setEnabled(false);
257 tgt_reqs_button->setEnabled(false);
258
259 repair_button->setEnabled(false);
260 repair_button->setText(QString::fromUtf8(R__("Enabler Issues")));
261
262 comment->setText("");
263
264 delete_button->setEnabled(false);
265 }
266
267 // The enabler may have gotten (rid of) a problem.
270
271 if (item == nullptr) {
272 continue;
273 }
274
277}
278
279/**********************************************************************/
283{
284 int i = 0;
285
286 // Save previously selected one's comment.
288
291
292 if (item != nullptr && item->isSelected()) {
294 }
296}
297
298/**********************************************************************/
302{
303 if (selected != nullptr) {
306
307 refresh();
308 update_enabler_info(nullptr);
309 }
310}
311
312/**********************************************************************/
316{
317 if (enabler->rulesave.comment != nullptr) {
318 free(enabler->rulesave.comment);
319 enabler->rulesave.comment = nullptr;
320 }
321
322 enabler->rulesave.ruledit_disabled = false;
323
324 return true;
325}
326
327/**********************************************************************/
331{
333
334 // Try to reuse freed enabler slot
336 if (enabler->rulesave.ruledit_disabled) {
339 refresh();
340 }
341 return;
342 }
344
345 // Try to add completely new enabler
347
350 new_enabler->action = (NUM_ACTIONS - 1);
351
353
355 refresh();
356}
357
358/**********************************************************************/
362{
363 if (selected == nullptr) {
364 // Nothing to repair
365 return;
366 }
367
369}
370
371/**********************************************************************/
376{
378 if (&enabler->actor_reqs == vec || &enabler->target_reqs == vec) {
380 }
382}
383
384/**********************************************************************/
388{
389 struct action *paction;
391
392 an_bytes = action->text().toUtf8();
394
395 if (selected != nullptr && paction != nullptr) {
396 // Must remove and add back because enablers are stored by action.
400
401 // Show the changes.
403 refresh();
404 }
405}
406
407/**********************************************************************/
411{
412 if (selected != nullptr) {
413 ui->open_req_edit(QString::fromUtf8(R__("Enabler (target)")),
415 }
416}
417
418/**********************************************************************/
422{
423 if (selected != nullptr) {
424 ui->open_req_edit(QString::fromUtf8(R__("Enabler (actor)")),
426 }
427}
428
429/**********************************************************************/
433{
434 if (selected != nullptr) {
435 if (selected->rulesave.comment != nullptr) {
437 }
438
439 if (!comment->text().isEmpty()) {
440 selected->rulesave.comment = fc_strdup(comment->text().toUtf8());
441 } else {
442 selected->rulesave.comment = nullptr;
443 }
444 }
445}
446
447/**********************************************************************/
452{
453 char buf[MAX_LEN_NAME * 2];
455
456 fc_assert_ret(paction != nullptr);
457
458 /* Can't use QString::asprintf() as msys libintl.h defines asprintf()
459 * as a macro */
460 fc_snprintf(buf, sizeof(buf),
461 R__("action enabler for %s"), action_rule_name(paction));
462
463 // Don't modify the original until the user accepts
466
467 // As precise a title as possible
469}
470
471/**********************************************************************/
478
479/********************************************************************/
483{
484 delete this;
485}
486
487/********************************************************************/
492{
493 return current_enabler;
494}
495
496/********************************************************************/
504
505/**********************************************************************/
512{
513 return my_name.toUtf8().data();
514}
515
516/**********************************************************************/
523{
525
526 if (out != nullptr) {
527 return out;
528 }
529
531}
532
533/**********************************************************************/
549
550/**********************************************************************/
565
566/********************************************************************/
572{
573 return 2;
574}
575
576/********************************************************************/
587
588/********************************************************************/
599
600/**********************************************************************/
607{
608 return (&current_enabler->actor_reqs == vec
609 || &current_enabler->target_reqs == vec);
610}
struct req_vec_problem * action_enabler_suggest_repair(const struct action_enabler *enabler)
Definition actions.c:1897
struct req_vec_problem * action_enabler_suggest_improvement(const struct action_enabler *enabler)
Definition actions.c:1963
const char * action_enabler_vector_by_number_name(req_vec_num_in_item vec)
Definition actions.c:2074
struct action * action_by_rule_name(const char *name)
Definition actions.c:1079
const char * action_rule_name(const struct action *action)
Definition actions.c:1216
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1239
void action_enabler_free(struct action_enabler *enabler)
Definition actions.c:1495
void action_enabler_add(struct action_enabler *enabler)
Definition actions.c:1526
struct action_enabler * action_enabler_new(void)
Definition actions.c:1475
bool action_id_exists(const action_id act_id)
Definition actions.c:1068
bool action_enabler_remove(struct action_enabler *enabler)
Definition actions.c:1543
struct action_enabler * action_enabler_copy(const struct action_enabler *original)
Definition actions.c:1511
struct requirement_vector * action_enabler_vector_by_number(const void *enabler, req_vec_num_in_item number)
Definition actions.c:2050
#define action_enablers_iterate_end
Definition actions.h:283
#define enabler_get_action(_enabler_)
Definition actions.h:186
#define NUM_ACTIONS
Definition actions.h:63
#define action_iterate_end
Definition actions.h:218
#define action_enablers_iterate(_enabler_)
Definition actions.h:277
#define action_id(_act_)
Definition actions.h:426
#define action_iterate(_act_)
Definition actions.h:214
#define n
Definition astring.c:77
fix_enabler_item(struct action_enabler *enabler)
void apply_accepted_changes()
struct req_vec_problem * find_next_problem(void)
bool vector_in_item(const struct requirement_vector *vec)
requirement_vector_namer vector_namer()
virtual ~fix_enabler_item()
struct action_enabler * local_copy
const void * item()
struct action_enabler * current_enabler
requirement_vector_by_number vector_getter()
void * item_working_copy()
const char * name()
void open_req_vec_fix(req_vec_fix_item *item_info)
void open_req_edit(QString target, struct requirement_vector *preqs)
ruledit_gui * ui
void incoming_req_vec_change(const requirement_vector *vec)
void comment_given()
void edit_type(QAction *action)
struct action_enabler * selected
QPushButton * act_reqs_button
QToolButton * type_button
void edit_actor_reqs()
tab_enabler(ruledit_gui *ui_in)
QLineEdit * comment
QPushButton * tgt_reqs_button
QMenu * type_menu
void edit_target_reqs()
void select_enabler()
QListWidget * enabler_list
QPushButton * delete_button
bool initialize_new_enabler(struct action_enabler *enabler)
QPushButton * repair_button
void update_enabler_info(struct action_enabler *enabler)
char * incite_cost
Definition comments.c:76
#define MAX_LEN_NAME
Definition fc_types.h:66
#define R__(String)
Definition fcintl.h:75
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_strdup(str)
Definition mem.h:43
void mark_item(QListWidgetItem *item, enum req_vec_problem_seriousness problem_level)
req_vec_problem_seriousness
Definition req_vec_fix.h:35
@ RVPS_NO_PROBLEM
Definition req_vec_fix.h:36
@ RVPS_REPAIR
Definition req_vec_fix.h:38
@ RVPS_IMPROVE
Definition req_vec_fix.h:37
void req_vec_problem_free(struct req_vec_problem *issue)
const char *(* requirement_vector_namer)(req_vec_num_in_item number)
struct requirement_vector *(* requirement_vector_by_number)(const void *parent_item, req_vec_num_in_item number)
char * comment
Definition actions.h:180
struct action_enabler::@14 rulesave
action_id action
Definition actions.h:171
struct requirement_vector actor_reqs
Definition actions.h:172
struct requirement_vector target_reqs
Definition actions.h:173
Definition climisc.h:82
struct universal item
Definition climisc.h:83
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define TRUE
Definition support.h:46
static enum req_vec_problem_seriousness enabler_problem_level(struct action_enabler *enabler)