OSDN Git Service

a39e1bc19a988bf6913993f300a69a8953a3e2d1
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / propertyeditor / genericpropertieswidget.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 <QSet>
35 #include <QtDebug>
36 #include <QWidget>
37 #include <QApplication>
38
39 #include "qteditorfactory.h"
40 #include "qtpropertymanager.h"
41 #include "qttreepropertybrowser.h"
42 #include "qtvariantproperty.h"
43
44 #include "genericpropertieswidget.h"
45 #include "nodemetainfo.h"
46 #include "propertymetainfo.h"
47
48 #include "modelutilities.h"
49
50 using namespace QmlDesigner;
51
52 GenericPropertiesWidget::GenericPropertiesWidget(QWidget* parent):
53     AbstractView(parent),
54     editor(new QtTreePropertyBrowser()),
55     variantManager(new QtVariantPropertyManager()),
56     enumManager(new QtEnumPropertyManager())
57 {
58 }
59
60 QWidget* GenericPropertiesWidget::createPropertiesPage()
61 {
62     editor->setRootIsDecorated(false);
63
64     QtEnumEditorFactory *enumFactory = new QtEnumEditorFactory();
65     editor->setFactoryForManager(enumManager, enumFactory);
66
67     QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory();
68     editor->setFactoryForManager(variantManager, variantFactory);
69
70     reconnectEditor();
71
72     return editor;
73 }
74
75 void GenericPropertiesWidget::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
76                                                    const QList<ModelNode> &lastSelectedNodeList)
77 {
78     Q_UNUSED(lastSelectedNodeList);
79
80     if (selectedNode.isValid() && selectedNodeList.contains(selectedNode))
81         return;
82
83     if (selectedNodeList.isEmpty())
84         select(ModelNode());
85     else
86         select(selectedNodeList.at(0));
87 }
88
89 void GenericPropertiesWidget::select(const ModelNode& node)
90 {
91     if (!node.isValid() || (selectedNode.isValid() && node == selectedNode))
92         return;
93
94     selectedNode = node;
95
96     disconnectEditor();
97
98     editor->setUpdatesEnabled(false);
99     editor->clear();
100     buildPropertyEditorItems();
101     editor->setUpdatesEnabled(true);
102
103     reconnectEditor();
104 }
105
106 void GenericPropertiesWidget::disconnectEditor()
107 {
108     disconnect(variantManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
109     disconnect(enumManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
110 }
111
112 void GenericPropertiesWidget::reconnectEditor()
113 {
114     connect(enumManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
115     connect(variantManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
116 }
117
118 QtProperty* GenericPropertiesWidget::addVariantProperty(const PropertyMetaInfo& propertyMetaInfo,
119                                                         const QHash<QString, NodeProperty>& propertiesWithValues,
120                                                         const NodeInstance& instance)
121 {
122 //    qDebug() << "\t\tAdding variant property" << propertyMetaInfo.name() << "...";
123
124     if (variantManager->isPropertyTypeSupported(propertyMetaInfo.variantTypeId())) {
125         QtVariantProperty* item = variantManager->addProperty(propertyMetaInfo.variantTypeId(), propertyMetaInfo.name());
126
127         if (propertiesWithValues.contains(propertyMetaInfo.name())) {
128             NodeProperty nodeProperty(propertiesWithValues[propertyMetaInfo.name()]);
129
130             if (nodeProperty.isValid()) {
131                 item->setValue(nodeProperty.value());
132                 item->setModified(true);
133             }
134         }
135
136         if (!item->isModified()) {
137             item->setValue(instance.property(propertyMetaInfo.name())); //TODO fix this
138         }
139
140         return item;
141     } else {
142 //        qDebug() << "\t\t"
143 //                 << "Property type not supported for property"
144 //                 << propertyMetaInfo.name()
145 //                 << ", type name:" << propertyMetaInfo.variantTypeId()
146 //                 << "(" << propertyMetaInfo.type() << ")";
147         return 0;
148     }
149 }
150
151 QtProperty* GenericPropertiesWidget::addEnumProperty(const PropertyMetaInfo& propertyMetaInfo,
152                                                      const QHash<QString, NodeProperty>& propertiesWithValues,
153                                                      const NodeInstance& instance)
154 {
155 //    qDebug() << "\t\tAdding enum property" << propertyMetaInfo.name() << "...";
156
157     QtProperty* item = enumManager->addProperty(propertyMetaInfo.name());
158
159     QList<QString> elementNames(propertyMetaInfo.enumerator().elementNames());
160     enumManager->setEnumNames(item, elementNames);
161
162     if (propertiesWithValues.contains(propertyMetaInfo.name())) {
163         NodeProperty nodeProperty(propertiesWithValues[propertyMetaInfo.name()]);
164
165         int selectionIndex = elementNames.indexOf(nodeProperty.value().toString());
166         if (selectionIndex != -1) {
167             enumManager->setValue(item, selectionIndex);
168             item->setModified(true);
169         }
170     }
171
172     if (!item->isModified()) {
173         int selectionIndex = elementNames.indexOf(instance.property(propertyMetaInfo.name()).toString()); // TODO Fix this
174         if (selectionIndex != -1) {
175             enumManager->setValue(item, selectionIndex);
176         }
177     }
178
179     return item;
180 }
181
182 QtProperty* GenericPropertiesWidget::addFlagProperty(const PropertyMetaInfo &/*propertyMetaInfo*/,
183                                                      const QHash<QString, NodeProperty> &/*propertiesWithValues*/,
184                                                      const NodeInstance &/*instance*/)
185 {
186 //    qDebug() << "\t\tFlags are not yet supported. (Property: " << propertyMetaInfo.name() << ")";
187
188     return 0;
189 }
190
191 QtProperty* GenericPropertiesWidget::addProperties(const NodeMetaInfo& nodeMetaInfo,
192                                                    const QHash<QString, NodeProperty>& propertiesWithValues,
193                                                    const NodeInstance& instance)
194 {
195 //    qDebug() << "\tAdding" << nodeMetaInfo.properties().size()
196 //             << "properties for (super)node with type" << nodeMetaInfo.className() << "...";
197
198     QtProperty* groupItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
199                                                         nodeMetaInfo.typeName());
200
201     foreach (PropertyMetaInfo propMetaInfo, nodeMetaInfo.properties()) {
202         if (!propMetaInfo.isVisibleToPropertyEditor())
203             continue;
204
205         QtProperty* property = 0;
206
207         if (propMetaInfo.isEnumType()) {
208             property = addEnumProperty(propMetaInfo, propertiesWithValues, instance);
209         } else if (propMetaInfo.isFlagType()) {
210             property = addFlagProperty(propMetaInfo, propertiesWithValues, instance);
211         } else {
212             property = addVariantProperty(propMetaInfo, propertiesWithValues, instance);
213         }
214
215         if (property) {
216             groupItem->addSubProperty(property);
217         }
218     }
219
220     return groupItem;
221 }
222
223 void GenericPropertiesWidget::buildPropertyEditorItems()
224 {
225     if (!selectedNode.isValid()) {
226         return;
227     }
228
229 //    qDebug() << "buildPropertyEditorItems for node" << selectedNode.name() << "...";
230
231     QList<NodeMetaInfo> allClasses;
232     allClasses.append(selectedNode.metaInfo());
233     allClasses += selectedNode.metaInfo().superClasses();
234 //    qDebug() << "\tNode has" << allClasses.size() << "(super) classes";
235
236     QHash<QString, NodeProperty> propertiesWithValues;
237     foreach (const NodeProperty & nodeProperty, selectedNode.properties()) {
238         propertiesWithValues[nodeProperty.name()] = nodeProperty;
239     }
240
241 //    qDebug() << "\tNode has" << propertiesWithValues.size() << "properties set.";
242
243 //    NodeInstance instance = ModelUtilities::instanceForNode(selectedNode);
244 //
245 //    foreach (const NodeMetaInfo &info, allClasses) {
246 //        // FIXME: the add property is quite (too) expensive!
247 //        editor->addProperty(addProperties(info, propertiesWithValues, instance));
248 //    }
249 }
250
251 static bool nodeHasProperty(const ModelNode& node, const QString& name)
252 {
253     if (node.metaInfo().hasProperty(name))
254         return true;
255
256     foreach (const NodeMetaInfo& info, node.metaInfo().superClasses()) {
257         if (info.hasProperty(name))
258             return true;
259     }
260
261     return false;
262 }
263
264 void GenericPropertiesWidget::propertyChanged(QtProperty* property)
265 {
266 //    qDebug() << "property" << property->propertyName() << "changed...";
267
268     QtVariantProperty* variantProperty = dynamic_cast<QtVariantProperty*>(property);
269
270     if (variantProperty) {
271 //        qDebug() << "\tnew value: " << variantProperty->value();
272
273         if (selectedNode.property(property->propertyName()).value() == variantProperty->value()) {
274 //            qDebug() << "\twhich is the same as the old one, so we'll forget about it.";
275         } else {
276             if (nodeHasProperty(selectedNode, property->propertyName())) {
277                 selectedNode.setPropertyValue(property->propertyName(), variantProperty->value());
278
279                 if (!property->isModified())
280                     property->setModified(true);
281             } else {
282 //                qDebug() << "--- property " << property->propertyName() << "ignored: it doesn't exist in the metadata.";
283             }
284         }
285     } else {
286 //        qDebug("\tEep: changed property is not a variant property");
287     }
288 }
289
290 void GenericPropertiesWidget::modelAttached(Model *model)
291 {
292     AbstractView::modelAttached(model);
293 }
294
295 void GenericPropertiesWidget::nodeCreated(const ModelNode&)
296 {
297 }
298
299 void GenericPropertiesWidget::nodeAboutToBeRemoved(const ModelNode &removedNode)
300 {
301     if (selectedNode.isValid() && removedNode.isValid() && selectedNode == removedNode) {
302         select(selectedNode.parentNode());
303     }
304 }
305
306 void GenericPropertiesWidget::propertyAdded(const NodeState& state, const NodeProperty&)
307 {
308     if (selectedNode.isValid() && state.isValid() && selectedNode == state.modelNode()) {
309         select(state.modelNode());
310     }
311 }
312
313 void GenericPropertiesWidget::propertyAboutToBeRemoved(const NodeState& /* state */, const NodeProperty&)
314 {
315 }
316
317 void GenericPropertiesWidget::nodeReparented(const ModelNode &node, const ModelNode &oldParent, const ModelNode &newParent)
318 {
319     Q_UNUSED(node);
320     Q_UNUSED(oldParent);
321     Q_UNUSED(newParent);
322 }
323
324 void GenericPropertiesWidget::propertyValueChanged(const NodeState& state, const NodeProperty& property,
325                               const QVariant&, const QVariant& )
326 {
327     if (!selectedNode.isValid() || selectedNode != state.modelNode())
328         return;
329
330     disconnectEditor();
331
332     ModelNode node(state.modelNode());
333
334     foreach (QtProperty* qtProperty, enumManager->properties()) {
335         if (qtProperty->propertyName() == property.name()) {
336             QList<QString> elementNames = node.property(property.name()).metaInfo().enumerator().elementNames();
337             int selectionIndex = elementNames.indexOf(property.value().toString());
338             if (selectionIndex != -1) {
339                 enumManager->setValue(qtProperty, selectionIndex);
340                 qtProperty->setModified(true);
341             }
342
343             reconnectEditor();
344             return;
345         }
346     }
347
348     foreach (QtProperty* qtProperty, variantManager->properties()) {
349         if (qtProperty->propertyName() == property.name()) {
350             (dynamic_cast<QtVariantProperty*>(qtProperty))->setValue(property.value());
351             qtProperty->setModified(true);
352
353             reconnectEditor();
354             return;
355         }
356     }
357
358     reconnectEditor();
359 }
360
361 void GenericPropertiesWidget::modelStateAboutToBeRemoved(const ModelState &/*modelState*/)
362 {
363 //    TODO: implement
364 }
365
366 void GenericPropertiesWidget::modelStateAdded(const ModelState &/*modelState*/)
367 {
368 //    TODO: implement
369 }
370
371 void GenericPropertiesWidget::nodeStatesAboutToBeRemoved(const QList<NodeState> &/*nodeStateList*/)
372 {
373 //    TODO: implement
374 }
375
376 void GenericPropertiesWidget::nodeStatesAdded(const QList<NodeState> &/*nodeStateList*/)
377 {
378 //    TODO: implement
379 }
380
381 void GenericPropertiesWidget::anchorsChanged(const NodeState &/*nodeState*/)
382 {
383 //    TODO: implement
384 }