Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourceServer< T > Class Template Reference

#include <KoResourceServer.h>

Public Types

typedef KoResourceServerObserver< T > ObserverType
 

Public Member Functions

void addObserver (ObserverType *observer)
 
bool addResource (QSharedPointer< T > resource, bool save=true)
 Adds an already loaded resource to the server.
 
QVector< KisTagSPassignedTagsList (KoResourceSP resource) const
 
QSharedPointer< T > firstResource () const
 Return the first resource available.
 
KoResourceSP importResourceFile (const QString &filename, const bool allowOverwrite)
 
 KoResourceServer (const QString &type)
 
bool reloadResource (QSharedPointer< T > resource)
 
void removeObserver (ObserverType *observer)
 
void removeResourceFile (const QString &filename)
 Removes the resource file from the resource server.
 
bool removeResourceFromServer (QSharedPointer< T > resource)
 Remove a resource from Resource Server but not from a file.
 
QSharedPointer< T > resource (const QString &md5, const QString &fileName, const QString &name)
 resource retrieves a resource. If the md5sum is not empty, the resource will only be retrieved if a resource with that md5sum exists. If it is empty, a fallback to filename or name is possible.
 
int resourceCount () const
 
KisResourceModelresourceModel () const
 
QString saveLocation ()
 Returns path where to save user defined and imported resources to.
 
bool updateResource (QSharedPointer< T > resource)
 
virtual ~KoResourceServer ()
 

Protected Member Functions

void notifyRemovingResource (QSharedPointer< T > resource)
 
void notifyResourceAdded (QSharedPointer< T > resource)
 
void notifyResourceChanged (QSharedPointer< T > resource)
 

Private Member Functions

QSharedPointer< T > resourceByFilename (const QString &filename) const
 
QSharedPointer< T > resourceByMD5 (const QString &md5) const
 
QSharedPointer< T > resourceByName (const QString &name) const
 

Private Attributes

QList< ObserverType * > m_observers
 
KisResourceModelm_resourceModel {0}
 
KisTagModelm_tagModel {0}
 
QString m_type
 

Detailed Description

template<class T>
class KoResourceServer< T >

KoResourceServer is a shim around KisResourceModel. It knows nothing by its own, and does nothing on its own. It can only be used in the gui thread.

Definition at line 75 of file KoResourceServer.h.

Member Typedef Documentation

◆ ObserverType

template<class T >
typedef KoResourceServerObserver<T> KoResourceServer< T >::ObserverType

Definition at line 79 of file KoResourceServer.h.

Constructor & Destructor Documentation

◆ KoResourceServer()

template<class T >
KoResourceServer< T >::KoResourceServer ( const QString & type)
inline

Definition at line 81 of file KoResourceServer.h.

83 , m_tagModel(new KisTagModel(type))
84 , m_type(type)
85 {
86 Q_ASSERT(!type.isEmpty());
87 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
88 if (QThread::currentThread() != qApp->thread()) {
89 qDebug().noquote() << kisBacktrace();
90 }
91
92 }
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
KisTagModel * m_tagModel
KisResourceModel * m_resourceModel
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
QString kisBacktrace()
Definition kis_debug.cpp:51

References KIS_SAFE_ASSERT_RECOVER_NOOP, and kisBacktrace().

◆ ~KoResourceServer()

template<class T >
virtual KoResourceServer< T >::~KoResourceServer ( )
inlinevirtual

Definition at line 94 of file KoResourceServer.h.

95 {
96 delete m_resourceModel;
97 delete m_tagModel;
98 Q_FOREACH (ObserverType* observer, m_observers) {
99 observer->unsetResourceServer();
100 }
101 }
KoResourceServerObserver< T > ObserverType
QList< ObserverType * > m_observers

References KoResourceServer< T >::m_observers, KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::m_tagModel, and KoResourceServerObserver< T >::unsetResourceServer().

Member Function Documentation

◆ addObserver()

template<class T >
void KoResourceServer< T >::addObserver ( ObserverType * observer)
inline

Adds an observer to the server

Parameters
observerthe observer to be added
notifyLoadedResourcesdetermines if the observer should be notified about the already loaded resources

Definition at line 208 of file KoResourceServer.h.

209 {
210 if (observer && !m_observers.contains(observer)) {
211 m_observers.append(observer);
212 }
213 }

References KoResourceServer< T >::m_observers.

◆ addResource()

template<class T >
bool KoResourceServer< T >::addResource ( QSharedPointer< T > resource,
bool save = true )
inline

Adds an already loaded resource to the server.

Definition at line 133 of file KoResourceServer.h.

133 {
134
135 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
136 if (QThread::currentThread() != qApp->thread()) {
137 qDebug().noquote() << kisBacktrace();
138 }
139
140 if (!resource || !resource->valid()) {
141 warnResource << "Tried to add an invalid resource!" << resource;
142 return false;
143 }
144
145 if (m_resourceModel->addResource(resource, save ? QString() : "memory")) {
147 return true;
148 }
149
150 return false;
151 }
#define warnResource
bool addResource(KoResourceSP resource, const QString &storageId=QString("")) override
addResource adds the given resource to the database and storage. If the resource already exists in th...
void notifyResourceAdded(QSharedPointer< T > resource)
QSharedPointer< T > resource(const QString &md5, const QString &fileName, const QString &name)
resource retrieves a resource. If the md5sum is not empty, the resource will only be retrieved if a r...

References KisResourceModel::addResource(), KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::notifyResourceAdded(), KoResourceServer< T >::resource(), and warnResource.

◆ assignedTagsList()

template<class T >
QVector< KisTagSP > KoResourceServer< T >::assignedTagsList ( KoResourceSP resource) const
inline

Definition at line 336 of file KoResourceServer.h.

337 {
338 if (resource.isNull()) {
339 return QVector<KisTagSP>();
340 }
341 return m_resourceModel->tagsForResource(resource->resourceId());
342 }
QVector< KisTagSP > tagsForResource(int resourceId) const

References KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::resource(), and KisResourceModel::tagsForResource().

◆ firstResource()

template<class T >
QSharedPointer< T > KoResourceServer< T >::firstResource ( ) const
inline

Return the first resource available.

Definition at line 115 of file KoResourceServer.h.

116 {
117
118 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
119 if (QThread::currentThread() != qApp->thread()) {
120 qDebug().noquote() << kisBacktrace();
121 }
122
123 return m_resourceModel->resourceForIndex(m_resourceModel->index(0, 0)).dynamicCast<T>();
124 }
KoResourceSP resourceForIndex(QModelIndex index=QModelIndex()) const override
resourceForIndex returns a properly versioned and id'ed resource object

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, and KisResourceModel::resourceForIndex().

◆ importResourceFile()

template<class T >
KoResourceSP KoResourceServer< T >::importResourceFile ( const QString & filename,
const bool allowOverwrite )
inline

Creates a new resource from a given file and adds them to the resource server The base implementation does only load one resource per file, override to implement collections

Parameters
filenamefile name of the resource file to be imported
fileCreationdecides whether to create the file in the saveLocation() directory

Definition at line 179 of file KoResourceServer.h.

180 {
181
182 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
183 if (QThread::currentThread() != qApp->thread()) {
184 qDebug().noquote() << kisBacktrace();
185 }
186
187 return m_resourceModel->importResourceFile(filename, allowOverwrite);
188 }
KoResourceSP importResourceFile(const QString &filename, const bool allowOverwrite, const QString &storageId=QString("")) override
importResourceFile

References KisResourceModel::importResourceFile(), KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), and KoResourceServer< T >::m_resourceModel.

◆ notifyRemovingResource()

template<class T >
void KoResourceServer< T >::notifyRemovingResource ( QSharedPointer< T > resource)
inlineprotected

Definition at line 353 of file KoResourceServer.h.

354 {
355 Q_FOREACH (ObserverType* observer, m_observers) {
356 observer->removingResource(resource);
357 }
358 }

References KoResourceServer< T >::m_observers, KoResourceServerObserver< T >::removingResource(), and KoResourceServer< T >::resource().

◆ notifyResourceAdded()

template<class T >
void KoResourceServer< T >::notifyResourceAdded ( QSharedPointer< T > resource)
inlineprotected

Definition at line 346 of file KoResourceServer.h.

347 {
348 Q_FOREACH (ObserverType* observer, m_observers) {
349 observer->resourceAdded(resource);
350 }
351 }

References KoResourceServer< T >::m_observers, KoResourceServer< T >::resource(), and KoResourceServerObserver< T >::resourceAdded().

◆ notifyResourceChanged()

template<class T >
void KoResourceServer< T >::notifyResourceChanged ( QSharedPointer< T > resource)
inlineprotected

Definition at line 360 of file KoResourceServer.h.

361 {
362 Q_FOREACH (ObserverType* observer, m_observers) {
363 observer->resourceChanged(resource);
364 }
365 }

References KoResourceServer< T >::m_observers, KoResourceServer< T >::resource(), and KoResourceServerObserver< T >::resourceChanged().

◆ reloadResource()

template<class T >
bool KoResourceServer< T >::reloadResource ( QSharedPointer< T > resource)
inline

Reloads the resource from the persistent storage

Definition at line 324 of file KoResourceServer.h.

325 {
326 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
327 if (QThread::currentThread() != qApp->thread()) {
328 qDebug().noquote() << kisBacktrace();
329 }
332
333 return result;
334 }
bool reloadResource(KoResourceSP resource) override
reloadResource
void notifyResourceChanged(QSharedPointer< T > resource)

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::notifyResourceChanged(), KisResourceModel::reloadResource(), and KoResourceServer< T >::resource().

◆ removeObserver()

template<class T >
void KoResourceServer< T >::removeObserver ( ObserverType * observer)
inline

Removes an observer from the server

Parameters
observerthe observer to be removed

Definition at line 219 of file KoResourceServer.h.

220 {
221 int index = m_observers.indexOf(observer);
222 if (index < 0) {
223 return;
224 }
225 m_observers.removeAt( index );
226 }

References KoResourceServer< T >::m_observers.

◆ removeResourceFile()

template<class T >
void KoResourceServer< T >::removeResourceFile ( const QString & filename)
inline

Removes the resource file from the resource server.

Definition at line 191 of file KoResourceServer.h.

192 {
193 QFileInfo fi(filename);
194
196 if (!resource) {
197 warnResource << "Resource file do not exist ";
198 return;
199 }
201 }
bool removeResourceFromServer(QSharedPointer< T > resource)
Remove a resource from Resource Server but not from a file.
QSharedPointer< T > resourceByFilename(const QString &filename) const

References KoResourceServer< T >::removeResourceFromServer(), KoResourceServer< T >::resource(), KoResourceServer< T >::resourceByFilename(), and warnResource.

◆ removeResourceFromServer()

template<class T >
bool KoResourceServer< T >::removeResourceFromServer ( QSharedPointer< T > resource)
inline

Remove a resource from Resource Server but not from a file.

Definition at line 154 of file KoResourceServer.h.

154 {
155
156 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
157 if (QThread::currentThread() != qApp->thread()) {
158 qDebug().noquote() << kisBacktrace();
159 }
160
163 return true;
164 }
165 return false;
166 }
bool setResourceInactive(const QModelIndex &index)
QModelIndex indexForResource(KoResourceSP resource) const override
indexFromResource
void notifyRemovingResource(QSharedPointer< T > resource)

References KisResourceModel::indexForResource(), KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::notifyRemovingResource(), KoResourceServer< T >::resource(), and KisAbstractResourceModel::setResourceInactive().

◆ resource()

template<class T >
QSharedPointer< T > KoResourceServer< T >::resource ( const QString & md5,
const QString & fileName,
const QString & name )
inline

resource retrieves a resource. If the md5sum is not empty, the resource will only be retrieved if a resource with that md5sum exists. If it is empty, a fallback to filename or name is possible.

Parameters
md5This is the hex-encoded md5sum as stored in e.g. configuration objects
fileNameA filename without the path
nameThe name of the resource
Returns
a resource, or nullptr

Definition at line 301 of file KoResourceServer.h.

302 {
303 return KisGlobalResourcesInterface::instance()->source<T>(m_type).bestMatch(md5, fileName, name);
304 }
static KisResourcesInterfaceSP instance()

References KisGlobalResourcesInterface::instance(), and KoResourceServer< T >::m_type.

◆ resourceByFilename()

template<class T >
QSharedPointer< T > KoResourceServer< T >::resourceByFilename ( const QString & filename) const
inlineprivate

Definition at line 230 of file KoResourceServer.h.

231 {
232 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
233 if (QThread::currentThread() != qApp->thread()) {
234 qDebug().noquote() << kisBacktrace();
235 }
236
237
238 if (filename.isEmpty() || filename.isNull()) {
239 return nullptr;
240 }
242
243 if (resources.size() > 0) {
244 return resources.first().dynamicCast<T>();
245 }
246
247 return nullptr;
248 }
QVector< KoResourceSP > resourcesForFilename(QString fileName) const

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, and KisResourceModel::resourcesForFilename().

◆ resourceByMD5()

template<class T >
QSharedPointer< T > KoResourceServer< T >::resourceByMD5 ( const QString & md5) const
inlineprivate

Definition at line 272 of file KoResourceServer.h.

273 {
274 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
275 if (QThread::currentThread() != qApp->thread()) {
276 qDebug().noquote() << kisBacktrace();
277 }
278 if (md5.isEmpty() || md5.isNull()) {
279 return nullptr;
280 }
282
283 if (resources.size() > 0) {
284 return resources.first().dynamicCast<T>();
285 }
286
287 return nullptr;
288 }
QVector< KoResourceSP > resourcesForMD5(const QString md5sum) const

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, and KisResourceModel::resourcesForMD5().

◆ resourceByName()

template<class T >
QSharedPointer< T > KoResourceServer< T >::resourceByName ( const QString & name) const
inlineprivate

Definition at line 251 of file KoResourceServer.h.

252 {
253 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
254 if (QThread::currentThread() != qApp->thread()) {
255 qDebug().noquote() << kisBacktrace();
256 }
257
258 if (name.isEmpty() || name.isNull()) {
259 return nullptr;
260 }
261
263
264 if (resources.size() > 0) {
265 return resources.first().dynamicCast<T>();
266 }
267
268 return nullptr;
269
270 }
QVector< KoResourceSP > resourcesForName(QString name) const
const char * name(StandardAction id)

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, and KisResourceModel::resourcesForName().

◆ resourceCount()

template<class T >
int KoResourceServer< T >::resourceCount ( ) const
inline

Definition at line 126 of file KoResourceServer.h.

126 {
127
128 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
129 return m_resourceModel->rowCount();
130 }

References KIS_SAFE_ASSERT_RECOVER_NOOP, and KoResourceServer< T >::m_resourceModel.

◆ resourceModel()

template<class T >
KisResourceModel * KoResourceServer< T >::resourceModel ( ) const
inline
Returns
the active resource model

Definition at line 104 of file KoResourceServer.h.

105 {
106 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
107 if (QThread::currentThread() != qApp->thread()) {
108 qDebug().noquote() << kisBacktrace();
109 }
110
111 return m_resourceModel;
112 }

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), and KoResourceServer< T >::m_resourceModel.

◆ saveLocation()

template<class T >
QString KoResourceServer< T >::saveLocation ( )
inline

Returns path where to save user defined and imported resources to.

Definition at line 169 of file KoResourceServer.h.

169 {
171 }
QString resourceLocationBase() const
resourceLocationBase is the place where all resource storages (folder, bundles etc....
static KisResourceLocator * instance()

References KisResourceLocator::instance(), KoResourceServer< T >::m_type, and KisResourceLocator::resourceLocationBase().

◆ updateResource()

template<class T >
bool KoResourceServer< T >::updateResource ( QSharedPointer< T > resource)
inline

Call after changing the content of a resource and saving it; Notifies the connected views.

Definition at line 310 of file KoResourceServer.h.

311 {
312 KIS_SAFE_ASSERT_RECOVER_NOOP(QThread::currentThread() == qApp->thread());
313 if (QThread::currentThread() != qApp->thread()) {
314 qDebug().noquote() << kisBacktrace();
315 }
318 return result;
319 }
bool updateResource(KoResourceSP resource) override
updateResource creates a new version of the resource in the storage and in the database....

References KIS_SAFE_ASSERT_RECOVER_NOOP, kisBacktrace(), KoResourceServer< T >::m_resourceModel, KoResourceServer< T >::notifyResourceChanged(), KoResourceServer< T >::resource(), and KisResourceModel::updateResource().

Member Data Documentation

◆ m_observers

template<class T >
QList<ObserverType*> KoResourceServer< T >::m_observers
private

Definition at line 369 of file KoResourceServer.h.

◆ m_resourceModel

template<class T >
KisResourceModel* KoResourceServer< T >::m_resourceModel {0}
private

Definition at line 370 of file KoResourceServer.h.

370{0};

◆ m_tagModel

template<class T >
KisTagModel* KoResourceServer< T >::m_tagModel {0}
private

Definition at line 371 of file KoResourceServer.h.

371{0};

◆ m_type

template<class T >
QString KoResourceServer< T >::m_type
private

Definition at line 372 of file KoResourceServer.h.


The documentation for this class was generated from the following file: