Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the city size and citizen happiness next to other city stats #1204

Merged
merged 5 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 109 additions & 146 deletions client/citydlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QVBoxLayout>
#include <QWidgetAction>
// utility
#include "fc_types.h"
#include "fcintl.h"
#include "support.h"
// common
Expand Down Expand Up @@ -876,22 +877,33 @@ void cityIconInfoLabel::updateText()
}

labs[1].setText(QString::number(pcity->surplus[O_FOOD]));
labs[0].setToolTip(get_city_dialog_output_text(pcity, O_FOOD));
labs[1].setToolTip(get_city_dialog_output_text(pcity, O_FOOD));

labs[3].setText(QString::number(pcity->surplus[O_SHIELD]));
labs[9].setText(QString::number(pcity->surplus[O_TRADE]));
labs[2].setToolTip(get_city_dialog_output_text(pcity, O_SHIELD));
labs[3].setToolTip(get_city_dialog_output_text(pcity, O_SHIELD));

labs[5].setText(QString::number(pcity->surplus[O_GOLD]));
labs[4].setToolTip(get_city_dialog_output_text(pcity, O_GOLD));
labs[5].setToolTip(get_city_dialog_output_text(pcity, O_GOLD));

labs[7].setText(QString::number(pcity->surplus[O_SCIENCE]));
labs[6].setToolTip(get_city_dialog_output_text(pcity, O_SCIENCE));
labs[7].setToolTip(get_city_dialog_output_text(pcity, O_SCIENCE));

labs[9].setText(QString::number(pcity->surplus[O_TRADE]));
labs[8].setToolTip(get_city_dialog_output_text(pcity, O_TRADE));
labs[9].setToolTip(get_city_dialog_output_text(pcity, O_TRADE));

if (city_turns_to_grow(pcity) < 1000) {
grow_time = QString::number(city_turns_to_grow(pcity));
} else {
grow_time = QStringLiteral("∞");
}
labs[11].setText(grow_time);
}

void cityIconInfoLabel::updateTooltip(int nr, const QString &tooltipText)
{
labs[nr].setToolTip(tooltipText);
labs[nr + 1].setToolTip(tooltipText);
labs[10].setToolTip(get_city_dialog_growth_value(pcity));
labs[11].setToolTip(get_city_dialog_growth_value(pcity));
}

/**
Expand Down Expand Up @@ -938,160 +950,111 @@ void city_label::set_city(city *pciti) { pcity = pciti; }

city_info::city_info(QWidget *parent) : QWidget(parent)
{
int iter;
QLabel *ql;
QStringList info_list;

QGridLayout *info_grid_layout = new QGridLayout();
auto small_font = fcFont::instance()->getFont(fonts::notify_label);
info_list << _("Food:") << _("Prod:") << _("Trade:") << _("Gold:")
<< _("Luxury:") << _("Science:") << _("Granary:")
<< _("Change in:") << _("Corruption:") << _("Waste:")
<< _("Culture:") << _("Pollution:") << _("Plague risk:")
<< _("Tech Stolen:") << _("Airlift:");
setFont(small_font);
info_grid_layout->setSpacing(0);
info_grid_layout->setContentsMargins(0, 0, 0, 0);

fc_assert(info_list.count() == NUM_INFO_FIELDS);
for (iter = 0; iter < NUM_INFO_FIELDS; iter++) {
ql = new QLabel(info_list[iter], this);
ql->setFont(small_font);
ql->setProperty(fonts::notify_label, "true");
info_grid_layout->addWidget(ql, iter, 0);
qlt[iter] = new QLabel(this);
qlt[iter]->setFont(small_font);
qlt[iter]->setProperty(fonts::notify_label, "true");
info_grid_layout->addWidget(qlt[iter], iter, 1);
}
setLayout(info_grid_layout);
}

void city_info::update_labels(struct city *pcity, cityIconInfoLabel *ciil)
{
int illness = 0;
char buffer[512];
char buf[2 * NUM_INFO_FIELDS][512];
int granaryturns;

enum {
FOOD = 0,
SHIELD = 2,
TRADE = 4,
GOLD = 6,
LUXURY = 8,
SCIENCE = 10,
GRANARY = 12,
GROWTH = 14,
CORRUPTION = 16,
WASTE = 18,
CULTURE = 20,
POLLUTION = 22,
ILLNESS = 24,
STEAL = 26,
AIRLIFT = 28,
auto small_font = fcFont::instance()->getFont(fonts::notify_label);

// We use this to shorten the code below, as it would otherwise be very
// repetitive. It creates a label for the description and another for the
// value, and returns the second.
const auto create_labels = [&](const char *title) {
auto label = new QLabel(title, this);
label->setFont(small_font);
label->setProperty(fonts::notify_label, "true");
info_grid_layout->addWidget(label, info_grid_layout->rowCount(), 0);

label = new QLabel(this);
label->setFont(small_font);
label->setProperty(fonts::notify_label, "true");
info_grid_layout->addWidget(label, info_grid_layout->rowCount() - 1, 1);
return label;
};

// fill the buffers with the necessary info
fc_snprintf(buf[FOOD], sizeof(buf[FOOD]), "%3d (%+4d)",
pcity->prod[O_FOOD], pcity->surplus[O_FOOD]);
fc_snprintf(buf[SHIELD], sizeof(buf[SHIELD]), "%3d (%+4d)",
pcity->prod[O_SHIELD] + pcity->waste[O_SHIELD],
pcity->surplus[O_SHIELD]);
fc_snprintf(buf[TRADE], sizeof(buf[TRADE]), "%3d (%+4d)",
pcity->surplus[O_TRADE] + pcity->waste[O_TRADE],
pcity->surplus[O_TRADE]);
fc_snprintf(buf[GOLD], sizeof(buf[GOLD]), "%3d (%+4d)",
pcity->prod[O_GOLD], pcity->surplus[O_GOLD]);
fc_snprintf(buf[LUXURY], sizeof(buf[LUXURY]), "%3d",
pcity->prod[O_LUXURY]);
fc_snprintf(buf[SCIENCE], sizeof(buf[SCIENCE]), "%3d",
pcity->prod[O_SCIENCE]);
fc_snprintf(buf[GRANARY], sizeof(buf[GRANARY]), "%4d/%-4d",
pcity->food_stock, city_granary_size(city_size_get(pcity)));

get_city_dialog_output_text(pcity, O_FOOD, buf[FOOD + 1],
sizeof(buf[FOOD + 1]));
get_city_dialog_output_text(pcity, O_SHIELD, buf[SHIELD + 1],
sizeof(buf[SHIELD + 1]));
get_city_dialog_output_text(pcity, O_TRADE, buf[TRADE + 1],
sizeof(buf[TRADE + 1]));
get_city_dialog_output_text(pcity, O_GOLD, buf[GOLD + 1],
sizeof(buf[GOLD + 1]));
get_city_dialog_output_text(pcity, O_SCIENCE, buf[SCIENCE + 1],
sizeof(buf[SCIENCE + 1]));
get_city_dialog_output_text(pcity, O_LUXURY, buf[LUXURY + 1],
sizeof(buf[LUXURY + 1]));
get_city_dialog_culture_text(pcity, buf[CULTURE + 1],
sizeof(buf[CULTURE + 1]));
get_city_dialog_pollution_text(pcity, buf[POLLUTION + 1],
sizeof(buf[POLLUTION + 1]));
get_city_dialog_illness_text(pcity, buf[ILLNESS + 1],
sizeof(buf[ILLNESS + 1]));

granaryturns = city_turns_to_grow(pcity);

if (granaryturns == 0) {
// TRANS: city growth is blocked. Keep short.
fc_snprintf(buf[GROWTH], sizeof(buf[GROWTH]), _("blocked"));
} else if (granaryturns == FC_INFINITY) {
// TRANS: city is not growing. Keep short.
fc_snprintf(buf[GROWTH], sizeof(buf[GROWTH]), _("never"));
} else {
/* A negative value means we'll have famine in that many turns.
But that's handled down below. */
// TRANS: city growth turns. Keep short.
fc_snprintf(buf[GROWTH], sizeof(buf[GROWTH]),
PL_("%d turn", "%d turns", abs(granaryturns)),
abs(granaryturns));
}

fc_snprintf(buf[CORRUPTION], sizeof(buf[CORRUPTION]), "%4d",
pcity->waste[O_TRADE]);
fc_snprintf(buf[WASTE], sizeof(buf[WASTE]), "%4d", pcity->waste[O_SHIELD]);
fc_snprintf(buf[CULTURE], sizeof(buf[CULTURE]), "%4d",
pcity->client.culture);
fc_snprintf(buf[POLLUTION], sizeof(buf[POLLUTION]), "%4d",
pcity->pollution);
m_size = create_labels(_("Citizens:"));
m_food = create_labels(_("Food:"));
m_production = create_labels(_("Prod:"));
m_trade = create_labels(_("Trade:"));
m_gold = create_labels(_("Gold:"));
m_luxury = create_labels(_("Luxury:"));
m_science = create_labels(_("Science:"));
m_granary = create_labels(_("Granary:"));
m_growth = create_labels(_("Change in:"));
m_corruption = create_labels(_("Corruption:"));
m_waste = create_labels(_("Waste:"));
m_culture = create_labels(_("Culture:"));
m_pollution = create_labels(_("Pollution:"));
m_plague = create_labels(_("Plague risk:"));
m_stolen = create_labels(_("Tech Stolen:"));
m_airlift = create_labels(_("Airlift:"));
}

void city_info::update_labels(struct city *pcity)
{
m_size->setText(QString::asprintf("%3d", pcity->size));
m_size->setToolTip(get_city_dialog_size_text(pcity));

m_food->setText(QString::asprintf("%3d (%+4d)", pcity->prod[O_FOOD],
pcity->surplus[O_FOOD]));
m_food->setToolTip(get_city_dialog_output_text(pcity, O_FOOD));

m_production->setText(QString::asprintf(
"%3d (%+4d)", pcity->prod[O_SHIELD], pcity->surplus[O_SHIELD]));
m_production->setToolTip(get_city_dialog_output_text(pcity, O_SHIELD));

m_trade->setText(QString::asprintf("%3d (%+4d)", pcity->prod[O_TRADE],
pcity->surplus[O_TRADE]));
m_trade->setToolTip(get_city_dialog_output_text(pcity, O_TRADE));

m_gold->setText(QString::asprintf("%3d (%+4d)", pcity->prod[O_GOLD],
pcity->surplus[O_GOLD]));
m_gold->setToolTip(get_city_dialog_output_text(pcity, O_GOLD));

m_luxury->setText(QString::asprintf("%3d (%+4d)", pcity->prod[O_LUXURY],
pcity->surplus[O_LUXURY]));
m_luxury->setToolTip(get_city_dialog_output_text(pcity, O_LUXURY));

m_science->setText(QString::asprintf("%3d (%+4d)", pcity->prod[O_SCIENCE],
pcity->surplus[O_SCIENCE]));
m_science->setToolTip(get_city_dialog_output_text(pcity, O_SCIENCE));

m_granary->setText(
QString::asprintf("%4d/%-4d", pcity->food_stock,
city_granary_size(city_size_get(pcity))));

m_growth->setText(get_city_dialog_growth_value(pcity));

m_corruption->setText(QString::asprintf("%4d", pcity->waste[O_TRADE]));

m_waste->setText(QString::asprintf("%4d", pcity->waste[O_SHIELD]));

m_culture->setText(QString::asprintf("%4d", pcity->client.culture));
m_culture->setToolTip(get_city_dialog_culture_text(pcity));

m_pollution->setText(QString::asprintf("%4d", pcity->pollution));
m_pollution->setToolTip(get_city_dialog_pollution_text(pcity));

if (!game.info.illness_on) {
fc_snprintf(buf[ILLNESS], sizeof(buf[ILLNESS]), " -.-");
m_plague->setText(QStringLiteral(" -.-"));
} else {
illness = city_illness_calc(pcity, nullptr, nullptr, nullptr, nullptr);
auto illness =
city_illness_calc(pcity, nullptr, nullptr, nullptr, nullptr);
// illness is in tenth of percent
fc_snprintf(buf[ILLNESS], sizeof(buf[ILLNESS]), "%4.1f%%",
static_cast<float>(illness) / 10.0);
m_plague->setText(
QString::asprintf("%4.1f%%", static_cast<float>(illness) / 10.0));
}
m_plague->setToolTip(get_city_dialog_illness_text(pcity));

if (pcity->steal) {
fc_snprintf(buf[STEAL], sizeof(buf[STEAL]), _("%d times"), pcity->steal);
m_stolen->setText(QString::asprintf(_("%d times"), pcity->steal));
} else {
fc_snprintf(buf[STEAL], sizeof(buf[STEAL]), _("Not stolen"));
}

get_city_dialog_airlift_value(pcity, buf[AIRLIFT], sizeof(buf[AIRLIFT]));
get_city_dialog_airlift_text(pcity, buf[AIRLIFT + 1],
sizeof(buf[AIRLIFT + 1]));

get_city_dialog_output_text(pcity, O_FOOD, buffer, sizeof(buffer));

for (int i = 0; i < NUM_INFO_FIELDS; i++) {
int j = 2 * i;

qlt[i]->setText(QString(buf[2 * i]));

if (j != GROWTH && j != GRANARY && j != WASTE && j != CORRUPTION
&& j != STEAL) {
qlt[i]->setToolTip("<pre>" + QString(buf[2 * i + 1]).toHtmlEscaped()
+ "</pre>");
}
m_stolen->setText(QString::asprintf(_("Not stolen")));
}

ciil->updateTooltip(0, buf[FOOD + 1]);
ciil->updateTooltip(2, buf[SHIELD + 1]);
ciil->updateTooltip(4, buf[GOLD + 1]);
ciil->updateTooltip(6, buf[SCIENCE + 1]);
ciil->updateTooltip(8, buf[TRADE + 1]);
ciil->updateTooltip(10, buf[GROWTH]);
m_airlift->setText(get_city_dialog_airlift_value(pcity));
m_airlift->setToolTip(get_city_dialog_airlift_text(pcity));
}

governor_sliders::governor_sliders(QWidget *parent) : QGroupBox(parent)
Expand Down Expand Up @@ -2173,7 +2136,7 @@ void city_dialog::update_nation_table()
*/
void city_dialog::update_info_label()
{
ui.info_wdg->update_labels(pcity, ui.info_icon_label);
ui.info_wdg->update_labels(pcity);
ui.info_icon_label->setCity(pcity);
ui.info_icon_label->updateText();
}
Expand Down
8 changes: 4 additions & 4 deletions client/citydlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class icon_list : public QListWidget {
bool oneliner;
};

#define NUM_INFO_FIELDS 15
/****************************************************************************
Custom progressbar with animated progress and right click event
****************************************************************************/
Expand Down Expand Up @@ -281,7 +280,6 @@ class cityIconInfoLabel : public QWidget {
cityIconInfoLabel(QWidget *parent = 0);
void setCity(struct city *pcity);
void updateText();
void updateTooltip(int, const QString &);

private:
void initLayout();
Expand Down Expand Up @@ -315,10 +313,12 @@ class city_info : public QWidget {

public:
city_info(QWidget *parent = 0);
void update_labels(struct city *ci_city, cityIconInfoLabel *);
void update_labels(struct city *ci_city);

private:
QLabel *qlt[NUM_INFO_FIELDS];
QLabel *m_size, *m_food, *m_production, *m_trade, *m_gold, *m_luxury,
*m_science, *m_granary, *m_growth, *m_corruption, *m_waste, *m_culture,
*m_pollution, *m_plague, *m_stolen, *m_airlift;
};

class governor_sliders : public QGroupBox {
Expand Down
Loading