diff --git a/src/plugins/coreplugin/welcomepagehelper.cpp b/src/plugins/coreplugin/welcomepagehelper.cpp index 262d8145c70..55652cb6214 100644 --- a/src/plugins/coreplugin/welcomepagehelper.cpp +++ b/src/plugins/coreplugin/welcomepagehelper.cpp @@ -150,8 +150,8 @@ QVariant ListModel::data(const QModelIndex &index, int role) const QPixmap pixmap; if (QPixmapCache::find(item->imageUrl, &pixmap)) return pixmap; - if (pixmap.isNull()) - pixmap = fetchPixmapAndUpdatePixmapCache(item->imageUrl); + if (pixmap.isNull() && m_fetchPixmapAndUpdatePixmapCache) + pixmap = m_fetchPixmapAndUpdatePixmapCache(item->imageUrl); return pixmap; } case ItemTagsRole: @@ -161,6 +161,11 @@ QVariant ListModel::data(const QModelIndex &index, int role) const } } +void ListModel::setPixmapFunction(const PixmapFunction &fetchPixmapAndUpdatePixmapCache) +{ + m_fetchPixmapAndUpdatePixmapCache = fetchPixmapAndUpdatePixmapCache; +} + ListModelFilter::ListModelFilter(ListModel *sourceModel, QObject *parent) : QSortFilterProxyModel(parent) { diff --git a/src/plugins/coreplugin/welcomepagehelper.h b/src/plugins/coreplugin/welcomepagehelper.h index 0157020eaf1..938b3bad5f3 100644 --- a/src/plugins/coreplugin/welcomepagehelper.h +++ b/src/plugins/coreplugin/welcomepagehelper.h @@ -12,6 +12,7 @@ #include #include +#include #include namespace Utils { class FancyLineEdit; } @@ -58,23 +59,22 @@ public: class CORE_EXPORT ListModel : public QAbstractListModel { public: - enum ListDataRole { - ItemRole = Qt::UserRole, - ItemImageRole, - ItemTagsRole - }; + enum ListDataRole { ItemRole = Qt::UserRole, ItemImageRole, ItemTagsRole }; + + using PixmapFunction = std::function; explicit ListModel(QObject *parent); ~ListModel() override; int rowCount(const QModelIndex &parent = QModelIndex()) const final; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - virtual QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url) const = 0; + void setPixmapFunction(const PixmapFunction &fetchPixmapAndUpdatePixmapCache); static const QSize defaultImageSize; protected: QList m_items; + PixmapFunction m_fetchPixmapAndUpdatePixmapCache; }; class CORE_EXPORT ListModelFilter : public QSortFilterProxyModel diff --git a/src/plugins/marketplace/productlistmodel.cpp b/src/plugins/marketplace/productlistmodel.cpp index bae2d59de7a..390b0c2c815 100644 --- a/src/plugins/marketplace/productlistmodel.cpp +++ b/src/plugins/marketplace/productlistmodel.cpp @@ -72,6 +72,11 @@ public: ProductListModel::ProductListModel(QObject *parent) : Core::ListModel(parent) { + setPixmapFunction([this](const QString &url) -> QPixmap { + if (auto sectionedProducts = qobject_cast(this->parent())) + sectionedProducts->queueImageForDownload(url); + return {}; + }); } void ProductListModel::appendItems(const QList &items) @@ -165,13 +170,6 @@ void SectionedProducts::updateCollections() this, [this, reply]() { onFetchCollectionsFinished(reply); }); } -QPixmap ProductListModel::fetchPixmapAndUpdatePixmapCache(const QString &url) const -{ - if (auto sectionedProducts = qobject_cast(parent())) - sectionedProducts->queueImageForDownload(url); - return QPixmap(); -} - void SectionedProducts::onFetchCollectionsFinished(QNetworkReply *reply) { QTC_ASSERT(reply, return); diff --git a/src/plugins/marketplace/productlistmodel.h b/src/plugins/marketplace/productlistmodel.h index ab50bd0a86d..5c6607d8618 100644 --- a/src/plugins/marketplace/productlistmodel.h +++ b/src/plugins/marketplace/productlistmodel.h @@ -31,9 +31,6 @@ public: void appendItems(const QList &items); const QList items() const; void updateModelIndexesForUrl(const QString &url); - -protected: - QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url) const override; }; struct Section diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp index 21261b384e3..d0c85ef241b 100644 --- a/src/plugins/qtsupport/exampleslistmodel.cpp +++ b/src/plugins/qtsupport/exampleslistmodel.cpp @@ -223,6 +223,45 @@ int ExampleSetModel::getExtraExampleSetIndex(int i) const return variant.toInt(); } +static QString resourcePath() +{ + // normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows + return Core::ICore::resourcePath().normalizedPathName().toString(); +} + +static QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url) +{ + QPixmap pixmap; + if (QPixmapCache::find(url, &pixmap)) + return pixmap; + + if (url.startsWith("qthelp://")) { + QByteArray fetchedData = Core::HelpManager::fileData(url); + if (!fetchedData.isEmpty()) { + QBuffer imgBuffer(&fetchedData); + imgBuffer.open(QIODevice::ReadOnly); + QImageReader reader(&imgBuffer, QFileInfo(url).suffix().toLatin1()); + QImage img = reader.read(); + img.convertTo(QImage::Format_RGB32); + const int dpr = qApp->devicePixelRatio(); + // boundedTo -> don't scale thumbnails up + const QSize scaledSize = Core::ListModel::defaultImageSize.boundedTo(img.size()) * dpr; + pixmap = QPixmap::fromImage( + img.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + pixmap.setDevicePixelRatio(dpr); + } + } else { + pixmap.load(url); + + if (pixmap.isNull()) + pixmap.load(resourcePath() + "/welcomescreen/widgets/" + url); + } + + QPixmapCache::insert(url, pixmap); + + return pixmap; +} + ExamplesListModel::ExamplesListModel(QObject *parent) : Core::ListModel(parent) { @@ -232,6 +271,7 @@ ExamplesListModel::ExamplesListModel(QObject *parent) &Core::HelpManager::Signals::documentationChanged, this, &ExamplesListModel::updateExamples); + setPixmapFunction(fetchPixmapAndUpdatePixmapCache); } static QString fixStringForTags(const QString &string) @@ -435,12 +475,6 @@ void ExamplesListModel::parseTutorials(QXmlStreamReader *reader, const QString & } } -static QString resourcePath() -{ - // normalize paths so QML doesn't freak out if it's wrongly capitalized on Windows - return Core::ICore::resourcePath().normalizedPathName().toString(); -} - void ExamplesListModel::updateExamples() { QString examplesInstallPath; @@ -491,39 +525,6 @@ void ExamplesListModel::updateExamples() endResetModel(); } -QPixmap ExamplesListModel::fetchPixmapAndUpdatePixmapCache(const QString &url) const -{ - QPixmap pixmap; - if (QPixmapCache::find(url, &pixmap)) - return pixmap; - - if (url.startsWith("qthelp://")) { - QByteArray fetchedData = Core::HelpManager::fileData(url); - if (!fetchedData.isEmpty()) { - QBuffer imgBuffer(&fetchedData); - imgBuffer.open(QIODevice::ReadOnly); - QImageReader reader(&imgBuffer, QFileInfo(url).suffix().toLatin1()); - QImage img = reader.read(); - img.convertTo(QImage::Format_RGB32); - const int dpr = qApp->devicePixelRatio(); - // boundedTo -> don't scale thumbnails up - const QSize scaledSize = ListModel::defaultImageSize.boundedTo(img.size()) * dpr; - pixmap = QPixmap::fromImage( - img.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); - pixmap.setDevicePixelRatio(dpr); - } - } else { - pixmap.load(url); - - if (pixmap.isNull()) - pixmap.load(resourcePath() + "/welcomescreen/widgets/" + url); - } - - QPixmapCache::insert(url, pixmap); - - return pixmap; -} - void ExampleSetModel::updateQtVersionList() { QtVersions versions = QtVersionManager::sortVersions(QtVersionManager::versions( diff --git a/src/plugins/qtsupport/exampleslistmodel.h b/src/plugins/qtsupport/exampleslistmodel.h index 7360e825922..5e5535ea550 100644 --- a/src/plugins/qtsupport/exampleslistmodel.h +++ b/src/plugins/qtsupport/exampleslistmodel.h @@ -111,8 +111,6 @@ public: QStringList exampleSets() const; ExampleSetModel *exampleSetModel() { return &m_exampleSetModel; } - QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url) const override; - signals: void selectedExampleSetChanged(int);