Skip to content

Commit

Permalink
Top bar: Use QToolButton's icon functionality
Browse files Browse the repository at this point in the history
Replace custom-drawn icons with a standard QToolButton feature. Now they're
even SVG-capable!

See longturn#940.
  • Loading branch information
lmoureaux committed Mar 19, 2022
1 parent 3584ce2 commit 1318e50
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 73 deletions.
7 changes: 4 additions & 3 deletions client/gui-qt/diplodlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ void diplo_wdg::update_wdg()
*/
void diplo_wdg::restore_pixmap()
{
queen()->sw_diplo->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("nations")));
queen()->sw_diplo->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("nations")));
queen()->sw_diplo->setCustomLabels(QString());
queen()->sw_diplo->update();
}
Expand Down Expand Up @@ -884,11 +884,12 @@ void handle_diplomacy_init_meeting(int counterpart, int initiated_from)
hmid = qMax(1, hmid);
p.drawPixmap(1, hmid, *pix2);
p.end();
queen()->sw_diplo->setPixmap(pix3);
queen()->sw_diplo->setIcon(QIcon(*pix3));
queen()->sw_diplo->setCustomLabels(
QString(nation_plural_for_player(player_by_number(counterpart))));
queen()->sw_diplo->update();
delete pix2;
delete pix3;
if (!queen()->isRepoDlgOpen(QStringLiteral("DDI"))) {
dd = new diplo_dlg(counterpart, initiated_from);

Expand Down
56 changes: 28 additions & 28 deletions client/gui-qt/page_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,45 @@ pageGame::pageGame(QWidget *parent)
mapview_wdg->setFocusPolicy(Qt::WheelFocus);
top_bar_wdg = new top_bar();
sw_map = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("view")),
Q_("?noun:View"), QStringLiteral("MAP"), top_bar_show_map);
sw_map->setIcon(fcIcons::instance()->getIcon(QStringLiteral("view")));
sw_map->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_tax = new top_bar_widget(nullptr, nullptr, QLatin1String(""),
top_bar_rates_wdg, top_bar_widget::SW_TAX);
sw_tax = new top_bar_widget(nullptr, QLatin1String(""), top_bar_rates_wdg,
top_bar_widget::SW_TAX);
sw_indicators =
new top_bar_widget(nullptr, nullptr, QLatin1String(""),
top_bar_show_map, top_bar_widget::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_cunit = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("units")), _("Units"),
QLatin1String(""), toggle_units_report);
sw_cunit =
new top_bar_widget(_("Units"), QLatin1String(""), toggle_units_report);
sw_cunit->setIcon(fcIcons::instance()->getIcon(QStringLiteral("units")));
sw_cunit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_cities = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("cities")), _("Cities"),
QStringLiteral("CTS"), city_report_dialog_popup);
sw_cities = new top_bar_widget(_("Cities"), QStringLiteral("CTS"),
city_report_dialog_popup);
sw_cities->setIcon(fcIcons::instance()->getIcon(QStringLiteral("cities")));
sw_cities->setWheelUp(center_next_enemy_city);
sw_cities->setWheelDown(center_next_player_city);
sw_cities->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_diplo = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("nations")),
_("Nations"), QStringLiteral("PLR"), popup_players_dialog);
sw_diplo->setIcon(fcIcons::instance()->getIcon(QStringLiteral("nations")));
sw_diplo->setWheelUp(center_next_player_capital);
sw_diplo->setWheelDown(key_center_capital);
sw_diplo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_science = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("research")),
_("Research"), QStringLiteral("SCI"), top_bar_left_click_science);
sw_science->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("research")));
sw_science->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_economy = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("economy")),
_("Economy"), QStringLiteral("ECO"), economy_report_dialog_popup);
sw_economy->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("economy")));
sw_economy->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_endturn = new top_bar_widget(
fcIcons::instance()->getPixmap(QStringLiteral("endturn")),
_("Turn Done"), QLatin1String(""), top_bar_finish_turn);
sw_endturn->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("endturn")));
sw_endturn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sw_cunit->setRightClick(top_bar_center_unit);
sw_cunit->setWheelUp(cycle_enemy_units);
Expand Down Expand Up @@ -178,19 +181,16 @@ pageGame::~pageGame() = default;
*/
void pageGame::reloadSidebarIcons()
{
sw_map->setPixmap(fcIcons::instance()->getPixmap(QStringLiteral("view")));
sw_cunit->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("units")));
sw_cities->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("cities")));
sw_diplo->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("nations")));
sw_science->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("research")));
sw_economy->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("economy")));
sw_endturn->setPixmap(
fcIcons::instance()->getPixmap(QStringLiteral("endturn")));
sw_map->setIcon(fcIcons::instance()->getIcon(QStringLiteral("view")));
sw_cunit->setIcon(fcIcons::instance()->getIcon(QStringLiteral("units")));
sw_cities->setIcon(fcIcons::instance()->getIcon(QStringLiteral("cities")));
sw_diplo->setIcon(fcIcons::instance()->getIcon(QStringLiteral("nations")));
sw_science->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("research")));
sw_economy->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("economy")));
sw_endturn->setIcon(
fcIcons::instance()->getIcon(QStringLiteral("endturn")));
}

/**
Expand Down
41 changes: 3 additions & 38 deletions client/gui-qt/top_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,16 @@ void reduce_mod(int &mod, int &val)
/**
Sidewidget constructor
*/
top_bar_widget::top_bar_widget(QPixmap *pix, const QString &label,
const QString &pg, pfcn_bool func,
standards type)
top_bar_widget::top_bar_widget(const QString &label, const QString &pg,
pfcn_bool func, standards type)
: QToolButton(), blink(false), keep_blinking(false), standard(type),
page(pg), hover(false), right_click(nullptr), wheel_down(nullptr),
wheel_up(nullptr), left_click(func), def_pixmap(pix), desc(label)
wheel_up(nullptr), left_click(func), desc(label)
{
if (def_pixmap == nullptr) {
def_pixmap = new QPixmap(5, 5);
}

setText(label);
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
setContextMenuPolicy(Qt::CustomContextMenu);

if (def_pixmap) {
setIcon(QIcon(*def_pixmap));
}

timer = new QTimer;
timer->setSingleShot(false);
timer->setInterval(700);
Expand All @@ -87,20 +78,9 @@ top_bar_widget::top_bar_widget(QPixmap *pix, const QString &label,
*/
top_bar_widget::~top_bar_widget()
{
NFCN_FREE(def_pixmap);

delete timer;
}

/**
Sets default pixmap for sidewidget
*/
void top_bar_widget::setPixmap(QPixmap *pm)
{
NFCN_FREE(def_pixmap);
def_pixmap = pm;
}

/**
Sets custom text visible on top of sidewidget
*/
Expand Down Expand Up @@ -193,21 +173,6 @@ void top_bar_widget::paintEvent(QPaintEvent *event)
pos = pos + w;
sprite = client_government_sprite();
p.drawPixmap(pos, 5, *sprite);

} else {
auto pix_rect = QRectF(def_pixmap->rect());

// Shrink to make it fit
if (pix_rect.width() > width()) {
pix_rect.setSize(pix_rect.size() * width() / pix_rect.width());
}
if (pix_rect.height() > height()) {
pix_rect.setSize(pix_rect.size() * height() / pix_rect.height());
}

pix_rect.moveCenter(rect.center());
p.drawPixmap(pix_rect, *def_pixmap, def_pixmap->rect());
p.drawText(rect, desc);
}

p.setPen(palette().color(QPalette::Text));
Expand Down
6 changes: 2 additions & 4 deletions client/gui-qt/top_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ class top_bar_widget : public QToolButton {
public:
enum standards { SW_STD, SW_TAX, SW_INDICATORS };

top_bar_widget(QPixmap *pix, const QString &label, const QString &pg,
pfcn_bool func, standards type = SW_STD);
top_bar_widget(const QString &label, const QString &pg, pfcn_bool func,
standards type = SW_STD);
~top_bar_widget() override;
int getPriority();
QPixmap *get_pixmap();
void paint(QPainter *painter, QPaintEvent *event);
void setCustomLabels(const QString &);
void setLabel(const QString &str);
void setLeftClick(pfcn_bool func);
void setPixmap(QPixmap *pm);
void setRightClick(pfcn func);
void setTooltip(const QString &tooltip);
void setWheelDown(pfcn func);
Expand Down Expand Up @@ -78,7 +77,6 @@ public slots:
pfcn_bool left_click;
QFont sfont;
QFont info_font;
QPixmap *def_pixmap;
QString custom_label;
QString desc;
QTimer *timer;
Expand Down

0 comments on commit 1318e50

Please sign in to comment.