Commit b462f4ad authored by Richard Lin's avatar Richard Lin
Browse files

Modify: Add snapshot preview

parent e273648e
#include <QGuiApplication>
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include "scenemanager.h"
......@@ -9,10 +8,9 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
SceneManager* cm = new SceneManager();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("sceneManager", QVariant::fromValue(cm));
SceneManager* cm = new SceneManager(&engine);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
......
......@@ -50,6 +50,9 @@ Window {
Text {
text: sceneManager.sceneList[index].name
}
Image {
source: "image://sceneManager/" + index
}
}
}
......
......@@ -27,6 +27,7 @@ bool SceneInfo::loadFile(const QString& filePath) {
_entryPath = meta["MetaData"]["entryPath"].toString(QString());
_pLoader = newLoader;
_pSnapShot = plugin->snapShot();
emit propertyChanged();
plugin->deleteLater();
return true;
}
......@@ -47,6 +48,7 @@ void SceneInfo::unload(void) {
_name = QString();
_entryPath = QString();
_pSnapShot.clear();
emit propertyChanged();
}
QPluginLoader* SceneInfo::loader(void) {
......
......@@ -8,9 +8,9 @@
class SceneInfo: public QObject
{
Q_OBJECT
Q_PROPERTY(QString name MEMBER _name)
Q_PROPERTY(int version MEMBER _version)
Q_PROPERTY(QString entryPath MEMBER _entryPath)
Q_PROPERTY(QString name MEMBER _name NOTIFY propertyChanged)
Q_PROPERTY(int version MEMBER _version NOTIFY propertyChanged)
Q_PROPERTY(QString entryPath MEMBER _entryPath NOTIFY propertyChanged)
public:
explicit SceneInfo(QObject *parent = nullptr);
......@@ -28,6 +28,9 @@ private:
QSharedPointer<QImage> _pSnapShot;
QPluginLoader* _pLoader;
signals:
void propertyChanged();
};
#endif // SCENEINFO_H
#include "scenemanager.h"
#include "sceneinfo.h"
#include <QQuickWindow>
#include <QGuiApplication>
#include <QDir>
#include <QQmlEngine>
#include <QQmlContext>
SceneManager::SceneManager(QObject *parent) : QObject(parent) {
SceneManager::SceneManager(QQmlEngine *engine, QObject *parent)
: QObject(parent),
QQuickImageProvider(Image),
_pEngine(engine)
{
_pEngine->rootContext()->setContextProperty("sceneManager", QVariant::fromValue(this));
_pEngine->addImageProvider("sceneManager", this);
_sceneInit();
_loadScene();
}
......@@ -16,17 +24,43 @@ SceneManager::~SceneManager() {
QList<QObject*> SceneManager::sceneList(void) {
QList<QObject* > ret;
foreach (SceneInfo* s, _pSceneInfo) {
foreach (SceneInfo* s, _sceneList) {
ret << qobject_cast<QObject*>(s);
}
return ret;
}
QImage SceneManager::requestImage(const QString &id, QSize *size, const QSize &requestedSize){
if (_pEngine == NULL) {
size->setHeight(0);
size->setWidth(0);
return QImage();
}
bool ok;
int number = id.toInt(&ok);
if (!ok || number<0 || number>=_sceneList.size() ) {
size->setHeight(0);
size->setWidth(0);
return QImage();
}
SceneInfo* scene = _sceneList.at(number);
QImage* image = scene->snapShot().data();
if (requestedSize.isValid()) {
size->setHeight(requestedSize.height());
size->setWidth(requestedSize.width());
return image->scaled(requestedSize);
}
size->setHeight(image->height());
size->setWidth(image->width());
return *image;
}
void SceneManager::_sceneInit(void){
QList<SceneInfo*> _pSceneInfo;
foreach (SceneInfo* d, _pSceneInfo) {
d->deleteLater();
_pSceneInfo.removeOne(d);
foreach (SceneInfo* d, _sceneList) {
if(d) {
d->deleteLater();
_sceneList.removeOne(d);
}
}
}
......@@ -48,7 +82,7 @@ void SceneManager::_loadScene(void) {
SceneInfo* newScene = new SceneInfo();
bool ret = newScene->loadFile(_pluginsDir.absoluteFilePath(fileName));
if (ret) {
_pSceneInfo.append(newScene);
_sceneList.append(newScene);
emit sceneListChanged();
} else {
newScene->deleteLater();
......
......@@ -2,27 +2,31 @@
#define SCENEMANAGER_H
#include <QObject>
#include <QQuickImageProvider>
class QQmlEngine;
class SceneInfo;
class SceneManager : public QObject
class SceneManager : public QObject, public QQuickImageProvider
{
Q_OBJECT
Q_PROPERTY(QList<QObject*> sceneList READ sceneList NOTIFY sceneListChanged)
public:
explicit SceneManager(QObject *parent = nullptr);
explicit SceneManager(QQmlEngine *engine, QObject *parent = 0);
virtual ~SceneManager();
signals:
void sceneListChanged(void);
public slots:
QList<QObject*> sceneList(void);
QList<QObject *> sceneList(void);
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
private:
void _sceneInit(void);
void _loadScene(void);
QList<SceneInfo*> _pSceneInfo;
QQmlEngine *_pEngine;
QList<SceneInfo *> _sceneList;
};
#endif // SCENEMANAGER_H
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