1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "bindingproperty.h"
35 #include "nodeabstractproperty.h"
36 #include "nodeproperty.h"
37 #include "internalproperty.h"
38 #include "internalbindingproperty.h"
39 #include "invalidmodelnodeexception.h"
40 #include "invalidpropertyexception.h"
41 #include "invalidargumentexception.h"
42 #include "internalnode_p.h"
45 namespace QmlDesigner {
47 BindingProperty::BindingProperty()
51 BindingProperty::BindingProperty(const BindingProperty &property, AbstractView *view)
52 : AbstractProperty(property.name(), property.internalNode(), property.model(), view)
57 BindingProperty::BindingProperty(const QString &propertyName, const Internal::InternalNodePointer &internalNode, Model* model, AbstractView *view)
58 : AbstractProperty(propertyName, internalNode, model, view)
63 void BindingProperty::setExpression(const QString &expression)
65 Internal::WriteLocker locker(model());
67 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
69 if (name() == "id") { // the ID for a node is independent of the state, so it has to be set with ModelNode::setId
70 throw InvalidPropertyException(__LINE__, __FUNCTION__, __FILE__, name());
73 if (expression.isEmpty())
74 throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
76 if (internalNode()->hasProperty(name())) { //check if oldValue != value
77 Internal::InternalProperty::Pointer internalProperty = internalNode()->property(name());
78 if (internalProperty->isBindingProperty()
79 && internalProperty->toBindingProperty()->expression() == expression)
84 if (internalNode()->hasProperty(name()) && !internalNode()->property(name())->isBindingProperty())
85 model()->m_d->removeProperty(internalNode()->property(name()));
87 model()->m_d->setBindingProperty(internalNode(), name(), expression);
90 QString BindingProperty::expression() const
92 if (internalNode()->hasProperty(name())
93 && internalNode()->property(name())->isBindingProperty())
94 return internalNode()->bindingProperty(name())->expression();
99 BindingProperty& BindingProperty::operator= (const QString &expression)
101 setExpression(expression);
106 static ModelNode resolveBinding(const QString &binding, ModelNode currentNode, AbstractView* view)
109 QString element = binding.split(QLatin1Char('.')).at(0);
110 while (!element.isEmpty())
112 if (element == "parent") {
113 if (currentNode.hasParentProperty())
114 currentNode = currentNode.parentProperty().toNodeAbstractProperty().parentModelNode();
116 return ModelNode(); //binding not valid
117 } else if (currentNode.hasProperty(element)) {
118 if (currentNode.property(element).isNodeProperty()) {
119 currentNode = currentNode.nodeProperty(element).modelNode();
121 currentNode = view->modelNodeForId(element); //id
122 if (!currentNode.isValid())
123 return ModelNode(); //binding not valid
126 currentNode = view->modelNodeForId(element); //id
129 if (i < binding.split(QLatin1Char('.')).count())
130 element = binding.split(QLatin1Char('.')).at(i);
138 ModelNode BindingProperty::resolveToModelNode() const
141 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
143 return resolveBinding(expression(), parentModelNode(), view());
146 static inline QStringList commaSeparatedSimplifiedStringList(const QString &string)
148 QStringList stringList = string.split(QLatin1String(","));
149 QStringList simpleList;
150 foreach (const QString &simpleString, stringList)
151 simpleList.append(simpleString.simplified());
156 AbstractProperty BindingProperty::resolveToProperty() const
159 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
161 QString binding = expression();
162 ModelNode node = parentModelNode();
164 if (binding.contains(QLatin1Char('.'))) {
165 element = binding.split(QLatin1Char('.')).last();
166 QString nodeBinding = binding;
167 nodeBinding.chop(element.length());
168 node = resolveBinding(nodeBinding, parentModelNode(), view());
174 return node.property(element);
176 return AbstractProperty();
179 bool BindingProperty::isList() const
182 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
184 return expression().startsWith('[') && expression().endsWith(']');
187 QList<ModelNode> BindingProperty::resolveToModelNodeList() const
189 QList<ModelNode> returnList;
191 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
193 QString string = expression();
196 QStringList simplifiedList = commaSeparatedSimplifiedStringList(string);
197 foreach (const QString &nodeId, simplifiedList) {
198 ModelNode modelNode = view()->modelNodeForId(nodeId);
199 if (modelNode.isValid())
200 returnList.append(modelNode);
206 void BindingProperty::setDynamicTypeNameAndExpression(const QString &typeName, const QString &expression)
208 Internal::WriteLocker locker(model());
210 throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
212 if (name() == "id") { // the ID for a node is independent of the state, so it has to be set with ModelNode::setId
213 throw InvalidPropertyException(__LINE__, __FUNCTION__, __FILE__, name());
216 if (expression.isEmpty())
217 throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
219 if (typeName.isEmpty()) {
220 throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
223 if (internalNode()->hasProperty(name())) { //check if oldValue != value
224 Internal::InternalProperty::Pointer internalProperty = internalNode()->property(name());
225 if (internalProperty->isBindingProperty()
226 && internalProperty->toBindingProperty()->expression() == expression
227 && internalProperty->toBindingProperty()->dynamicTypeName() == typeName)
232 if (internalNode()->hasProperty(name()) && !internalNode()->property(name())->isBindingProperty())
233 model()->m_d->removeProperty(internalNode()->property(name()));
235 model()->m_d->setDynamicBindingProperty(internalNode(), name(), typeName, expression);
238 BindingProperty& BindingProperty::operator= (const QPair<QString, QString> &typeExpressionPair)
240 setDynamicTypeNameAndExpression(typeExpressionPair.first, typeExpressionPair.second);
245 } // namespace QmlDesigner