1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "bindingeditor.h"
27 
28 #include <qmldesignerplugin.h>
29 #include <coreplugin/icore.h>
30 #include <coreplugin/actionmanager/actionmanager.h>
31 #include <bindingeditor/bindingeditordialog.h>
32 #include <qmldesignerconstants.h>
33 
34 #include <metainfo.h>
35 #include <qmlmodelnodeproxy.h>
36 #include <nodeabstractproperty.h>
37 #include <nodelistproperty.h>
38 #include <propertyeditorvalue.h>
39 
40 #include <bindingproperty.h>
41 #include <variantproperty.h>
42 
43 namespace QmlDesigner {
44 
45 static BindingEditor *s_lastBindingEditor = nullptr;
46 
BindingEditor(QObject *)47 BindingEditor::BindingEditor(QObject *)
48 {
49 }
50 
~BindingEditor()51 BindingEditor::~BindingEditor()
52 {
53     hideWidget();
54 }
55 
registerDeclarativeType()56 void BindingEditor::registerDeclarativeType()
57 {
58     qmlRegisterType<BindingEditor>("HelperWidgets", 2, 0, "BindingEditor");
59 }
60 
prepareDialog()61 void BindingEditor::prepareDialog()
62 {
63     QmlDesignerPlugin::emitUsageStatistics(Constants::EVENT_BINDINGEDITOR_OPENED);
64 
65     if (s_lastBindingEditor)
66         s_lastBindingEditor->hideWidget();
67 
68     s_lastBindingEditor = this;
69 
70     m_dialog = new BindingEditorDialog(Core::ICore::dialogParent());
71 
72     QObject::connect(m_dialog, &AbstractEditorDialog::accepted,
73                      this, &BindingEditor::accepted);
74     QObject::connect(m_dialog, &AbstractEditorDialog::rejected,
75                      this, &BindingEditor::rejected);
76 
77     m_dialog->setAttribute(Qt::WA_DeleteOnClose);
78 }
79 
showWidget()80 void BindingEditor::showWidget()
81 {
82     prepareDialog();
83     m_dialog->showWidget();
84 }
85 
showWidget(int x,int y)86 void BindingEditor::showWidget(int x, int y)
87 {
88     prepareDialog();
89     m_dialog->showWidget(x, y);
90 }
91 
hideWidget()92 void BindingEditor::hideWidget()
93 {
94     if (s_lastBindingEditor == this)
95         s_lastBindingEditor = nullptr;
96 
97     if (m_dialog) {
98         m_dialog->unregisterAutoCompletion(); //we have to do it separately, otherwise we have an autocompletion action override
99         m_dialog->close();
100     }
101 }
102 
bindingValue() const103 QString BindingEditor::bindingValue() const
104 {
105     if (!m_dialog)
106         return {};
107 
108     return m_dialog->editorValue();
109 }
110 
setBindingValue(const QString & text)111 void BindingEditor::setBindingValue(const QString &text)
112 {
113     if (m_dialog)
114         m_dialog->setEditorValue(text);
115 }
116 
setBackendValue(const QVariant & backendValue)117 void BindingEditor::setBackendValue(const QVariant &backendValue)
118 {
119     if (!backendValue.isNull() && backendValue.isValid()) {
120         m_backendValue = backendValue;
121         const QObject *backendValueObj = backendValue.value<QObject*>();
122         const PropertyEditorValue *propertyEditorValue = qobject_cast<const PropertyEditorValue *>(backendValueObj);
123         const ModelNode node = propertyEditorValue->modelNode();
124 
125         if (node.isValid()) {
126             m_backendValueTypeName = node.metaInfo().propertyTypeName(propertyEditorValue->name());
127 
128             if (m_backendValueTypeName == "alias" || m_backendValueTypeName == "unknown")
129                 if (QmlObjectNode::isValidQmlObjectNode(node))
130                     m_backendValueTypeName = QmlObjectNode(node).instanceType(propertyEditorValue->name());
131         }
132 
133         emit backendValueChanged();
134     }
135 }
136 
setModelNodeBackend(const QVariant & modelNodeBackend)137 void BindingEditor::setModelNodeBackend(const QVariant &modelNodeBackend)
138 {
139     if (!modelNodeBackend.isNull() && modelNodeBackend.isValid()) {
140         m_modelNodeBackend = modelNodeBackend;
141 
142         const auto modelNodeBackendObject = m_modelNodeBackend.value<QObject*>();
143 
144         const auto backendObjectCasted =
145                 qobject_cast<const QmlDesigner::QmlModelNodeProxy *>(modelNodeBackendObject);
146 
147         if (backendObjectCasted)
148             m_modelNode = backendObjectCasted->qmlObjectNode().modelNode();
149 
150         emit modelNodeBackendChanged();
151     }
152 }
153 
setStateModelNode(const QVariant & stateModelNode)154 void BindingEditor::setStateModelNode(const QVariant &stateModelNode)
155 {
156     if (stateModelNode.isValid()) {
157         m_stateModelNode = stateModelNode;
158         m_modelNode = m_stateModelNode.value<QmlDesigner::ModelNode>();
159 
160         if (m_modelNode.isValid())
161             m_backendValueTypeName = "bool";
162 
163         emit stateModelNodeChanged();
164     }
165 }
166 
setModelNode(const ModelNode & modelNode)167 void BindingEditor::setModelNode(const ModelNode &modelNode)
168 {
169     if (modelNode.isValid())
170         m_modelNode = modelNode;
171 }
172 
setBackendValueTypeName(const TypeName & backendValueTypeName)173 void BindingEditor::setBackendValueTypeName(const TypeName &backendValueTypeName)
174 {
175     m_backendValueTypeName = backendValueTypeName;
176 
177     emit backendValueChanged();
178 }
179 
prepareBindings()180 void BindingEditor::prepareBindings()
181 {
182     if (!m_modelNode.isValid() || m_backendValueTypeName.isEmpty())
183         return;
184 
185     const QList<QmlDesigner::ModelNode> allNodes = m_modelNode.view()->allModelNodes();
186 
187     QList<BindingEditorDialog::BindingOption> bindings;
188 
189     const QList<TypeName> variantTypes = {"alias", "unknown", "variant", "var"};
190     const QList<TypeName> numericTypes = {"double", "real", "int"};
191     const QList<TypeName> colorTypes = {"QColor", "color"};
192     auto isVariant = [&variantTypes](const TypeName &compareType) { return variantTypes.contains(compareType); };
193     auto isNumeric = [&numericTypes](const TypeName &compareType) { return numericTypes.contains(compareType); };
194     auto isColor = [&colorTypes](const TypeName &compareType) { return colorTypes.contains(compareType); };
195 
196     const bool skipTypeFiltering = isVariant(m_backendValueTypeName);
197     const bool targetTypeIsNumeric = isNumeric(m_backendValueTypeName);
198 
199     for (const auto &objnode : allNodes) {
200         BindingEditorDialog::BindingOption binding;
201         for (const auto &propertyName : objnode.metaInfo().propertyNames()) {
202             TypeName propertyTypeName = objnode.metaInfo().propertyTypeName(propertyName);
203 
204             if (skipTypeFiltering
205                     || (m_backendValueTypeName == propertyTypeName)
206                     || isVariant(propertyTypeName)
207                     || (targetTypeIsNumeric && isNumeric(propertyTypeName))) {
208                 binding.properties.append(QString::fromUtf8(propertyName));
209             }
210         }
211 
212         //dynamic properties:
213         for (const BindingProperty &bindingProperty : objnode.bindingProperties()) {
214             if (bindingProperty.isValid()) {
215                 if (bindingProperty.isDynamic()) {
216                     const TypeName dynamicTypeName = bindingProperty.dynamicTypeName();
217                     if (skipTypeFiltering
218                             || (dynamicTypeName == m_backendValueTypeName)
219                             || isVariant(dynamicTypeName)
220                             || (targetTypeIsNumeric && isNumeric(dynamicTypeName))) {
221                         binding.properties.append(QString::fromUtf8(bindingProperty.name()));
222                     }
223                 }
224             }
225         }
226         for (const VariantProperty &variantProperty : objnode.variantProperties()) {
227             if (variantProperty.isValid()) {
228                 if (variantProperty.isDynamic()) {
229                     const TypeName dynamicTypeName = variantProperty.dynamicTypeName();
230                     if (skipTypeFiltering
231                             || (dynamicTypeName == m_backendValueTypeName)
232                             || isVariant(dynamicTypeName)
233                             || (targetTypeIsNumeric && isNumeric(dynamicTypeName))) {
234                         binding.properties.append(QString::fromUtf8(variantProperty.name()));
235                     }
236                 }
237             }
238         }
239 
240         if (!binding.properties.isEmpty() && objnode.hasId()) {
241             binding.item = objnode.displayName();
242             bindings.append(binding);
243         }
244     }
245 
246     //singletons:
247     if (RewriterView *rv = m_modelNode.view()->rewriterView()) {
248         for (const QmlTypeData &data : rv->getQMLTypes()) {
249             if (!data.typeName.isEmpty()) {
250                 NodeMetaInfo metaInfo = m_modelNode.view()->model()->metaInfo(data.typeName.toUtf8());
251 
252                 if (metaInfo.isValid()) {
253                     BindingEditorDialog::BindingOption binding;
254 
255                     for (const PropertyName &propertyName : metaInfo.propertyNames()) {
256                         TypeName propertyTypeName = metaInfo.propertyTypeName(propertyName);
257 
258                         if (skipTypeFiltering
259                                 || (m_backendValueTypeName == propertyTypeName)
260                                 || (isVariant(propertyTypeName))
261                                 || (targetTypeIsNumeric && isNumeric(propertyTypeName))
262                                 || (isColor(m_backendValueTypeName) && isColor(propertyTypeName))) {
263                             binding.properties.append(QString::fromUtf8(propertyName));
264                         }
265                     }
266 
267                     if (!binding.properties.isEmpty()) {
268                         binding.item = data.typeName;
269                         bindings.append(binding);
270                     }
271                 }
272             }
273         }
274     }
275 
276     if (!bindings.isEmpty() && !m_dialog.isNull())
277         m_dialog->setAllBindings(bindings, m_backendValueTypeName);
278 }
279 
updateWindowName()280 void BindingEditor::updateWindowName()
281 {
282     if (!m_dialog.isNull() && !m_backendValueTypeName.isEmpty())
283         m_dialog->setWindowTitle(m_dialog->defaultTitle() + " [" + m_backendValueTypeName + "]");
284 }
285 
backendValue() const286 QVariant BindingEditor::backendValue() const
287 {
288     return m_backendValue;
289 }
290 
modelNodeBackend() const291 QVariant BindingEditor::modelNodeBackend() const
292 {
293     return m_modelNodeBackend;
294 }
295 
stateModelNode() const296 QVariant BindingEditor::stateModelNode() const
297 {
298     return m_stateModelNode;
299 }
300 
301 } // QmlDesigner namespace
302