Commit 4fbc4749 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add a KisResource Loader interface and registry

The resource loader can load a given resource and return information
the cache database needs.

Ref T8772
parent 4ac2fce4
......@@ -2,6 +2,8 @@ add_subdirectory(tests)
set(kritaresources_LIB_SRCS
KisResourceCacheDb.cpp
KisResourceLoader.cpp
KisResourceLoaderRegistry.cpp
)
qt5_add_resources(kritaresources_LIB_SRCS sql.qrc)
......@@ -13,6 +15,7 @@ generate_export_header(kritaresources BASE_NAME kritaresources)
target_link_libraries(kritaresources
PUBLIC
Qt5::Core
Qt5::Widgets
PRIVATE
kritaversion
kritaglobal
......
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KisResourceLoader.h"
class KisResourceLoader::Private
{
public:
QString folder;
QStringList mimetypes;
};
KisResourceLoader::KisResourceLoader(const QString &folder, const QStringList &mimetypes)
: d(new Private())
{
d->folder = folder;
d->mimetypes = mimetypes;
}
KisResourceLoader::~KisResourceLoader()
{
}
QStringList KisResourceLoader::mimetypes() const
{
return d->mimetypes;
}
QString KisResourceLoader::folder() const
{
return d->folder;
}
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KISRESOURCELOADER_H
#define KISRESOURCELOADER_H
#include <QString>
#include <QStringList>
#include <QImage>
#include <QScopedPointer>
#include <QSharedPointer>
#include <kritaresources_export.h>
/**
* @brief The KisResourceLoader class is an abstract interface
* class that must be implemented by actual resource classes and
* registered with the KisResourceLoaderRegistry.
*/
class KRITARESOURCES_EXPORT KisResourceLoader
{
public:
KisResourceLoader(const QString &folder, const QStringList &mimetypes);
virtual ~KisResourceLoader();
/**
* @return the mimetypes this resource can load
*/
QStringList mimetypes() const;
/**
* @return the folder in the resource storage where resources
* of this type are located
*/
QString folder() const;
/// For registration in KisResourceLoaderRegistry
virtual QString id() const = 0;
/**
* Load this resource.
* @return true if loading the resource succeeded.
*/
virtual bool load(QIODevice &dev) = 0;
/**
* Save this resource.
*@return true if saving the resource succeeded.
*/
virtual bool save(QIODevice &dev) const = 0;
virtual QImage thumbnail() const = 0;
virtual QString name() const = 0;
private:
class Private;
QScopedPointer<Private> d;
};
typedef QSharedPointer<KisResourceLoader> KisResourceLoaderSP;
#endif // KISRESOURCELOADER_H
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KisResourceLoaderRegistry.h"
#include <QApplication>
KisResourceLoaderRegistry::KisResourceLoaderRegistry(QObject *parent)
: QObject(parent)
{
}
KisResourceLoaderRegistry::~KisResourceLoaderRegistry()
{
}
KisResourceLoaderRegistry* KisResourceLoaderRegistry::instance()
{
KisResourceLoaderRegistry *reg = qApp->findChild<KisResourceLoaderRegistry *>(QString());
if (!reg) {
reg = new KisResourceLoaderRegistry(qApp);
}
return reg;
}
/*
* Copyright (C) 2018 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KISRESOURCELOADERREGISTRY_H
#define KISRESOURCELOADERREGISTRY_H
#include <KoGenericRegistry.h>
#include <QObject>
#include "KisResourceLoader.h"
#include <kritaresources_export.h>
class KRITARESOURCES_EXPORT KisResourceLoaderRegistry : public QObject, public KoGenericRegistry<KisResourceLoaderSP>
{
Q_OBJECT
public:
~KisResourceLoaderRegistry() override;
static KisResourceLoaderRegistry* instance();
private:
KisResourceLoaderRegistry(QObject *parent);
KisResourceLoaderRegistry(const KisResourceLoaderRegistry&);
KisResourceLoaderRegistry operator=(const KisResourceLoaderRegistry&);
};
#endif // KISRESOURCELOADERREGISTRY_H
......@@ -3,6 +3,6 @@ CREATE TABLE IF NOT EXISTS resources (
, resource_type_id INTEGER
, name TEXT
, tooltip TEXT
, icon BLOB
, thumbnail BLOB
, FOREIGN KEY(resource_type_id) REFERENCES resource_types(id)
);
......@@ -7,5 +7,6 @@ macro_add_unittest_definitions()
ecm_add_tests(
TestResourceCacheDb
TestResourceLoaderRegistry
NAME_PREFIX "libs-kritaresources-"
LINK_LIBRARIES kritaresources KF5::ConfigCore Qt5::Sql Qt5::Test)
/*
* Copyright (C) 2017 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "TestResourceLoaderRegistry.h"
#include <QTest>
#include <QtSql>
#include <QStandardPaths>
#include <QDir>
#include <KisResourceLoaderRegistry.h>
#include <KisResourceLoader.h>
class DummyResourceLoader : public KisResourceLoader {
public:
DummyResourceLoader()
: KisResourceLoader("dummy", QStringList() << "x-application/dummy")
{}
QString id() const override
{
return "dummy";
}
bool load(QIODevice&) override
{
return true;
}
bool save(QIODevice&) const override
{
return true;
}
QImage thumbnail() const override
{
return QImage();
}
QString name() const override
{
return ("onedummy");
}
};
void TestResourceLoaderRegistry::testRegistry()
{
KisResourceLoaderRegistry *reg = KisResourceLoaderRegistry::instance();
QSharedPointer<KisResourceLoader> loader(new DummyResourceLoader);
reg->add(loader);
QVERIFY(reg->count() == 1);
}
QTEST_MAIN(TestResourceLoaderRegistry)
/*
* Copyright (C) 2017 Boudewijn Rempt <boud@valdyas.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef TESTRESOURCECACHEDB_H
#define TESTRESOURCECACHEDB_H
#include <QObject>
class TestResourceLoaderRegistry : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testRegistry();
private:
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment