Freeciv21
Develop your civilization from humble roots to a global empire
hudwidget.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2020 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 #pragma once
11 
12 // Qt
13 #include <QDialog>
14 #include <QElapsedTimer>
15 #include <QLabel>
16 #include <QLineEdit>
17 #include <QMessageBox>
18 #include <QRubberBand>
19 #include <QTableWidget>
20 // utility
21 #include "fc_types.h"
22 #include "shortcuts.h"
23 
24 class QComboBox;
25 class QFontMetrics;
26 class QHBoxLayout;
27 class QIcon;
28 class QItemSelection;
29 class QKeyEvent;
30 class QMouseEvent;
31 class QMoveEvent;
32 class QObject;
33 class QPaintEvent;
34 class QPushButton;
35 class QRadioButton;
36 class QTimerEvent;
37 class QVBoxLayout;
38 class move_widget;
39 class scale_widget;
40 class close_widget;
41 
42 struct tile;
43 struct unit;
44 struct unit_list;
45 
46 void show_new_turn_info();
48 
49 /****************************************************************************
50  Custom message box with animated background
51 ****************************************************************************/
52 class hud_message_box : public QMessageBox {
53  Q_OBJECT
54  QElapsedTimer m_timer;
55 
56 public:
57  hud_message_box(QWidget *parent);
58  ~hud_message_box() override;
59  void set_text_title(const QString &s1, const QString &s2);
60 
61 protected:
62  void paintEvent(QPaintEvent *event) override;
63  void timerEvent(QTimerEvent *event) override;
64  void keyPressEvent(QKeyEvent *event) override;
65 
66 private:
68  QString text;
69  QString title;
70  QFontMetrics *fm_text;
71  QFontMetrics *fm_title;
72  QFont f_text;
73  QString cs1, cs2;
74  QFont f_title;
75  int top;
76  int mult;
77 };
78 
79 /****************************************************************************
80  Class for showing text on screen
81 ****************************************************************************/
82 class hud_text : public QWidget {
83  Q_OBJECT
84 
85 public:
86  hud_text(const QString &s, int time_secs, QWidget *parent);
87  ~hud_text() override;
88  void show_me();
89 
90 protected:
91  void paintEvent(QPaintEvent *event) override;
92  void timerEvent(QTimerEvent *event) override;
93 
94 private:
95  void center_me();
96  QRect bound_rect;
97  int timeout;
99  QString text;
100  QElapsedTimer m_timer;
101  QFontMetrics *fm_text;
102  QFont f_text;
103  QFont f_title;
104 };
105 
106 /****************************************************************************
107  Custom input box with animated background
108 ****************************************************************************/
109 class hud_input_box : public QDialog {
110  Q_OBJECT
111  QElapsedTimer m_timer;
112 
113 public:
114  hud_input_box(QWidget *parent);
115  ~hud_input_box() override;
116  void set_text_title_definput(const QString &s1, const QString &s2,
117  const QString &def_input);
118  QLineEdit input_edit;
119 
120 protected:
121  void paintEvent(QPaintEvent *event) override;
122  void timerEvent(QTimerEvent *event) override;
123 
124 private:
126  QString text;
127  QString title;
128  QFontMetrics *fm_text;
129  QFontMetrics *fm_title;
130  QFont f_text;
131  QString cs1, cs2;
132  QFont f_title;
133  int top;
134  int mult;
135 };
136 
137 /****************************************************************************
138  Custom label to center on current unit
139 ****************************************************************************/
140 class click_label : public QLabel {
141  Q_OBJECT
142 
143 public:
144  click_label();
145 signals:
146  void left_clicked();
147 private slots:
148  void mouse_clicked();
149 
150 protected:
151  void mousePressEvent(QMouseEvent *e) override;
152 };
153 
154 /****************************************************************************
155  Single action on unit actions
156 ****************************************************************************/
157 class hud_action : public QWidget {
158  Q_OBJECT
159  const QIcon icon;
160  bool focus;
161 
162 public:
163  hud_action(QWidget *parent, const QIcon &icon, shortcut_id shortcut);
164  ~hud_action() override;
166 signals:
167  void left_clicked();
169 
170 protected:
171  void paintEvent(QPaintEvent *event) override;
172  void mousePressEvent(QMouseEvent *e) override;
173  void mouseMoveEvent(QMouseEvent *event) override;
174  void leaveEvent(QEvent *event) override;
175  void enterEvent(QEvent *event) override;
176 private slots:
177  void mouse_clicked();
178  void mouse_right_clicked();
179 };
180 
181 /****************************************************************************
182  List of unit actions
183 ****************************************************************************/
184 class unit_actions : public QWidget {
185  Q_OBJECT
186 
187 public:
188  unit_actions(QWidget *parent, unit *punit);
189  ~unit_actions() override;
190  void init_layout();
191  int update_actions();
192  void clear_layout();
193  QHBoxLayout *layout;
194  QList<hud_action *> actions;
195 
196 private:
198 };
199 
200 /****************************************************************************
201  Widget showing current unit, tile and possible actions
202 ****************************************************************************/
203 class hud_units : public QFrame {
204  Q_OBJECT
207  QLabel text_label;
208  QFont *ufont;
209  QHBoxLayout *main_layout;
211 
212 public:
213  hud_units(QWidget *parent);
214  ~hud_units() override;
215  void update_actions();
216 
217 protected:
218  void moveEvent(QMoveEvent *event) override;
219 
220 private:
222  unit_list *ul_units;
224 };
225 
226 /****************************************************************************
227  Widget allowing load units on transport
228 ****************************************************************************/
229 class hud_unit_loader : public QTableWidget {
230  QList<unit *> transports;
231  Q_OBJECT
232 public:
233  hud_unit_loader(struct unit *pcargo, struct tile *ptile);
234  ~hud_unit_loader() override;
235  void show_me();
236 protected slots:
237  void selection_changed(const QItemSelection &, const QItemSelection &);
238 
239 private:
240  struct unit *cargo;
241  struct tile *qtile;
242 };
243 
244 /****************************************************************************
245  Widget showing one combat result
246 ****************************************************************************/
247 class hud_unit_combat : public QWidget {
248  Q_OBJECT
249 
250 public:
251  hud_unit_combat(int attacker_unit_id, int defender_unit_id,
252  int attacker_hp, int defender_hp, bool make_att_veteran,
253  bool make_def_veteran, float scale, QWidget *parent);
254  ~hud_unit_combat() override;
255  bool get_focus();
256  void set_fading(float fade);
257  void set_scale(float scale);
258 
259 protected:
260  void paintEvent(QPaintEvent *event) override;
261  void mousePressEvent(QMouseEvent *e) override;
262  void leaveEvent(QEvent *event) override;
263  void enterEvent(QEvent *event) override;
264 
265 private:
266  void init_images(bool redraw = false);
267  int att_hp = 0;
268  int def_hp = 0;
269  int att_hp_loss = 0;
270  int def_hp_loss = 0;
271  bool att_veteran = false;
272  bool def_veteran = false;
273  struct unit *attacker = nullptr;
274  struct unit *defender = nullptr;
275  const struct unit_type *type_attacker = nullptr;
276  const struct unit_type *type_defender = nullptr;
277  struct tile *center_tile = nullptr;
278  bool focus = false;
279  float fading = 0.0f;
280  float hud_scale;
281  QImage dimg, aimg;
282  bool att_win = false;
283  bool def_win = false;
284 };
285 
286 /****************************************************************************
287  Widget showing combat log
288 ****************************************************************************/
289 class hud_battle_log : public QFrame {
290  Q_OBJECT
291  QVBoxLayout *main_layout;
292  QList<hud_unit_combat *> lhuc;
293 
294 public:
295  hud_battle_log(QWidget *parent);
296  ~hud_battle_log() override;
297  void add_combat_info(hud_unit_combat *huc);
298  void set_scale(float s);
299  float scale;
300 
301 protected:
302  void paintEvent(QPaintEvent *event) override;
303  void moveEvent(QMoveEvent *event) override;
304  void timerEvent(QTimerEvent *event) override;
305  void showEvent(QShowEvent *event) override;
306 
307 private:
308  void update_size();
312  QElapsedTimer m_timer;
313 };
void mousePressEvent(QMouseEvent *e) override
Mouse event for click_label.
Definition: hudwidget.cpp:785
void left_clicked()
void mouse_clicked()
Centers on current unit.
Definition: hudwidget.cpp:795
click_label()
Custom label with extra mouse events.
Definition: hudwidget.cpp:776
const QIcon icon
Definition: hudwidget.h:159
~hud_action() override
Hud action destructor.
Definition: hudwidget.cpp:833
void leaveEvent(QEvent *event) override
Leave event for hud_action, used to get status of pixmap higlight.
Definition: hudwidget.cpp:859
hud_action(QWidget *parent, const QIcon &icon, shortcut_id shortcut)
Hud action constructor, used to show one action.
Definition: hudwidget.cpp:804
void mousePressEvent(QMouseEvent *e) override
Mouse press event for hud_action.
Definition: hudwidget.cpp:838
void paintEvent(QPaintEvent *event) override
Custom painting for hud_action.
Definition: hudwidget.cpp:817
void enterEvent(QEvent *event) override
Enter event for hud_action, used to get status of pixmap higlight.
Definition: hudwidget.cpp:869
void right_clicked()
bool focus
Definition: hudwidget.h:160
void mouseMoveEvent(QMouseEvent *event) override
Mouse move event for hud_action, draw focus.
Definition: hudwidget.cpp:850
shortcut_id action_shortcut
Definition: hudwidget.h:165
void mouse_right_clicked()
Right click event for hud_action.
Definition: hudwidget.cpp:879
void left_clicked()
void mouse_clicked()
Left click event for hud_action.
Definition: hudwidget.cpp:884
void update_size()
Updates size when scale has changed.
Definition: hudwidget.cpp:1613
void moveEvent(QMoveEvent *event) override
Move event, saves current position.
Definition: hudwidget.cpp:1689
move_widget * mw
Definition: hudwidget.h:311
void timerEvent(QTimerEvent *event) override
Timer event.
Definition: hudwidget.cpp:1704
QElapsedTimer m_timer
Definition: hudwidget.h:312
void add_combat_info(hud_unit_combat *huc)
Adds combat information to battle log.
Definition: hudwidget.cpp:1646
~hud_battle_log() override
Hud battle log destructor.
Definition: hudwidget.cpp:1603
hud_battle_log(QWidget *parent)
Hud battle log contructor.
Definition: hudwidget.cpp:1586
QVBoxLayout * main_layout
Definition: hudwidget.h:291
void paintEvent(QPaintEvent *event) override
Paint event for hud battle log.
Definition: hudwidget.cpp:1675
close_widget * clw
Definition: hudwidget.h:310
scale_widget * sw
Definition: hudwidget.h:309
QList< hud_unit_combat * > lhuc
Definition: hudwidget.h:292
void set_scale(float s)
Set scale.
Definition: hudwidget.cpp:1637
void showEvent(QShowEvent *event) override
Show event, restart fading timer.
Definition: hudwidget.cpp:1726
QString cs1
Definition: hudwidget.h:131
int m_animate_step
Definition: hudwidget.h:125
QFont f_title
Definition: hudwidget.h:132
QElapsedTimer m_timer
Definition: hudwidget.h:111
QString cs2
Definition: hudwidget.h:131
void set_text_title_definput(const QString &s1, const QString &s2, const QString &def_input)
Sets text, title and default text and shows input box.
Definition: hudwidget.cpp:372
QFontMetrics * fm_text
Definition: hudwidget.h:128
QFont f_text
Definition: hudwidget.h:130
hud_input_box(QWidget *parent)
Custom input box constructor.
Definition: hudwidget.cpp:331
void paintEvent(QPaintEvent *event) override
Paint event for custom input box.
Definition: hudwidget.cpp:441
void timerEvent(QTimerEvent *event) override
Timer event used to animate input box.
Definition: hudwidget.cpp:432
QLineEdit input_edit
Definition: hudwidget.h:118
QFontMetrics * fm_title
Definition: hudwidget.h:129
QString text
Definition: hudwidget.h:126
~hud_input_box() override
Custom input box destructor.
Definition: hudwidget.cpp:363
QString title
Definition: hudwidget.h:127
QFontMetrics * fm_text
Definition: hudwidget.h:70
QString title
Definition: hudwidget.h:69
QString text
Definition: hudwidget.h:68
QElapsedTimer m_timer
Definition: hudwidget.h:54
QString cs1
Definition: hudwidget.h:73
void set_text_title(const QString &s1, const QString &s2)
Sets text and title and shows message box.
Definition: hudwidget.cpp:117
hud_message_box(QWidget *parent)
Custom message box constructor.
Definition: hudwidget.cpp:67
int m_animate_step
Definition: hudwidget.h:67
void timerEvent(QTimerEvent *event) override
Timer event used to animate message box.
Definition: hudwidget.cpp:157
void keyPressEvent(QKeyEvent *event) override
Key press event for hud message box.
Definition: hudwidget.cpp:104
QFontMetrics * fm_title
Definition: hudwidget.h:71
~hud_message_box() override
Custom message box destructor.
Definition: hudwidget.cpp:95
QString cs2
Definition: hudwidget.h:73
void paintEvent(QPaintEvent *event) override
Paint event for custom message box.
Definition: hudwidget.cpp:166
QElapsedTimer m_timer
Definition: hudwidget.h:100
QString text
Definition: hudwidget.h:99
void paintEvent(QPaintEvent *event) override
Paint event for custom hud_text.
Definition: hudwidget.cpp:296
QFont f_text
Definition: hudwidget.h:102
int timeout
Definition: hudwidget.h:97
void timerEvent(QTimerEvent *event) override
Timer event, closes widget after timeout.
Definition: hudwidget.cpp:283
void center_me()
Moves to top center parent widget and sets size new size.
Definition: hudwidget.cpp:261
QFont f_title
Definition: hudwidget.h:103
~hud_text() override
Destructor for hud text.
Definition: hudwidget.cpp:278
QRect bound_rect
Definition: hudwidget.h:96
void show_me()
Shows hud text.
Definition: hudwidget.cpp:252
QFontMetrics * fm_text
Definition: hudwidget.h:101
hud_text(const QString &s, int time_secs, QWidget *parent)
Hud text constructor takes text to display and time.
Definition: hudwidget.cpp:221
int m_animate_step
Definition: hudwidget.h:98
void set_fading(float fade)
Sets widget fading.
Definition: hudwidget.cpp:1471
void mousePressEvent(QMouseEvent *e) override
Mouse press event, centers on highlighted combat.
Definition: hudwidget.cpp:1560
hud_unit_combat(int attacker_unit_id, int defender_unit_id, int attacker_hp, int defender_hp, bool make_att_veteran, bool make_def_veteran, float scale, QWidget *parent)
Hud unit combat contructor, prepares images to show as result.
Definition: hudwidget.cpp:1358
struct unit * defender
Definition: hudwidget.h:274
void paintEvent(QPaintEvent *event) override
Paint event for hud_unit combat.
Definition: hudwidget.cpp:1485
void init_images(bool redraw=false)
Draws images of units to pixmaps for later use.
Definition: hudwidget.cpp:1393
struct unit * attacker
Definition: hudwidget.h:273
bool get_focus()
Returns true if widget has focus (used to prevent hiding parent)
Definition: hudwidget.cpp:1480
const struct unit_type * type_attacker
Definition: hudwidget.h:275
void set_scale(float scale)
Sets scale for images.
Definition: hudwidget.cpp:1457
struct tile * center_tile
Definition: hudwidget.h:277
void enterEvent(QEvent *event) override
Leave event for hud unit combat.
Definition: hudwidget.cpp:1577
~hud_unit_combat() override
Hud unit combat destructor.
const struct unit_type * type_defender
Definition: hudwidget.h:276
void leaveEvent(QEvent *event) override
Leave event for hud unit combat.
Definition: hudwidget.cpp:1568
hud_unit_loader(struct unit *pcargo, struct tile *ptile)
Constructor for widget allowing loading units on transports.
Definition: hudwidget.cpp:1155
~hud_unit_loader() override
Destructor for units loader.
struct tile * qtile
Definition: hudwidget.h:241
struct unit * cargo
Definition: hudwidget.h:240
QList< unit * > transports
Definition: hudwidget.h:230
void selection_changed(const QItemSelection &, const QItemSelection &)
Selects given tranport and closes widget.
Definition: hudwidget.cpp:1247
void show_me()
Shows unit loader, adds possible tranportsand units to table Calculates table size.
Definition: hudwidget.cpp:1182
QLabel text_label
Definition: hudwidget.h:207
hud_units(QWidget *parent)
Constructor for hud_units (holds layout for whole uunits info)
Definition: hudwidget.cpp:500
click_label unit_label
Definition: hudwidget.h:205
~hud_units() override
Hud_units destructor.
QFont * ufont
Definition: hudwidget.h:208
click_label tile_label
Definition: hudwidget.h:206
move_widget * mw
Definition: hudwidget.h:221
unit_list * ul_units
Definition: hudwidget.h:222
tile * current_tile
Definition: hudwidget.h:223
void update_actions()
Update possible action for given units.
Definition: hudwidget.cpp:552
QHBoxLayout * main_layout
Definition: hudwidget.h:209
unit_actions * unit_icons
Definition: hudwidget.h:210
void moveEvent(QMoveEvent *event) override
Move Event for hud_units, used to save position.
Definition: hudwidget.cpp:541
unit_actions(QWidget *parent, unit *punit)
Units action contructor, holds possible hud_actions.
Definition: hudwidget.cpp:893
void clear_layout()
Cleans layout - run it before layout initialization.
Definition: hudwidget.cpp:1134
QList< hud_action * > actions
Definition: hudwidget.h:194
void init_layout()
Initiazlizes layout ( layout needs to be changed after adding units )
Definition: hudwidget.cpp:915
unit * current_unit
Definition: hudwidget.h:197
~unit_actions() override
Destructor for unit_actions.
Definition: hudwidget.cpp:906
int update_actions()
Updates avaialable actions, returns actions count.
Definition: hudwidget.cpp:927
QHBoxLayout * layout
Definition: hudwidget.h:193
enum event_type event
Definition: events.cpp:68
int Unit_type_id
Definition: fc_types.h:299
bool has_player_unit_type(Unit_type_id utype)
Returns true if player has any unit of unit_type.
Definition: hudwidget.cpp:51
void show_new_turn_info()
Shows new turn information with big font.
Definition: hudwidget.cpp:1305
shortcut_id
Definition: shortcuts.h:33
Definition: tile.h:42
Definition: unit.h:134