Freeciv-3.2: /fast/freeciv/nightly/S3_2/tools/ruledit/req_vec_fix.cpp Source File

Freeciv-3.2

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