Commit 56aa8ec3 authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesigner.NodeInstances: Add dummydata per file

A component which is asking for the e.g. parent.width in root object
has mostly not the wanted size. To get around this problem you can put
a file in the dummydata directory with the signature:

originalfilename_dummydata.qml:


import QtQuick 1.0
import QmlDesigner 1.0

DummyContextObject {
    parent: QtObject {
        property real width: 1000
        property real height: 400
    }
}

The file is reloaded if you change it so you can make changes on the fly.
parent edee2a5f
#include "dummycontextobject.h"
namespace QmlDesigner {
DummyContextObject::DummyContextObject(QObject *parent) :
QObject(parent)
{
}
QObject *DummyContextObject::parentDummy() const
{
return m_dummyParent.data();
}
void DummyContextObject::setParentDummy(QObject *parentDummy)
{
if (m_dummyParent.data() != parentDummy) {
m_dummyParent = parentDummy;
emit parentDummyChanged();
}
}
} // namespace QmlDesigner
#ifndef DUMMYCONTEXTOBJECT_H
#define DUMMYCONTEXTOBJECT_H
#include <QObject>
#include <QWeakPointer>
#include <qdeclarative.h>
namespace QmlDesigner {
class DummyContextObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QObject * parent READ parentDummy WRITE setParentDummy NOTIFY parentDummyChanged DESIGNABLE false FINAL)
public:
explicit DummyContextObject(QObject *parent = 0);
QObject *parentDummy() const;
void setParentDummy(QObject *parentDummy);
signals:
void parentDummyChanged();
private:
Q_DISABLE_COPY(DummyContextObject)
QWeakPointer<QObject> m_dummyParent;
};
} // namespace QmlDesigner
QML_DECLARE_TYPE(QmlDesigner::DummyContextObject)
#endif // DUMMYCONTEXTOBJECT_H
......@@ -3,6 +3,7 @@ INCLUDEPATH += $$PWD/../include
HEADERS += $$PWD/behaviornodeinstance.h
HEADERS += $$PWD/dummycontextobject.h
HEADERS += $$PWD/rendernodeinstanceserver.h
HEADERS += $$PWD/synchronizecommand.h
HEADERS += $$PWD/addimportcontainer.h
......@@ -51,6 +52,7 @@ HEADERS += $$PWD/../include/nodeinstanceserverinterface.h
SOURCES += $$PWD/behaviornodeinstance.cpp
SOURCES += $$PWD/dummycontextobject.cpp
SOURCES += $$PWD/rendernodeinstanceserver.cpp
SOURCES += $$PWD/synchronizecommand.cpp
SOURCES += $$PWD/addimportcontainer.cpp
......
......@@ -4,6 +4,7 @@
#include <QSharedPointer>
#include <QMetaProperty>
#include <qnumeric.h>
#include <QtDebug>
namespace QmlDesigner {
namespace Internal {
......@@ -59,10 +60,26 @@ int NodeInstanceMetaObject::metaCall(QMetaObject::Call call, int id, void **a)
oldValue = property(id).read(m_nodeInstance->object());
}
if (parent() && id < parent()->propertyOffset())
ObjectNodeInstance::Pointer objectNodeInstance = m_nodeInstance.toStrongRef();
if (parent() && id < parent()->propertyOffset()) {
metaCallReturnValue = parent()->metaCall(call, id, a);
else
} else {
metaCallReturnValue = QDeclarativeOpenMetaObject::metaCall(call, id, a);
}
if ((call == QMetaObject::WriteProperty || call == QMetaObject::ReadProperty) && metaCallReturnValue < 0) {
if (objectNodeInstance
&& objectNodeInstance->nodeInstanceServer()
&& objectNodeInstance->nodeInstanceServer()->dummyContextObject()
&& !(objectNodeInstance && !objectNodeInstance->isRootNodeInstance() && property(id).name() == QLatin1String("parent"))) {
QObject *contextDummyObject = objectNodeInstance->nodeInstanceServer()->dummyContextObject();
int properyIndex = contextDummyObject->metaObject()->indexOfProperty(property(id).name());
if (properyIndex >= 0)
metaCallReturnValue = contextDummyObject->qt_metacall(call, properyIndex, a);
}
}
if (metaCallReturnValue >= 0
&& call == QMetaObject::WriteProperty
......
......@@ -45,6 +45,8 @@
#include "componentcompletedcommand.h"
#include "createscenecommand.h"
#include "dummycontextobject.h"
#include <iostream>
#include <stdio.h>
......@@ -60,6 +62,8 @@ NodeInstanceServer::NodeInstanceServer(NodeInstanceClientInterface *nodeInstance
m_slowRenderTimer(false),
m_slowRenderTimerInterval(200)
{
qmlRegisterType<DummyContextObject>("QmlDesigner", 1, 0, "DummyContextObject");
m_importList.append("import Qt 4.7\n");
connect(m_childrenChangeEventFilter.data(), SIGNAL(childrenChanged(QObject*)), this, SLOT(emitParentChanged(QObject*)));
}
......@@ -398,6 +402,12 @@ void NodeInstanceServer::clearChangedPropertyList()
m_changedPropertyList.clear();
}
void NodeInstanceServer::refreshBindings()
{
static int counter = 0;
engine()->rootContext()->setContextProperty(QString("__%1").arg(counter++), 0); // refreshing bindings
}
void NodeInstanceServer::removeAllInstanceRelationships()
{
// prevent destroyed() signals calling back
......@@ -480,7 +490,12 @@ void NodeInstanceServer::refreshLocalFileProperty(const QString &path)
void NodeInstanceServer::refreshDummyData(const QString &path)
{
engine()->clearComponentCache();
loadDummyDataFile(QFileInfo(path));
QFileInfo filePath(path);
if (filePath.completeBaseName().contains("_dummycontext")) {
loadDummyContextObjectFile(filePath);
} else {
loadDummyDataFile(filePath);
}
startRenderTimer();
}
......@@ -783,6 +798,11 @@ QStringList NodeInstanceServer::imports() const
return m_importList;
}
QObject *NodeInstanceServer::dummyContextObject() const
{
return m_dummyContextObject.data();
}
void NodeInstanceServer::notifyPropertyChange(qint32 instanceid, const QString &propertyName)
{
if (hasInstanceForId(instanceid))
......@@ -915,12 +935,41 @@ void NodeInstanceServer::loadDummyDataFile(const QFileInfo& qmlFileInfo)
dummydataFileSystemWatcher()->addPath(qmlFileInfo.filePath());
}
void NodeInstanceServer::loadDummyContextObjectFile(const QFileInfo& qmlFileInfo)
{
delete m_dummyContextObject.data();
QDeclarativeComponent component(engine(), qmlFileInfo.filePath());
m_dummyContextObject = component.create();
if(component.isError()) {
QList<QDeclarativeError> errors = component.errors();
foreach (const QDeclarativeError &error, errors) {
qWarning() << error;
}
}
if (m_dummyContextObject) {
qWarning() << "Loaded dummy context object:" << qmlFileInfo.filePath();
m_dummyContextObject->setParent(this);
}
if (!dummydataFileSystemWatcher()->files().contains(qmlFileInfo.filePath()))
dummydataFileSystemWatcher()->addPath(qmlFileInfo.filePath());
}
void NodeInstanceServer::loadDummyDataFiles(const QString& directory)
{
QDir dir(directory, "*.qml");
QList<QFileInfo> filePathList = dir.entryInfoList();
foreach (const QFileInfo &qmlFileInfo, filePathList)
QString baseName = QFileInfo(fileUrl().toLocalFile()).completeBaseName();
foreach (const QFileInfo &qmlFileInfo, filePathList) {
if (!qmlFileInfo.completeBaseName().contains("_dummycontext")) {
loadDummyDataFile(qmlFileInfo);
} else if (qmlFileInfo.completeBaseName() == baseName+"_dummycontext") {
loadDummyContextObjectFile(qmlFileInfo);
}
}
}
QStringList dummyDataDirectories(const QString& directoryPath)
......
......@@ -83,6 +83,7 @@ public:
void notifyPropertyChange(qint32 instanceid, const QString &propertyName);
QStringList imports() const;
QObject *dummyContextObject() const;
public slots:
void refreshLocalFileProperty(const QString &path);
......@@ -132,6 +133,7 @@ protected:
QList<ServerNodeInstance> setupScene(const CreateSceneCommand &command);
void loadDummyDataFiles(const QString& directory);
void loadDummyDataFile(const QFileInfo& fileInfo);
void loadDummyContextObjectFile(const QFileInfo& fileInfo);
QImage renderPreviewImage();
void setTimerId(int timerId);
......@@ -141,6 +143,8 @@ protected:
const QVector<InstancePropertyPair> changedPropertyList() const;
void clearChangedPropertyList();
void refreshBindings();
private:
ServerNodeInstance m_rootNodeInstance;
ServerNodeInstance m_activeStateInstance;
......@@ -160,6 +164,7 @@ private:
QVector<InstancePropertyPair> m_changedPropertyList;
QStringList m_importList;
QList<ServerNodeInstance> m_completedComponentList;
QWeakPointer<QObject> m_dummyContextObject;
};
}
......
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