Skip to content

Commit

Permalink
Restore the display of the current tax rates
Browse files Browse the repository at this point in the history
Use a specialized class instead of a special mode of top_bar_widget.

See #940.
  • Loading branch information
lmoureaux authored and jwrober committed Mar 27, 2022
1 parent 6fbd37e commit 17f676e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 55 deletions.
7 changes: 4 additions & 3 deletions client/gui-qt/page_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(""));
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 @@ -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;
Expand Down Expand Up @@ -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;
};

/**
Expand Down
129 changes: 80 additions & 49 deletions client/gui-qt/top_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#include <QPainter>
#include <QPixmap>
#include <QScreen>
#include <QStyle>
#include <QStyleOptionToolButton>
#include <QTimer>

// common
#include "chatline_common.h"
#include "government.h"
Expand All @@ -39,18 +42,84 @@

#include <cmath>

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();
}
}

Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 18 additions & 2 deletions client/gui-qt/top_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
***************************************************************************/
Expand Down Expand Up @@ -82,8 +98,8 @@ class top_bar : public QWidget {
public:
top_bar();
~top_bar() override;
void addWidget(top_bar_widget *fsw);
QList<top_bar_widget *> objects;
void addWidget(QWidget *fsw);
QList<QWidget *> objects;

private:
QHBoxLayout *layout;
Expand Down

0 comments on commit 17f676e

Please sign in to comment.