Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -0,0 +1,128 @@
|
||||
#include "EntryImageProvider.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include "AsyncImageResponse.h"
|
||||
#include "ImageScaling.h"
|
||||
#include "ImageUrl.h"
|
||||
|
||||
QImage EntryImageProvider::loadImageFromSource(IByteSource &source,
|
||||
const QString &entryName,
|
||||
int displayWidth,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
const QByteArray data = source.readEntry(entryName);
|
||||
if (data.isEmpty())
|
||||
return {};
|
||||
|
||||
QImage image;
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
return scaleToFit(image, displayWidth, requestedSize);
|
||||
}
|
||||
|
||||
EntryImageProvider::EntryImageProvider(SourceFactory sourceFactory, QString scheme)
|
||||
: m_sourceFactory(std::move(sourceFactory))
|
||||
, m_scheme(std::move(scheme))
|
||||
{
|
||||
}
|
||||
|
||||
QQuickImageResponse *EntryImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString sourcePath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
// id may be "entry/name.jpg?src=<encoded-path>"
|
||||
const ImageUrl::Parsed parsed = ImageUrl::parseProviderId(id);
|
||||
const QString entryName = parsed.entry;
|
||||
if (!parsed.sourcePath.isEmpty())
|
||||
sourcePath = parsed.sourcePath;
|
||||
|
||||
auto *response = new AsyncImageResponse(
|
||||
[factory = m_sourceFactory, sourcePath, entryName, displayWidth, requestedSize]() -> QImage {
|
||||
auto source = factory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, displayWidth, requestedSize);
|
||||
});
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage EntryImageProvider::load(const QUrl &url, int maxWidth) const
|
||||
{
|
||||
if (url.scheme() != QLatin1String("image"))
|
||||
return {};
|
||||
|
||||
const ImageUrl::Parsed parsed = ImageUrl::parseImageUrl(url);
|
||||
const QString entryName = parsed.entry;
|
||||
if (entryName.isEmpty())
|
||||
return {};
|
||||
|
||||
QString sourcePath = parsed.sourcePath;
|
||||
if (sourcePath.isEmpty()) {
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
|
||||
auto source = m_sourceFactory();
|
||||
if (!source->open(sourcePath))
|
||||
return {};
|
||||
return loadImageFromSource(*source, entryName, maxWidth, {});
|
||||
}
|
||||
|
||||
std::unique_ptr<IImageLoader> EntryImageProvider::clone() const
|
||||
{
|
||||
QString sourcePath;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
sourcePath = m_sourcePath;
|
||||
}
|
||||
auto c = std::make_unique<EntryImageProvider>(m_sourceFactory, m_scheme);
|
||||
c->setFilePath(sourcePath);
|
||||
return c;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setFilePath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath = filePath;
|
||||
}
|
||||
|
||||
QString EntryImageProvider::coverImageUrl(const QString &coverPath) const
|
||||
{
|
||||
QString sourcePath;
|
||||
{ QMutexLocker lock(&m_mutex); sourcePath = m_sourcePath; }
|
||||
return ImageUrl::build(m_scheme, coverPath, sourcePath);
|
||||
}
|
||||
|
||||
QString EntryImageProvider::scheme() const
|
||||
{
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
void EntryImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void EntryImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_sourcePath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
Reference in new issue
Block a user