Freeciv21
Develop your civilization from humble roots to a global empire
gui_main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2023 Freeciv21 and Freeciv contributors. This file is
3  part of Freeciv21. Freeciv21 is free software: you can redistribute it
4  and/or modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation, either version 3 of the
6  License, or (at your option) any later version. You should have received
7  a copy of the GNU General Public License along with Freeciv21. If not,
8  see https://www.gnu.org/licenses/.
9  */
10 
11 // Keep this first. In particular before any Qt include.
12 #ifdef AUDIO_SDL
13 #include <SDL2/SDL.h>
14 #endif // AUDIO_SDL
15 
16 #include "gui_main.h"
17 // Qt
18 #include <QApplication>
19 // utility
20 #include "log.h"
21 // client
22 #include "client_main.h"
23 #include "clinet.h"
24 #include "fc_client.h"
25 #include "fonts.h"
26 #include "helpdlg.h"
27 #include "hudwidget.h"
28 #include "mapview_g.h"
29 #include "messagewin.h"
30 #include "options.h"
31 #include "page_game.h"
32 #include "page_pregame.h"
33 #include "qtg_cxxside.h"
34 #include "themes_common.h"
35 #include "tileset/sprite.h"
36 #include "tileset/tilespec.h"
37 #include "unitselect.h"
38 #include "views/view_map.h"
39 #include "views/view_map_common.h"
40 #include "widgets/report_widget.h"
41 
43 
44 extern void city_font_update();
45 
46 const bool gui_use_transliteration = false;
47 const char *const gui_character_encoding = "UTF-8";
48 const char *client_string = "gui-qt";
50 
51 static void apply_help_font(struct option *poption);
52 static void apply_notify_font(struct option *poption);
53 static void apply_titlebar(struct option *poption);
54 
58 class fc_client *king() { return freeciv_qt; }
59 
63 int main(int argc, char **argv) { return client_main(argc, argv); }
64 
69 void ui_main()
70 {
71  // Load window icons
72  QIcon::setThemeSearchPaths(get_data_dirs() + QIcon::themeSearchPaths());
73  QIcon::setFallbackThemeName(QIcon::themeName());
74  QIcon::setThemeName(QStringLiteral("icons"));
75 
76  qApp->setWindowIcon(QIcon::fromTheme(QStringLiteral("freeciv21-client")));
77 
78  if (true) {
83  }
84  freeciv_qt = new fc_client();
85  freeciv_qt->fc_main(qApp);
86  }
87 }
88 
93 {
94  struct option *poption;
95 
96 #define option_var_set_callback(var, callback) \
97  if ((poption = optset_option_by_name(client_optset, #var))) { \
98  option_set_changed_callback(poption, callback); \
99  } else { \
100  qCritical("Didn't find option %s!", #var); \
101  }
102  option_var_set_callback(gui_qt_font_city_names, gui_qt_apply_font);
103  option_var_set_callback(gui_qt_font_city_productions, gui_qt_apply_font);
104  option_var_set_callback(gui_qt_font_reqtree_text, gui_qt_apply_font);
105  option_var_set_callback(gui_qt_font_default, gui_qt_apply_font);
106  option_var_set_callback(gui_qt_font_help_text, apply_help_font);
107  option_var_set_callback(gui_qt_font_chatline, gui_qt_apply_font);
108  option_var_set_callback(gui_qt_font_notify_label, apply_notify_font);
109  option_var_set_callback(gui_qt_show_titlebar, apply_titlebar);
110 #undef option_var_set_callback
111 }
112 
116 void ui_exit() { delete freeciv_qt; }
117 
122 {
123  if (get_current_client_page() == PAGE_NETWORK) {
124  real_set_client_page(PAGE_START);
125  }
126  qobject_cast<page_pregame *>(king()->pages[PAGE_START])
127  ->update_start_page();
128 }
129 
135 {
136  QApplication::beep();
137  QApplication::alert(king()->central_wdg);
138 }
139 
147 void add_net_input(QIODevice *sock) { king()->add_server_source(sock); }
148 
156 {
157  units_select *unit_sel = queen()->unit_selector;
158  if (unit_sel != nullptr && unit_sel->isVisible()) {
159  unit_sel->update_units();
160  }
161 }
162 
166 void apply_titlebar(struct option *poption)
167 {
168  bool val;
169  QWidget *w;
170  Qt::WindowFlags flags = Qt::Window;
171  val = option_bool_get(poption);
172 
173  if (king()->current_page() < PAGE_GAME) {
174  return;
175  }
176 
177  if (val) {
178  w = new QWidget();
179  king()->setWindowFlags(flags);
180  delete king()->corner_wid;
181  king()->corner_wid = nullptr;
182  king()->menu_bar->setCornerWidget(w);
183  } else {
184  flags |= Qt::CustomizeWindowHint;
185  king()->setWindowFlags(flags);
186  king()->corner_wid = new fc_corner(king());
187  king()->menu_bar->setCornerWidget(king()->corner_wid);
188  }
189  king()->show();
190 }
191 
195 void gui_qt_apply_font(struct option *poption)
196 {
197  if (king()) {
198  auto f = option_font_get(poption);
199  auto s = option_name(poption);
200  fcFont::instance()->setFont(s, f);
202  queen()->chat->update_font();
203  QApplication::setFont(fcFont::instance()->getFont(fonts::default_font));
205  }
206  apply_help_font(poption);
207 }
208 
212 static void apply_help_font(struct option *poption)
213 {
214  if (king()) {
216  option_font_get(poption));
218  }
219 }
220 
224 static void apply_notify_font(struct option *poption)
225 {
226  if (auto page_game = queen(); page_game) {
227  gui_update_font(QStringLiteral("notify_label"),
228  option_font_get(poption));
229 
230  auto list =
231  page_game->mapview_wdg->findChildren<freeciv::report_widget *>();
232  for (auto report : list) {
233  QApplication::postEvent(report, new QEvent(QEvent::FontChange));
234  }
235  }
236  if (king() && get_current_client_page() == PAGE_GAME) {
237  gui_update_font(QStringLiteral("city_label"), option_font_get(poption));
239  }
240 }
241 
246 
251 
255 void editgui_popup_properties(const struct tile_list *tiles, int objtype) {}
256 
261 
265 void editgui_notify_object_changed(int objtype, int object_id, bool removal)
266 {
267 }
268 
272 void editgui_notify_object_created(int tag, int id) {}
273 
277 void gui_update_font(const QString &font_name, const QFont &font)
278 {
279  auto fname = QStringLiteral("gui_qt_font_") + QString(font_name);
280  auto remove_old = fcFont::instance()->getFont(fname);
281  fcFont::instance()->setFont(fname, font);
282 }
283 
285 {
288 }
289 
294 {
295  hud_message_box *ask = new hud_message_box(king()->central_wdg);
296 
297  ask->set_text_title(_("Are you sure you want to quit?"), _("Quit?"));
298  ask->setStandardButtons(QMessageBox::No | QMessageBox::Yes);
299  ask->setDefaultButton(QMessageBox::No);
300  ask->button(QMessageBox::Yes)->setText(_("Yes Quit"));
301  ask->button(QMessageBox::No)->setText(_("Keep Playing"));
302  ask->setAttribute(Qt::WA_DeleteOnClose);
303  QObject::connect(ask, &hud_message_box::accepted, [=]() {
304  start_quitting();
305  if (client.conn.used) {
307  }
308  king()->write_settings();
309  qApp->quit();
310  });
311  ask->show();
312 }
void update_font()
Updates font for chat_widget.
Definition: chatline.cpp:522
void setSizeAll(int)
Increases/decreases all fonts sizes.
Definition: fonts.cpp:99
QFont getFont(const QString &name, double zoom=1.0) const
Returns desired font.
Definition: fonts.cpp:57
static fcFont * instance()
Returns instance of fc_font.
Definition: fonts.cpp:34
void setFont(const QString &name, const QFont &qf)
Adds new font or overwrite old one.
Definition: fonts.cpp:122
mr_menu * menu_bar
Definition: fc_client.h:127
fc_client()
Constructor.
Definition: fc_client.cpp:58
void write_settings()
Save qt-specific options.
Definition: fc_client.cpp:514
void add_server_source(QIODevice *socket)
Add notifier for server input.
Definition: fc_client.cpp:279
void fc_main(QApplication *)
This is not called simply 'fc_client::main()', since SDL includes ould sometimes cause 'main' to be c...
Definition: fc_client.cpp:144
fc_corner * corner_wid
Definition: fc_client.h:125
Widget used to display the demographics, top 5 cities, and travelers' reports.
Definition: report_widget.h:20
void set_text_title(const QString &s1, const QString &s2)
Sets text and title and shows message box.
Definition: hudwidget.cpp:117
chat_widget * chat
Definition: page_game.h:80
units_select * unit_selector
Definition: page_game.h:75
void update_units()
Updates unit list on tile.
Definition: unitselect.cpp:314
int client_main(int argc, char *argv[])
Entry point for common client code.
void start_quitting()
Mark client as one going to quit as soon as possible,.
struct civclient client
void disconnect_from_server()
Get rid of server connection.
Definition: clinet.cpp:219
enum client_pages get_current_client_page()
Returns current client page.
Definition: fc_client.cpp:589
void real_set_client_page(enum client_pages page)
Sets the "page" that the client should show.
Definition: fc_client.cpp:570
#define _(String)
Definition: fcintl.h:50
void gui_update_allfonts()
Definition: gui_main.cpp:284
const char * client_string
Definition: gui_main.cpp:48
void editgui_tileset_changed()
Stub for editor function.
Definition: gui_main.cpp:245
#define option_var_set_callback(var, callback)
void editgui_popup_properties(const struct tile_list *tiles, int objtype)
Stub for editor function.
Definition: gui_main.cpp:255
void editgui_notify_object_created(int tag, int id)
Stub for editor function.
Definition: gui_main.cpp:272
void gui_qt_apply_font(struct option *poption)
Change the given font.
Definition: gui_main.cpp:195
static void apply_notify_font(struct option *poption)
Applies help font.
Definition: gui_main.cpp:224
void popup_quit_dialog()
Open dialog to confirm that user wants to quit client.
Definition: gui_main.cpp:293
void options_extra_init()
Extra initializers for client options.
Definition: gui_main.cpp:92
int main(int argc, char **argv)
Entry point for whole freeciv client program.
Definition: gui_main.cpp:63
void real_focus_units_changed()
Called when the set of units in focus (get_units_in_focus()) changes.
Definition: gui_main.cpp:155
void city_font_update()
Updates city font.
Definition: citydlg.cpp:2284
void add_net_input(QIODevice *sock)
Wait for data on the given socket.
Definition: gui_main.cpp:147
void gui_update_font(const QString &font_name, const QFont &font)
Updates a gui font style.
Definition: gui_main.cpp:277
void sound_bell()
Make a bell noise (beep).
Definition: gui_main.cpp:134
void ui_exit()
Do any necessary UI-specific cleanup.
Definition: gui_main.cpp:116
void real_conn_list_dialog_update(void *unused)
Update the connected users list at pregame state.
Definition: gui_main.cpp:121
static fc_client * freeciv_qt
Definition: gui_main.cpp:49
static void apply_help_font(struct option *poption)
Applies help font.
Definition: gui_main.cpp:212
static void apply_titlebar(struct option *poption)
Shows/Hides titlebar.
Definition: gui_main.cpp:166
void real_science_report_dialog_update(void *)
Update the science report.
void editgui_notify_object_changed(int objtype, int object_id, bool removal)
Stub for editor function.
Definition: gui_main.cpp:265
class fc_client * king()
Return fc_client instance.
Definition: gui_main.cpp:58
void editgui_popdown_all()
Stub for editor function.
Definition: gui_main.cpp:260
const char *const gui_character_encoding
Definition: gui_main.cpp:47
void ui_main()
The main loop for the UI.
Definition: gui_main.cpp:69
const bool gui_use_transliteration
Definition: gui_main.cpp:46
void editgui_refresh()
Stub for editor function.
Definition: gui_main.cpp:250
void update_help_fonts()
Updates fonts.
Definition: helpdlg.cpp:87
const char *const default_font
Definition: fonts.h:18
const char * option_name(const struct option *poption)
Returns the name of the option.
Definition: options.cpp:300
client_options * gui_options
Definition: options.cpp:74
bool option_bool_get(const struct option *poption)
Returns the current value of this boolean option.
Definition: options.cpp:457
QFont option_font_get(const struct option *poption)
Returns the current value of this font option.
Definition: options.cpp:749
pageGame * queen()
Return game instandce.
Definition: page_game.cpp:557
void gui_clear_theme()
Clears a theme (sets default system theme)
Definition: themes.cpp:180
const QStringList & get_data_dirs()
Returns a list of data directory paths, in the order in which they should be searched.
Definition: shared.cpp:533
struct connection conn
Definition: client_main.h:89
char gui_qt_default_theme_name[512]
Definition: options.h:168
int gui_qt_increase_fonts
Definition: options.h:167
The base class for options.
Definition: options.cpp:209
bool load_theme(const QString &theme_name)
Loads a theme with the given name.
void tileset_load_tiles(struct tileset *t)
Load the tiles; requires tilespec_read_toplevel() called previously.
Definition: tilespec.cpp:2714
void tileset_init(struct tileset *t)
Initialize tileset structure.
Definition: tilespec.cpp:3630
void update_map_canvas_visible()
Schedules an update of (only) the visible part of the map at the next unqueue_mapview_update().