Freeciv21
Develop your civilization from humble roots to a global empire
view_cities.h
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 #pragma once
11 
12 // Qt
13 #include <QAbstractListModel>
14 #include <QItemDelegate>
15 #include <QMenu>
16 #include <QSortFilterProxyModel>
17 #include <QTreeView>
18 #include <QWidget>
19 // client
20 #include "climisc.h"
21 #include "views/view_cities_data.h"
22 
23 #define CMA_NONE (10000)
24 
25 class QHBoxLayout;
26 class QItemSelection;
27 class QLabel;
28 class QMenu;
29 class QMenu;
30 class QPainter;
31 class QPoint;
32 class QPushButton;
33 class QSortFilterProxyModel;
34 class QTableWidget;
35 class QVBoxLayout;
36 class QVBoxLayout;
37 class city_report;
38 class city_report;
39 struct city;
40 template <class Key, class T> class QMap;
41 
42 class city_sort_model : public QSortFilterProxyModel {
43  bool lessThan(const QModelIndex &left,
44  const QModelIndex &right) const override;
45 };
46 
47 /***************************************************************************
48  Item delegate for painting in model of city table
49 ***************************************************************************/
50 class city_item_delegate : public QItemDelegate {
51  Q_OBJECT
52 
53 public:
54  city_item_delegate(QObject *parent);
55  ~city_item_delegate() override = default;
56  void paint(QPainter *painter, const QStyleOptionViewItem &option,
57  const QModelIndex &index) const override;
58  QSize sizeHint(const QStyleOptionViewItem &option,
59  const QModelIndex &index) const override;
60 
61 private:
63 };
64 
65 /***************************************************************************
66  Single item in model of city view table
67 ***************************************************************************/
68 class city_item : public QObject {
69  Q_OBJECT
70 
71 public:
72  city_item(struct city *pcity);
73  inline int columnCount() const { return NUM_CREPORT_COLS; }
74  QVariant data(int column, int role = Qt::DisplayRole) const;
75  bool setData(int column, const QVariant &value,
76  int role = Qt::DisplayRole);
77  struct city *get_city();
78 
79 private:
80  struct city *i_city;
81 };
82 
83 /***************************************************************************
84  City model
85 ***************************************************************************/
86 class city_model : public QAbstractListModel {
87  Q_OBJECT
88 
89 public:
90  city_model(QObject *parent = 0);
91  ~city_model() override;
92  inline int
93  rowCount(const QModelIndex &index = QModelIndex()) const override
94  {
95  Q_UNUSED(index);
96  return city_list.size();
97  }
98  int columnCount(const QModelIndex &parent = QModelIndex()) const override
99  {
100  Q_UNUSED(parent);
101  return NUM_CREPORT_COLS;
102  }
103  QVariant data(const QModelIndex &index,
104  int role = Qt::DisplayRole) const override;
105  bool setData(const QModelIndex &index, const QVariant &value,
106  int role = Qt::DisplayRole) override;
107  QVariant headerData(int section, Qt::Orientation orientation,
108  int role) const override;
109  QVariant menu_data(int section) const;
110  QVariant hide_data(int section) const;
111  void populate();
112  void city_changed(struct city *pcity);
113  void all_changed();
114 private slots:
115  void notify_city_changed(int row);
116 
117 private:
118  QList<city_item *> city_list;
119 };
120 
121 /***************************************************************************
122  City widget to show city model
123 ***************************************************************************/
124 class city_widget : public QTreeView {
125  Q_OBJECT
127  QSortFilterProxyModel *filter_model;
130  enum menu_labels {
146  };
147 
148 public:
149  city_widget(city_report *ctr);
150  ~city_widget() override;
151  QList<city *> selected_cities;
152  void update_model();
153  void update_city(struct city *pcity);
154 public slots:
155  void display_header_menu(const QPoint);
156  void hide_columns();
157  void city_doubleclick(const QModelIndex &index);
158  void city_view();
159  void clear_worlist();
160  void cities_selected(const QItemSelection &sl, const QItemSelection &ds);
161  void display_list_menu(const QPoint);
162  void buy();
163  void center();
164  void select_all();
165  void select_none();
166  void invert_selection();
167  void select_coastal();
169  void select_same_island();
170 
171 private:
172  void restore_selection();
173  void select_city(struct city *pcity);
175  void gen_select_labels(QMenu *menu);
178  bool append_units, bool append_wonders,
179  TestCityFunc test_func, bool global = false);
180  void fill_data(menu_labels which, QMap<QString, cid> &custom_labels,
181  QMenu *menu);
183  QMap<QString, cid> &custom_labels,
184  TestCityFunc test_func, QMenu *menu);
185 };
186 
187 /***************************************************************************
188  Widget to show as tab widget in cities view.
189 ***************************************************************************/
190 class city_report : public QWidget {
191  Q_OBJECT
193  QVBoxLayout *layout;
194 
195 public:
196  city_report();
197  ~city_report() override;
198  void update_report();
199  void update_city(struct city *pcity);
200  void init();
201 
202 private:
203  int index;
204 };
205 
206 void popdown_city_report();
~city_item_delegate() override=default
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
City item delgate paint event.
Definition: view_cities.cpp:75
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Size hint for city item delegate.
city_item_delegate(QObject *parent)
City item delegate constructor.
Definition: view_cities.cpp:63
int columnCount() const
Definition: view_cities.h:73
struct city * get_city()
Returns used city pointer for city item creation.
QVariant data(int column, int role=Qt::DisplayRole) const
Returns data from city item (or city pointer from Qt::UserRole)
struct city * i_city
Definition: view_cities.h:80
city_item(struct city *pcity)
Constructor for city item.
bool setData(int column, const QVariant &value, int role=Qt::DisplayRole)
Sets nothing, but must be declared.
void city_changed(struct city *pcity)
Notifies about changed item.
QVariant hide_data(int section) const
Hides given column if show is false.
int rowCount(const QModelIndex &index=QModelIndex()) const override
Definition: view_cities.h:93
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Returns header data for given section(column)
void populate()
Creates city model.
QVariant menu_data(int section) const
Returns header information about section.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: view_cities.h:98
void all_changed()
Notifies about whole model changed.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::DisplayRole) override
Sets data in model under index.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns stored data in index.
QList< city_item * > city_list
Definition: view_cities.h:118
~city_model() override
Destructor for city model.
city_model(QObject *parent=0)
Constructor for city model.
void notify_city_changed(int row)
Notifies about changed row.
~city_report() override
Destructor for city report.
city_report()
Constructor for city report.
void init()
Inits place in game tab widget.
QVBoxLayout * layout
Definition: view_cities.h:193
void update_report()
Updates whole report.
city_widget * city_wdg
Definition: view_cities.h:192
void update_city(struct city *pcity)
Updates single city.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Overriden compare for sorting items.
Definition: view_cities.cpp:42
void city_view()
Shows first selected city.
void buy()
Buys current item in city.
void select_city(struct city *pcity)
Marks given city selected.
city_widget(city_report *ctr)
Constructor for city widget.
void fill_data(menu_labels which, QMap< QString, cid > &custom_labels, QMenu *menu)
Fills menu actions.
void city_doubleclick(const QModelIndex &index)
Slot for double clicking row.
void center()
Centers map on city.
void update_model()
Updates whole model.
city_item_delegate * c_i_d
Definition: view_cities.h:128
~city_widget() override
Destructor for city widget.
void display_list_menu(const QPoint)
Displays right click menu on city row.
QSortFilterProxyModel * filter_model
Definition: view_cities.h:127
void fill_production_menus(city_widget::menu_labels what, QMap< QString, cid > &custom_labels, TestCityFunc test_func, QMenu *menu)
Fills menu items that can be produced or sold.
city_report * cr
Definition: view_cities.h:129
void select_coastal()
Selects coastal cities on report.
void gen_production_labels(menu_labels which, QMap< QString, cid > &list, bool append_units, bool append_wonders, TestCityFunc test_func, bool global=false)
Creates menu labels and id about available production targets.
void select_building_something()
Selects cities building units or buildings or wonders depending on data stored in QAction.
void hide_columns()
Hides columns for city widget, depending on stored data (bool spec->show)
void clear_worlist()
Clears worklist for selected cities.
void update_city(struct city *pcity)
Updates single city.
city_model * list_model
Definition: view_cities.h:126
void select_all()
Selects all cities on report.
void select_none()
Selects no cities on report.
void gen_worklist_labels(QMap< QString, int > &list)
Creates menu labels and info of available worklists, stored in list.
void cities_selected(const QItemSelection &sl, const QItemSelection &ds)
Slot for selecting items in city widget, they are stored in selected_cities until deselected.
void gen_cma_labels(QMap< QString, int > &list)
Creates menu labels and id of available cma, stored in list.
QList< city * > selected_cities
Definition: view_cities.h:151
void restore_selection()
Restores last selection.
void invert_selection()
Inverts selection on report.
void gen_select_labels(QMenu *menu)
Creates menu labels for selecting cities.
void display_header_menu(const QPoint)
Context menu for header.
void select_same_island()
Selects same cities on the same island.
@ CHANGE_PROD_BEF_LAST
Definition: view_cities.h:134
@ SELECT_AVAIL_UNITS
Definition: view_cities.h:143
@ SELECT_AVAIL_WONDERS
Definition: view_cities.h:145
bool(* TestCityFunc)(const struct city *, const struct universal *)
Definition: climisc.h:71
Definition: city.h:291
The base class for options.
Definition: options.cpp:209
void popdown_city_report()
Closes city report.
#define NUM_CREPORT_COLS