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 **************************************************************************/
37 #include <QApplication>
39 #include "qteditorfactory.h"
40 #include "qtpropertymanager.h"
41 #include "qttreepropertybrowser.h"
42 #include "qtvariantproperty.h"
44 #include "genericpropertieswidget.h"
45 #include "nodemetainfo.h"
46 #include "propertymetainfo.h"
48 #include "modelutilities.h"
50 using namespace QmlDesigner;
52 GenericPropertiesWidget::GenericPropertiesWidget(QWidget* parent):
54 editor(new QtTreePropertyBrowser()),
55 variantManager(new QtVariantPropertyManager()),
56 enumManager(new QtEnumPropertyManager())
60 QWidget* GenericPropertiesWidget::createPropertiesPage()
62 editor->setRootIsDecorated(false);
64 QtEnumEditorFactory *enumFactory = new QtEnumEditorFactory();
65 editor->setFactoryForManager(enumManager, enumFactory);
67 QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory();
68 editor->setFactoryForManager(variantManager, variantFactory);
75 void GenericPropertiesWidget::selectedNodesChanged(const QList<ModelNode> &selectedNodeList,
76 const QList<ModelNode> &lastSelectedNodeList)
78 Q_UNUSED(lastSelectedNodeList);
80 if (selectedNode.isValid() && selectedNodeList.contains(selectedNode))
83 if (selectedNodeList.isEmpty())
86 select(selectedNodeList.at(0));
89 void GenericPropertiesWidget::select(const ModelNode& node)
91 if (!node.isValid() || (selectedNode.isValid() && node == selectedNode))
98 editor->setUpdatesEnabled(false);
100 buildPropertyEditorItems();
101 editor->setUpdatesEnabled(true);
106 void GenericPropertiesWidget::disconnectEditor()
108 disconnect(variantManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
109 disconnect(enumManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
112 void GenericPropertiesWidget::reconnectEditor()
114 connect(enumManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
115 connect(variantManager, SIGNAL(propertyChanged(QtProperty*)), this, SLOT(propertyChanged(QtProperty*)));
118 QtProperty* GenericPropertiesWidget::addVariantProperty(const PropertyMetaInfo& propertyMetaInfo,
119 const QHash<QString, NodeProperty>& propertiesWithValues,
120 const NodeInstance& instance)
122 // qDebug() << "\t\tAdding variant property" << propertyMetaInfo.name() << "...";
124 if (variantManager->isPropertyTypeSupported(propertyMetaInfo.variantTypeId())) {
125 QtVariantProperty* item = variantManager->addProperty(propertyMetaInfo.variantTypeId(), propertyMetaInfo.name());
127 if (propertiesWithValues.contains(propertyMetaInfo.name())) {
128 NodeProperty nodeProperty(propertiesWithValues[propertyMetaInfo.name()]);
130 if (nodeProperty.isValid()) {
131 item->setValue(nodeProperty.value());
132 item->setModified(true);
136 if (!item->isModified()) {
137 item->setValue(instance.property(propertyMetaInfo.name())); //TODO fix this
142 // qDebug() << "\t\t"
143 // << "Property type not supported for property"
144 // << propertyMetaInfo.name()
145 // << ", type name:" << propertyMetaInfo.variantTypeId()
146 // << "(" << propertyMetaInfo.type() << ")";
151 QtProperty* GenericPropertiesWidget::addEnumProperty(const PropertyMetaInfo& propertyMetaInfo,
152 const QHash<QString, NodeProperty>& propertiesWithValues,
153 const NodeInstance& instance)
155 // qDebug() << "\t\tAdding enum property" << propertyMetaInfo.name() << "...";
157 QtProperty* item = enumManager->addProperty(propertyMetaInfo.name());
159 QList<QString> elementNames(propertyMetaInfo.enumerator().elementNames());
160 enumManager->setEnumNames(item, elementNames);
162 if (propertiesWithValues.contains(propertyMetaInfo.name())) {
163 NodeProperty nodeProperty(propertiesWithValues[propertyMetaInfo.name()]);
165 int selectionIndex = elementNames.indexOf(nodeProperty.value().toString());
166 if (selectionIndex != -1) {
167 enumManager->setValue(item, selectionIndex);
168 item->setModified(true);
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);
182 QtProperty* GenericPropertiesWidget::addFlagProperty(const PropertyMetaInfo &/*propertyMetaInfo*/,
183 const QHash<QString, NodeProperty> &/*propertiesWithValues*/,
184 const NodeInstance &/*instance*/)
186 // qDebug() << "\t\tFlags are not yet supported. (Property: " << propertyMetaInfo.name() << ")";
191 QtProperty* GenericPropertiesWidget::addProperties(const NodeMetaInfo& nodeMetaInfo,
192 const QHash<QString, NodeProperty>& propertiesWithValues,
193 const NodeInstance& instance)
195 // qDebug() << "\tAdding" << nodeMetaInfo.properties().size()
196 // << "properties for (super)node with type" << nodeMetaInfo.className() << "...";
198 QtProperty* groupItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(),
199 nodeMetaInfo.typeName());
201 foreach (PropertyMetaInfo propMetaInfo, nodeMetaInfo.properties()) {
202 if (!propMetaInfo.isVisibleToPropertyEditor())
205 QtProperty* property = 0;
207 if (propMetaInfo.isEnumType()) {
208 property = addEnumProperty(propMetaInfo, propertiesWithValues, instance);
209 } else if (propMetaInfo.isFlagType()) {
210 property = addFlagProperty(propMetaInfo, propertiesWithValues, instance);
212 property = addVariantProperty(propMetaInfo, propertiesWithValues, instance);
216 groupItem->addSubProperty(property);
223 void GenericPropertiesWidget::buildPropertyEditorItems()
225 if (!selectedNode.isValid()) {
229 // qDebug() << "buildPropertyEditorItems for node" << selectedNode.name() << "...";
231 QList<NodeMetaInfo> allClasses;
232 allClasses.append(selectedNode.metaInfo());
233 allClasses += selectedNode.metaInfo().superClasses();
234 // qDebug() << "\tNode has" << allClasses.size() << "(super) classes";
236 QHash<QString, NodeProperty> propertiesWithValues;
237 foreach (const NodeProperty & nodeProperty, selectedNode.properties()) {
238 propertiesWithValues[nodeProperty.name()] = nodeProperty;
241 // qDebug() << "\tNode has" << propertiesWithValues.size() << "properties set.";
243 // NodeInstance instance = ModelUtilities::instanceForNode(selectedNode);
245 // foreach (const NodeMetaInfo &info, allClasses) {
246 // // FIXME: the add property is quite (too) expensive!
247 // editor->addProperty(addProperties(info, propertiesWithValues, instance));
251 static bool nodeHasProperty(const ModelNode& node, const QString& name)
253 if (node.metaInfo().hasProperty(name))
256 foreach (const NodeMetaInfo& info, node.metaInfo().superClasses()) {
257 if (info.hasProperty(name))
264 void GenericPropertiesWidget::propertyChanged(QtProperty* property)
266 // qDebug() << "property" << property->propertyName() << "changed...";
268 QtVariantProperty* variantProperty = dynamic_cast<QtVariantProperty*>(property);
270 if (variantProperty) {
271 // qDebug() << "\tnew value: " << variantProperty->value();
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.";
276 if (nodeHasProperty(selectedNode, property->propertyName())) {
277 selectedNode.setPropertyValue(property->propertyName(), variantProperty->value());
279 if (!property->isModified())
280 property->setModified(true);
282 // qDebug() << "--- property " << property->propertyName() << "ignored: it doesn't exist in the metadata.";
286 // qDebug("\tEep: changed property is not a variant property");
290 void GenericPropertiesWidget::modelAttached(Model *model)
292 AbstractView::modelAttached(model);
295 void GenericPropertiesWidget::nodeCreated(const ModelNode&)
299 void GenericPropertiesWidget::nodeAboutToBeRemoved(const ModelNode &removedNode)
301 if (selectedNode.isValid() && removedNode.isValid() && selectedNode == removedNode) {
302 select(selectedNode.parentNode());
306 void GenericPropertiesWidget::propertyAdded(const NodeState& state, const NodeProperty&)
308 if (selectedNode.isValid() && state.isValid() && selectedNode == state.modelNode()) {
309 select(state.modelNode());
313 void GenericPropertiesWidget::propertyAboutToBeRemoved(const NodeState& /* state */, const NodeProperty&)
317 void GenericPropertiesWidget::nodeReparented(const ModelNode &node, const ModelNode &oldParent, const ModelNode &newParent)
324 void GenericPropertiesWidget::propertyValueChanged(const NodeState& state, const NodeProperty& property,
325 const QVariant&, const QVariant& )
327 if (!selectedNode.isValid() || selectedNode != state.modelNode())
332 ModelNode node(state.modelNode());
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);
348 foreach (QtProperty* qtProperty, variantManager->properties()) {
349 if (qtProperty->propertyName() == property.name()) {
350 (dynamic_cast<QtVariantProperty*>(qtProperty))->setValue(property.value());
351 qtProperty->setModified(true);
361 void GenericPropertiesWidget::modelStateAboutToBeRemoved(const ModelState &/*modelState*/)
366 void GenericPropertiesWidget::modelStateAdded(const ModelState &/*modelState*/)
371 void GenericPropertiesWidget::nodeStatesAboutToBeRemoved(const QList<NodeState> &/*nodeStateList*/)
376 void GenericPropertiesWidget::nodeStatesAdded(const QList<NodeState> &/*nodeStateList*/)
381 void GenericPropertiesWidget::anchorsChanged(const NodeState &/*nodeState*/)