From 17f676e20816c987d1d7c8a85b7439b1c6d826f4 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Sun, 20 Mar 2022 03:01:44 +0100 Subject: [PATCH] Restore the display of the current tax rates Use a specialized class instead of a special mode of top_bar_widget. See #940. --- client/gui-qt/page_game.cpp | 7 +- client/gui-qt/page_game.h | 3 +- client/gui-qt/top_bar.cpp | 129 ++++++++++++++++++++++-------------- client/gui-qt/top_bar.h | 20 +++++- 4 files changed, 104 insertions(+), 55 deletions(-) diff --git a/client/gui-qt/page_game.cpp b/client/gui-qt/page_game.cpp index 46d0ee7392..10e7f51b29 100644 --- a/client/gui-qt/page_game.cpp +++ b/client/gui-qt/page_game.cpp @@ -70,8 +70,10 @@ pageGame::pageGame(QWidget *parent) sw_map = new top_bar_widget( Q_("?noun:View"), QStringLiteral("MAP"), top_bar_show_map); sw_map->setIcon(fcIcons::instance()->getIcon(QStringLiteral("view"))); - sw_tax = new top_bar_widget(nullptr, QLatin1String(""), top_bar_rates_wdg, - top_bar_widget::SW_TAX); + + sw_tax = new tax_rates_widget(); + connect(sw_tax, &QAbstractButton::clicked, top_bar_rates_wdg); + sw_indicators = new top_bar_widget(nullptr, QLatin1String(""), top_bar_show_map, top_bar_widget::SW_INDICATORS); @@ -297,7 +299,6 @@ void pageGame::updateSidebarTooltips() text_happiness_cities(player_primary_capital(client_player()))); } } else { - sw_tax->setTooltip(QLatin1String("")); sw_science->setTooltip(QLatin1String("")); sw_map->setTooltip(QLatin1String("")); sw_economy->setTooltip(QLatin1String("")); diff --git a/client/gui-qt/page_game.h b/client/gui-qt/page_game.h index c2c18f2446..6b81c8c00b 100644 --- a/client/gui-qt/page_game.h +++ b/client/gui-qt/page_game.h @@ -26,6 +26,7 @@ class chat_widget; class message_widget; class hud_battle_log; class goto_dialog; +class tax_rates_widget; class top_bar; class top_bar_widget; class unitinfo_wdg; @@ -92,7 +93,7 @@ private slots: top_bar_widget *sw_cunit; top_bar_widget *sw_economy; top_bar_widget *sw_map; - top_bar_widget *sw_tax; + tax_rates_widget *sw_tax; }; /** diff --git a/client/gui-qt/top_bar.cpp b/client/gui-qt/top_bar.cpp index 1676ba9b4e..3f99f21074 100644 --- a/client/gui-qt/top_bar.cpp +++ b/client/gui-qt/top_bar.cpp @@ -17,7 +17,10 @@ #include #include #include +#include +#include #include + // common #include "chatline_common.h" #include "government.h" @@ -39,18 +42,84 @@ #include -extern void pixmap_copy(QPixmap *dest, QPixmap *src, int src_x, int src_y, - int dest_x, int dest_y, int width, int height); -static void reduce_mod(int &val, int &mod); +/** + * Constructor + */ +tax_rates_widget::tax_rates_widget() +{ + setToolButtonStyle(Qt::ToolButtonIconOnly); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} + +/** + * Destructor + */ +tax_rates_widget::~tax_rates_widget() {} /** - Helper function to fit tax sprites, reduces modulo, increasing value + * Size hint */ -void reduce_mod(int &mod, int &val) +QSize tax_rates_widget::sizeHint() const { - if (mod > 0) { - val++; - mod--; + if (client_is_global_observer()) { + // Nothing to show + return QSize(); + } + + // Assume that all icons have the same size + auto content_size = get_tax_sprite(tileset, O_GOLD)->size(); + content_size.setWidth(10 * content_size.width()); + + // See QToolButton::sizeHint + ensurePolished(); + + QStyleOptionToolButton opt; + initStyleOption(&opt); + + return style() + ->sizeFromContents(QStyle::CT_ToolButton, &opt, content_size, this) + .expandedTo(QApplication::globalStrut()); +} + +/** + * Renders the tax rates widget + */ +void tax_rates_widget::paintEvent(QPaintEvent *event) +{ + if (client_is_global_observer()) { + // Nothing to show + return; + } + + // Draw a button without contents + QToolButton::paintEvent(event); + + // Draw the tax icons on top (centered; the style might expect something + // else but screw it) + auto tax = get_tax_sprite(tileset, O_GOLD); + auto sci = get_tax_sprite(tileset, O_SCIENCE); + auto lux = get_tax_sprite(tileset, O_LUXURY); + + // Assume that they have the same size + auto icon_size = tax->size(); + auto center = size() / 2; + + auto x = center.width() - 5 * icon_size.width(); + auto y = center.height() - icon_size.height() / 2; + + QPainter p(this); + for (int i = 0; i < 10; ++i) { + if (i < client.conn.playing->economic.tax / 10) { + p.drawPixmap(QPointF(x, y), *tax); + } else if (i < (client.conn.playing->economic.tax + + client.conn.playing->economic.science) + / 10) { + p.drawPixmap(QPointF(x, y), *sci); + } else { + p.drawPixmap(QPointF(x, y), *lux); + } + + x += icon_size.width(); } } @@ -100,7 +169,7 @@ void top_bar_widget::setTooltip(const QString &tooltip) */ void top_bar_widget::paintEvent(QPaintEvent *event) { - int w, h, pos, i; + int w, pos, i; QPainter p; QPen pen; @@ -112,45 +181,7 @@ void top_bar_widget::paintEvent(QPaintEvent *event) pen.setColor(QColor(232, 255, 0)); p.setPen(pen); - if (standard == SW_TAX && !client_is_global_observer()) { - pos = 0; - int d, modulo; - auto sprite = get_tax_sprite(tileset, O_GOLD); - if (sprite == nullptr) { - return; - } - w = width() / 10.; - modulo = std::fmod(qreal(width()), 10); - h = sprite->height(); - reduce_mod(modulo, pos); - if (client.conn.playing == nullptr) { - return; - } - for (d = 0; d < client.conn.playing->economic.tax / 10; ++d) { - p.drawPixmap(pos, 5, sprite->scaled(w, h), 0, 0, w, h); - pos = pos + w; - reduce_mod(modulo, pos); - } - - sprite = get_tax_sprite(tileset, O_SCIENCE); - - for (; d < (client.conn.playing->economic.tax - + client.conn.playing->economic.science) - / 10; - ++d) { - p.drawPixmap(pos, 5, sprite->scaled(w, h), 0, 0, w, h); - pos = pos + w; - reduce_mod(modulo, pos); - } - - sprite = get_tax_sprite(tileset, O_LUXURY); - - for (; d < 10; ++d) { - p.drawPixmap(pos, 5, sprite->scaled(w, h), 0, 0, w, h); - pos = pos + w; - reduce_mod(modulo, pos); - } - } else if (standard == SW_INDICATORS) { + if (standard == SW_INDICATORS) { auto sprite = client_research_sprite(); w = sprite->width() / sprite->devicePixelRatioF(); pos = width() / 2 - 2 * w; @@ -313,7 +344,7 @@ top_bar::~top_bar() = default; /** Adds new top_bar widget */ -void top_bar::addWidget(top_bar_widget *fsw) +void top_bar::addWidget(QWidget *fsw) { objects.append(fsw); layout->addWidget(fsw); diff --git a/client/gui-qt/top_bar.h b/client/gui-qt/top_bar.h index 1906c68206..9ace0cf44e 100644 --- a/client/gui-qt/top_bar.h +++ b/client/gui-qt/top_bar.h @@ -29,6 +29,22 @@ void top_bar_right_click_science(); void top_bar_left_click_science(); void top_bar_show_map(); +/** + * Top bar widget for tax rates. + */ +class tax_rates_widget : public QToolButton { + Q_OBJECT + +public: + tax_rates_widget(); + ~tax_rates_widget() override; + + QSize sizeHint() const override; + +protected: + void paintEvent(QPaintEvent *event) override; +}; + /*************************************************************************** Class representing single widget(icon) on top_bar ***************************************************************************/ @@ -82,8 +98,8 @@ class top_bar : public QWidget { public: top_bar(); ~top_bar() override; - void addWidget(top_bar_widget *fsw); - QList objects; + void addWidget(QWidget *fsw); + QList objects; private: QHBoxLayout *layout;