OSDN Git Service

f6a1478f704c49aad858c79f64673b350e759dd3
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / model / bindingproperty.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
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"
43 #include "model.h"
44 #include "model_p.h"
45 namespace QmlDesigner {
46
47 BindingProperty::BindingProperty()
48 {
49 }
50
51 BindingProperty::BindingProperty(const BindingProperty &property, AbstractView *view)
52     : AbstractProperty(property.name(), property.internalNode(), property.model(), view)
53 {
54 }
55
56
57 BindingProperty::BindingProperty(const QString &propertyName, const Internal::InternalNodePointer &internalNode, Model* model, AbstractView *view)
58     : AbstractProperty(propertyName, internalNode, model, view)
59 {
60 }
61
62
63 void BindingProperty::setExpression(const QString &expression)
64 {
65     Internal::WriteLocker locker(model());
66     if (!isValid())
67         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
68
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());
71     }
72
73     if (expression.isEmpty())
74         throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
75
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)
80
81             return;
82     }
83
84     if (internalNode()->hasProperty(name()) && !internalNode()->property(name())->isBindingProperty())
85         model()->m_d->removeProperty(internalNode()->property(name()));
86
87     model()->m_d->setBindingProperty(internalNode(), name(), expression);
88 }
89
90 QString BindingProperty::expression() const
91 {
92     if (internalNode()->hasProperty(name())
93         && internalNode()->property(name())->isBindingProperty())
94         return internalNode()->bindingProperty(name())->expression();
95
96     return QString();
97 }
98
99 BindingProperty& BindingProperty::operator= (const QString &expression)
100 {
101     setExpression(expression);
102
103     return *this;
104 }
105
106 static ModelNode resolveBinding(const QString &binding, ModelNode currentNode, AbstractView* view)
107 {
108     int i = 0;
109     QString element = binding.split(QLatin1Char('.')).at(0);
110     while (!element.isEmpty())
111     {
112         if (element == "parent") {
113             if (currentNode.hasParentProperty())
114                 currentNode = currentNode.parentProperty().toNodeAbstractProperty().parentModelNode();
115             else
116                 return ModelNode(); //binding not valid
117         } else if (currentNode.hasProperty(element)) {
118             if (currentNode.property(element).isNodeProperty()) {
119                 currentNode = currentNode.nodeProperty(element).modelNode();
120             } else {
121                 currentNode = view->modelNodeForId(element); //id
122                 if (!currentNode.isValid())
123                     return ModelNode(); //binding not valid
124             }
125         } else {
126             currentNode = view->modelNodeForId(element); //id
127         }
128         i++;
129         if (i < binding.split(QLatin1Char('.')).count())
130             element = binding.split(QLatin1Char('.')).at(i);
131         else
132             element.clear();
133     }
134     return currentNode;
135
136 }
137
138 ModelNode BindingProperty::resolveToModelNode() const
139 {
140     if (!isValid())
141         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
142
143     return resolveBinding(expression(), parentModelNode(), view());
144 }
145
146 static inline QStringList commaSeparatedSimplifiedStringList(const QString &string)
147 {
148     QStringList stringList = string.split(QLatin1String(","));
149     QStringList simpleList;
150     foreach (const QString &simpleString, stringList)
151         simpleList.append(simpleString.simplified());
152     return simpleList;
153 }
154
155
156 AbstractProperty BindingProperty::resolveToProperty() const
157 {
158     if (!isValid())
159         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
160
161     QString binding = expression();
162     ModelNode node = parentModelNode();
163     QString element;
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());
169     } else {
170         element = binding;
171     }
172
173     if (node.isValid())
174         return node.property(element);
175     else
176         return AbstractProperty();
177 }
178
179 bool BindingProperty::isList() const
180 {
181     if (!isValid())
182         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
183
184     return expression().startsWith('[') && expression().endsWith(']');
185 }
186
187 QList<ModelNode> BindingProperty::resolveToModelNodeList() const
188 {
189     QList<ModelNode> returnList;
190     if (!isValid())
191         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
192     if (isList()) {
193         QString string = expression();
194         string.chop(1);
195         string.remove(0, 1);
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);
201         }
202     }
203     return returnList;
204 }
205
206 void BindingProperty::setDynamicTypeNameAndExpression(const QString &typeName, const QString &expression)
207 {
208     Internal::WriteLocker locker(model());
209     if (!isValid())
210         throw InvalidModelNodeException(__LINE__, __FUNCTION__, __FILE__);
211
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());
214     }
215
216     if (expression.isEmpty())
217         throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
218
219     if (typeName.isEmpty()) {
220         throw InvalidArgumentException(__LINE__, __FUNCTION__, __FILE__, name());
221     }
222
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)
228
229             return;
230     }
231
232     if (internalNode()->hasProperty(name()) && !internalNode()->property(name())->isBindingProperty())
233         model()->m_d->removeProperty(internalNode()->property(name()));
234
235      model()->m_d->setDynamicBindingProperty(internalNode(), name(), typeName, expression);
236 }
237
238 BindingProperty& BindingProperty::operator= (const QPair<QString, QString> &typeExpressionPair)
239 {
240    setDynamicTypeNameAndExpression(typeExpressionPair.first, typeExpressionPair.second);
241
242    return *this;
243 }
244
245 } // namespace QmlDesigner