Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResource Class Referenceabstract

#include <KoResource.h>

+ Inheritance diagram for KoResource:

Public Member Functions

bool active () const
 
void addMetaData (QString key, QVariant value)
 store the given key, value pair in the resource
 
virtual void clearSideLoadedResources ()
 
virtual KoResourceSP clone () const =0
 
virtual QString defaultFileExtension () const
 
virtual QList< KoResourceLoadResultembeddedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
QString filename () const
 
QImage image () const
 
bool isDirty () const
 
virtual bool isEphemeral () const
 
virtual bool isSerializable () const
 
 KoResource ()
 
 KoResource (const KoResource &rhs)
 
 KoResource (const QString &filename)
 
virtual QList< KoResourceLoadResultlinkedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
bool load (KisResourcesInterfaceSP resourcesInterface)
 
virtual bool loadFromDevice (QIODevice *dev, KisResourcesInterfaceSP resourcesInterface)=0
 
QString md5Sum (bool generateIfEmpty=true) const
 
QMap< QString, QVariant > metadata () const
 get a map with all the metadata
 
virtual QString name () const
 
KoResourceoperator= (const KoResource &rhs)=delete
 
bool permanent () const
 
virtual QList< int > requiredCanvasResources () const
 
QList< KoResourceLoadResultrequiredResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
int resourceId () const
 
virtual QPair< QString, QString > resourceType () const =0
 
bool save ()
 
virtual bool saveToDevice (QIODevice *dev) const
 
void setActive (bool active)
 
void setDirty (bool value)
 Mark the preset as modified but not saved.
 
void setFilename (const QString &filename)
 
void setImage (const QImage &image)
 
void setMD5Sum (const QString &md5sum)
 Set the md5sum of this resource. It must be in hex-encoded string format.
 
void setName (const QString &name)
 
void setPermanent (bool permanent)
 
void setResourceId (int id)
 
void setStorageLocation (const QString &location)
 
void setValid (bool valid)
 
void setVersion (int version)
 
virtual QList< KoResourceLoadResultsideLoadedResources (KisResourcesInterfaceSP globalResourcesInterface) const
 
KoResourceSignature signature () const
 
QString storageLocation () const
 
QList< KoResourceLoadResulttakeSideLoadedResources (KisResourcesInterfaceSP globalResourcesInterface)
 
virtual QImage thumbnail () const
 thumbnail the thumbnail image to use in resource selectors
 
virtual QString thumbnailPath () const
 thumbnailPath returns the path to a separate thumbnail image, outside the actual resource file itself. If the path is relative, it is supposed start in the same location as the resource itself. If it's absolute, that is, it starts with "/", it is from the root of the storage.
 
virtual void updateThumbnail ()
 updateThumbnail updates the thumbnail for this resource. Reimplement if your thumbnail is something else than the image set with setImage.
 
bool valid () const
 
int version () const
 Get the version of the resource.
 
virtual ~KoResource ()
 
- Public Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Public Attributes

bool active {true}
 
QString filename
 
QImage image
 
QString md5sum
 
QMap< QString, QVariant > metadata
 
bool modified {false}
 
QString name
 
bool permanent {false}
 
int resourceId {-1}
 
QString storageLocation
 
bool valid {false}
 
int version {-1}
 
- Public Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Private Attributes

Private *const d
 

Detailed Description

The KoResource class provides a representation of resources. This includes, but not limited to, brushes and patterns.

A resource knows its filename, but not the location where it's stored. A new version of a resource is stored with an updated filename, the old version isn't renamed.

Definition at line 27 of file KoResource.cpp.

Constructor & Destructor Documentation

◆ KoResource() [1/3]

KoResource::KoResource ( )

Creates a new KoResource object using filename. No file is opened in the constructor, you have to call load.

Parameters
filenamethe file name to save and load from.

Definition at line 42 of file KoResource.cpp.

43 : d(new Private)
44{
45}
Private *const d
Definition KoResource.h:274

◆ KoResource() [2/3]

KoResource::KoResource ( const QString & filename)
explicit

Definition at line 47 of file KoResource.cpp.

48 : d(new Private)
49{
50 d->filename = filename;
51 d->name = QFileInfo(filename).fileName();
52}
QString filename

References d, and filename.

◆ ~KoResource()

KoResource::~KoResource ( )
virtual

Definition at line 54 of file KoResource.cpp.

55{
56 delete d;
57}

References d.

◆ KoResource() [3/3]

KoResource::KoResource ( const KoResource & rhs)

Definition at line 59 of file KoResource.cpp.

60 : d(new Private(*rhs.d))
61{
62}

Member Function Documentation

◆ active()

bool KoResource::active ( ) const
Returns
true if the resource is marked as active in the database (inactive means "deleted")

◆ addMetaData()

void KoResource::addMetaData ( QString key,
QVariant value )

store the given key, value pair in the resource

It is responsibility of the resource itself to load all the necessary metadata right on loading in loadFromDevice(). The resource locator will not try to load this information from the database. Database only caches metadata.

To make sure that metadata is correctly populated in the database, it should be set up right in loadFromDevice().

Definition at line 289 of file KoResource.cpp.

290{
300 d->metadata.insert(key, value);
301}
float value(const T *src, size_t ch)

References d, and value().

◆ clearSideLoadedResources()

void KoResource::clearSideLoadedResources ( )
virtual

Clears memory under side-loaded resources. The method is called by KisResourceLocator after the resources have been fetched and added to the global store.

Reimplemented in KisPaintOpPreset.

Definition at line 265 of file KoResource.cpp.

266{
267}

◆ clone()

◆ defaultFileExtension()

QString KoResource::defaultFileExtension ( ) const
virtual
Returns
the default file extension which should be used when saving the resource

Reimplemented in KisAbrBrush, KisGbrBrush, KisImagePipeBrush, KisPngBrush, KisSvgBrush, KisSeExprScript, KoCssStylePreset, KoGamutMask, KoSvgSymbolCollectionResource, KisPaintOpPreset, KoColorSet, KoPattern, KoSegmentGradient, KoStopGradient, KisWorkspaceResource, KisSessionResource, KisWindowLayoutResource, and TasksetResource.

Definition at line 212 of file KoResource.cpp.

213{
214 return QString();
215}

◆ embeddedResources()

QList< KoResourceLoadResult > KoResource::embeddedResources ( KisResourcesInterfaceSP globalResourcesInterface) const
virtual
Returns
all the resources that were embedded into (*this) resource. If the resources were already added to the global database, then they are fetched from globalResourcesInterface to save time/memory.

These resources are embedded into the resource itself and are available throughout the entire lifetime of the resource.

Reimplemented in KisPaintOpPreset, and KisPSDLayerStyle.

Definition at line 246 of file KoResource.cpp.

247{
248 Q_UNUSED(globalResourcesInterface);
249 return {};
250}

◆ filename()

QString KoResource::filename ( ) const
Returns
the filename of this resource within the container (folder, bundle, ...)

◆ image()

QImage KoResource::image ( ) const
Returns
a QImage image representing this resource: in the case of some resources, it is the actual resource.

This image could be null. The image can be in any valid format.

◆ isDirty()

bool KoResource::isDirty ( ) const
Returns
true if the preset has been modified, but not saved

Definition at line 284 of file KoResource.cpp.

285{
286 return d->modified;
287}

References d.

◆ isEphemeral()

bool KoResource::isEphemeral ( ) const
virtual

Ephemeral resource is a type of resource that has no physical representation on disk. Therefore, its load()/save() calls do nothing. Such resources will always have empty md5Sum() and, therefore, will never be stored in the resources database.

This type of resources is created directly by the corresponding factory or other object (e.g. KisAutoBrushFactory).

Ephemeral resource cannot be serializable.

Reimplemented in KisAutoBrush, and KisTextBrush.

Definition at line 328 of file KoResource.cpp.

329{
330 return false;
331}

◆ isSerializable()

bool KoResource::isSerializable ( ) const
virtual

Serializable resource is the one which can be saved/loaded into a specific storage via saveToDevice()/loadFromDevice() methods. Some resources, like KisAbrBrush or KisPsdLayerStyle, are stored in specific libraries in "batches". Such resources cannot be saved individually. They are created by the corresponding factories.

In contrast to ephemeral resource, non-serializable resource will always have a correct md5Sum() and may be stored in the resources database.

Reimplemented in KisAbrBrush, KoFontFamily, and KisPSDLayerStyle.

Definition at line 333 of file KoResource.cpp.

334{
335 return !isEphemeral();
336}
virtual bool isEphemeral() const

References isEphemeral().

◆ linkedResources()

QList< KoResourceLoadResult > KoResource::linkedResources ( KisResourcesInterfaceSP globalResourcesInterface) const
virtual
Returns
all the resources that are needed but (*this) resource and are not embedded into it. The resources are fetched from globalResourcesInterface. If fetching of some resources is failed, then (*this) resource is invalid.

Reimplemented in KisPaintOpPreset.

Definition at line 237 of file KoResource.cpp.

238{
240 Q_UNUSED(list);
241
242 Q_UNUSED(globalResourcesInterface);
243 return {};
244}

◆ load()

bool KoResource::load ( KisResourcesInterfaceSP resourcesInterface)

Load this resource.

Returns
true if loading the resource succeeded.

Definition at line 64 of file KoResource.cpp.

65{
66 QFile file(filename());
67
68 if (!file.exists()) {
69 qWarning() << "Resource file doesn't exist: " << filename();
70 return false;
71 }
72
73 if (file.size() == 0) {
74 qWarning() << "Resource file is empty: " << filename();
75 return false;
76 }
77
78 if (!file.open(QIODevice::ReadOnly)) {
79 qWarning() << "Cannot open resource file for reading" << filename();
80 return false;
81 }
82
83 const bool res = loadFromDevice(&file, resourcesInterface);
84
85 if (!res) {
86 qWarning() << "Could not load resource file" << filename();
87 }
88
89 file.close();
90
91 return res;
92}
virtual bool loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP resourcesInterface)=0

References filename, and loadFromDevice().

◆ loadFromDevice()

◆ md5Sum()

QString KoResource::md5Sum ( bool generateIfEmpty = true) const
Parameters
generateIfEmptyif the resource does not have an md5sum set, if this is true, the resource saves itself into a buffer and calculates the md5sum over that. This may be different from the actual md5sum you would get calculated over the actual resource file.
Returns
the md5sum calculated over the contents of the resource. This is in hex-encoded string format.

Definition at line 141 of file KoResource.cpp.

142{
143 // [this assert is disputable] ephemeral resources have no md5
145
146 if (d->md5sum.isEmpty() && generateIfEmpty) {
147 // non-serializable resources should always have an externally generated md5
149 dbgResources << "No MD5 for" << this << this->name();
150 QBuffer buf;
151 buf.open(QFile::WriteOnly);
152 saveToDevice(&buf);
153 buf.close();
154 const_cast<KoResource*>(this)->setMD5Sum(KoMD5Generator::generateHash(buf.data()));
155 }
156 return d->md5sum;
157}
static QString generateHash(const QString &filename)
generateHash reads the given file and generates a hex-encoded md5sum for the file.
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
#define dbgResources
Definition kis_debug.h:43
virtual bool isSerializable() const
void setMD5Sum(const QString &md5sum)
Set the md5sum of this resource. It must be in hex-encoded string format.
virtual bool saveToDevice(QIODevice *dev) const
QString name

References d, dbgResources, KoMD5Generator::generateHash(), isEphemeral(), isSerializable(), KIS_SAFE_ASSERT_RECOVER_NOOP, KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE, name, saveToDevice(), and setMD5Sum().

◆ metadata()

QMap< QString, QVariant > KoResource::metadata ( ) const

get a map with all the metadata

◆ name()

virtual QString KoResource::name ( ) const
virtual
Returns
the user-visible name of the resource

Reimplemented in KisSeExprScript, KisPaintOpPreset, and KisPSDLayerStyle.

◆ operator=()

KoResource & KoResource::operator= ( const KoResource & rhs)
delete

◆ permanent()

bool KoResource::permanent ( ) const
Returns
true if the resource is permanent and can't be removed by the user

◆ requiredCanvasResources()

QList< int > KoResource::requiredCanvasResources ( ) const
virtual

A list of per-canvas active resources that are needed for this resource to function properly. E.g. some gradients may require Fg/Bg colors and some presets would like to know about the current gradient selection.

Returns
a list of canvas resources needed for the current resource

Reimplemented in KisPaintOpPreset, KisPSDLayerStyle, KoSegmentGradient, and KoStopGradient.

Definition at line 269 of file KoResource.cpp.

270{
271 return {};
272}

◆ requiredResources()

QList< KoResourceLoadResult > KoResource::requiredResources ( KisResourcesInterfaceSP globalResourcesInterface) const

Loads all the required resources either from globalResourcesInterface or from embedded data. The preset first tries to fetch the required resource from the global source, and only if it fails, tries to load it from the embedded data. One can check if the loaded resource is embedded by checking its resourceId().

The set of resources returned is basically: linkedResources() + embeddedResources()

Definition at line 232 of file KoResource.cpp.

233{
234 return linkedResources(globalResourcesInterface) + embeddedResources(globalResourcesInterface);
235}
virtual QList< KoResourceLoadResult > embeddedResources(KisResourcesInterfaceSP globalResourcesInterface) const
virtual QList< KoResourceLoadResult > linkedResources(KisResourcesInterfaceSP globalResourcesInterface) const

References embeddedResources(), and linkedResources().

◆ resourceId()

int KoResource::resourceId ( ) const
Returns
the unique id of the resource in the resource database

◆ resourceType()

◆ save()

bool KoResource::save ( )

Save this resource.

Returns
true if saving the resource succeeded.

Definition at line 94 of file KoResource.cpp.

95{
96 if (filename().isEmpty()) return false;
97
98 QFile file(filename());
99
100 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
101 warnKrita << "Can't open file for writing" << filename();
102 return false;
103 }
104
105 saveToDevice(&file);
106
107 file.close();
108 return true;
109}
#define warnKrita
Definition kis_debug.h:87

References filename, saveToDevice(), and warnKrita.

◆ saveToDevice()

◆ setActive()

void KoResource::setActive ( bool active)

Definition at line 206 of file KoResource.cpp.

207{
208 d->active = active;
209}

References active, and d.

◆ setDirty()

void KoResource::setDirty ( bool value)

Mark the preset as modified but not saved.

Definition at line 279 of file KoResource.cpp.

280{
281 d->modified = value;
282}

References d, and value().

◆ setFilename()

void KoResource::setFilename ( const QString & filename)

Definition at line 176 of file KoResource.cpp.

177{
178 d->filename = QFileInfo(filename).fileName();
179}

References d, and filename.

◆ setImage()

void KoResource::setImage ( const QImage & image)

Definition at line 136 of file KoResource.cpp.

137{
138 d->image = image;
139}
QImage image

References d, and image.

◆ setMD5Sum()

void KoResource::setMD5Sum ( const QString & md5sum)

Set the md5sum of this resource. It must be in hex-encoded string format.

ephemeral resources have no md5, trying to assign them one is considered an error

Definition at line 159 of file KoResource.cpp.

160{
164
165 if (valid()) {
166 Q_ASSERT(!md5sum.isEmpty());
167 }
168 d->md5sum = md5sum;
169}
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QString md5sum

References d, isEphemeral(), KIS_SAFE_ASSERT_RECOVER_RETURN, md5sum, and valid.

◆ setName()

void KoResource::setName ( const QString & name)

Definition at line 186 of file KoResource.cpp.

187{
188 d->name = name;
189}

References d, and name.

◆ setPermanent()

void KoResource::setPermanent ( bool permanent)

Definition at line 222 of file KoResource.cpp.

223{
224 d->permanent = permanent;
225}

References d, and permanent.

◆ setResourceId()

void KoResource::setResourceId ( int id)

Definition at line 318 of file KoResource.cpp.

319{
320 d->resourceId = id;
321}

References d.

◆ setStorageLocation()

void KoResource::setStorageLocation ( const QString & location)

Definition at line 338 of file KoResource.cpp.

339{
340 d->storageLocation = location;
341}

References d.

◆ setValid()

void KoResource::setValid ( bool valid)

Definition at line 196 of file KoResource.cpp.

197{
198 d->valid = valid;
199}

References d, and valid.

◆ setVersion()

void KoResource::setVersion ( int version)

Definition at line 313 of file KoResource.cpp.

314{
315 d->version = version;
316}

References d, and version.

◆ sideLoadedResources()

QList< KoResourceLoadResult > KoResource::sideLoadedResources ( KisResourcesInterfaceSP globalResourcesInterface) const
virtual

Side-loaded resources are the resources embedded into the file format and loaded alongside the main resource. After being loaded in loadFromDevice() they are stored separately and may be fetched by the resource locator.

After the locator has loaded them into the global storage, it can free the memory by calling clearSideLoadedResources().

Reimplemented in KisPaintOpPreset.

Definition at line 259 of file KoResource.cpp.

260{
261 Q_UNUSED(globalResourcesInterface);
262 return {};
263}

◆ signature()

KoResourceSignature KoResource::signature ( ) const
Returns
the signature of the resource which is enough for referencing the resource in somewhat unique way

Definition at line 323 of file KoResource.cpp.

324{
325 return KoResourceSignature(resourceType().first, md5Sum(false), filename(), name());
326}
A simple wrapper object for the main information about the resource.
virtual QPair< QString, QString > resourceType() const =0
QString md5Sum(bool generateIfEmpty=true) const

References filename, md5Sum(), name, and resourceType().

◆ storageLocation()

QString KoResource::storageLocation ( ) const
Returns
the name of the storage location of the resource

◆ takeSideLoadedResources()

QList< KoResourceLoadResult > KoResource::takeSideLoadedResources ( KisResourcesInterfaceSP globalResourcesInterface)

Returns all the side-loaded resources and clears the memory under them, the caller is expected to add them into the global store.

It is basically a combination of sideLoadedResources() + clearSideLoadedResources().

Definition at line 252 of file KoResource.cpp.

253{
254 QList<KoResourceLoadResult> result = sideLoadedResources(globalResourcesInterface);
256 return result;
257}
virtual void clearSideLoadedResources()
virtual QList< KoResourceLoadResult > sideLoadedResources(KisResourcesInterfaceSP globalResourcesInterface) const

References clearSideLoadedResources(), and sideLoadedResources().

◆ thumbnail()

QImage KoResource::thumbnail ( ) const
virtual

thumbnail the thumbnail image to use in resource selectors

Returns
a valid qimage. All thumbnails for a given resource have the same size (which is not true for image(), but that size need not be square. By default it's the same as image(), but that is not guaranteed.

Definition at line 126 of file KoResource.cpp.

127{
128 return image();
129}

References image.

◆ thumbnailPath()

QString KoResource::thumbnailPath ( ) const
virtual

thumbnailPath returns the path to a separate thumbnail image, outside the actual resource file itself. If the path is relative, it is supposed start in the same location as the resource itself. If it's absolute, that is, it starts with "/", it is from the root of the storage.

Returns
an empty string if the thumbnail is part of the resource

Reimplemented in KisMyPaintPaintOpPreset.

Definition at line 131 of file KoResource.cpp.

132{
133 return QString();
134}

◆ updateThumbnail()

void KoResource::updateThumbnail ( )
virtual

updateThumbnail updates the thumbnail for this resource. Reimplement if your thumbnail is something else than the image set with setImage.

Reimplemented in KoCssStylePreset, KoFontFamily, KoColorSet, and KisMyPaintPaintOpPreset.

Definition at line 122 of file KoResource.cpp.

123{
124}

◆ valid()

bool KoResource::valid ( ) const
Returns
true if the resource is ready for use

◆ version()

int KoResource::version ( ) const

Get the version of the resource.

Member Data Documentation

◆ active

bool KoResource::active {true}

Definition at line 31 of file KoResource.cpp.

31{true};

◆ d

Private* const KoResource::d
private

Definition at line 274 of file KoResource.h.

◆ filename

QString KoResource::filename

Definition at line 35 of file KoResource.cpp.

◆ image

QImage KoResource::image

Definition at line 38 of file KoResource.cpp.

◆ md5sum

QString KoResource::md5sum

Definition at line 37 of file KoResource.cpp.

◆ metadata

QMap< QString, QVariant > KoResource::metadata

Definition at line 39 of file KoResource.cpp.

◆ modified

bool KoResource::modified {false}

Definition at line 33 of file KoResource.cpp.

33{false};

◆ name

QString KoResource::name

Definition at line 34 of file KoResource.cpp.

◆ permanent

bool KoResource::permanent {false}

Definition at line 32 of file KoResource.cpp.

32{false};

◆ resourceId

int KoResource::resourceId {-1}

Definition at line 29 of file KoResource.cpp.

29{-1};

◆ storageLocation

QString KoResource::storageLocation

Definition at line 36 of file KoResource.cpp.

◆ valid

bool KoResource::valid {false}

Definition at line 30 of file KoResource.cpp.

30{false};

◆ version

int KoResource::version {-1}

Definition at line 28 of file KoResource.cpp.

28{-1};

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