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

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/**********************************************************************/
36 void 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/**********************************************************************/
58req_vec_fix_problem::req_vec_fix_problem(
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(
110 req_vec_change_translation(&problem->suggested_solutions[i],
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/**********************************************************************/
131 void req_vec_fix_problem::accept_solution()
132{
133 emit solution_accepted(solutions->checkedId());
134}
135
136/**********************************************************************/
143req_vec_fix::req_vec_fix(ruledit_gui *ui_in,
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/**********************************************************************/
202req_vec_fix::~req_vec_fix()
203{
204 if (current_problem != nullptr) {
205 req_vec_problem_free(current_problem);
206 }
207
208 this->item_info->close();
209
210 this->ui->unregister_req_vec_fix(this);
211}
212
213/**********************************************************************/
217 const void *req_vec_fix::item()
218{
219 return this->item_info->item();
220}
221
222/**********************************************************************/
226 bool req_vec_fix::refresh()
227{
228 if (current_problem != nullptr) {
229 /* The old problem is hopefully solved. If it isn't it will probably be
230 * returned again. */
231 req_vec_problem_free(current_problem);
232 }
233
234 /* Update the current problem. */
235 this->current_problem = item_info->find_next_problem();
236
237 /* Display the new problem */
238 if (current_problem_viewer != nullptr) {
239 current_problem_viewer->hide();
240 current_problem_viewer->deleteLater();
241 }
242 current_problem_viewer = new req_vec_fix_problem(current_problem,
243 item_info);
244 connect(current_problem_viewer, SIGNAL(solution_accepted(int)),
245 this, SLOT(apply_solution(int)));
246 current_problem_area->addWidget(current_problem_viewer);
247 current_problem_area->setCurrentWidget(current_problem_viewer);
248
249 /* Only shown when there is something to do */
250 apply_changes->setVisible(did_apply_a_solution);
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/**********************************************************************/
263 void req_vec_fix::apply_solution(int selected_solution)
264{
265 const struct req_vec_change *solution;
266
267 fc_assert_ret(current_problem);
268 fc_assert_ret(selected_solution >= 0);
269 fc_assert_ret(selected_solution
270 < current_problem->num_suggested_solutions);
271
272 solution = &current_problem->suggested_solutions[selected_solution];
273
274 if (!req_vec_change_apply(solution, item_info->vector_getter(),
275 item_info->item_working_copy())) {
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,
284 item_info->vector_namer()),
285 current_problem->description, item_info->name()));
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/**********************************************************************/
299 void req_vec_fix::accept_applied_solutions()
300{
301 int i;
302
303 this->item_info->apply_accepted_changes();
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++) {
312 emit req_vec_may_have_changed(item_info->vector_getter()
313 (item_info->item(), i));
314 }
315}
316
317/**********************************************************************/
320 void req_vec_fix::reject_applied_solutions()
321{
322 this->item_info->undo_accepted_changes();
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/**********************************************************************/
335 void 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. */
339 reject_applied_solutions();
340 }
341}
req_vec_fix_item
Definition req_vec_fix.h:49
req_vec_fix_item::vector_getter
virtual requirement_vector_by_number vector_getter()=0
req_vec_fix_item::vector_namer
virtual requirement_vector_namer vector_namer()=0
req_vec_fix_item::close
virtual void close()=0
req_vec_fix_item::undo_accepted_changes
virtual void undo_accepted_changes()=0
req_vec_fix_item::apply_accepted_changes
virtual void apply_accepted_changes()=0
req_vec_fix_item::vector_in_item
virtual bool vector_in_item(const struct requirement_vector *vec)=0
req_vec_fix_item::num_vectors
virtual int num_vectors()=0
req_vec_fix_item::name
virtual const char * name()=0
req_vec_fix_item::find_next_problem
virtual struct req_vec_problem * find_next_problem()=0
req_vec_fix_item::item_working_copy
virtual void * item_working_copy()=0
req_vec_fix_item::item
virtual const void * item()=0
req_vec_fix_problem
Definition req_vec_fix.h:137
req_vec_fix_problem::req_vec_fix_problem
req_vec_fix_problem(const struct req_vec_problem *problem, req_vec_fix_item *item_info)
Definition req_vec_fix.cpp:58
req_vec_fix_problem::solution_accepted
void solution_accepted(int selected_solution)
req_vec_fix_problem::solutions
QButtonGroup * solutions
Definition req_vec_fix.h:145
req_vec_fix_problem::accept_solution
void accept_solution()
Definition req_vec_fix.cpp:131
req_vec_fix::refresh
bool refresh(void)
Definition req_vec_fix.cpp:226
req_vec_fix::current_problem
struct req_vec_problem * current_problem
Definition req_vec_fix.h:177
req_vec_fix::close
QPushButton * close
Definition req_vec_fix.h:185
req_vec_fix::item
const void * item()
Definition req_vec_fix.cpp:217
req_vec_fix::~req_vec_fix
~req_vec_fix()
Definition req_vec_fix.cpp:202
req_vec_fix::accept_applied_solutions
void accept_applied_solutions()
Definition req_vec_fix.cpp:299
req_vec_fix::incoming_req_vec_change
void incoming_req_vec_change(const requirement_vector *vec)
Definition req_vec_fix.cpp:335
req_vec_fix::current_problem_viewer
req_vec_fix_problem * current_problem_viewer
Definition req_vec_fix.h:183
req_vec_fix::abort
QPushButton * abort
Definition req_vec_fix.h:185
req_vec_fix::current_problem_area
QStackedLayout * current_problem_area
Definition req_vec_fix.h:184
req_vec_fix::reject_applied_solutions
void reject_applied_solutions()
Definition req_vec_fix.cpp:320
req_vec_fix::ui
ruledit_gui * ui
Definition req_vec_fix.h:181
req_vec_fix::req_vec_may_have_changed
void req_vec_may_have_changed(const requirement_vector *vec)
req_vec_fix::apply_solution
void apply_solution(int selected_solution)
Definition req_vec_fix.cpp:263
req_vec_fix::req_vec_fix
req_vec_fix(ruledit_gui *ui_in, req_vec_fix_item *item_info)
Definition req_vec_fix.cpp:143
req_vec_fix::item_info
req_vec_fix_item * item_info
Definition req_vec_fix.h:178
req_vec_fix::did_apply_a_solution
bool did_apply_a_solution
Definition req_vec_fix.h:179
req_vec_fix::apply_changes
QPushButton * apply_changes
Definition req_vec_fix.h:185
ruledit_gui
Definition ruledit_qt.h:96
ruledit_gui::unregister_req_vec_fix
void unregister_req_vec_fix(req_vec_fix *fixer)
Definition ruledit_qt.cpp:392
MAX_LEN_NAME
#define MAX_LEN_NAME
Definition fc_types.h:66
R__
#define R__(String)
Definition fcintl.h:75
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_fix.h
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_change_apply
bool req_vec_change_apply(const struct req_vec_change *modification, requirement_vector_by_number getter, const void *parent_item)
Definition requirements.c:4897
req_vec_change_translation
const char * req_vec_change_translation(const struct req_vec_change *change, const requirement_vector_namer namer)
Definition requirements.c:4824
req_vec_problem_free
void req_vec_problem_free(struct req_vec_problem *issue)
Definition requirements.c:4994
ruledit_qt.h
item
Definition climisc.h:80
req_vec_change
Definition requirements.h:280
req_vec_problem
Definition requirements.h:287
req_vec_problem::num_suggested_solutions
int num_suggested_solutions
Definition requirements.h:293
req_vec_problem::description
char description[500]
Definition requirements.h:290
req_vec_problem::description_translated
char description_translated[500]
Definition requirements.h:291
req_vec_problem::suggested_solutions
struct req_vec_change * suggested_solutions
Definition requirements.h:294
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969