Commit c3be93f2 authored by Marco Bubke's avatar Marco Bubke

QmlDesigner: Refactor QmlItemNode::createQmlItemNodeFromImage

Change-Id: I1fa0eb94d36ae48bb131da2ceca75e19a4175a4d
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 1c3e9e01
......@@ -171,66 +171,66 @@ QmlItemNode QmlItemNode::createQmlItemNode(AbstractView *view, const ItemLibrary
return newQmlItemNode;
}
static void checkImageImport(AbstractView *view)
{
const QString newImportUrl = QLatin1String("QtQuick");
const QString newImportVersion = QLatin1String("1.1");
Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);
foreach (const Import &import, view->model()->imports()) {
if (import.isLibraryImport()
&& import.url() == newImport.url()) {
// reuse this import
newImport = import;
break;
}
}
if (!view->model()->hasImport(newImport, true, true))
view->model()->changeImports(QList<Import>() << newImport, QList<Import>());
}
QmlItemNode QmlItemNode::createQmlItemNodeFromImage(AbstractView *view, const QString &imageName, const QPointF &position, QmlItemNode parentQmlItemNode)
{
if (!parentQmlItemNode.isValid() && QmlItemNode::isValidQmlItemNode(view->rootModelNode()))
QmlItemNode newQmlItemNode;
if (!parentQmlItemNode.isValid())
parentQmlItemNode = QmlItemNode(view->rootModelNode());
else
return QmlItemNode();
QmlItemNode newQmlItemNode;
RewriterTransaction transaction = view->beginRewriterTransaction();
{
const QString newImportUrl = QLatin1String("QtQuick");
const QString newImportVersion = QLatin1String("1.1");
Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);
foreach (const Import &import, view->model()->imports()) {
if (import.isLibraryImport()
&& import.url() == newImport.url()) {
// reuse this import
newImport = import;
break;
}
}
if (parentQmlItemNode.isValid()) {
RewriterTransaction transaction = view->beginRewriterTransaction();
if (!view->model()->hasImport(newImport, true, true))
view->model()->changeImports(QList<Import>() << newImport, QList<Import>());
checkImageImport(view);
QList<QPair<PropertyName, QVariant> > propertyPairList;
propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));
if (view->model()->hasNodeMetaInfo("QtQuick.Image")) {
NodeMetaInfo metaInfo = view->model()->metaInfo("QtQuick.Image");
QList<QPair<PropertyName, QVariant> > propertyPairList;
propertyPairList.append(qMakePair(PropertyName("x"), QVariant(qRound(position.x()))));
propertyPairList.append(qMakePair(PropertyName("y"), QVariant(qRound(position.y()))));
QString relativeImageName = imageName;
QString relativeImageName = imageName;
//use relative path
if (QFileInfo(view->model()->fileUrl().toLocalFile()).exists()) {
QDir fileDir(QFileInfo(view->model()->fileUrl().toLocalFile()).absolutePath());
relativeImageName = fileDir.relativeFilePath(imageName);
}
//use relative path
if (QFileInfo(view->model()->fileUrl().toLocalFile()).exists()) {
QDir fileDir(QFileInfo(view->model()->fileUrl().toLocalFile()).absolutePath());
relativeImageName = fileDir.relativeFilePath(imageName);
propertyPairList.append(qMakePair(PropertyName("source"), QVariant(relativeImageName)));
}
propertyPairList.append(qMakePair(PropertyName("source"), QVariant(relativeImageName)));
NodeMetaInfo metaInfo = view->model()->metaInfo("QtQuick.Image");
if (metaInfo.isValid()) {
int minorVersion = metaInfo.minorVersion();
int majorVersion = metaInfo.majorVersion();
newQmlItemNode = QmlItemNode(view->createModelNode("QtQuick.Image", majorVersion, minorVersion, propertyPairList));
parentQmlItemNode.nodeAbstractProperty("data").reparentHere(newQmlItemNode);
}
newQmlItemNode = QmlItemNode(view->createModelNode("QtQuick.Image", metaInfo.majorVersion(), metaInfo.minorVersion(), propertyPairList));
parentQmlItemNode.defaultNodeAbstractProperty().reparentHere(newQmlItemNode);
Q_ASSERT(newQmlItemNode.isValid());
newQmlItemNode.setId(view->generateNewId("image"));
newQmlItemNode.setId(view->generateNewId("image"));
if (!QmlModelState(view->actualStateNode()).isBaseState()) {
newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
newQmlItemNode.setVariantProperty("opacity", 1);
}
if (!QmlModelState(view->actualStateNode()).isBaseState()) {
newQmlItemNode.modelNode().variantProperty("opacity").setValue(0);
newQmlItemNode.setVariantProperty("opacity", 1);
Q_ASSERT(newQmlItemNode.isValid());
}
Q_ASSERT(newQmlItemNode.isValid());
}
Q_ASSERT(newQmlItemNode.isValid());
return newQmlItemNode;
}
......
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