Freeciv-3.1
Loading...
Searching...
No Matches
ruledit_qt.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 <QApplication>
20#include <QCloseEvent>
21#include <QLabel>
22#include <QLineEdit>
23#include <QMainWindow>
24#include <QMessageBox>
25#include <QPushButton>
26#include <QStackedLayout>
27#include <QVBoxLayout>
28
29// utility
30#include "fcintl.h"
31#include "log.h"
32#include "registry.h"
33
34// common
35#include "game.h"
36#include "version.h"
37
38// server
39#include "ruleset.h"
40
41// ruledit
42#include "conversion_log.h"
43#include "effect_edit.h"
44#include "requirers_dlg.h"
45#include "req_edit.h"
46#include "req_vec_fix.h"
47#include "ruledit.h"
48#include "tab_achievement.h"
49#include "tab_building.h"
50#include "tab_enablers.h"
51#include "tab_extras.h"
52#include "tab_good.h"
53#include "tab_gov.h"
54#include "tab_misc.h"
55#include "tab_multiplier.h"
56#include "tab_nation.h"
57#include "tab_tech.h"
58#include "tab_terrains.h"
59#include "tab_unit.h"
60
61#include "ruledit_qt.h"
62
64static QApplication *qapp;
66
67/**********************************************************************/
70int ruledit_qt_run(int argc, char **argv)
71{
72 ruledit_main *main_window;
73 QWidget *central;
74 int ret;
75
76 qapp = new QApplication(argc, argv);
77 central = new QWidget;
78 main_window = new ruledit_main(qapp, central);
79
80 gui = new ruledit_gui;
81 gui->setup(central);
82 main_window->setCentralWidget(central);
83 main_window->setVisible(true);
84
85 ret = qapp->exec();
86
87 delete gui;
88 delete qapp;
89
90 return ret;
91}
92
93/**********************************************************************/
96void ruledit_qt_display_requirers(const char *msg, void *data)
97{
98 requirers_dlg *requirers = (requirers_dlg *)data;
99
100 gui->show_required(requirers, msg);
101}
102
103/**********************************************************************/
106void ruledit_gui::setup(QWidget *central_in)
107{
108 QVBoxLayout *full_layout = new QVBoxLayout();
109 QVBoxLayout *edit_layout = new QVBoxLayout();
110 QWidget *edit_widget = new QWidget();
111 QPushButton *ruleset_accept;
112 QLabel *rs_label;
113 QLabel *version_label;
114 char verbuf[2048];
115 const char *rev_ver;
116 const char *mode;
117
118 data.nationlist = NULL;
119 data.nationlist_saved = NULL;
120
121 central = central_in;
122
123 rev_ver = fc_git_revision();
124
125#ifndef FC_QT5_MODE
126 mode = R__("built in Qt6 mode.");
127#else // FC_QT5_MODE
128 mode = R__("built in Qt5 mode.");
129#endif // FC_QT5_MODE
130
131 if (rev_ver == NULL) {
132 fc_snprintf(verbuf, sizeof(verbuf), "%s%s\n%s", word_version(),
133 VERSION_STRING, mode);
134 } else {
135 fc_snprintf(verbuf, sizeof(verbuf), R__("%s%s\ncommit: %s\n%s"),
136 word_version(), VERSION_STRING, rev_ver, mode);
137 }
138
139 main_layout = new QStackedLayout();
140
141 if (reargs.ruleset == nullptr) {
142 QVBoxLayout *preload_layout = new QVBoxLayout();
143 QWidget *preload_widget = new QWidget();
144
145 preload_layout->setSizeConstraint(QLayout::SetMaximumSize);
146 version_label = new QLabel(verbuf);
147 version_label->setAlignment(Qt::AlignHCenter);
148 version_label->setParent(central);
149 preload_layout->addWidget(version_label);
150 rs_label = new QLabel(QString::fromUtf8(R__("Give ruleset to use as starting point.")));
151 rs_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
152 preload_layout->addWidget(rs_label);
153 ruleset_select = new QLineEdit(central);
155 connect(ruleset_select, SIGNAL(returnPressed()),
156 this, SLOT(rulesetdir_given()));
157 preload_layout->addWidget(ruleset_select);
158 ruleset_accept = new QPushButton(QString::fromUtf8(R__("Start editing")));
159 connect(ruleset_accept, SIGNAL(pressed()), this, SLOT(rulesetdir_given()));
160 preload_layout->addWidget(ruleset_accept);
161
162 preload_widget->setLayout(preload_layout);
163 main_layout->addWidget(preload_widget);
164 }
165
166 stack = new QTabWidget(central);
167
168 misc = new tab_misc(this);
169 stack->addTab(misc, QString::fromUtf8(R__("Misc")));
170 tech = new tab_tech(this);
171 stack->addTab(tech, QString::fromUtf8(R__("Tech")));
172 bldg = new tab_building(this);
173 stack->addTab(bldg, QString::fromUtf8(R__("Buildings")));
174 unit = new tab_unit(this);
175 stack->addTab(unit, QString::fromUtf8(R__("Units")));
176 ach = new tab_achievement(this);
177 stack->addTab(ach, QString::fromUtf8(R__("Achievements")));
178 good = new tab_good(this);
179 stack->addTab(good, QString::fromUtf8(R__("Goods")));
180 gov = new tab_gov(this);
181 stack->addTab(gov, QString::fromUtf8(R__("Governments")));
182 enablers = new tab_enabler(this);
183 stack->addTab(enablers, QString::fromUtf8(R__("Enablers")));
184 extras = new tab_extras(this);
185 stack->addTab(extras, QString::fromUtf8(R__("Extras")));
186 terrains = new tab_terrains(this);
187 stack->addTab(terrains, QString::fromUtf8(R__("Terrains")));
188 multipliers = new tab_multiplier(this);
189 stack->addTab(multipliers, QString::fromUtf8(R__("Multipliers")));
190 nation = new tab_nation(this);
191 stack->addTab(nation, QString::fromUtf8(R__("Nations")));
192
193 edit_layout->addWidget(stack);
194
195 edit_widget->setLayout(edit_layout);
196 main_layout->addWidget(edit_widget);
197
198 full_layout->addLayout(main_layout);
199
200 msg_dspl = new QLabel(QString::fromUtf8(R__("Welcome to freeciv-ruledit")));
201 msg_dspl->setParent(central);
202
203 msg_dspl->setAlignment(Qt::AlignHCenter);
204
205 full_layout->addWidget(msg_dspl);
206
207 central->setLayout(full_layout);
208
209 req_edits = req_edit_list_new();
210 this->req_vec_fixers = req_vec_fix_list_new();
211 effect_edits = effect_edit_list_new();
212
213 if (reargs.ruleset != nullptr) {
215 launch_now();
216 }
217}
218
219/**********************************************************************/
222static void conversion_log_cb(const char *msg)
223{
224 convlog->add(msg);
225}
226
227/**********************************************************************/
231{
232 QByteArray rn_bytes;
233
234 rn_bytes = ruleset_select->text().toUtf8();
235 sz_strlcpy(game.server.rulesetdir, rn_bytes.data());
236
237 launch_now();
238}
239
240/**********************************************************************/
246{
247 convlog = new conversion_log(QString::fromUtf8(R__("Old ruleset to a new format")));
248
249 if (load_rulesets(NULL, NULL, TRUE, conversion_log_cb, FALSE, TRUE, TRUE)) {
250 display_msg(R__("Ruleset loaded"));
251
252 /* Make freeable copy */
253 if (game.server.ruledit.nationlist != NULL) {
255 } else {
256 data.nationlist = NULL;
257 }
258
259 ach->refresh();
260 bldg->refresh();
262 nation->refresh();
263 tech->refresh();
264 unit->refresh();
265 good->refresh();
266 gov->refresh();
267 enablers->refresh();
268 extras->refresh();
270 terrains->refresh();
271 main_layout->setCurrentIndex(1);
272 } else {
273 display_msg(R__("Ruleset loading failed!"));
274 }
275}
276
277/**********************************************************************/
281void ruledit_gui::incoming_req_vec_change(const requirement_vector *vec)
282{
283 emit req_vec_may_have_changed(vec);
284}
285
286/**********************************************************************/
289void ruledit_gui::display_msg(const char *msg)
290{
291 msg_dspl->setText(QString::fromUtf8(msg));
292}
293
294/**********************************************************************/
298{
299 requirers_dlg *requirers;
300
301 requirers = new requirers_dlg(this);
302
303 requirers->clear(title);
304
305 return requirers;
306}
307
308/**********************************************************************/
311void ruledit_gui::show_required(requirers_dlg *requirers, const char *msg)
312{
313 requirers->add(msg);
314
315 // Show dialog if not already visible
316 requirers->show();
317}
318
319/**********************************************************************/
327
328/**********************************************************************/
331void ruledit_gui::open_req_edit(QString target, struct requirement_vector *preqs)
332{
333 req_edit *redit;
334
336 if (old_edit->req_vector == preqs) {
337 // Already open
338 return;
339 }
341
342 redit = new req_edit(this, target, preqs);
343
344 redit->show();
345
346 connect(redit,
347 SIGNAL(req_vec_may_have_changed(const requirement_vector *)),
348 this, SLOT(incoming_req_vec_change(const requirement_vector *)));
349
350 req_edit_list_append(req_edits, redit);
351}
352
353/**********************************************************************/
357{
358 req_edit_list_remove(req_edits, redit);
359}
360
361/**********************************************************************/
365{
366 req_vec_fix *fixer;
367
369 if (old_fixer->item() == item_info->item()) {
370 item_info->close();
371
372 /* Already open */
373 return;
374 }
376
377 fixer = new req_vec_fix(this, item_info);
378
379 fixer->refresh();
380 fixer->show();
381
382 connect(fixer,
383 SIGNAL(rec_veq_may_have_changed(const requirement_vector *)),
384 this, SLOT(incoming_req_vec_change(const requirement_vector *)));
385
386 req_vec_fix_list_append(req_vec_fixers, fixer);
387}
388
389/**********************************************************************/
393{
394 req_vec_fix_list_remove(req_vec_fixers, fixer);
395}
396
397/**********************************************************************/
400void ruledit_gui::open_effect_edit(QString target, struct universal *uni,
401 enum effect_filter_main_class efmc)
402{
403 effect_edit *e_edit;
404
406 struct universal *old = old_edit->filter_get();
407
408 if (uni != nullptr) {
409 if (are_universals_equal(old, uni)) {
410 // Already open
411 return;
412 }
413 } else if (old->kind == VUT_NONE && old_edit->efmc == efmc) {
414 // Already open
415 return;
416 }
418
419 e_edit = new effect_edit(this, target, uni, efmc);
420
421 e_edit->show();
422
423 effect_edit_list_append(effect_edits, e_edit);
424}
425
426/**********************************************************************/
430{
431 effect_edit_list_remove(effect_edits, e_edit);
432}
433
434/**********************************************************************/
443
444/**********************************************************************/
447ruledit_main::ruledit_main(QApplication *qapp_in, QWidget *central_in) : QMainWindow()
448{
449 QPixmap *pm = new QPixmap;
450 const char *full_icon_path = fileinfoname(get_data_dirs(), "freeciv-ruledit.png");
451 const QString title = QString::fromUtf8(R__("Freeciv Ruleset Editor"));
452 const QString fip = QString(full_icon_path);
453 QSize old_size;
454 int width;
455 int height;
456
457 pm->load(fip);
458 qapp_in->setWindowIcon(QIcon(*pm));
459
460 qapp = qapp_in;
461 central = central_in;
462
463 setWindowTitle(title);
464
465 // Adjust Window size
466 old_size = size();
467 width = old_size.width();
468 height = old_size.height();
469
472
473 resize(width, height);
474}
475
476/**********************************************************************/
480{
481 QMessageBox ask(central);
482 int ret;
483
484 ask.setText(R__("Are you sure you want to quit?"));
485 ask.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
486 ask.setDefaultButton(QMessageBox::Cancel);
487 ask.setIcon(QMessageBox::Warning);
488 ask.setWindowTitle(R__("Quit?"));
489 ret = ask.exec();
490
491 switch (ret) {
492 case QMessageBox::Cancel:
493 return;
494 break;
495 case QMessageBox::Ok:
496 qapp->quit();
497 break;
498 }
499}
500
501/**********************************************************************/
504void ruledit_main::closeEvent(QCloseEvent *cevent)
505{
506 // Handle quit via confirmation dialog.
508
509 // Do not handle quit here, but let user to answer to confirmation dialog.
510 cevent->ignore();
511}
struct canvas int int struct sprite int int int int height
Definition canvas_g.h:44
struct canvas int int struct sprite int int int width
Definition canvas_g.h:44
void add(const char *msg)
virtual void close()=0
virtual const void * item()=0
bool refresh(void)
void clear(const char *title)
void add(const char *msg)
tab_nation * nation
Definition ruledit_qt.h:146
tab_achievement * ach
Definition ruledit_qt.h:135
void req_vec_may_have_changed(const requirement_vector *vec)
void open_req_vec_fix(req_vec_fix_item *item_info)
tab_good * good
Definition ruledit_qt.h:140
tab_enabler * enablers
Definition ruledit_qt.h:142
void unregister_effect_edit(class effect_edit *e_edit)
void flush_widgets()
tab_multiplier * multipliers
Definition ruledit_qt.h:144
QStackedLayout * main_layout
Definition ruledit_qt.h:133
struct rule_data data
Definition ruledit_qt.h:117
tab_misc * misc
Definition ruledit_qt.h:137
struct effect_edit_list * effect_edits
Definition ruledit_qt.h:150
tab_extras * extras
Definition ruledit_qt.h:143
void incoming_req_vec_change(const requirement_vector *vec)
tab_terrains * terrains
Definition ruledit_qt.h:145
void display_msg(const char *msg)
void refresh_effect_edits()
void setup(QWidget *central_in)
void open_req_edit(QString target, struct requirement_vector *preqs)
tab_gov * gov
Definition ruledit_qt.h:141
void unregister_req_vec_fix(req_vec_fix *fixer)
tab_building * bldg
Definition ruledit_qt.h:136
requirers_dlg * create_requirers(const char *title)
tab_tech * tech
Definition ruledit_qt.h:138
void launch_now()
struct req_edit_list * req_edits
Definition ruledit_qt.h:148
QTabWidget * stack
Definition ruledit_qt.h:130
struct req_vec_fix_list * req_vec_fixers
Definition ruledit_qt.h:149
void show_required(requirers_dlg *requirers, const char *msg)
QLabel * msg_dspl
Definition ruledit_qt.h:129
QWidget * central
Definition ruledit_qt.h:132
void rulesetdir_given()
void open_effect_edit(QString target, struct universal *uni, enum effect_filter_main_class efmc)
QLineEdit * ruleset_select
Definition ruledit_qt.h:131
void unregister_req_edit(class req_edit *redit)
QWidget * central
Definition ruledit_qt.h:62
void popup_quit_dialog()
ruledit_main(QApplication *qapp_in, QWidget *central_in)
void closeEvent(QCloseEvent *cevent)
QApplication * qapp
Definition ruledit_qt.h:61
void refresh()
void refresh()
Definition tab_good.cpp:114
void refresh()
Definition tab_gov.cpp:116
void flush_widgets()
Definition tab_misc.cpp:504
void ruleset_loaded()
Definition tab_misc.cpp:246
void flush_widgets()
void refresh()
void refresh()
Definition tab_tech.cpp:141
effect_filter_main_class
Definition effect_edit.h:33
#define R__(String)
Definition fcintl.h:75
struct civ_game game
Definition game.c:57
#define GAME_DEFAULT_RULESETDIR
Definition game.h:654
const char * title
Definition repodlgs.c:1313
#define fc_strdup(str)
Definition mem.h:43
bool are_universals_equal(const struct universal *psource1, const struct universal *psource2)
struct ruledit_arguments reargs
Definition ruledit.cpp:56
void ruledit_qt_display_requirers(const char *msg, void *data)
static QApplication * qapp
int ruledit_qt_run(int argc, char **argv)
static void conversion_log_cb(const char *msg)
static ruledit_gui * gui
static conversion_log * convlog
#define RULEDIT_WINHEIGHT
Definition ruledit_qt.h:50
#define effect_edit_list_iterate_end
Definition ruledit_qt.h:84
#define req_edit_list_iterate(reqeditlist, preqedit)
Definition ruledit_qt.h:73
#define RULEDIT_WINWIDTH
Definition ruledit_qt.h:49
#define req_vec_fix_list_iterate(reqvecfixlist, preqvecfix)
Definition ruledit_qt.h:91
#define effect_edit_list_iterate(effecteditlist, peffectedit)
Definition ruledit_qt.h:82
#define req_edit_list_iterate_end
Definition ruledit_qt.h:75
#define req_vec_fix_list_iterate_end
Definition ruledit_qt.h:93
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Definition ruleset.c:9227
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1094
const struct strvec * get_data_dirs(void)
Definition shared.c:886
#define MAX(x, y)
Definition shared.h:54
size_t size
Definition specvec.h:72
char * nationlist
Definition game.h:274
struct civ_game::@30::@34 server
char rulesetdir[MAX_LEN_NAME]
Definition game.h:236
struct civ_game::@30::@34::@39 ruledit
char * nationlist
Definition rulesave.h:22
char * nationlist_saved
Definition rulesave.h:23
Definition unit.h:138
enum universals_n kind
Definition fc_types.h:758
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
#define sz_strlcpy(dest, src)
Definition support.h:167
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
const char * fc_git_revision(void)
Definition version.c:75
const char * word_version(void)
Definition version.c:62