From 639ec66561a23b174def4b9f9a36282fa7691034 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Sun, 20 Mar 2022 03:19:34 +0100 Subject: [PATCH] Restore the display of the "indicators" Use a near-copy of tax_rates_widget instead of a special mode of top_bar_widget. See #940. --- client/gui-qt/page_game.cpp | 9 ++-- client/gui-qt/page_game.h | 3 +- client/gui-qt/top_bar.cpp | 105 +++++++++++++++++++++++++----------- client/gui-qt/top_bar.h | 17 ++++++ 4 files changed, 96 insertions(+), 38 deletions(-) diff --git a/client/gui-qt/page_game.cpp b/client/gui-qt/page_game.cpp index 10e7f51b29..ff035fe276 100644 --- a/client/gui-qt/page_game.cpp +++ b/client/gui-qt/page_game.cpp @@ -74,10 +74,9 @@ pageGame::pageGame(QWidget *parent) 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); - sw_indicators->setRightClick(top_bar_indicators_menu); + sw_indicators = new indicators_widget(); + connect(sw_indicators, &QAbstractButton::clicked, top_bar_indicators_menu); + sw_cunit = new top_bar_widget(_("Units"), QLatin1String(""), toggle_units_report); sw_cunit->setIcon(fcIcons::instance()->getIcon(QStringLiteral("units"))); @@ -303,7 +302,7 @@ void pageGame::updateSidebarTooltips() sw_map->setTooltip(QLatin1String("")); sw_economy->setTooltip(QLatin1String("")); } - sw_indicators->setTooltip(QString(get_info_label_text_popup())); + sw_indicators->setToolTip(get_info_label_text_popup()); } /** diff --git a/client/gui-qt/page_game.h b/client/gui-qt/page_game.h index 6b81c8c00b..71cdc2f1aa 100644 --- a/client/gui-qt/page_game.h +++ b/client/gui-qt/page_game.h @@ -22,6 +22,7 @@ class map_view; class civstatus; class minimap_view; class hud_units; +class indicators_widget; class chat_widget; class message_widget; class hud_battle_log; @@ -79,7 +80,7 @@ class pageGame : public QWidget { xvote *x_vote; civstatus *civ_status; top_bar_widget *sw_diplo; - top_bar_widget *sw_indicators; + indicators_widget *sw_indicators; top_bar_widget *sw_endturn; top_bar_widget *sw_science; public slots: diff --git a/client/gui-qt/top_bar.cpp b/client/gui-qt/top_bar.cpp index 3f99f21074..13c3d51905 100644 --- a/client/gui-qt/top_bar.cpp +++ b/client/gui-qt/top_bar.cpp @@ -123,6 +123,76 @@ void tax_rates_widget::paintEvent(QPaintEvent *event) } } +/** + * Constructor + */ +indicators_widget::indicators_widget() +{ + setToolButtonStyle(Qt::ToolButtonIconOnly); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} + +/** + * Destructor + */ +indicators_widget::~indicators_widget() {} + +/** + * Size hint + */ +QSize indicators_widget::sizeHint() const +{ + // Assume that all icons have the same size + auto content_size = client_research_sprite()->size(); + if (client_is_global_observer()) { + // Global observers can only see climate change + content_size.setWidth(2 * content_size.width()); + } else { + content_size.setWidth(4 * 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 indicators widget + */ +void indicators_widget::paintEvent(QPaintEvent *event) +{ + // Draw a button without contents + QToolButton::paintEvent(event); + + // Draw the icons on top (centered; the style might expect something else + // but screw it) + // Assume that they have the same size + auto icon_size = client_warming_sprite()->size(); + auto center = size() / 2; + + auto x = center.width() + - (client_is_global_observer() ? 1 : 2) * icon_size.width(); + auto y = center.height() - icon_size.height() / 2; + + QPainter p(this); + p.drawPixmap(QPointF(x, y), *client_warming_sprite()); + x += icon_size.width(); + p.drawPixmap(QPointF(x, y), *client_cooling_sprite()); + + if (!client_is_global_observer()) { + x += icon_size.width(); + p.drawPixmap(QPointF(x, y), *client_research_sprite()); + x += icon_size.width(); + p.drawPixmap(QPointF(x, y), *client_government_sprite()); + } +} + /** Sidewidget constructor */ @@ -169,48 +239,19 @@ void top_bar_widget::setTooltip(const QString &tooltip) */ void top_bar_widget::paintEvent(QPaintEvent *event) { - int w, pos, i; - QPainter p; - QPen pen; - // HACK Should improve this logic, paintEvent is NOT the right place. - i = queen()->gimmeIndexOf(page); + int i = queen()->gimmeIndexOf(page); setChecked(i == queen()->game_tab_widget->currentIndex()); - p.begin(this); - pen.setColor(QColor(232, 255, 0)); - p.setPen(pen); - - if (standard == SW_INDICATORS) { - auto sprite = client_research_sprite(); - w = sprite->width() / sprite->devicePixelRatioF(); - pos = width() / 2 - 2 * w; - p.drawPixmap(pos, 5, *sprite); - pos = pos + w; - sprite = client_warming_sprite(); - p.drawPixmap(pos, 5, *sprite); - pos = pos + w; - sprite = client_cooling_sprite(); - p.drawPixmap(pos, 5, *sprite); - pos = pos + w; - sprite = client_government_sprite(); - p.drawPixmap(pos, 5, *sprite); - } - - // Remove 1px for the border on the right and at the bottom - const auto highlight_rect = - QRectF(0.5, 0, width() - 1. / devicePixelRatio(), - height() - 1. / devicePixelRatio()); - p.end(); - QToolButton::paintEvent(event); if (blink) { + QPainter p; p.begin(this); p.setPen(Qt::NoPen); p.setCompositionMode(QPainter::CompositionMode_SoftLight); p.setBrush(palette().color(QPalette::HighlightedText)); - p.drawRect(highlight_rect); + p.drawRect(0, 0, width(), height()); p.end(); } } diff --git a/client/gui-qt/top_bar.h b/client/gui-qt/top_bar.h index 9ace0cf44e..f1502f468a 100644 --- a/client/gui-qt/top_bar.h +++ b/client/gui-qt/top_bar.h @@ -45,6 +45,23 @@ class tax_rates_widget : public QToolButton { void paintEvent(QPaintEvent *event) override; }; +/** + * Top bar widget for indicators (global warming/nuclear winter/science/ + * government). + */ +class indicators_widget : public QToolButton { + Q_OBJECT + +public: + indicators_widget(); + ~indicators_widget() override; + + QSize sizeHint() const override; + +protected: + void paintEvent(QPaintEvent *event) override; +}; + /*************************************************************************** Class representing single widget(icon) on top_bar ***************************************************************************/