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) {
305
306 refresh();
307 update_enabler_info(nullptr);
308 }
309}
310
311/**********************************************************************/
315{
316 if (enabler->rulesave.comment != nullptr) {
317 free(enabler->rulesave.comment);
318 enabler->rulesave.comment = nullptr;
319 }
320
321 enabler->rulesave.ruledit_disabled = false;
322
323 return true;
324}
325
326/**********************************************************************/
330{
332
333 // Try to reuse freed enabler slot
335 if (enabler->rulesave.ruledit_disabled) {
338 refresh();
339 }
340 return;
341 }
343
344 // Try to add completely new enabler
346
349 new_enabler->action = (NUM_ACTIONS - 1);
350
352
354 refresh();
355}
356
357/**********************************************************************/
361{
362 if (selected == nullptr) {
363 // Nothing to repair
364 return;
365 }
366
368}
369
370/**********************************************************************/
375{
377 if (&enabler->actor_reqs == vec || &enabler->target_reqs == vec) {
379 }
381}
382
383/**********************************************************************/
387{
388 struct action *paction;
390
391 an_bytes = action->text().toUtf8();
393
394 if (selected != nullptr && paction != nullptr) {
395 // Must remove and add back because enablers are stored by action.
399
400 // Show the changes.
402 refresh();
403 }
404}
405
406/**********************************************************************/
410{
411 if (selected != nullptr) {
412 ui->open_req_edit(QString::fromUtf8(R__("Enabler (target)")),
414 }
415}
416
417/**********************************************************************/
421{
422 if (selected != nullptr) {
423 ui->open_req_edit(QString::fromUtf8(R__("Enabler (actor)")),
425 }
426}
427
428/**********************************************************************/
432{
433 if (selected != nullptr) {
434 if (selected->rulesave.comment != nullptr) {
436 }
437
438 if (!comment->text().isEmpty()) {
439 selected->rulesave.comment = fc_strdup(comment->text().toUtf8());
440 } else {
441 selected->rulesave.comment = nullptr;
442 }
443 }
444}
445
446/**********************************************************************/
451{
452 char buf[MAX_LEN_NAME * 2];
454
455 fc_assert_ret(paction != nullptr);
456
457 /* Can't use QString::asprintf() as msys libintl.h defines asprintf()
458 * as a macro */
459 fc_snprintf(buf, sizeof(buf),
460 R__("action enabler for %s"), action_rule_name(paction));
461
462 // Don't modify the original until the user accepts
465
466 // As precise a title as possible
468}
469
470/**********************************************************************/
477
478/********************************************************************/
482{
483 delete this;
484}
485
486/********************************************************************/
491{
492 return current_enabler;
493}
494
495/********************************************************************/
503
504/**********************************************************************/
511{
512 return my_name.toUtf8().data();
513}
514
515/**********************************************************************/
522{
524
525 if (out != nullptr) {
526 return out;
527 }
528
530}
531
532/**********************************************************************/
548
549/**********************************************************************/
564
565/********************************************************************/
571{
572 return 2;
573}
574
575/********************************************************************/
586
587/********************************************************************/
598
599/**********************************************************************/
606{
607 return (&current_enabler->actor_reqs == vec
608 || &current_enabler->target_reqs == vec);
609}
struct req_vec_problem * action_enabler_suggest_repair(const struct action_enabler *enabler)
Definition actions.c:1922
struct req_vec_problem * action_enabler_suggest_improvement(const struct action_enabler *enabler)
Definition actions.c:1988
const char * action_enabler_vector_by_number_name(req_vec_num_in_item vec)
Definition actions.c:2099
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:2075
#define action_enablers_iterate_end
Definition actions.h:279
#define enabler_get_action(_enabler_)
Definition actions.h:182
#define NUM_ACTIONS
Definition actions.h:59
#define action_iterate_end
Definition actions.h:214
#define action_enablers_iterate(_enabler_)
Definition actions.h:273
#define action_id(_act_)
Definition actions.h:422
#define action_iterate(_act_)
Definition actions.h:210
#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:176
struct action_enabler::@14 rulesave
bool ruledit_disabled
Definition actions.h:174
action_id action
Definition actions.h:167
struct requirement_vector actor_reqs
Definition actions.h:168
struct requirement_vector target_reqs
Definition actions.h:169
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)