Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/backgroundlistmodel.cpp
Show All 33 Lines | |||||
34 | #include <QMimeDatabase> | 34 | #include <QMimeDatabase> | ||
35 | #include <QMimeType> | 35 | #include <QMimeType> | ||
36 | #include <QMutex> | 36 | #include <QMutex> | ||
37 | #include <QMutexLocker> | 37 | #include <QMutexLocker> | ||
38 | 38 | | |||
39 | #include <QDebug> | 39 | #include <QDebug> | ||
40 | #include <KIO/PreviewJob> | 40 | #include <KIO/PreviewJob> | ||
41 | #include <KLocalizedString> | 41 | #include <KLocalizedString> | ||
42 | #include <kimagecache.h> | | |||
43 | #include <kaboutdata.h> | 42 | #include <kaboutdata.h> | ||
44 | 43 | | |||
45 | #include <KPackage/Package> | 44 | #include <KPackage/Package> | ||
46 | #include <KPackage/PackageStructure> | 45 | #include <KPackage/PackageStructure> | ||
47 | #include <KPackage/PackageLoader> | 46 | #include <KPackage/PackageLoader> | ||
48 | 47 | | |||
49 | #include <KIO/OpenFileManagerWindowJob> | 48 | #include <KIO/OpenFileManagerWindowJob> | ||
50 | 49 | | |||
Show All 14 Lines | 62 | { | |||
65 | Q_EMIT sizeFound(m_path, reader.size()); | 64 | Q_EMIT sizeFound(m_path, reader.size()); | ||
66 | } | 65 | } | ||
67 | 66 | | |||
68 | 67 | | |||
69 | BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent) | 68 | BackgroundListModel::BackgroundListModel(Image *wallpaper, QObject *parent) | ||
70 | : QAbstractListModel(parent), | 69 | : QAbstractListModel(parent), | ||
71 | m_wallpaper(wallpaper) | 70 | m_wallpaper(wallpaper) | ||
72 | { | 71 | { | ||
72 | m_imageCache.setMaxCost(10 * 1024 * 1024); // 10 MiB | ||||
73 | | ||||
73 | connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground); | 74 | connect(&m_dirwatch, &KDirWatch::deleted, this, &BackgroundListModel::removeBackground); | ||
74 | 75 | | |||
75 | //TODO: on Qt 4.4 use the ui scale factor | 76 | //TODO: on Qt 4.4 use the ui scale factor | ||
76 | QFontMetrics fm(QGuiApplication::font()); | 77 | QFontMetrics fm(QGuiApplication::font()); | ||
77 | m_screenshotSize = fm.width('M') * 15; | 78 | m_screenshotSize = fm.width('M') * 15; | ||
78 | | ||||
79 | m_imageCache = new KImageCache(QStringLiteral("plasma_wallpaper_preview"), 10485760); | | |||
80 | } | 79 | } | ||
81 | 80 | | |||
82 | BackgroundListModel::~BackgroundListModel() | 81 | BackgroundListModel::~BackgroundListModel() = default; | ||
83 | { | | |||
84 | delete m_imageCache; | | |||
85 | } | | |||
86 | 82 | | |||
87 | QHash<int, QByteArray> BackgroundListModel::BackgroundListModel::roleNames() const | 83 | QHash<int, QByteArray> BackgroundListModel::BackgroundListModel::roleNames() const | ||
88 | { | 84 | { | ||
89 | return { | 85 | return { | ||
90 | { Qt::DisplayRole, "display" }, | 86 | { Qt::DisplayRole, "display" }, | ||
91 | { Qt::DecorationRole, "decoration" }, | 87 | { Qt::DecorationRole, "decoration" }, | ||
92 | { AuthorRole, "author" }, | 88 | { AuthorRole, "author" }, | ||
93 | { ScreenshotRole, "screenshot" }, | 89 | { ScreenshotRole, "screenshot" }, | ||
▲ Show 20 Lines • Show All 244 Lines • ▼ Show 20 Line(s) | 331 | case Qt::DisplayRole: { | |||
338 | if (title.isEmpty()) { | 334 | if (title.isEmpty()) { | ||
339 | return QFileInfo(b.filePath("preferred")).completeBaseName(); | 335 | return QFileInfo(b.filePath("preferred")).completeBaseName(); | ||
340 | } | 336 | } | ||
341 | 337 | | |||
342 | return title; | 338 | return title; | ||
343 | } | 339 | } | ||
344 | 340 | | |||
345 | case ScreenshotRole: { | 341 | case ScreenshotRole: { | ||
346 | QPixmap preview = QPixmap(QSize(m_screenshotSize*1.6, | 342 | const QString path = b.filePath("preferred"); | ||
347 | m_screenshotSize)); | 343 | | ||
348 | if (m_imageCache->findPixmap(b.filePath("preferred"), &preview)) { | 344 | QPixmap *cachedPreview = m_imageCache.object(path); | ||
349 | return preview; | 345 | if (cachedPreview) { | ||
350 | } | 346 | return *cachedPreview; | ||
351 | // qCDebug(IMAGEWALLPAPER) << "WP preferred: " << b.filePath("preferred"); | 347 | } | ||
352 | // qCDebug(IMAGEWALLPAPER) << "WP screenshot: " << b.filePath("screenshot"); | 348 | | ||
353 | QUrl file = QUrl::fromLocalFile(b.filePath("preferred")); | 349 | const QUrl file = QUrl::fromLocalFile(path); | ||
354 | if (!m_previewJobs.contains(file) && file.isValid()) { | 350 | if (!m_previewJobs.contains(file) && file.isValid()) { | ||
355 | 351 | | |||
356 | KFileItemList list; | 352 | KFileItemList list; | ||
357 | list.append(KFileItem(file, QString(), 0)); | 353 | list.append(KFileItem(file, QString(), 0)); | ||
358 | QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); | 354 | QStringList availablePlugins = KIO::PreviewJob::availablePlugins(); | ||
359 | KIO::PreviewJob* job = KIO::filePreview(list, | 355 | KIO::PreviewJob* job = KIO::filePreview(list, | ||
360 | QSize(m_screenshotSize*1.6, | 356 | QSize(m_screenshotSize*1.6, | ||
361 | m_screenshotSize), &availablePlugins); | 357 | m_screenshotSize), &availablePlugins); | ||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Line(s) | 441 | if (!index.isValid()) { | |||
446 | return; | 442 | return; | ||
447 | } | 443 | } | ||
448 | 444 | | |||
449 | KPackage::Package b = package(index.row()); | 445 | KPackage::Package b = package(index.row()); | ||
450 | if (!b.isValid()) { | 446 | if (!b.isValid()) { | ||
451 | return; | 447 | return; | ||
452 | } | 448 | } | ||
453 | 449 | | |||
454 | m_imageCache->insertPixmap(b.filePath("preferred"), preview); | 450 | const int cost = preview.width() * preview.height() * preview.depth() / 8; | ||
451 | m_imageCache.insert(b.filePath("preferred"), new QPixmap(preview), cost); | ||||
452 | | ||||
455 | //qCDebug(IMAGEWALLPAPER) << "WP preview size:" << preview.size(); | 453 | //qCDebug(IMAGEWALLPAPER) << "WP preview size:" << preview.size(); | ||
456 | emit dataChanged(index, index); | 454 | emit dataChanged(index, index); | ||
457 | } | 455 | } | ||
458 | 456 | | |||
459 | void BackgroundListModel::previewFailed(const KFileItem &item) | 457 | void BackgroundListModel::previewFailed(const KFileItem &item) | ||
460 | { | 458 | { | ||
461 | m_previewJobs.remove(item.url()); | 459 | m_previewJobs.remove(item.url()); | ||
462 | } | 460 | } | ||
▲ Show 20 Lines • Show All 130 Lines • Show Last 20 Lines |