Commit 323dcb16 authored by Ronan's avatar Ronan

feat(App): use a image provider for qrc images

parent 8572347b
...@@ -97,6 +97,7 @@ set(SOURCES ...@@ -97,6 +97,7 @@ set(SOURCES
src/app/logger/Logger.cpp src/app/logger/Logger.cpp
src/app/paths/Paths.cpp src/app/paths/Paths.cpp
src/app/providers/AvatarProvider.cpp src/app/providers/AvatarProvider.cpp
src/app/providers/ImageProvider.cpp
src/app/providers/ThumbnailProvider.cpp src/app/providers/ThumbnailProvider.cpp
src/app/translator/DefaultTranslator.cpp src/app/translator/DefaultTranslator.cpp
src/components/assistant/AssistantModel.cpp src/components/assistant/AssistantModel.cpp
...@@ -149,6 +150,7 @@ set(HEADERS ...@@ -149,6 +150,7 @@ set(HEADERS
src/app/logger/Logger.hpp src/app/logger/Logger.hpp
src/app/paths/Paths.hpp src/app/paths/Paths.hpp
src/app/providers/AvatarProvider.hpp src/app/providers/AvatarProvider.hpp
src/app/providers/ImageProvider.hpp
src/app/providers/ThumbnailProvider.hpp src/app/providers/ThumbnailProvider.hpp
src/app/translator/DefaultTranslator.hpp src/app/translator/DefaultTranslator.hpp
src/components/assistant/AssistantModel.hpp src/components/assistant/AssistantModel.hpp
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "logger/Logger.hpp" #include "logger/Logger.hpp"
#include "paths/Paths.hpp" #include "paths/Paths.hpp"
#include "providers/AvatarProvider.hpp" #include "providers/AvatarProvider.hpp"
#include "providers/ImageProvider.hpp"
#include "providers/ThumbnailProvider.hpp" #include "providers/ThumbnailProvider.hpp"
#include "translator/DefaultTranslator.hpp" #include "translator/DefaultTranslator.hpp"
...@@ -172,6 +173,7 @@ void App::initContentApp () { ...@@ -172,6 +173,7 @@ void App::initContentApp () {
// Provide avatars/thumbnails providers. // Provide avatars/thumbnails providers.
mEngine->addImageProvider(AvatarProvider::PROVIDER_ID, new AvatarProvider()); mEngine->addImageProvider(AvatarProvider::PROVIDER_ID, new AvatarProvider());
mEngine->addImageProvider(ImageProvider::PROVIDER_ID, new ImageProvider());
mEngine->addImageProvider(ThumbnailProvider::PROVIDER_ID, new ThumbnailProvider()); mEngine->addImageProvider(ThumbnailProvider::PROVIDER_ID, new ThumbnailProvider());
registerTypes(); registerTypes();
......
/*
* ImageProvider.cpp
* Copyright (C) 2017 Belledonne Communications, Grenoble, France
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Created on: June 19, 2017
* Author: Ronan Abhamon
*/
#include <QImage>
#include <QPainter>
#include <QSvgRenderer>
#include "ImageProvider.hpp"
// =============================================================================
const QString ImageProvider::PROVIDER_ID = "internal";
ImageProvider::ImageProvider () : QQuickImageProvider(
QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading
) {}
QImage ImageProvider::requestImage (const QString &id, QSize *, const QSize &) {
const QString path = QStringLiteral(":/assets/images/%1").arg(id);
// 1. Build svg renderer.
QSvgRenderer renderer(path);
if (!renderer.isValid())
return QImage(path); // Not a svg file?
// 2. Create en empty image.
const QRectF viewBox = renderer.viewBoxF();
QImage image(static_cast<int>(viewBox.width()), static_cast<int>(viewBox.height()), QImage::Format_ARGB32);
if (image.isNull())
return QImage(); // Memory cannot be allocated.
image.fill(0x00000000);
// 3. Paint!
QPainter painter(&image);
renderer.render(&painter);
return image;
}
/*
* ImageProvider.hpp
* Copyright (C) 2017 Belledonne Communications, Grenoble, France
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Created on: June 19, 2017
* Author: Ronan Abhamon
*/
#ifndef IMAGE_PROVIDER_H_
#define IMAGE_PROVIDER_H_
#include <QQuickImageProvider>
// =============================================================================
class ImageProvider : public QQuickImageProvider {
public:
ImageProvider ();
~ImageProvider () = default;
QImage requestImage (const QString &id, QSize *size, const QSize &requestedSize) override;
static const QString PROVIDER_ID;
private:
};
#endif // IMAGE_PROVIDER_H_
...@@ -9,5 +9,5 @@ QtObject { ...@@ -9,5 +9,5 @@ QtObject {
property int sizeMax: 999999 property int sizeMax: 999999
property string imagesFormat: '.svg' property string imagesFormat: '.svg'
property string imagesPath: 'qrc:/assets/images/' property string imagesPath: 'image://internal/'
} }
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