From 9d6543ea4035bd174d13134f35538fcb685615d0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 5 Jun 2023 15:49:57 +0200 Subject: [PATCH] QPixmapCache: DRY insert() Implement insert(QString, QPixmap) in terms of insert(QPixmap) to avoid duplicating code and to separate concerns: insert(QString, QPixmap) is now only dealing with the cacheKeys, insert(QPixmap) is only concerned with the Key-based lookup. Task-number: QTBUG-112200 Change-Id: I30394da43a5e93b7bd41ef9ce9c7aea044523c30 Reviewed-by: Volker Hilsheimer (cherry picked from commit aa8e8e94b99dfff8613bcbcc8ac6de5d0d8bcb12) Reviewed-by: Qt Cherry-pick Bot --- src/gui/image/qpixmapcache.cpp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index 6c62bae69e1..6cc5fd28a3c 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -316,25 +316,16 @@ QPixmap *QPMCache::object(const QPixmapCache::Key &key) const bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) { - QPixmapCache::Key &cacheKey = cacheKeys[key]; //If for the same key we add already a pixmap we should delete it - if (cacheKey.d) - QCache::remove(cacheKey); + remove(key); - //we create a new key the old one has been removed - cacheKey = createKey(); - - bool success = QCache::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); - if (success) { - if (!theid) { - theid = startTimer(flush_time); - t = false; - } - } else { - //Insertion failed we released the new allocated key - cacheKeys.remove(key); + // this will create a new key; the old one has been removed + auto k = insert(pixmap, cost); + if (k.isValid()) { + cacheKeys[key] = std::move(k); + return true; } - return success; + return false; } QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost)