Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -0,0 +1,105 @@
|
||||
#include "FormatRegistry.h"
|
||||
|
||||
#include "EntryImageProvider.h"
|
||||
#include "WebImageProvider.h"
|
||||
#include "backend/FormatReader.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QQmlEngine>
|
||||
|
||||
// Dispatches load() calls to the right child loader based on the image:// host (scheme name).
|
||||
class CompositeImageLoader : public IImageLoader {
|
||||
public:
|
||||
void add(const QString &host, std::unique_ptr<IImageLoader> loader)
|
||||
{
|
||||
m_loaders.emplace_back(host, std::move(loader));
|
||||
}
|
||||
|
||||
QImage load(const QUrl &url, int maxWidth) const override
|
||||
{
|
||||
const QString host = url.host();
|
||||
for (const auto &[h, loader] : m_loaders) {
|
||||
if (h == host)
|
||||
return loader->load(url, maxWidth);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> clone() const override
|
||||
{
|
||||
auto c = std::make_unique<CompositeImageLoader>();
|
||||
for (const auto &[host, loader] : m_loaders)
|
||||
c->add(host, loader->clone());
|
||||
return c;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<QString, std::unique_ptr<IImageLoader>>> m_loaders;
|
||||
};
|
||||
|
||||
FormatRegistry::FormatRegistry(QVector<FormatSpec> specs)
|
||||
: m_specs(std::move(specs))
|
||||
{
|
||||
}
|
||||
|
||||
const FormatSpec *FormatRegistry::specFor(const QString &filePath) const
|
||||
{
|
||||
const QFileInfo fi(filePath);
|
||||
const bool isDir = fi.isDir();
|
||||
const QString ext = fi.suffix().toLower();
|
||||
|
||||
for (const FormatSpec &spec : m_specs) {
|
||||
if (isDir ? spec.matchesDirectory : (!spec.matchesDirectory && spec.extension == ext))
|
||||
return &spec;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<FormatReader> FormatRegistry::createReader(const QString &filePath) const
|
||||
{
|
||||
const FormatSpec *spec = specFor(filePath);
|
||||
return spec ? spec->makeReader() : nullptr;
|
||||
}
|
||||
|
||||
IImageProvider *FormatRegistry::createImageProvider(const QString &filePath) const
|
||||
{
|
||||
const FormatSpec *spec = specFor(filePath);
|
||||
if (!spec || spec->scheme.isEmpty() || !spec->makeSource)
|
||||
return nullptr;
|
||||
return new EntryImageProvider(spec->makeSource, spec->scheme);
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> FormatRegistry::createImageLoader(const QString &filePath) const
|
||||
{
|
||||
const FormatSpec *spec = specFor(filePath);
|
||||
if (!spec || spec->scheme.isEmpty() || !spec->makeSource)
|
||||
return nullptr;
|
||||
|
||||
auto loader = std::make_unique<EntryImageProvider>(spec->makeSource, spec->scheme);
|
||||
loader->setFilePath(filePath);
|
||||
|
||||
if (!spec->loaderIncludesWeb)
|
||||
return loader;
|
||||
|
||||
auto composite = std::make_unique<CompositeImageLoader>();
|
||||
composite->add(spec->scheme, std::move(loader));
|
||||
composite->add(QStringLiteral("web"), std::make_unique<WebImageProvider>());
|
||||
return composite;
|
||||
}
|
||||
|
||||
QStringList FormatRegistry::supportedExtensions() const
|
||||
{
|
||||
QStringList out;
|
||||
for (const FormatSpec &spec : m_specs)
|
||||
if (!spec.matchesDirectory && !spec.extension.isEmpty())
|
||||
out << spec.extension;
|
||||
return out;
|
||||
}
|
||||
|
||||
void FormatRegistry::registerImageProviders(QQmlEngine &engine) const
|
||||
{
|
||||
for (const FormatSpec &spec : m_specs)
|
||||
if (!spec.scheme.isEmpty() && spec.makeSource)
|
||||
engine.addImageProvider(spec.scheme,
|
||||
new EntryImageProvider(spec.makeSource, spec.scheme));
|
||||
}
|
||||
Reference in new issue
Block a user