![]() |
LeechCraft
0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
|
Utility class for loading a file from a set of locations. More...
#include "resourceloader.h"
Signals | |
void | watchedDirectoriesChanged () |
Public Member Functions | |
ResourceLoader (const QString &relPath, QObject *obj=0) | |
Initializes the loader with the given path. More... | |
void | AddGlobalPrefix () |
Registers global OS-dependent prefixes. More... | |
void | AddLocalPrefix (QString prefix=QString()) |
Registers a local search prefix. More... | |
void | SetCacheParams (int size, int timeout) |
Sets the caching parameters of this loader. More... | |
void | FlushCache () |
Forcefully flushes the cache. More... | |
QFileInfoList | List (const QString &option, const QStringList &names={}, QDir::Filters filters=QDir::NoFilter) const |
Lists the available files for the given option. More... | |
QString | GetPath (const QStringList &pathVariants) const |
Returns the first found path for the list of variants. More... | |
QString | GetIconPath (const QString &basename) const |
Calls GetPath() with standard variants for the icon extensions. More... | |
QIODevice_ptr | Load (const QStringList &pathVariants, bool open=false) const |
Returns the QIODevice for the corresponding resource. More... | |
QIODevice_ptr | Load (const QString &pathVariant, bool open=false) const |
Returns the QIODevice for the corresponding resource. More... | |
QIODevice_ptr | GetIconDevice (const QString &basename, bool open=false) const |
Returns the QIODevice for the corresponding icon. More... | |
QPixmap | LoadPixmap (const QString &basename) const |
Returns the pixmap for the given basename. More... | |
QAbstractItemModel * | GetSubElemModel () const |
Returns the subelement model with the contents of registered paths. More... | |
void | SetAttrFilters (QDir::Filters) |
Sets the attribute filters for the subelement model. More... | |
void | SetNameFilters (const QStringList &) |
Sets the name filters for the subelement model. More... | |
Utility class for loading a file from a set of locations.
One may want to load a resource file from a set of locations, trying them in order and sticking with the first found file. There may be different sets of resources (icon packs, theme packs, etc.) with the same layout of files in them, and one may want to easily load a file from the currently selected set. For example, a theme file may be first searched for in user's home directory, then in some global place, and maybe even somewhere else as well.
This class provides means to make this task easy. The constructor takes a relative path from each location that should be looked for. AddGlobalPrefix() function registers platform-specific search paths (like ones in /usr/share on UNIX and relative to main application binary on Windows). AddLocalPrefix() registers paths relatively to the ~/.leechcraft/data/ path. Then GetPath() function is used to get a full path to the corresponding object or Load() for loading that object. Prefixes are searched for in reverse order, from last registered to first one.
A model suitable for using in XmlSettingsDialog is also provided, which watches for different subdirectories in registered paths and is handy for supporting different sets of resources.
The class also supports filtering its results, filters may be set with SetAttrFilters() and SetNameFilters().
All registered paths are watched for changes, so, for example, installation of new icon packs would require no actions from both user (to make LeechCraft fetch newly installed data) and programmer (to support this feature).
Definition at line 92 of file resourceloader.h.
LeechCraft::Util::ResourceLoader::ResourceLoader | ( | const QString & | relPath, |
QObject * | obj = 0 |
||
) |
Initializes the loader with the given path.
[in] | relPath | Path relative to registered prefixes where the resources will be searched for. |
[in] | obj | Parent object. |
Definition at line 45 of file resourceloader.cpp.
void LeechCraft::Util::ResourceLoader::AddGlobalPrefix | ( | ) |
Registers global OS-dependent prefixes.
Registers global prefixes as search pathes. It's /usr/local/share/leechcraft/ and /usr/share/leechcraft/ on UNIX, share/ in the directory with the main executable on Windows and ../Resources/ dir relative to application path.
Definition at line 104 of file resourceloader.cpp.
void LeechCraft::Util::ResourceLoader::AddLocalPrefix | ( | QString | prefix = QString () | ) |
Registers a local search prefix.
The prefix string is relative to ~/.leechcraft/data. It may have several directories in the path, like "plugin/directory/subdir".
Definition at line 76 of file resourceloader.cpp.
void LeechCraft::Util::ResourceLoader::FlushCache | ( | ) |
Forcefully flushes the cache.
This function may be used to flush the contents of this resource loader cache. It may be useful, for example, when the resources have been updated on disk, and the new versions should be updated right now.
Definition at line 161 of file resourceloader.cpp.
QIODevice_ptr LeechCraft::Util::ResourceLoader::GetIconDevice | ( | const QString & | basename, |
bool | open = false |
||
) | const |
Returns the QIODevice for the corresponding icon.
This function is analogous to GetIconPath().
[in] | basename | Base name of the icon — without extension. |
[in] | open | Whether the returned QIODevice should be opened automatically. |
Definition at line 267 of file resourceloader.cpp.
References Load().
Referenced by LoadPixmap().
QString LeechCraft::Util::ResourceLoader::GetIconPath | ( | const QString & | basename | ) | const |
Calls GetPath() with standard variants for the icon extensions.
This functions just calls the GetPath() function with standard icon extensions, that is, the path variants are basename + (".svg", ".png", ".jpg")
[in] | basename | Base name of the icon — without extension. |
Definition at line 216 of file resourceloader.cpp.
References GetPath().
QString LeechCraft::Util::ResourceLoader::GetPath | ( | const QStringList & | pathVariants | ) | const |
Returns the first found path for the list of variants.
For each registered prefix this function iterates over pathVariants list and checks if the file at $prefix/$relPath/$pathVariant exists, and if it does, the full path to this file is returned. Here, relPath is the path passed to the constructor.
pathVariants may be useful if there may be several representations of each resource. For example, an icon can be in jpg, png or svg format, thus pathVariants would contain something like:
If nothing is found, a null string is returned.
[in] | pathVariants | The list of variants to try. |
Definition at line 189 of file resourceloader.cpp.
Referenced by GetIconPath(), and Load().
QAbstractItemModel * LeechCraft::Util::ResourceLoader::GetSubElemModel | ( | ) | const |
Returns the subelement model with the contents of registered paths.
For each prefix in the list of registered prefixes, the contents of $prefix/$relPath are listed in this model, where relPath is the path passed to the constructor. The results are filtered according to the filters set with SetAttrFilters() and SetNameFilters().
This model may be used in settings dialogs as a data source, which may be handy when one wants to implement support for iconsets, theme packs and such things in his plugin.
Definition at line 289 of file resourceloader.cpp.
QFileInfoList LeechCraft::Util::ResourceLoader::List | ( | const QString & | option, |
const QStringList & | names = {} , |
||
QDir::Filters | filters = QDir::NoFilter |
||
) | const |
Lists the available files for the given option.
The option should be the one of the elements of the model returned by GetSubElemModel().
This method supports basic filtering: list of name filters could be passed via the names parameter, and additional filter flags via the filters parameter. The default is to perform no additional filtering.
[in] | option | The option for which to list available files/directories. |
[in] | names | Name filters. |
[in] | filters | Additional filters. |
Definition at line 166 of file resourceloader.cpp.
QIODevice_ptr LeechCraft::Util::ResourceLoader::Load | ( | const QStringList & | pathVariants, |
bool | open = false |
||
) | const |
Returns the QIODevice for the corresponding resource.
This function behaves exactly like GetPath(), except it creates a QIODevice for the found path and opens it.
[in] | pathVariants | The list of variants to try. |
[in] | open | Whether the returned QIODevice should be opened automatically. |
Definition at line 221 of file resourceloader.cpp.
References GetPath().
Referenced by GetIconDevice(), and Load().
QIODevice_ptr LeechCraft::Util::ResourceLoader::Load | ( | const QString & | pathVariant, |
bool | open = false |
||
) | const |
Returns the QIODevice for the corresponding resource.
This function is the overloaded variant of Load(), provided for convenience. This function constructs a stringlist of a single element, the passed pathVariant, and calls the other Load() with it.
[in] | pathVariant | The path variant to try. |
[in] | open | Whether the returned QIODevice should be opened automatically. |
Definition at line 262 of file resourceloader.cpp.
References Load().
QPixmap LeechCraft::Util::ResourceLoader::LoadPixmap | ( | const QString & | basename | ) | const |
Returns the pixmap for the given basename.
This function is analogous to GetIconPath(), but instead of returning the path, it reads the pixmap at that path and returns the pixmap.
If there is no such path, or the pixmap failed to read, a null pixmap is returned.
[in] | basename | Base name of the pixmap — without extension. |
Definition at line 272 of file resourceloader.cpp.
References GetIconDevice().
void LeechCraft::Util::ResourceLoader::SetAttrFilters | ( | QDir::Filters | filters | ) |
Sets the attribute filters for the subelement model.
This is analogous to QDir's functions for filtering its results.
By default, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable is set.
Definition at line 294 of file resourceloader.cpp.
void LeechCraft::Util::ResourceLoader::SetCacheParams | ( | int | size, |
int | timeout | ||
) |
Sets the caching parameters of this loader.
This function sets the size of the cache in kibibytes and the timeout between cache flushes in milliseconds. That's it, the cache would be flushed each timeout milliseconds automatically. If timeout is less than 0, then automatic cache flushing is disabled.
[in] | size | The size of the cache in kibibytes. |
[in] | timeout | The timeout between cache flushes. |
Definition at line 140 of file resourceloader.cpp.
void LeechCraft::Util::ResourceLoader::SetNameFilters | ( | const QStringList & | filters | ) |
Sets the name filters for the subelement model.
Wildcards may be used in name fitlers. Use an empty QStringList to disable filtering. By default, no filter is set.
This is analogous to QDir's functions for filtering its results.
Definition at line 299 of file resourceloader.cpp.
|
signal |