Freeciv-3.1
Loading...
Searching...
No Matches
gui_main.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#ifdef AUDIO_SDL
19/* Though it would happily compile without this include,
20 * it is needed for sound to work.
21 * It defines "main" macro to rename our main() so that
22 * it can install SDL's own. */
23#ifdef SDL2_PLAIN_INCLUDE
24#include <SDL.h>
25#else /* PLAIN_INCLUDE */
26#include <SDL2/SDL.h>
27#endif /* PLAIN_INCLUDE */
28#endif /* AUDIO_SDL */
29
30#include <stdio.h>
31
32// Qt
33#include <QApplication>
34#include <QLibraryInfo>
35#include <QMessageBox>
36#include <QScrollBar>
37#include <QStyleFactory>
38
39// utility
40#include "fc_cmdline.h"
41#include "fciconv.h"
42#include "log.h"
43
44// client
45#include "client_main.h"
46#include "editgui_g.h"
47#include "options.h"
48#include "sprite.h"
49#include "themes_common.h"
50#include "tilespec.h"
51
52// gui-qt
53#include "fc_client.h"
54#include "fonts.h"
55#include "gui_main.h"
56#include "helpdlg.h"
57#include "hudwidget.h"
58#include "qtg_cxxside.h"
59
60extern "C" {
62}
63extern void restart_notify_dialogs();
64extern void city_font_update();
65
66const bool gui_use_transliteration = false;
67const char * const gui_character_encoding = "UTF-8";
68const char *client_string = "gui-qt";
70static QApplication *qapp = nullptr;
71
72void reset_unit_table(void);
73static void apply_font(struct option *poption);
74static void apply_help_font(struct option *poption);
75static void apply_notify_font(struct option *poption);
76static void apply_sidebar(struct option *poption);
77static void apply_titlebar(struct option *poption);
78static void apply_fullscreen(struct option *poption);
79
80/**********************************************************************/
83class fc_client *gui()
84{
85 return freeciv_qt;
86}
87
88/**********************************************************************/
92{}
93
94/**********************************************************************/
97int main(int argc, char **argv)
98{
100
101 return client_main(argc, argv, TRUE);
102}
103
104/**********************************************************************/
108static void print_usage()
109{
110 /* add client-specific usage information here */
111 fc_fprintf(stderr,
112 _("This client accepts the standard Qt command-line options\n"
113 "after '--'. See the Qt documentation.\n\n"));
114
115 fc_fprintf(stderr,
116 _("Other gui-specific options are:\n"));
117
118 fc_fprintf(stderr,
119 _("-s, --shortcutreset\tReset shortcut keys to "
120 "default values\n"));
121
122 fc_fprintf(stderr, "\n");
123
124 /* TRANS: No full stop after the URL, could cause confusion. */
125 fc_fprintf(stderr, _("Report bugs at %s\n"), BUG_URL);
126}
127
128/**********************************************************************/
133static bool parse_options(int argc, char **argv)
134{
135 int i = 1;
136
137 while (i < argc) {
138 if (is_option("--help", argv[i])) {
139 print_usage();
140 return false;
141 }
142 if (is_option("--shortcutreset", argv[i])) {
144 }
145 // Can't check against unknown options, as those might be Qt options
146
147 i++;
148 }
149
150 return true;
151}
152
153/**********************************************************************/
157{
158 log_normal(_("Migrating Qt-client options from freeciv-2.5 options."));
159
161
163}
164
165/**********************************************************************/
169int qtg_ui_main(int argc, char *argv[])
170{
171 if (parse_options(argc, argv)) {
172 qapp = new QApplication(argc, argv);
173 QPixmap *qpm;
174 QIcon app_icon;
175 int tsret;
176
177 tsret = default_tileset_select();
178 if (tsret != EXIT_SUCCESS) {
179 return tsret;
180 }
181
185 app_icon = ::QIcon(*qpm);
186 qapp->setWindowIcon(app_icon);
188 /* We're using fresh defaults for this version of this client,
189 * so prevent any future migrations from other versions */
193 }
196 }
197 freeciv_qt = new fc_client();
198
202 }
203
204 // Initial fonts setup by forcing running change-callback for each
206 if (OT_FONT == option_type(poption)) {
207 option_changed(poption);
208 }
210
212 }
213
214 return EXIT_SUCCESS;
215}
216
217/**********************************************************************/
220QApplication *current_app()
221{
222 return qapp;
223}
224
225/**********************************************************************/
229{
230 struct option *poption;
231
232#define option_var_set_callback(var, callback) \
233 if ((poption = optset_option_by_name(client_optset, #var))) { \
234 option_set_changed_callback(poption, callback); \
235 } else { \
236 log_error("Didn't find option %s!", #var); \
237 }
238 option_var_set_callback(gui_qt_font_city_names,
239 apply_font);
240 option_var_set_callback(gui_qt_font_city_productions,
241 apply_font);
242 option_var_set_callback(gui_qt_font_reqtree_text,
243 apply_font);
244 option_var_set_callback(gui_qt_font_default,
245 apply_font);
246 option_var_set_callback(gui_qt_font_help_text,
248 option_var_set_callback(gui_qt_font_chatline,
249 apply_font);
250 option_var_set_callback(gui_qt_font_notify_label,
252 option_var_set_callback(gui_qt_show_titlebar,
254 option_var_set_callback(gui_qt_sidebar_left,
256 option_var_set_callback(gui_qt_fullscreen,
258#undef option_var_set_callback
259}
260
261/**********************************************************************/
265{
266 delete freeciv_qt;
267 delete qapp;
268 qapp = nullptr;
269}
270
271/**********************************************************************/
275{
276 if (qtg_get_current_client_page() == PAGE_NETWORK) {
277 qtg_real_set_client_page(PAGE_START);
278 }
280}
281
282/**********************************************************************/
287{
288 QApplication::beep();
289 QApplication::alert(gui()->central_wdg);
290}
291
292/**********************************************************************/
299void qtg_add_net_input(int sock)
300{
301 gui()->add_server_source(sock);
302}
303
304/**********************************************************************/
313
314/**********************************************************************/
325void qtg_set_unit_icon(int idx, struct unit *punit)
326{
327 /* PORTME */
328}
329
330/**********************************************************************/
338{
339 /* PORTME */
340}
341
342/**********************************************************************/
348{
349 if (gui()->unit_sel != nullptr && gui()->unit_sel->isVisible()) {
351 }
352
353 if (gui()->gtd != nullptr && gui()->gtd->isVisible()) {
354 gui()->gtd->update_dlg();
355 }
356}
357
358/**********************************************************************/
363void qtg_add_idle_callback(void (callback)(void *), void *data)
364{
365 call_me_back *cb = new call_me_back; /* removed in mr_idler:idling() */
366
367 cb->callback = callback;
368 cb->data = data;
370}
371
372/**********************************************************************/
375void apply_titlebar(struct option *poption)
376{
377 bool val;
378 QWidget *w;
379 Qt::WindowFlags flags = Qt::Window;
380 val = option_bool_get(poption);
381
382 if (gui()->current_page() < PAGE_GAME) {
383 return;
384 }
385
386 if (val) {
387 w = new QWidget();
388 gui()->setWindowFlags(flags);
389 delete gui()->corner_wid;
390 gui()->corner_wid = nullptr;
391 gui()->menu_bar->setCornerWidget(w);
392 } else {
393 flags |= Qt::CustomizeWindowHint;
394 gui()->setWindowFlags(flags);
395 gui()->corner_wid = new fc_corner(gui());
396 gui()->menu_bar->setCornerWidget(gui()->corner_wid);
397 }
398 gui()->show();
399}
400
401/**********************************************************************/
404void apply_sidebar(struct option *poption)
405{
407}
408
409/**********************************************************************/
412static void apply_font(struct option *poption)
413{
414 QFont *f;
415 QFont *remove_old;
416 QString s;
417
418 if (gui()) {
419 // FIXME: All this should be within gui()->update_fonts()
420 f = new QFont;
421 s = option_font_get(poption);
422 f->fromString(s);
423 s = option_name(poption);
424 remove_old = fc_font::instance()->get_font(s);
425 delete remove_old;
429 QApplication::setFont(*fc_font::instance()->get_font(fonts::default_font));
432 gui()->update_fonts();
433 }
434
435 apply_help_font(poption);
436}
437
438/**********************************************************************/
441static void apply_help_font(struct option *poption)
442{
443 QFont *f;
444 QFont *remove_old;
445 QString s;
446
447 if (gui()) {
448 f = new QFont;
449 s = option_font_get(poption);
450 f->fromString(s);
451 s = option_name(poption);
452 remove_old = fc_font::instance()->get_font(s);
453 delete remove_old;
456 }
457}
458
459/**********************************************************************/
462static void apply_notify_font(struct option *poption)
463{
464 if (gui()) {
465 qtg_gui_update_font("notify_label", option_font_get(poption));
467 }
468 if (gui() && qtg_get_current_client_page() == PAGE_GAME) {
469 qtg_gui_update_font("city_label", option_font_get(poption));
471 }
472}
473
474/**********************************************************************/
477static void apply_fullscreen(struct option *poption)
478{
480}
481
482/**********************************************************************/
487
488/**********************************************************************/
493
494/**********************************************************************/
497void qtg_editgui_popup_properties(const struct tile_list *tiles, int objtype)
498{}
499
500/**********************************************************************/
505
506/**********************************************************************/
510 bool removal)
511{}
512
513/**********************************************************************/
517{}
518
519/**********************************************************************/
522void qtg_gui_update_font(const char *font_name, const char *font_value)
523{
524 QFont *f;
525 QFont *remove_old;
526 QString fname;
527
528 fname = "gui_qt_font_" + QString(font_name);
529 f = new QFont;
530 f->fromString(font_value);
531 remove_old = fc_font::instance()->get_font(fname);
532 delete remove_old;
533 fc_font::instance()->set_font(fname, f);
535}
536
537/**********************************************************************/
540enum gui_type qtg_get_gui_type()
541{
542 return GUI_QT;
543}
544
545/**********************************************************************/
549{
550 /* FIXME */
551}
552
553/**********************************************************************/
557{
558 hud_message_box *ask = new hud_message_box(gui()->central_wdg);
559
560 ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
561 ask->setDefaultButton(QMessageBox::Cancel);
562 ask->set_text_title(_("Are you sure you want to quit?"), _("Quit?"));
563 ask->setAttribute(Qt::WA_DeleteOnClose);
564 QObject::connect(ask, &hud_message_box::accepted, [=]() {
566 if (client.conn.used) {
568 }
569 gui()->write_settings();
570 qapp->quit();
571 });
572 ask->show();
573}
574
575/**********************************************************************/
578void qtg_insert_client_build_info(char *outbuf, size_t outlen)
579{
580 /* There's also an separate entry about Qt in help menu. */
581
582 QByteArray ver = QLibraryInfo::version().toString().toUtf8();
583
584 cat_snprintf(outbuf, outlen, _("\nBuilt against Qt %s, using %s"),
585 QT_VERSION_STR, ver.data());
586
587#ifndef FC_QT5_MODE
588 cat_snprintf(outbuf, outlen, _("\nBuilt in Qt6 mode."));
589#else /* FC_QT5_MODE */
590 cat_snprintf(outbuf, outlen, _("\nBuilt in Qt5 mode."));
591#endif /* FC_QT5_MODE */
592}
static QFont * get_font(enum client_font font)
Definition canvas.cpp:359
void update_font()
Definition chatline.cpp:352
void apply_fullscreen()
goto_dialog * gtd
Definition fc_client.h:265
mr_menu * menu_bar
Definition fc_client.h:257
mr_idle mr_idler
Definition fc_client.h:255
units_select * unit_sel
Definition fc_client.h:263
void write_settings()
info_tab * infotab
Definition fc_client.h:261
void add_server_source(int)
void update_sidebar_position()
Definition pages.cpp:2307
void update_start_page()
Definition pages.cpp:1674
void update_fonts()
void fc_main(QApplication *qapp)
fc_corner * corner_wid
Definition fc_client.h:258
void remove_server_source()
static fc_font * instance()
Definition fonts.cpp:41
void set_font(QString name, QFont *qf)
Definition fonts.cpp:125
QFont * get_font(QString name)
Definition fonts.cpp:63
void get_mapfont_size()
Definition fonts.cpp:116
void update_dlg()
Definition gotodlg.cpp:236
int set_text_title(QString s1, QString s2, bool return_exec=false)
chatwdg * chtwdg
Definition messagewin.h:79
void add_callback(call_me_back *cb)
Definition mapview.cpp:188
void update_units()
Definition dialogs.cpp:4655
int client_main(int argc, char *argv[], bool postpone_tileset)
struct civclient client
void start_quitting(void)
int default_tileset_select(void)
void disconnect_from_server(void)
Definition clinet.c:305
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
int objtype
Definition editgui_g.h:28
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define _(String)
Definition fcintl.h:67
void configure_fonts()
Definition fonts.cpp:156
void update_city_descriptions(void)
Definition mapview.c:470
static enum client_pages current_page
Definition pages.c:78
static bool load_theme
Definition theme_dlg.c:30
const char * client_string
Definition gui_main.cpp:68
#define option_var_set_callback(var, callback)
void qtg_ui_init()
Definition gui_main.cpp:91
void qtg_editgui_popup_properties(const struct tile_list *tiles, int objtype)
Definition gui_main.cpp:497
void qtg_remove_net_input()
Definition gui_main.cpp:309
static void apply_notify_font(struct option *poption)
Definition gui_main.cpp:462
void popup_quit_dialog()
Definition gui_main.cpp:556
int main(int argc, char **argv)
Definition gui_main.cpp:97
void qtg_options_extra_init()
Definition gui_main.cpp:228
static QApplication * qapp
Definition gui_main.cpp:70
static void print_usage()
Definition gui_main.cpp:108
void qtg_editgui_refresh()
Definition gui_main.cpp:491
class fc_client * gui()
Definition gui_main.cpp:83
void city_font_update()
Definition citydlg.cpp:3783
void reset_unit_table(void)
Definition gui_main.cpp:548
static void apply_fullscreen(struct option *poption)
Definition gui_main.cpp:477
int qtg_ui_main(int argc, char *argv[])
Definition gui_main.cpp:169
void qtg_editgui_popdown_all()
Definition gui_main.cpp:503
QApplication * current_app()
Definition gui_main.cpp:220
void qtg_real_conn_list_dialog_update(void *unused)
Definition gui_main.cpp:274
static fc_client * freeciv_qt
Definition gui_main.cpp:69
static void apply_help_font(struct option *poption)
Definition gui_main.cpp:441
void qtg_editgui_tileset_changed()
Definition gui_main.cpp:485
void qtg_editgui_notify_object_created(int tag, int id)
Definition gui_main.cpp:516
void qtg_add_net_input(int sock)
Definition gui_main.cpp:299
static void apply_titlebar(struct option *poption)
Definition gui_main.cpp:375
static void apply_sidebar(struct option *poption)
Definition gui_main.cpp:404
void real_science_report_dialog_update(void *)
Definition repodlgs.c:751
void qtg_editgui_notify_object_changed(int objtype, int object_id, bool removal)
Definition gui_main.cpp:509
void qtg_gui_update_font(const char *font_name, const char *font_value)
Definition gui_main.cpp:522
void qtg_set_unit_icon(int idx, struct unit *punit)
Definition gui_main.cpp:325
void qtg_ui_exit()
Definition gui_main.cpp:264
void qtg_real_focus_units_changed(void)
Definition gui_main.cpp:347
void qtg_insert_client_build_info(char *outbuf, size_t outlen)
Definition gui_main.cpp:578
void qtg_set_unit_icons_more_arrow(bool onoff)
Definition gui_main.cpp:337
const char *const gui_character_encoding
Definition gui_main.cpp:67
void qtg_add_idle_callback(void(callback)(void *), void *data)
Definition gui_main.cpp:363
enum gui_type qtg_get_gui_type()
Definition gui_main.cpp:540
static void migrate_options_from_2_5()
Definition gui_main.cpp:156
static bool parse_options(int argc, char **argv)
Definition gui_main.cpp:133
void restart_notify_dialogs()
Definition dialogs.cpp:3942
const bool gui_use_transliteration
Definition gui_main.cpp:66
static void apply_font(struct option *poption)
Definition gui_main.cpp:412
void qtg_sound_bell()
Definition gui_main.cpp:286
const char * font_name
Definition gui_main_g.h:43
void update_help_fonts()
Definition helpdlg.cpp:113
#define log_normal(message,...)
Definition log.h:107
const char *const default_font
Definition fonts.h:27
void option_changed(struct option *poption)
Definition options.c:723
const struct option_set * client_optset
Definition options.c:1255
const char * option_name(const struct option *poption)
Definition options.c:603
bool option_bool_get(const struct option *poption)
Definition options.c:772
enum option_type option_type(const struct option *poption)
Definition options.c:633
struct client_options gui_options
Definition options.c:71
const char * option_font_get(const struct option *poption)
Definition options.c:1116
#define options_iterate(poptset, poption)
Definition options.h:527
#define options_iterate_end
Definition options.h:532
void qtg_real_set_client_page(enum client_pages page)
Definition pages.cpp:102
enum client_pages qtg_get_current_client_page()
Definition pages.cpp:120
void setup_gui_funcs()
void qtg_gui_clear_theme()
Definition themes.cpp:104
void shortcutreset()
void(* callback)(void *data)
Definition mapview.h:54
void * data
Definition mapview.h:55
struct connection conn
Definition client_main.h:96
bool gui_qt_fullscreen
Definition options.h:386
char gui_qt_default_theme_name[512]
Definition options.h:390
bool first_boot
Definition options.h:124
bool gui_qt_migrated_from_2_5
Definition options.h:135
bool migrate_fullscreen
Definition options.h:138
bool gui_qt_default_fonts_set
Definition options.h:136
QPixmap * pm
Definition sprite.h:25
Definition unit.h:138
int cat_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:995
#define TRUE
Definition support.h:46
void tileset_load_tiles(struct tileset *t)
Definition tilespec.c:3448
struct sprite * get_icon_sprite(const struct tileset *t, enum icon_type icon)
Definition tilespec.c:6674
void tileset_init(struct tileset *t)
Definition tilespec.c:6823
@ ICON_FREECIV
Definition tilespec.h:310