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

Rework how the units view is updated #2400

Merged
merged 5 commits into from
Oct 2, 2024
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
1 change: 1 addition & 0 deletions client/top_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ void top_bar_units_view()
return;
}
uv = reinterpret_cast<units_view *>(w);
uv->update_units();
queen()->game_tab_widget->setCurrentWidget(uv);
}
}
81 changes: 68 additions & 13 deletions client/views/view_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,25 @@ void units_view::init() { queen()->game_tab_widget->setCurrentIndex(index); }
* Refresh all widgets for units view
*/
void units_view::update_view()
{
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &units_view::update_waiting);
timer->start(1000);

update_units();
update_waiting();
}

/**
Updates the units table
*/
void units_view::update_units()
{
// used to set the height of the unit sprite
QFont f = QApplication::font();
QFontMetrics fm(f);
int h = fm.height() + 24;

ui.units_table->setRowCount(0);
ui.units_table->clearContents();

int entries_used = 0; // Position in the units array
struct unit_view_entry unit_entries[U_LAST];

Expand All @@ -152,14 +162,43 @@ void units_view::update_view()
int total_gold = 0; // Sum of gold upkeep
int total_shield = 0; // Sum of shield upkeep
int total_food = 0; // Sum of food upkeep
int max_row = 0; // Max rows in the table widget
int max_row = ui.units_table->rowCount(); // Max rows in the table widget

// Remove "footer" rows from units table
if (max_row >= 1) {
max_row--;
ui.units_table->removeRow(max_row);
}

// Keep tab of all unit types currently in the table. This is used to
// update already existing rows in the table. After updating a unit type
// the corresponding entry is removed from the map. Once all unit types,
// used by the player, have been processed the remaining unit types can be
// removed from the units table.
std::map<cid, QTableWidgetItem *> unittypes_in_table;
for (int r = 0; r < ui.units_table->rowCount(); r++) {
QTableWidgetItem *item = ui.units_table->item(r, 0);

bool ok = false;
cid key = (cid) item->data(Qt::UserRole).toInt(&ok);
fc_assert_action(ok == true, continue);
unittypes_in_table[key] = ui.units_table->item(r, 0);
}

for (int i = 0; i < entries_used; i++) {
struct unit_view_entry *pentry = unit_entries + i;
const struct unit_type *putype = pentry->type;
cid id = cid_encode_unit(putype);

ui.units_table->insertRow(max_row);
auto existing_row_for_unittype = unittypes_in_table.find(id);
bool new_row = existing_row_for_unittype == unittypes_in_table.end();
int current_row = max_row;
if (new_row) {
ui.units_table->insertRow(max_row);
} else {
current_row = existing_row_for_unittype->second->row();
}

for (int j = 0; j < 8; j++) {
QTableWidgetItem *item = new QTableWidgetItem;
switch (j) {
Expand All @@ -172,7 +211,7 @@ void units_view::update_view()
QLabel *lbl = new QLabel;
lbl->setPixmap(QPixmap(sprite));
lbl->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
ui.units_table->setCellWidget(i, j, lbl);
ui.units_table->setCellWidget(current_row, j, lbl);
}
item->setData(Qt::UserRole, id);
break;
Expand Down Expand Up @@ -234,9 +273,20 @@ void units_view::update_view()
total_gold += pentry->gold_cost;
break;
}
ui.units_table->setItem(max_row, j, item);
ui.units_table->setItem(current_row, j, item);
}

if (new_row) {
max_row++;
} else {
unittypes_in_table.erase(id);
}
max_row++;
}

// Remove rows no longer needed in the table.
for (const auto &[_, value] : unittypes_in_table) {
ui.units_table->removeRow(value->row());
max_row--;
}

// Add a "footer" to the table showing the totals.
Expand Down Expand Up @@ -292,11 +342,7 @@ void units_view::update_view()
QHeaderView::ResizeToContents);
}

QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &units_view::update_waiting);
timer->start(1000);

update_waiting();
update_buttons(ui.units_table->selectionModel()->selection());
}

/**
Expand Down Expand Up @@ -410,6 +456,15 @@ void units_view::update_waiting()
*/
void units_view::selection_changed(const QItemSelection &sl,
const QItemSelection &ds)
{
update_buttons(sl);
}

/**
Updates the buttons according to the item selection sl in the units
table.
*/
void units_view::update_buttons(const QItemSelection &sl)
{
QTableWidgetItem *itm;
QVariant qvar;
Expand Down
4 changes: 3 additions & 1 deletion client/views/view_units.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class units_view : public QWidget {
units_view();
~units_view();
void update_view();
void update_waiting();
void update_units();
void init();

private:
Expand All @@ -72,6 +72,8 @@ class units_view : public QWidget {
cid uid{0};
int counter{0};
Ui::FormUnitsView ui;
void update_waiting();
void update_buttons(const QItemSelection &sl);

private slots:
void disband_units();
Expand Down
Loading