mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-16 20:17:45 -07:00
Convert queue to while loop.
Took 19 seconds
This commit is contained in:
parent
bba5b775ec
commit
5c021f0aad
1 changed files with 53 additions and 50 deletions
|
|
@ -79,65 +79,68 @@ void UserCardArtProvider::processQueue()
|
|||
return;
|
||||
}
|
||||
|
||||
if (queue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
while (!queue.isEmpty()) {
|
||||
const QString key = queue.dequeue();
|
||||
|
||||
const QString key = queue.dequeue();
|
||||
|
||||
const QStringList parts = key.split(u'|');
|
||||
if (parts.size() != 2) {
|
||||
pending.remove(key);
|
||||
processQueue();
|
||||
return;
|
||||
}
|
||||
|
||||
const QString userName = parts.at(0);
|
||||
const QString cardName = parts.at(1);
|
||||
|
||||
ExactCard card = CardDatabaseManager::query()->getCard({cardName});
|
||||
|
||||
if (!card) {
|
||||
pending.remove(key);
|
||||
processQueue();
|
||||
return;
|
||||
}
|
||||
|
||||
QPixmap fullRes;
|
||||
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
|
||||
|
||||
// If already available, store immediately
|
||||
if (!fullRes.isNull()) {
|
||||
insertIntoCache(key, cropCardArt(fullRes));
|
||||
pending.remove(key);
|
||||
|
||||
emit cardArtUpdated(userName);
|
||||
processQueue();
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise wait for async load
|
||||
QPointer<UserCardArtProvider> self(this);
|
||||
|
||||
connect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, this, [self, key, userName, card]() mutable {
|
||||
if (!self) {
|
||||
return;
|
||||
const QStringList parts = key.split(u'|');
|
||||
if (parts.size() != 2) {
|
||||
pending.remove(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
disconnect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, self, nullptr);
|
||||
const QString userName = parts.at(0);
|
||||
const QString cardName = parts.at(1);
|
||||
|
||||
ExactCard card = CardDatabaseManager::query()->getCard({cardName});
|
||||
|
||||
if (!card) {
|
||||
pending.remove(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
QPixmap fullRes;
|
||||
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
|
||||
|
||||
// Synchronous hit (already loaded/on disk)
|
||||
if (!fullRes.isNull()) {
|
||||
self->insertIntoCache(key, self->cropCardArt(fullRes));
|
||||
} else {
|
||||
self->insertIntoCache(key, QPixmap());
|
||||
insertIntoCache(key, cropCardArt(fullRes));
|
||||
pending.remove(key);
|
||||
|
||||
emit cardArtUpdated(userName);
|
||||
continue;
|
||||
}
|
||||
|
||||
self->pending.remove(key);
|
||||
// Async load required.
|
||||
QPointer<UserCardArtProvider> self(this);
|
||||
|
||||
emit self->cardArtUpdated(userName);
|
||||
self->processQueue();
|
||||
});
|
||||
auto conn = std::make_shared<QMetaObject::Connection>();
|
||||
|
||||
*conn = connect(card.getCardPtr().data(), &CardInfo::pixmapUpdated, this,
|
||||
[self, key, userName, card, conn]() mutable {
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
|
||||
QObject::disconnect(*conn);
|
||||
|
||||
QPixmap fullRes;
|
||||
CardPictureLoader::getPixmap(fullRes, card, QSize(745, 1040));
|
||||
|
||||
if (!fullRes.isNull()) {
|
||||
self->insertIntoCache(key, self->cropCardArt(fullRes));
|
||||
} else {
|
||||
self->insertIntoCache(key, QPixmap());
|
||||
}
|
||||
|
||||
self->pending.remove(key);
|
||||
|
||||
emit self->cardArtUpdated(userName);
|
||||
|
||||
// Resume processing remaining queued items.
|
||||
self->processQueue();
|
||||
});
|
||||
|
||||
// Stop here. We'll continue when the async load finishes.
|
||||
return;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue