[VDD] Defer heavy construction until after the tab paints

This commit is contained in:
Lukas Brübach 2026-08-13 22:52:42 +02:00
parent 2086deff5c
commit f9a6b5bd8d
2 changed files with 50 additions and 13 deletions

View file

@ -16,6 +16,7 @@
#include <QHeaderView>
#include <QScrollBar>
#include <QShowEvent>
#include <QStyledItemDelegate>
#include <libcockatrice/card/card_info_comparator.h>
#include <libcockatrice/card/database/card_database.h>
@ -140,9 +141,6 @@ void VisualDatabaseDisplayWidget::initialize()
{
databaseLoadIndicator->setVisible(false);
filterContainer->initialize();
filterContainer->setVisible(true);
searchContainer->addWidget(colorFilterWidget);
searchContainer->addWidget(clearFilterWidget);
searchContainer->addWidget(searchEdit);
@ -158,10 +156,31 @@ void VisualDatabaseDisplayWidget::initialize()
mainLayout->addWidget(cardSizeWidget);
databaseDisplayModel->setFilterTree(filterModel->filterTree());
connect(filterModel, &FilterTreeModel::layoutChanged, this, &VisualDatabaseDisplayWidget::onSearchModelChanged);
initializeFilters();
}
void VisualDatabaseDisplayWidget::initializeFilters()
{
if (filtersInitialized || !isVisible()) {
return;
}
filtersInitialized = true;
// The filter toolbar builds its widgets by iterating the entire card database
// (per-set, per-main-type, per-sub-type and per-format buttons). Building it
// inside showEvent would block the tab switch, so keep it hidden and defer the
// build to the next event loop turn, letting the tab paint first.
filterContainer->setVisible(false);
QTimer::singleShot(0, this, [this] {
filterContainer->initialize();
filterContainer->setVisible(true);
databaseDisplayModel->setFilterTree(filterModel->filterTree());
loadCardsTimer = new QTimer(this);
loadCardsTimer->setSingleShot(true); // Ensure it only fires once after the timeout
@ -169,6 +188,13 @@ void VisualDatabaseDisplayWidget::initialize()
loadCardsTimer->start(5000);
retranslateUi();
});
}
void VisualDatabaseDisplayWidget::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
initializeFilters();
}
void VisualDatabaseDisplayWidget::retranslateUi()
@ -292,9 +318,16 @@ void VisualDatabaseDisplayWidget::loadCurrentPage()
{
// Ensure only the initial page is loaded
if (currentPage == 0) {
// Only load the first page initially
if (!initialLoadScheduled) {
initialLoadScheduled = true;
qCDebug(VisualDatabaseDisplayLog) << "Loading the first page";
// Defer the first page so the tab switch stays responsive. The card
// grid builds one event loop turn later.
QTimer::singleShot(0, this, [this] {
initialLoadScheduled = false;
populateCards();
});
}
} else if (nearEndOfPage()) {
// If not the first page, just load the next page and append to the flow widget
loadNextPage();

View file

@ -120,12 +120,16 @@ private:
int debounceTime = 300; // in Ms
int currentPage = 0; // Current page index
int cardsPerPage = 100; // Number of cards per page
bool filtersInitialized = false;
bool initialLoadScheduled = false;
void initializeFilters();
void highlightAllSearchEdit();
bool nearEndOfPage() const;
protected:
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;
};
#endif // VISUAL_DATABASE_DISPLAY_WIDGET_H