Plasma Engine  2.0
Loading...
Searching...
No Matches
ImageCache.moc.h
1#pragma once
2
3#include <Foundation/Communication/Event.h>
4#include <Foundation/Configuration/Singleton.h>
5#include <Foundation/Containers/Deque.h>
6#include <Foundation/Containers/Map.h>
7#include <Foundation/Containers/Set.h>
8#include <Foundation/Strings/HashedString.h>
9#include <Foundation/Threading/Mutex.h>
10#include <Foundation/Time/Time.h>
11#include <GuiFoundation/GuiFoundationDLL.h>
12#include <QAbstractItemModel>
13#include <QIcon>
14#include <QPixmap>
15#include <QString>
16
21class PL_GUIFOUNDATION_DLL plQtImageCache : public QObject
22{
23 Q_OBJECT
24
25 PL_DECLARE_SINGLETON(plQtImageCache);
26
27public:
29
31 void SetFallbackImages(const char* szLoading, const char* szUnavailable);
32
39 const QPixmap* QueryPixmap(const char* szAbsolutePath, QModelIndex index = QModelIndex(), QVariant userData1 = QVariant(),
40 QVariant userData2 = QVariant(), plUInt32* out_pImageID = nullptr);
41
44 const QPixmap* QueryPixmapForType(const char* szType, const char* szAbsolutePath, QModelIndex index = QModelIndex(),
45 QVariant userData1 = QVariant(), QVariant userData2 = QVariant(), plUInt32* out_pImageID = nullptr);
46
49 void InvalidateCache(const char* szAbsolutePath);
50
52 void SetMemoryUsageThreshold(plUInt64 uiMemoryThreshold) { m_iMemoryUsageThreshold = (plInt64)uiMemoryThreshold; }
53
55 void StopRequestProcessing(bool bPurgeExistingCache);
56
58 void EnableRequestProcessing();
59
61 void RegisterTypeImage(const char* szType, QPixmap pixmap);
62
64 const QPixmap* QueryTypeImage(const char* szType) const;
65
66Q_SIGNALS:
67 void ImageLoaded(QString sPath, QModelIndex index, QVariant userData1, QVariant userData2);
68 void ImageInvalidated(QString sPath, unsigned int uiImageID);
69
70private:
71 void EmitLoadedSignal(QString sPath, QModelIndex index, QVariant UserData1, QVariant UserData2);
72
73 void RunLoadingTask();
74 static void LoadingTask(QString sPath, QModelIndex index, QVariant UserData1, QVariant UserData2);
75 void CleanupCache();
76
77 struct Request
78 {
79 plHashedString m_sPath;
80 QModelIndex m_Index;
81 QVariant m_UserData1;
82 QVariant m_UserData2;
83
84 bool operator<(const Request& rhs) const
85 {
86 if (m_sPath < rhs.m_sPath)
87 return true;
88 if (rhs.m_sPath < m_sPath)
89 return false;
90 if (m_Index < rhs.m_Index)
91 return true;
92 if (rhs.m_Index < m_Index)
93 return false;
94
95 // not supported in Qt 5.15 anymore, but doesn't look like it's vital
96 //if (m_UserData1 < rhs.m_UserData1)
97 // return true;
98 //if (rhs.m_UserData1 < m_UserData1)
99 // return false;
100 //if (m_UserData2 < rhs.m_UserData2)
101 // return true;
102 //if (rhs.m_UserData2 < m_UserData2)
103 // return false;
104
105 return false;
106 }
107 };
108
109 mutable plMutex m_Mutex;
110 plSet<Request> m_Requests;
111 bool m_bCacheEnabled;
112 bool m_bTaskRunning;
113 plTime m_LastCleanupTime;
114 plInt64 m_iMemoryUsageThreshold;
115 plInt64 m_iCurrentMemoryUsage;
116 QPixmap* m_pImageLoading;
117 QPixmap* m_pImageUnavailable;
118 plUInt32 m_uiCurImageID;
119
120 struct CacheEntry
121 {
122 QPixmap m_Pixmap;
123 plTime m_LastAccess;
124 plUInt32 m_uiImageID;
125
126 CacheEntry() { m_uiImageID = 0xFFFFFFFF; }
127 };
128
129 plMap<QString, CacheEntry> m_ImageCache;
130 plMap<QString, QPixmap> m_TypeImages;
131};
132
133constexpr static plUInt32 plThumbnailSize = 256;
134
135PL_ALWAYS_INLINE QPixmap plSvgThumbnailToPixmap(const char* szFilePath)
136{
137 return QIcon(szFilePath).pixmap(QSize(plThumbnailSize, plThumbnailSize));
138}
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Definition Map.h:408
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
A singleton class that caches Qt images that are typically used for thumbnails.
Definition ImageCache.moc.h:22
void SetMemoryUsageThreshold(plUInt64 uiMemoryThreshold)
When this threshold is reached, images that haven't been requested in a while are being evicted from ...
Definition ImageCache.moc.h:52
Definition Set.h:238
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12