Freeciv-3.3
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/**********************************************************************/
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/**********************************************************************/
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
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++) {
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/**********************************************************************/
136
137/**********************************************************************/
145 req_vec_fix_item *item) : QWidget()
146{
149
150 this->ui = ui_in;
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;
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()),
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/**********************************************************************/
204{
205 if (current_problem != nullptr) {
207 }
208
209 this->item_info->close();
210
211 this->ui->unregister_req_vec_fix(this);
212}
213
214/**********************************************************************/
218const void *req_vec_fix::item()
219{
220 return this->item_info->item();
221}
222
223/**********************************************************************/
228{
229 if (current_problem != nullptr) {
230 /* The old problem is hopefully solved. If it isn't it will probably be
231 * returned again. */
233 }
234
235 // Update the current problem.
237
238 // Display the new problem
239 if (current_problem_viewer != nullptr) {
241 current_problem_viewer->deleteLater();
242 }
244 item_info);
245 connect(current_problem_viewer, SIGNAL(solution_accepted(int)),
246 this, SLOT(apply_solution(int)));
249
250 // Only shown when there is something to do
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/**********************************************************************/
265{
266 const struct req_vec_change *solution;
267
272
274
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."))
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/**********************************************************************/
301{
302 int i;
303
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++) {
314 (item_info->item(), i));
315 }
316}
317
318/**********************************************************************/
322{
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/**********************************************************************/
337{
338 if (this->item_info->vector_in_item(vec)) {
339 // Can't trust the changes done against a previous version.
341 }
342}
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)
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
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]
struct req_vec_change * suggested_solutions
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960