Freeciv21
Develop your civilization from humble roots to a global empire
city_icon_widget.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2023 Louis Moureaux <m_louis30@yahoo.com>
3  *
4  * SPDX-License-Identifier: GPLv3-or-later
5  */
6 
7 #include "city_icon_widget.h"
8 
9 // client
10 #include "game.h"
11 #include "tileset/layer_city.h"
12 #include "tileset/tilespec.h"
13 
14 #include <QPainter>
15 
16 namespace freeciv {
17 
28 {
29  setAutoFillBackground(true);
30  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
31 }
32 
36 void city_icon_widget::set_city(int city_id)
37 {
38  if (city_id != m_city) {
39  m_city = city_id;
40  update();
41  }
42 }
43 
47 QSize city_icon_widget::minimumSizeHint() const { return QSize(0, 0); }
48 
53 {
54  auto size =
56 
57  if (auto city = game_city_by_number(m_city); city) {
58  auto sprs =
60  const auto bounds = sprite_array_bounds(sprs);
61  size = std::max(bounds.width(), bounds.height());
62  }
63 
64  return QSize(size, size); // Square
65 }
66 
71 {
72  Q_UNUSED(event);
73 
75  if (!city) {
76  return;
77  }
78 
79  // We don't show the occupied flag as it looks bad with amplio
80  auto sprs =
82 
83  // Center the sprites
84  auto bounds = sprite_array_bounds(sprs);
85  const auto origin = bounds.topLeft();
86  bounds.moveCenter(QPoint(width() / 2, height() / 2));
87 
88  QPainter p(this);
89  p.translate(bounds.topLeft() - origin);
90 
91  for (const auto sprite : sprs) {
92  // Floating-point API for DPI scaling.
93  p.drawPixmap(QPointF(sprite.offset), *sprite.sprite);
94  }
95 }
96 
101 {
102  if (event->type() == TilesetChanged) {
103  updateGeometry();
104  update();
105  return true;
106  }
107  return QWidget::event(event);
108 }
109 
110 } // namespace freeciv
void set_city(int city_id)
Changes the city displayed by this widget.
bool event(QEvent *event) override
Reimplemented to handle tileset changes.
QSize sizeHint() const override
Reimplemented to pick the size from the tileset.
QSize minimumSizeHint() const override
Reimplemented to allow for tiny tileset.
void paintEvent(QPaintEvent *event) override
Reimplemented to draw the widget.
city_icon_widget(QWidget *parent=nullptr)
Constructor.
std::vector< drawn_sprite > fill_sprite_array_no_flag(const city *pcity, bool show_occupied) const
Fill in the given sprite array with any city sprites.
Definition: layer_city.cpp:200
QRect sprite_array_bounds(const std::vector< drawn_sprite > &sprs)
Calculates the bounding rectangle of the given sprite array.
enum event_type event
Definition: events.cpp:68
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
Definition: game.cpp:103
Definition: path.cpp:10
size_t size
Definition: specvec.h:64
Definition: city.h:291
const freeciv::layer_city * tileset_layer_city(const struct tileset *t)
Returns the layer_city of the tileset.
Definition: tilespec.cpp:3665
QEvent::Type TilesetChanged
An event type sent to all widgets when the current tileset changes.
Definition: tilespec.cpp:719
int tileset_tile_height(const struct tileset *t)
Return the tile height of the current tileset.
Definition: tilespec.cpp:383
int tileset_tile_width(const struct tileset *t)
Return the tile width of the current tileset.
Definition: tilespec.cpp:371