Skip to content

Commit

Permalink
Restore the display of the "indicators"
Browse files Browse the repository at this point in the history
Use a near-copy of tax_rates_widget instead of a special mode of
top_bar_widget.

See longturn#940.
  • Loading branch information
lmoureaux committed Mar 25, 2022
1 parent d583b59 commit 639ec66
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 38 deletions.
9 changes: 4 additions & 5 deletions client/gui-qt/page_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down Expand Up @@ -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());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion client/gui-qt/page_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
105 changes: 73 additions & 32 deletions client/gui-qt/top_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}
}
Expand Down
17 changes: 17 additions & 0 deletions client/gui-qt/top_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
***************************************************************************/
Expand Down

0 comments on commit 639ec66

Please sign in to comment.