Freeciv-3.1
Loading...
Searching...
No Matches
req_vec_fix.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2020 - The Freeciv Project contributors.
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/* Qt */
15#include <QButtonGroup>
16#include <QHBoxLayout>
17#include <QLabel>
18#include <QListWidgetItem>
19#include <QMessageBox>
20#include <QPushButton>
21#include <QRadioButton>
22#include <QStackedLayout>
23#include <QVBoxLayout>
24
25/* ruledit */
26#include "ruledit_qt.h"
27
28
29#include "req_vec_fix.h"
30
31/**********************************************************************/
36void mark_item(QListWidgetItem *item,
37 enum req_vec_problem_seriousness problem_level)
38{
39 QWidget *in = item->listWidget();
40 switch (problem_level) {
41 case RVPS_NO_PROBLEM:
42 item->setIcon(QIcon());
43 break;
44 case RVPS_IMPROVE:
45 item->setIcon(in->style()->standardIcon(QStyle::SP_MessageBoxWarning));
46 break;
47 case RVPS_REPAIR:
48 item->setIcon(in->style()->standardIcon(QStyle::SP_MessageBoxCritical));
49 break;
50 }
51}
52
53/**********************************************************************/
59 const struct req_vec_problem *problem,
60 req_vec_fix_item *item_info) : QWidget()
61{
62 int i;
63 QLabel *description;
64 QPushButton *accept;
65
66 QVBoxLayout *layout_main = new QVBoxLayout();
67
68 /* Sanity check. */
69 fc_assert_ret(item_info);
70
71 description = new QLabel();
72 layout_main->addWidget(description);
73
74 if (problem == nullptr) {
75 /* Everything is OK. */
76
77 /* TRANS: Trying to fix a requirement vector problem but can't find
78 * any. */
79 description->setText(R__("No problem found"));
80 this->setLayout(layout_main);
81 return;
82 }
83
84 if (problem->num_suggested_solutions == 0) {
85 /* Didn't get any suggestions about how to solve this. */
86
87 char buf[MAX_LEN_NAME * 3];
88
89 fc_snprintf(buf, sizeof(buf),
90 /* TRANS: Trying to fix a requirement vector problem but
91 * don't know how to solve it. */
92 R__("Don't know how to fix %s: %s"),
93 item_info->name(), problem->description);
94
95 description->setText(buf);
96 this->setLayout(layout_main);
97 return;
98 }
99
100 /* A problem with at least one solution exists. */
101 fc_assert_ret(problem && problem->num_suggested_solutions > 0);
102
103 /* Display the problem text */
104 description->setText(QString::fromUtf8(problem->description_translated));
105
106 /* Display each solution */
107 solutions = new QButtonGroup(this);
108 for (i = 0; i < problem->num_suggested_solutions; i++) {
109 QRadioButton *solution = new QRadioButton(
111 item_info->vector_namer()));
112
113 solutions->addButton(solution, i);
114 solution->setChecked(i == 0);
115
116 layout_main->addWidget(solution);
117 }
118
119 /* TRANS: Apply the selected requirement vector problem fix. */
120 accept = new QPushButton(R__("Accept selected solution"));
121 connect(accept, SIGNAL(pressed()), this, SLOT(accept_solution()));
122 layout_main->addWidget(accept);
123
124 this->setLayout(layout_main);
125}
126
127/**********************************************************************/
132{
133 emit solution_accepted(solutions->checkedId());
134}
135
136/**********************************************************************/
144 req_vec_fix_item *item) : QWidget()
145{
146 QVBoxLayout *layout_main = new QVBoxLayout();
147 QHBoxLayout *layout_buttons = new QHBoxLayout();
148
149 this->ui = ui_in;
150 connect(ui, SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
151 this, SLOT(incoming_req_vec_change(const requirement_vector *)));
152
153 this->item_info = item;
154
155 this->current_problem = nullptr;
156 this->did_apply_a_solution = false;
157
158 this->setWindowTitle(R__("Requirement problem"));
159 this->setAttribute(Qt::WA_DeleteOnClose);
160
161 /* Set up the area for viewing problems */
162 this->current_problem_viewer = nullptr;
163 this->current_problem_area = new QStackedLayout();
164 layout_main->addLayout(current_problem_area);
165
166 /* TRANS: Button text in the requirement vector fixer dialog. Cancels all
167 * changes done since the last time all accepted changes were done. */
168 abort = new QPushButton(R__("Undo all"));
169 /* TRANS: Tool tip text in the requirement vector fixer dialog. Cancels
170 * all changes done since the last time all accepted changes were done. */
171 abort->setToolTip(R__("Undo all accepted solutions since you started or"
172 " since last time you ordered all accepted changes"
173 " done."));
174 connect(abort, SIGNAL(pressed()), this, SLOT(reject_applied_solutions()));
175 layout_buttons->addWidget(abort);
176
177 /* TRANS: Perform all the changes to the ruleset item the user has
178 * accepted. Button text in the requirement vector fixer dialog. */
179 apply_changes = new QPushButton(R__("Do accepted changes"));
180 /* TRANS: Perform all the changes to the ruleset item the user has
181 * accepted. Tool tip text in the requirement vector fixer dialog. */
182 apply_changes->setToolTip(R__("Perform all the changes you have accepted"
183 " to the ruleset item. You can then fix the"
184 " current issue by hand and come back here"
185 " to find the next issue."));
186 connect(apply_changes, SIGNAL(pressed()),
187 this, SLOT(accept_applied_solutions()));
188 layout_buttons->addWidget(apply_changes);
189
190 close = new QPushButton(R__("Close"));
191 connect(close, SIGNAL(pressed()), this, SLOT(close()));
192 layout_buttons->addWidget(close);
193
194 layout_main->addLayout(layout_buttons);
195
196 this->setLayout(layout_main);
197}
198
199/**********************************************************************/
203{
204 if (current_problem != nullptr) {
206 }
207
208 this->item_info->close();
209
210 this->ui->unregister_req_vec_fix(this);
211}
212
213/**********************************************************************/
217const void *req_vec_fix::item()
218{
219 return this->item_info->item();
220}
221
222/**********************************************************************/
227{
228 if (current_problem != nullptr) {
229 /* The old problem is hopefully solved. If it isn't it will probably be
230 * returned again. */
232 }
233
234 /* Update the current problem. */
236
237 /* Display the new problem */
238 if (current_problem_viewer != nullptr) {
240 current_problem_viewer->deleteLater();
241 }
243 item_info);
244 connect(current_problem_viewer, SIGNAL(solution_accepted(int)),
245 this, SLOT(apply_solution(int)));
248
249 /* Only shown when there is something to do */
251 abort->setVisible(did_apply_a_solution);
252
253 /* Only shown when no work will be lost */
254 close->setVisible(!did_apply_a_solution);
255
256 return current_problem != nullptr;
257}
258
259/**********************************************************************/
263void req_vec_fix::apply_solution(int selected_solution)
264{
265 const struct req_vec_change *solution;
266
268 fc_assert_ret(selected_solution >= 0);
269 fc_assert_ret(selected_solution
271
272 solution = &current_problem->suggested_solutions[selected_solution];
273
276 QMessageBox *box = new QMessageBox();
277
278 box->setWindowTitle(R__("Unable to apply solution"));
279
280 box->setText(
281 /* TRANS: requirement vector fix failed to apply */
282 QString(R__("Failed to apply solution %1 for %2 to %3."))
283 .arg(req_vec_change_translation(solution,
286 box->setStandardButtons(QMessageBox::Ok);
287 box->exec();
288 } else {
289 /* A solution has been applied */
290 this->did_apply_a_solution = true;
291 }
292
293 this->refresh();
294}
295
296/**********************************************************************/
300{
301 int i;
302
304
305 /* All is accepted */
306 this->did_apply_a_solution = false;
307
308 /* New check point. */
309 this->refresh();
310
311 for (i = 0; i < item_info->num_vectors(); i++) {
313 (item_info->item(), i));
314 }
315}
316
317/**********************************************************************/
321{
323
324 /* All is gone */
325 this->did_apply_a_solution = false;
326
327 /* Back to the start again. */
328 this->refresh();
329}
330
331/**********************************************************************/
335void req_vec_fix::incoming_req_vec_change(const requirement_vector *vec)
336{
337 if (this->item_info->vector_in_item(vec)) {
338 /* Can't trust the changes done against a previous version. */
340 }
341}
virtual requirement_vector_by_number vector_getter()=0
virtual requirement_vector_namer vector_namer()=0
virtual void close()=0
virtual void undo_accepted_changes()=0
virtual void apply_accepted_changes()=0
virtual bool vector_in_item(const struct requirement_vector *vec)=0
virtual int num_vectors()=0
virtual const char * name()=0
virtual struct req_vec_problem * find_next_problem()=0
virtual void * item_working_copy()=0
virtual const void * item()=0
req_vec_fix_problem(const struct req_vec_problem *problem, req_vec_fix_item *item_info)
void solution_accepted(int selected_solution)
QButtonGroup * solutions
bool refresh(void)
struct req_vec_problem * current_problem
QPushButton * close
const void * item()
void accept_applied_solutions()
void incoming_req_vec_change(const requirement_vector *vec)
req_vec_fix_problem * current_problem_viewer
QPushButton * abort
QStackedLayout * current_problem_area
void reject_applied_solutions()
ruledit_gui * ui
void req_vec_may_have_changed(const requirement_vector *vec)
void apply_solution(int selected_solution)
req_vec_fix(ruledit_gui *ui_in, req_vec_fix_item *item_info)
req_vec_fix_item * item_info
bool did_apply_a_solution
QPushButton * apply_changes
void unregister_req_vec_fix(req_vec_fix *fixer)
#define MAX_LEN_NAME
Definition fc_types.h:66
#define R__(String)
Definition fcintl.h:75
#define fc_assert_ret(condition)
Definition log.h:191
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
bool req_vec_change_apply(const struct req_vec_change *modification, requirement_vector_by_number getter, const void *parent_item)
const char * req_vec_change_translation(const struct req_vec_change *change, const requirement_vector_namer namer)
void req_vec_problem_free(struct req_vec_problem *issue)
Definition climisc.h:82
char description[500]
char description_translated[500]
struct req_vec_change * suggested_solutions
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969