aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qtsupport/exampleslistmodel.cpp
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2023-01-12 16:10:09 +0100
committerEike Ziller <eike.ziller@qt.io>2023-01-18 08:42:16 +0000
commitaff6c4b2d217b467d8cc213469d828e58c8f9a11 (patch)
treeb5b8ffd39a8f6504a3091d88cf2c72ae6c07e2ce /src/plugins/qtsupport/exampleslistmodel.cpp
parentab9935af73298399c9a804d0c74c4444c5f47691 (diff)
Welcome: Remove requirement for subclassing ListModel
By using aggregation for the pixmap fetching. Change-Id: I6647cc6fa7995581c692050af32e41ba9aa86491 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/plugins/qtsupport/exampleslistmodel.cpp')
-rw-r--r--src/plugins/qtsupport/exampleslistmodel.cpp79
1 files changed, 40 insertions, 39 deletions
diff --git a/src/plugins/qtsupport/exampleslistmodel.cpp b/src/plugins/qtsupport/exampleslistmodel.cpp
index 21261b384e..d0c85ef241 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(