Freeciv21
Develop your civilization from humble roots to a global empire
citydlg.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 #include "city.h"
13 #include "fc_types.h"
14 
15 // Qt
16 #include <QDialog>
17 #include <QElapsedTimer>
18 #include <QGroupBox>
19 #include <QItemDelegate>
20 #include <QLabel>
21 #include <QListWidget>
22 #include <QProgressBar>
23 #include <QTableWidget>
24 #include <QToolTip>
25 #include <QtMath>
26 #include <qobjectdefs.h>
27 // client
28 #include "dialogs.h"
29 
30 #define CITIZENS_PER_ROW 20
31 
32 class QAction;
33 class QCheckBox;
34 class QCloseEvent;
35 class QContextMenuEvent;
36 class QEvent;
37 class QFont;
38 class QGridLayout;
39 class QGroupBox;
40 class QHBoxLayout;
41 class QHideEvent;
42 class QItemSelection;
43 class QMenu;
44 class QMouseEvent;
45 class QPaintEvent;
46 class QPainter;
47 class QPushButton;
48 class QRadioButton;
49 class QRect;
50 class QResizeEvent;
51 class QShowEvent;
52 class QSplitter;
53 class QTableWidget;
54 class QTableWidgetItem;
55 class QTimerEvent;
56 class QVBoxLayout;
57 class QVariant;
58 class fc_tooltip;
59 class QPixmap;
60 
61 /****************************************************************************
62  A list widget that sets its size hint to the size of its contents.
63 ****************************************************************************/
64 class unit_list_widget : public QListWidget {
65  Q_OBJECT
66 
67 public:
68  explicit unit_list_widget(QWidget *parent = nullptr);
69  QSize viewportSizeHint() const override;
70 
72  void set_oneliner(bool oneliner) { m_oneliner = oneliner; }
73 
75  void set_show_upkeep(bool show) { m_show_upkeep = show; }
76 
77  void set_units(unit_list *units);
78  std::vector<unit *> selected_playable_units() const;
79 
80 protected:
81  void contextMenuEvent(QContextMenuEvent *event) override;
82 
83 private:
84  void activate();
85  QPixmap create_unit_image(const unit *punit);
86 
87 private:
88  bool m_oneliner = false;
89  bool m_show_upkeep = false;
90 };
91 
92 /****************************************************************************
93  Custom progressbar with animated progress and right click event
94 ****************************************************************************/
95 class progress_bar : public QProgressBar {
96  Q_OBJECT
97 signals:
98  void clicked();
99 
100 public:
101  progress_bar(QWidget *parent);
102  ~progress_bar() override;
103  void mousePressEvent(QMouseEvent *event) override
104  {
105  Q_UNUSED(event);
106  emit clicked();
107  }
108  void set_pixmap(struct universal *target);
109  void set_pixmap(int n);
110 
111 protected:
112  void paintEvent(QPaintEvent *event) override;
113  void resizeEvent(QResizeEvent *event) override;
114 
115 private:
116  void create_region();
117  QPixmap *pix;
118  QRegion reg;
119  QFont *sfont;
120 };
121 
122 /****************************************************************************
123  Item delegate for production popup
124 ****************************************************************************/
125 class city_production_delegate : public QItemDelegate {
126  Q_OBJECT
127 
128 public:
129  city_production_delegate(QPoint sh, QObject *parent, struct city *city);
130  ~city_production_delegate() override = default;
131  void paint(QPainter *painter, const QStyleOptionViewItem &option,
132  const QModelIndex &index) const override;
133  QSize sizeHint(const QStyleOptionViewItem &option,
134  const QModelIndex &index) const override;
135 
136 private:
138  QPoint pd;
139  struct city *pcity;
140 
141 protected:
142  void drawFocus(QPainter *painter, const QStyleOptionViewItem &option,
143  const QRect &rect) const override;
144 };
145 
146 /****************************************************************************
147  Single item in production popup
148 ****************************************************************************/
149 class production_item : public QObject {
150  Q_OBJECT
151 
152 public:
153  production_item(struct universal *ptarget, QObject *parent);
154  ~production_item() override;
155  inline int columnCount() const { return 1; }
156  QVariant data() const;
157 
158 private:
159  struct universal *target;
160 };
161 
162 /***************************************************************************
163  City production model
164 ***************************************************************************/
165 class city_production_model : public QAbstractListModel {
166  Q_OBJECT
167 
168 public:
169  city_production_model(struct city *pcity, bool f, bool su, bool sw,
170  bool sb, QObject *parent = 0);
171  ~city_production_model() override;
172  inline int
173  rowCount(const QModelIndex &index = QModelIndex()) const override
174  {
175  Q_UNUSED(index);
176  return (qCeil(static_cast<float>(city_target_list.size()) / 3));
177  }
178  int columnCount(const QModelIndex &parent = QModelIndex()) const override
179  {
180  Q_UNUSED(parent);
181  return 3;
182  }
183  QVariant data(const QModelIndex &index,
184  int role = Qt::DisplayRole) const override;
185  void populate();
186  QPoint sh;
187 
188 private:
189  QList<production_item *> city_target_list;
190  struct city *mcity;
191  bool future_t;
195 };
196 
197 /****************************************************************************
198  Class for popup avaialable production
199 ****************************************************************************/
200 class production_widget : public QTableView {
201  Q_OBJECT
202 
205 
206 public:
207  production_widget(QWidget *parent, struct city *pcity, bool future,
208  int when, int curr, bool show_units, bool buy = false,
209  bool show_wonders = true, bool show_buildings = true);
210  ~production_widget() override;
211 
212 public slots:
213  void prod_selected(const QItemSelection &sl, const QItemSelection &ds);
214 
215 protected:
216  void mousePressEvent(QMouseEvent *event) override;
217  bool eventFilter(QObject *obj, QEvent *ev) override;
218 
219 private:
220  struct city *pw_city;
223  bool buy_it;
225 };
226 
227 class cityIconInfoLabel : public QWidget {
228  Q_OBJECT
229 
230 public:
231  cityIconInfoLabel(QWidget *parent = 0);
232  void setCity(struct city *pcity);
233  void updateText();
234 
235 private:
236  void initLayout();
237  struct city *pcity{nullptr};
238  QLabel labs[12];
240 };
241 
242 /****************************************************************************
243  city_label is used only for showing citizens icons
244  and was created to catch mouse events
245 ****************************************************************************/
246 class city_label : public QLabel {
247  Q_OBJECT
248 
249 public:
250  city_label(QWidget *parent = 0);
251  void set_city(struct city *pcity);
252  void set_type(int);
253 
254 private:
255  struct city *pcity{nullptr};
256  int type;
257  QSize get_pixmap_size() const;
258 
259 protected:
260  void mousePressEvent(QMouseEvent *event) override;
261  QSize minimumSizeHint() const override;
262  QSize sizeHint() const override;
263 };
264 
265 class city_info : public QWidget {
266  Q_OBJECT
267 
268 public:
269  city_info(QWidget *parent = 0);
270  void update_labels(struct city *ci_city);
271 
272 private:
276 };
277 
278 #include "ui_citydlg.h"
279 /****************************************************************************
280  City dialog
281 ****************************************************************************/
282 class city_dialog : public QWidget {
283 
284  Q_OBJECT
286  Ui::FormCityDlg ui;
287  QPixmap *citizen_pixmap;
288  bool future_targets{false}, show_units{true}, show_wonders{true},
292 
293 public:
294  city_dialog(QWidget *parent = 0);
295 
296  ~city_dialog() override;
297  void setup_ui(struct city *qcity);
298  void refresh();
299  struct city *pcity = nullptr;
300  bool dont_focus{false};
301  bool present_units_exp = false;
302 
303 private:
304  void update_title();
305  void update_building();
306  void update_info_label();
307  void update_buy_button();
308  void fill_citizens_pixmap(QPixmap *pixmap, QPainter *painter,
309  const citizen_category *categories,
310  int num_citizens);
311  void update_citizens();
312  void update_improvements();
313  void update_units();
314  void update_nation_table();
315  void update_cma_tab();
316  void update_disabled();
317  void update_sliders();
318  void update_prod_buttons();
319  void change_production(bool next);
320 
321 private slots:
322  void next_city();
323  void prev_city();
324  void get_city(bool next);
325  void show_targets();
326  void show_targets_worklist();
327  void buy();
328  void dbl_click_p(QTableWidgetItem *item);
329  void item_selected(const QItemSelection &sl, const QItemSelection &ds);
330  void clear_worklist();
331  void save_worklist();
332  void worklist_up();
333  void worklist_down();
334  void worklist_del();
335  void display_worklist_menu(const QPoint);
336  void disband_state_changed(bool allow_disband);
337  void cma_remove();
338  void cma_enable();
339  void cma_changed();
340  void cma_check_agent(const cm_parameter &params);
341  void cma_selected(const QItemSelection &sl, const QItemSelection &ds);
342  void cma_double_clicked(int row, int column);
343  void cma_context_menu(const QPoint p);
344  void save_cma();
345  void city_rename();
346  void present_units_exp_col();
347 
348 protected:
349  void showEvent(QShowEvent *event) override;
350  void hideEvent(QHideEvent *event) override;
351  bool eventFilter(QObject *obj, QEvent *event) override;
352 };
353 
355 void city_font_update();
citizen_category
Definition: city.h:239
void city_font_update()
Updates city font.
Definition: citydlg.cpp:2284
void destroy_city_dialog()
cityIconInfoLabel(QWidget *parent=0)
Definition: citydlg.cpp:482
struct city * pcity
Definition: citydlg.h:237
void setCity(struct city *pcity)
Definition: citydlg.cpp:492
QLabel labs[12]
Definition: citydlg.h:238
void setup_ui(struct city *qcity)
Setups whole city dialog, public function.
Definition: citydlg.cpp:1733
void worklist_up()
Move current item on worklist up.
Definition: citydlg.cpp:2102
void update_nation_table()
Updates nationality table in happiness tab.
Definition: citydlg.cpp:1650
void next_city()
Changes city_dialog to next city after pushing next city button.
Definition: citydlg.cpp:1883
bool show_wonders
Definition: citydlg.h:288
void buy()
Buy button.
Definition: citydlg.cpp:1921
void cma_double_clicked(int row, int column)
Double click on some row ( column is unused )
Definition: citydlg.cpp:1202
void update_disabled()
Updates buttons/widgets which should be enabled/disabled.
Definition: citydlg.cpp:1015
Q_DISABLE_COPY(city_dialog)
bool present_units_exp
Definition: citydlg.h:301
void update_buy_button()
Enables/disables buy buttons depending on gold.
Definition: citydlg.cpp:1488
int selected_row_p
Definition: citydlg.h:290
void update_building()
Updates building improvement/unit.
Definition: citydlg.cpp:1893
bool future_targets
Definition: citydlg.h:288
void save_worklist()
Save worklist.
Definition: citydlg.cpp:2166
void worklist_del()
Remove current item on worklist.
Definition: citydlg.cpp:2126
void clear_worklist()
Clears worklist in production page.
Definition: citydlg.cpp:2087
Ui::FormCityDlg ui
Definition: citydlg.h:286
city_label * lab_table[6]
Definition: citydlg.h:291
void showEvent(QShowEvent *event) override
Show event.
Definition: citydlg.cpp:1088
bool dont_focus
Definition: citydlg.h:300
void update_info_label()
Updates information label ( food, prod ...
Definition: citydlg.cpp:1723
bool eventFilter(QObject *obj, QEvent *event) override
Event filter for catching keybaord events.
Definition: citydlg.cpp:1105
city_dialog(QWidget *parent=0)
Constructor for city_dialog, sets layouts, policies ...
Definition: citydlg.cpp:833
void disband_state_changed(bool allow_disband)
Received signal about changed qcheckbox - allow disbanding city.
Definition: citydlg.cpp:1341
void update_citizens()
Redraws citizens for city_label (citizens_label)
Definition: citydlg.cpp:1537
void cma_context_menu(const QPoint p)
Context menu on governor tab in city worklist.
Definition: citydlg.cpp:1361
void worklist_down()
Move current item on worklist down.
Definition: citydlg.cpp:2142
void update_title()
Puts city name and people count on title.
Definition: citydlg.cpp:2209
void present_units_exp_col()
Slot to expand or collapse the present units list.
Definition: citydlg.cpp:1812
void dbl_click_p(QTableWidgetItem *item)
Double clicked item in worklist table in production tab.
Definition: citydlg.cpp:1753
void cma_remove()
Removes selected CMA.
Definition: citydlg.cpp:1315
void prev_city()
Changes city_dialog to previous city after pushing prev city button.
Definition: citydlg.cpp:1888
void city_rename()
City rename dialog input.
Definition: citydlg.cpp:1128
void refresh()
Various refresh after getting new info/reply from server.
Definition: citydlg.cpp:1597
void update_cma_tab()
Updates cma tab.
Definition: citydlg.cpp:1256
void get_city(bool next)
Definition: citydlg.cpp:1850
void display_worklist_menu(const QPoint)
Context menu on production tab in city worklist.
Definition: citydlg.cpp:1380
void show_targets()
Shows customized table widget with available items to produce Shows default targets in overview city ...
Definition: citydlg.cpp:2060
void cma_check_agent(const cm_parameter &params)
Triggers a governor update if the parameters changed.
Definition: citydlg.cpp:2197
void fill_citizens_pixmap(QPixmap *pixmap, QPainter *painter, const citizen_category *categories, int num_citizens)
Fill a pixmap with citizen sprites.
Definition: citydlg.cpp:1513
void cma_enable()
Enables cma slot, triggered by clicked button or changed cma.
Definition: citydlg.cpp:1179
bool show_buildings
Definition: citydlg.h:289
void update_sliders()
Definition: citydlg.cpp:1634
void update_improvements()
Updates list of improvements.
Definition: citydlg.cpp:1961
bool show_units
Definition: citydlg.h:288
~city_dialog() override
City dialog destructor.
Definition: citydlg.cpp:1059
struct city * pcity
Definition: citydlg.h:299
void change_production(bool next)
Changes production to next one or previous.
Definition: citydlg.cpp:972
void cma_selected(const QItemSelection &sl, const QItemSelection &ds)
CMA has been selected from list.
Definition: citydlg.cpp:1221
void hideEvent(QHideEvent *event) override
Hide event.
Definition: citydlg.cpp:1068
void item_selected(const QItemSelection &sl, const QItemSelection &ds)
Selection changed in production tab, in worklist tab.
Definition: citydlg.cpp:1834
void show_targets_worklist()
Shows customized table widget with available items to produce Shows customized targets in city produc...
Definition: citydlg.cpp:2074
void cma_changed()
Sliders moved and cma has been changed.
Definition: citydlg.cpp:1193
void update_units()
Updates layouts for supported and present units in city.
Definition: citydlg.cpp:1770
void save_cma()
Save cma dialog input.
Definition: citydlg.cpp:1158
QPixmap * citizen_pixmap
Definition: citydlg.h:287
void update_prod_buttons()
Update sensitivity of buttons in production tab.
Definition: citydlg.cpp:1030
QLabel * m_corruption
Definition: citydlg.h:274
QLabel * m_food
Definition: citydlg.h:273
QLabel * m_plague
Definition: citydlg.h:275
QLabel * m_gold
Definition: citydlg.h:273
QLabel * m_luxury
Definition: citydlg.h:273
QLabel * m_production
Definition: citydlg.h:273
QLabel * m_waste
Definition: citydlg.h:274
QLabel * m_pollution
Definition: citydlg.h:275
QLabel * m_airlift
Definition: citydlg.h:275
QLabel * m_science
Definition: citydlg.h:274
QLabel * m_granary
Definition: citydlg.h:274
QLabel * m_culture
Definition: citydlg.h:274
QLabel * m_trade
Definition: citydlg.h:273
QLabel * m_stolen
Definition: citydlg.h:275
void update_labels(struct city *ci_city)
Definition: citydlg.cpp:697
QLabel * m_size
Definition: citydlg.h:273
QLabel * m_plague_label
Definition: citydlg.h:275
QLabel * m_growth
Definition: citydlg.h:274
city_info(QWidget *parent=0)
Definition: citydlg.cpp:639
QSize sizeHint() const override
Definition: citydlg.cpp:632
city_label(QWidget *parent=0)
city_label is used only for showing citizens icons and was created only to catch mouse events
Definition: citydlg.cpp:572
void set_city(struct city *pcity)
Just sets target city for city_label.
Definition: citydlg.cpp:637
void mousePressEvent(QMouseEvent *event) override
Mouse handler for city_label.
Definition: citydlg.cpp:581
QSize minimumSizeHint() const override
Definition: citydlg.cpp:630
struct city * pcity
Definition: citydlg.h:255
QSize get_pixmap_size() const
Definition: citydlg.cpp:620
int type
Definition: citydlg.h:256
void set_type(int)
Definition: citydlg.cpp:577
city_production_delegate(QPoint sh, QObject *parent, struct city *city)
City item delegate constructor.
Definition: citydlg.cpp:2347
~city_production_delegate() override=default
struct city * pcity
Definition: citydlg.h:139
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
City item delgate paint event.
Definition: citydlg.cpp:2359
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Size hint for city item delegate.
Definition: citydlg.cpp:2507
void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const override
Draws focus for given item.
Definition: citydlg.cpp:2490
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: citydlg.h:178
city_production_model(struct city *pcity, bool f, bool su, bool sw, bool sb, QObject *parent=0)
Constructor for city production model.
Definition: citydlg.cpp:2549
QList< production_item * > city_target_list
Definition: citydlg.h:189
int rowCount(const QModelIndex &index=QModelIndex()) const override
Definition: citydlg.h:173
void populate()
Fills model with data.
Definition: citydlg.cpp:2606
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns data from model.
Definition: citydlg.cpp:2574
struct city * mcity
Definition: citydlg.h:190
~city_production_model() override
Destructor for city production model.
Definition: citydlg.cpp:2565
struct universal * target
Definition: citydlg.h:159
~production_item() override
Production item destructor.
Definition: citydlg.cpp:2532
QVariant data() const
Returns stored data.
Definition: citydlg.cpp:2541
production_item(struct universal *ptarget, QObject *parent)
Production item constructor.
Definition: citydlg.cpp:2522
int columnCount() const
Definition: citydlg.h:155
bool eventFilter(QObject *obj, QEvent *ev) override
Event filter for production widget.
Definition: citydlg.cpp:2756
void prod_selected(const QItemSelection &sl, const QItemSelection &ds)
Changed selection in production widget.
Definition: citydlg.cpp:2789
~production_widget() override
Destructor for production widget.
Definition: citydlg.cpp:2864
void mousePressEvent(QMouseEvent *event) override
Mouse press event for production widget.
Definition: citydlg.cpp:2743
city_production_model * list_model
Definition: citydlg.h:203
struct city * pw_city
Definition: citydlg.h:220
fc_tooltip * fc_tt
Definition: citydlg.h:224
city_production_delegate * c_p_d
Definition: citydlg.h:204
production_widget(QWidget *parent, struct city *pcity, bool future, int when, int curr, bool show_units, bool buy=false, bool show_wonders=true, bool show_buildings=true)
Constructor for production widget future - show future targets show_units - if to show units when - w...
Definition: citydlg.cpp:2671
progress_bar(QWidget *parent)
Custom progressbar constructor.
Definition: citydlg.cpp:236
QRegion reg
Definition: citydlg.h:118
void set_pixmap(struct universal *target)
Sets pixmap from given universal for custom progressbar.
Definition: citydlg.cpp:264
~progress_bar() override
Custom progressbar destructor.
Definition: citydlg.cpp:246
void create_region()
Creates region with diagonal lines.
Definition: citydlg.cpp:446
void resizeEvent(QResizeEvent *event) override
Custom progressbar resize event.
Definition: citydlg.cpp:255
QPixmap * pix
Definition: citydlg.h:117
void paintEvent(QPaintEvent *event) override
Paint event for custom progress bar.
Definition: citydlg.cpp:317
QFont * sfont
Definition: citydlg.h:119
void clicked()
void mousePressEvent(QMouseEvent *event) override
Definition: citydlg.h:103
void set_show_upkeep(bool show)
Sets whether upkeep needs to be shown.
Definition: citydlg.h:75
void set_oneliner(bool oneliner)
Sets whether the list should try to use a single line.
Definition: citydlg.h:72
bool m_oneliner
Definition: citydlg.h:88
std::vector< unit * > selected_playable_units() const
Finds the list of currently selected units.
Definition: citydlg.cpp:128
void activate()
Activates unit and closes city dialog.
Definition: citydlg.cpp:166
void set_units(unit_list *units)
Sets the list of units to be displayed.
Definition: citydlg.cpp:101
void contextMenuEvent(QContextMenuEvent *event) override
Reimplemented to provide the unit context menu.
Definition: citydlg.cpp:148
QPixmap create_unit_image(const unit *punit)
Creates the image to represent the given unit in the list.
Definition: citydlg.cpp:184
unit_list_widget(QWidget *parent=nullptr)
Constructor.
Definition: citydlg.cpp:70
bool m_show_upkeep
Definition: citydlg.h:89
QSize viewportSizeHint() const override
Reimplemented virtual method.
Definition: citydlg.cpp:84
enum event_type event
Definition: events.cpp:68
Definition: city.h:291
Definition: climisc.h:66
The base class for options.
Definition: options.cpp:209
Definition: unit.h:134