Freeciv-3.3: /fast/freeciv/nightly/main/tools/ruledit/req_edit.cpp Source File

Freeciv-3.3

Loading...
Searching...
No Matches
req_edit.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 <QLabel>
21#include <QMenu>
22#include <QPushButton>
23#include <QSpinBox>
24
25// utility
26#include "fcintl.h"
27
28// common
29#include "reqtext.h"
30#include "requirements.h"
31
32// ruledit
33#include "ruledit.h"
34#include "ruledit_qt.h"
35#include "univ_value.h"
36
37#include "req_edit.h"
38
39/**********************************************************************/
42req_edit::req_edit(ruledit_gui *ui_in, QString target,
43 struct requirement_vector *preqs) : QDialog()
44{
45 QVBoxLayout *main_layout = new QVBoxLayout(this);
46 QGridLayout *reqedit_layout = new QGridLayout();
47 QHBoxLayout *active_layout = new QHBoxLayout();
48 QPushButton *close_button;
49 QPushButton *add_button;
50 QPushButton *delete_button;
51 QMenu *menu;
52 QLabel *lbl;
53
54 ui = ui_in;
55 connect(ui, SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
56 this, SLOT(incoming_req_vec_change(const requirement_vector *)));
57
58 clear_selected();
59 req_vector = preqs;
60
61 req_list = new QListWidget(this);
62
63 connect(req_list, SIGNAL(itemSelectionChanged()), this, SLOT(select_req()));
64 main_layout->addWidget(req_list);
65
66 lbl = new QLabel(R__("Type:"));
67 active_layout->addWidget(lbl, 0);
68 edit_type_button = new QToolButton();
69 menu = new QMenu();
70 edit_type_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
71 edit_type_button->setPopupMode(QToolButton::MenuButtonPopup);
72 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(req_type_menu(QAction *)));
73 edit_type_button->setMenu(menu);
74 universals_iterate(univ_id) {
75 struct universal dummy;
76
77 dummy.kind = univ_id;
78 if (universal_value_initial(&dummy)) {
79 menu->addAction(universals_n_name(univ_id));
80 }
81 } universals_iterate_end;
82 active_layout->addWidget(edit_type_button, 1);
83
84 lbl = new QLabel(R__("Value:"));
85 active_layout->addWidget(lbl, 2);
86 edit_value_enum_button = new QToolButton();
87 edit_value_enum_menu = new QMenu();
88 edit_value_enum_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
89 edit_value_enum_button->setPopupMode(QToolButton::MenuButtonPopup);
90 connect(edit_value_enum_menu, SIGNAL(triggered(QAction *)),
91 this, SLOT(univ_value_enum_menu(QAction *)));
92 edit_value_enum_button->setMenu(edit_value_enum_menu);
93 edit_value_enum_menu->setVisible(false);
94 active_layout->addWidget(edit_value_enum_button, 3);
95 edit_value_nbr_field = new QSpinBox();
96 edit_value_nbr_field->setRange(-100000, 100000);
97 edit_value_nbr_field->setVisible(false);
98 connect(edit_value_nbr_field, SIGNAL(valueChanged(int)), this,
99 SLOT(univ_value_edit(int)));
100 active_layout->addWidget(edit_value_nbr_field, 4);
101
102 lbl = new QLabel(R__("Range:"));
103 active_layout->addWidget(lbl, 5);
104 edit_range_button = new QToolButton();
105 menu = new QMenu();
106 edit_range_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
107 edit_range_button->setPopupMode(QToolButton::MenuButtonPopup);
108 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(req_range_menu(QAction *)));
109 edit_range_button->setMenu(menu);
110 req_range_iterate(range_id) {
111 menu->addAction(req_range_name(range_id));
112 } req_range_iterate_end;
113 active_layout->addWidget(edit_range_button, 6);
114
115 edit_present_button = new QToolButton();
116 menu = new QMenu();
117 edit_present_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
118 edit_present_button->setPopupMode(QToolButton::MenuButtonPopup);
119 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(req_present_menu(QAction *)));
120 edit_present_button->setMenu(menu);
121 menu->addAction(R__("Allows"));
122 menu->addAction(R__("Prevents"));
123 active_layout->addWidget(edit_present_button, 7);
124
125 main_layout->addLayout(active_layout);
126
127 add_button = new QPushButton(QString::fromUtf8(R__("Add Requirement")), this);
128 connect(add_button, SIGNAL(pressed()), this, SLOT(add_now()));
129 reqedit_layout->addWidget(add_button, 0, 0);
130
131 delete_button = new QPushButton(QString::fromUtf8(R__("Delete Requirement")), this);
132 connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now()));
133 reqedit_layout->addWidget(delete_button, 1, 0);
134
135 close_button = new QPushButton(QString::fromUtf8(R__("Close")), this);
136 connect(close_button, SIGNAL(pressed()), this, SLOT(close_now()));
137 reqedit_layout->addWidget(close_button, 2, 0);
138
139 refresh();
140
141 main_layout->addLayout(reqedit_layout);
142
143 setLayout(main_layout);
144 setWindowTitle(target);
145}
146
147/**********************************************************************/
150 void req_edit::refresh_item(QListWidgetItem *item, struct requirement *preq)
151{
152 char buf[512];
153
154 buf[0] = '\0';
155 if (!req_text_insert(buf, sizeof(buf), nullptr, preq, VERB_ACTUAL, "")) {
156 if (preq->present) {
157 universal_name_translation(&preq->source, buf, sizeof(buf));
158 } else {
159 char buf2[256];
160
161 universal_name_translation(&preq->source, buf2, sizeof(buf2));
162 fc_snprintf(buf, sizeof(buf), R__("%s prevents"), buf2);
163 }
164 }
165
166 item->setText(QString::fromUtf8(buf));
167}
168
169/**********************************************************************/
172 void req_edit::refresh_selected()
173{
174 if (selected != nullptr) {
175 int i;
176 QListWidgetItem *item;
177
178 for (i = 0; (item = req_list->item(i)) != nullptr; i++) {
179 if (item->isSelected()) {
180 refresh_item(item, selected);
181 }
182 }
183 }
184}
185
186/**********************************************************************/
189 void req_edit::refresh()
190{
191 int i = 0;
192
193 req_list->clear();
194
195 requirement_vector_iterate(req_vector, preq) {
196 QListWidgetItem *item = new QListWidgetItem();
197
198 refresh_item(item, preq);
199
200 req_list->insertItem(i++, item);
201 if (selected == preq) {
202 item->setSelected(true);
203 }
204 } requirement_vector_iterate_end;
205
206 fill_active();
207}
208
209/**********************************************************************/
212 void req_edit::update_selected()
213{
214 if (selected != nullptr) {
215 selected_values = *selected;
216 }
217}
218
219/**********************************************************************/
222 void req_edit::clear_selected()
223{
224 selected = nullptr;
225
226 selected_values.source.kind = VUT_NONE;
227 selected_values.source.value.advance = nullptr;
228 selected_values.range = REQ_RANGE_LOCAL;
229 selected_values.present = true;
230 selected_values.survives = false;
231 selected_values.quiet = false;
232}
233
234/**********************************************************************/
237 void req_edit::close_now()
238{
239 ui->unregister_req_edit(this);
240 done(0);
241}
242
243/**********************************************************************/
246 void req_edit::select_req()
247{
248 int i = 0;
249
250 requirement_vector_iterate(req_vector, preq) {
251 QListWidgetItem *item = req_list->item(i++);
252
253 if (item != nullptr && item->isSelected()) {
254 selected = preq;
255 update_selected();
256 fill_active();
257 return;
258 }
259 } requirement_vector_iterate_end;
260}
261
262 struct uvb_data
263{
264 QSpinBox *number;
265 QToolButton *enum_button;
266 QMenu *menu;
267 struct universal *univ;
268};
269
270/**********************************************************************/
273 static void universal_value_cb(const char *value, bool current, void *cbdata)
274{
275 struct uvb_data *data = (struct uvb_data *)cbdata;
276
277 if (value == nullptr) {
278 int kind, val;
279
280 universal_extraction(data->univ, &kind, &val);
281 data->number->setValue(val);
282 data->number->setVisible(true);
283 } else {
284 data->enum_button->setVisible(true);
285 data->menu->addAction(value);
286 if (current) {
287 data->enum_button->setText(value);
288 }
289 }
290}
291
292/**********************************************************************/
295 void req_edit::fill_active()
296{
297 if (selected != nullptr) {
298 struct uvb_data udata;
299
300 edit_type_button->setText(universals_n_name(selected->source.kind));
301 udata.number = edit_value_nbr_field;
302 udata.enum_button = edit_value_enum_button;
303 udata.menu = edit_value_enum_menu;
304 udata.univ = &selected->source;
305 edit_value_enum_menu->clear();
306 edit_value_enum_button->setVisible(false);
307 edit_value_nbr_field->setVisible(false);
308 universal_kind_values(&selected->source, universal_value_cb, &udata);
309 edit_range_button->setText(req_range_name(selected->range));
310 if (selected->present) {
311 edit_present_button->setText(R__("Allows"));
312 } else {
313 edit_present_button->setText(R__("Prevents"));
314 }
315 }
316}
317
318/**********************************************************************/
321 void req_edit::req_type_menu(QAction *action)
322{
323 QByteArray un_bytes = action->text().toUtf8();
324 enum universals_n univ = universals_n_by_name(un_bytes.data(),
325 fc_strcasecmp);
326
327 if (selected != nullptr) {
328 selected->source.kind = univ;
329 universal_value_initial(&selected->source);
330 update_selected();
331 }
332
333 refresh();
334}
335
336/**********************************************************************/
339 void req_edit::req_range_menu(QAction *action)
340{
341 QByteArray un_bytes = action->text().toUtf8();
342 enum req_range range = req_range_by_name(un_bytes.data(),
343 fc_strcasecmp);
344
345 if (selected != nullptr) {
346 selected->range = range;
347 update_selected();
348 }
349
350 refresh();
351}
352
353/**********************************************************************/
356 void req_edit::req_present_menu(QAction *action)
357{
358 if (selected != nullptr) {
359 /* FIXME: Should have more reliable implementation than checking
360 * against *translated* text */
361 if (action->text() == R__("Prevents")) {
362 selected->present = FALSE;
363 } else {
364 selected->present = TRUE;
365 }
366 update_selected();
367 }
368
369 refresh();
370}
371
372/**********************************************************************/
375 void req_edit::univ_value_enum_menu(QAction *action)
376{
377 if (selected != nullptr) {
378 QByteArray un_bytes = action->text().toUtf8();
379
380 universal_value_from_str(&selected->source, un_bytes.data());
381
382 update_selected();
383 refresh();
384 }
385}
386
387/**********************************************************************/
390 void req_edit::univ_value_edit(int value)
391{
392 if (selected != nullptr) {
393 char buf[32];
394
395 fc_snprintf(buf, sizeof(buf), "%d", value);
396
397 universal_value_from_str(&selected->source,
398 buf);
399
400 update_selected();
401
402 /* No full refresh() with fill_selected()
403 * as that would take focus out after every digit user types */
404 refresh_selected();
405 }
406}
407
408/**********************************************************************/
411 void req_edit::add_now()
412{
413 struct requirement new_req;
414
415 new_req = req_from_values(VUT_NONE, REQ_RANGE_LOCAL,
416 false, true, false, 0);
417
418 requirement_vector_append(req_vector, new_req);
419
420 refresh();
421
422 emit req_vec_may_have_changed(req_vector);
423}
424
425/**********************************************************************/
428 void req_edit::delete_now()
429{
430 if (selected != nullptr) {
431 size_t i;
432
433 for (i = 0; i < requirement_vector_size(req_vector); i++) {
434 if (requirement_vector_get(req_vector, i) == selected) {
435 requirement_vector_remove(req_vector, i);
436 break;
437 }
438 }
439
440 clear_selected();
441
442 refresh();
443
444 emit req_vec_may_have_changed(req_vector);
445 }
446}
447
448/**********************************************************************/
452 void req_edit::incoming_req_vec_change(const requirement_vector *vec)
453{
454 if (req_vector == vec) {
455 // The selected requirement may be gone
456
457 selected = nullptr;
458 requirement_vector_iterate(req_vector, preq) {
459 if (are_requirements_equal(preq, &selected_values)) {
460 selected = preq;
461 break;
462 }
463 } requirement_vector_iterate_end;
464
465 if (selected == nullptr) {
466 // The currently selected requirement was deleted.
467 clear_selected();
468 }
469
470 refresh();
471 }
472}
473
474/**********************************************************************/
477 void req_edit::closeEvent(QCloseEvent *event)
478{
479 ui->unregister_req_edit(this);
480}
req_edit::req_vector
struct requirement_vector * req_vector
Definition req_edit.h:45
req_edit::ui
ruledit_gui * ui
Definition req_edit.h:55
req_edit::req_present_menu
void req_present_menu(QAction *action)
Definition req_edit.cpp:356
req_edit::delete_now
void delete_now()
Definition req_edit.cpp:428
req_edit::edit_value_enum_button
QToolButton * edit_value_enum_button
Definition req_edit.h:67
req_edit::edit_present_button
QToolButton * edit_present_button
Definition req_edit.h:71
req_edit::refresh
void refresh()
Definition req_edit.cpp:189
req_edit::selected_values
struct requirement selected_values
Definition req_edit.h:60
req_edit::univ_value_enum_menu
void univ_value_enum_menu(QAction *action)
Definition req_edit.cpp:375
req_edit::close_now
void close_now()
Definition req_edit.cpp:237
req_edit::incoming_req_vec_change
void incoming_req_vec_change(const requirement_vector *vec)
Definition req_edit.cpp:452
req_edit::univ_value_edit
void univ_value_edit(int value)
Definition req_edit.cpp:390
req_edit::edit_value_nbr_field
QSpinBox * edit_value_nbr_field
Definition req_edit.h:69
req_edit::edit_range_button
QToolButton * edit_range_button
Definition req_edit.h:70
req_edit::fill_active
void fill_active()
Definition req_edit.cpp:295
req_edit::closeEvent
void closeEvent(QCloseEvent *event)
Definition req_edit.cpp:477
req_edit::selected
struct requirement * selected
Definition req_edit.h:59
req_edit::req_edit
req_edit(ruledit_gui *ui_in, QString target, struct requirement_vector *preqs)
Definition req_edit.cpp:42
req_edit::req_vec_may_have_changed
void req_vec_may_have_changed(const requirement_vector *vec)
req_edit::edit_value_enum_menu
QMenu * edit_value_enum_menu
Definition req_edit.h:68
req_edit::req_type_menu
void req_type_menu(QAction *action)
Definition req_edit.cpp:321
req_edit::update_selected
void update_selected()
Definition req_edit.cpp:212
req_edit::select_req
void select_req()
Definition req_edit.cpp:246
req_edit::edit_type_button
QToolButton * edit_type_button
Definition req_edit.h:66
req_edit::clear_selected
void clear_selected()
Definition req_edit.cpp:222
req_edit::refresh_item
void refresh_item(QListWidgetItem *item, struct requirement *preq)
Definition req_edit.cpp:150
req_edit::req_range_menu
void req_range_menu(QAction *action)
Definition req_edit.cpp:339
req_edit::refresh_selected
void refresh_selected()
Definition req_edit.cpp:172
req_edit::req_list
QListWidget * req_list
Definition req_edit.h:57
req_edit::add_now
void add_now()
Definition req_edit.cpp:411
ruledit_gui
Definition ruledit_qt.h:97
ruledit_gui::unregister_req_edit
void unregister_req_edit(class req_edit *redit)
Definition ruledit_qt.cpp:360
incite_cost
char * incite_cost
Definition comments.c:74
event
enum event_type event
Definition events.c:81
fcintl.h
R__
#define R__(String)
Definition fcintl.h:75
universal_value_cb
static void universal_value_cb(const char *value, bool current, void *cbdata)
Definition req_edit.cpp:273
req_edit.h
req_text_insert
bool req_text_insert(char *buf, size_t bufsz, struct player *pplayer, const struct requirement *preq, enum rt_verbosity verb, const char *prefix)
Definition reqtext.c:48
reqtext.h
VERB_ACTUAL
@ VERB_ACTUAL
Definition reqtext.h:20
universal_extraction
void universal_extraction(const struct universal *source, int *kind, int *value)
Definition requirements.c:871
are_requirements_equal
bool are_requirements_equal(const struct requirement *req1, const struct requirement *req2)
Definition requirements.c:1469
universal_value_from_str
void universal_value_from_str(struct universal *source, const char *value)
Definition requirements.c:280
req_from_values
struct requirement req_from_values(int type, int range, bool survives, bool present, bool quiet, int value)
Definition requirements.c:1422
universal_name_translation
const char * universal_name_translation(const struct universal *psource, char *buf, size_t bufsz)
Definition requirements.c:6962
requirements.h
universals_iterate
#define universals_iterate(_univ_)
Definition requirements.h:396
requirement_vector_iterate_end
#define requirement_vector_iterate_end
Definition requirements.h:94
req_range_iterate_end
#define req_range_iterate_end
Definition requirements.h:71
req_range_iterate
#define req_range_iterate(_range_)
Definition requirements.h:65
requirement_vector_iterate
#define requirement_vector_iterate(req_vec, preq)
Definition requirements.h:92
universals_iterate_end
#define universals_iterate_end
Definition requirements.h:401
ruledit.h
ruledit_qt.h
action
Definition actions.h:106
item
Definition climisc.h:80
item::item
struct universal item
Definition climisc.h:81
range
Definition unittype.c:879
requirement
Definition requirements.h:81
requirement::quiet
bool quiet
Definition requirements.h:86
requirement::survives
bool survives
Definition requirements.h:84
requirement::present
bool present
Definition requirements.h:85
requirement::range
enum req_range range
Definition requirements.h:83
requirement::source
struct universal source
Definition requirements.h:82
universal
Definition fc_types.h:908
universal::kind
enum universals_n kind
Definition fc_types.h:910
universal::value
universals_u value
Definition fc_types.h:909
uvb_data
Definition req_edit.cpp:263
uvb_data::number
QSpinBox * number
Definition req_edit.cpp:264
uvb_data::menu
QMenu * menu
Definition req_edit.cpp:266
uvb_data::enum_button
QToolButton * enum_button
Definition req_edit.cpp:265
uvb_data::univ
struct universal * univ
Definition req_edit.cpp:267
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
universals_u::advance
struct advance * advance
Definition fc_types.h:718
universal_value_initial
bool universal_value_initial(struct universal *src)
Definition univ_value.c:42
universal_kind_values
void universal_kind_values(struct universal *univ, univ_kind_values_cb cb, void *data)
Definition univ_value.c:266
univ_value.h