2019-08-09 14:19:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "bindingeditor.h"
|
|
|
|
|
|
|
|
|
|
#include <qmldesignerplugin.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
|
2019-10-10 16:02:34 +02:00
|
|
|
#include <metainfo.h>
|
|
|
|
|
#include <qmlmodelnodeproxy.h>
|
|
|
|
|
#include <nodeabstractproperty.h>
|
|
|
|
|
#include <nodelistproperty.h>
|
|
|
|
|
#include <propertyeditorvalue.h>
|
2019-08-09 14:19:10 +02:00
|
|
|
|
2020-05-29 17:02:53 +02:00
|
|
|
#include <bindingproperty.h>
|
|
|
|
|
#include <variantproperty.h>
|
|
|
|
|
|
2019-08-09 14:19:10 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
|
|
static BindingEditor *s_lastBindingEditor = nullptr;
|
|
|
|
|
|
|
|
|
|
BindingEditor::BindingEditor(QObject *)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BindingEditor::~BindingEditor()
|
|
|
|
|
{
|
|
|
|
|
hideWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::registerDeclarativeType()
|
|
|
|
|
{
|
|
|
|
|
qmlRegisterType<BindingEditor>("HelperWidgets", 2, 0, "BindingEditor");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 13:46:14 +02:00
|
|
|
void BindingEditor::prepareDialog()
|
2019-08-09 14:19:10 +02:00
|
|
|
{
|
|
|
|
|
if (s_lastBindingEditor)
|
|
|
|
|
s_lastBindingEditor->hideWidget();
|
|
|
|
|
s_lastBindingEditor = this;
|
|
|
|
|
|
|
|
|
|
m_dialog = new BindingEditorDialog(Core::ICore::dialogParent());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QObject::connect(m_dialog, &BindingEditorDialog::accepted,
|
|
|
|
|
this, &BindingEditor::accepted);
|
|
|
|
|
QObject::connect(m_dialog, &BindingEditorDialog::rejected,
|
|
|
|
|
this, &BindingEditor::rejected);
|
|
|
|
|
|
|
|
|
|
m_dialog->setAttribute(Qt::WA_DeleteOnClose);
|
2020-06-09 13:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::showWidget()
|
|
|
|
|
{
|
|
|
|
|
prepareDialog();
|
|
|
|
|
m_dialog->showWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::showWidget(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
prepareDialog();
|
2019-08-09 14:19:10 +02:00
|
|
|
m_dialog->showWidget(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::hideWidget()
|
|
|
|
|
{
|
|
|
|
|
if (s_lastBindingEditor == this)
|
|
|
|
|
s_lastBindingEditor = nullptr;
|
|
|
|
|
if (m_dialog)
|
|
|
|
|
{
|
|
|
|
|
m_dialog->unregisterAutoCompletion(); //we have to do it separately, otherwise we have an autocompletion action override
|
|
|
|
|
m_dialog->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BindingEditor::bindingValue() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_dialog)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return m_dialog->editorValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::setBindingValue(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
if (m_dialog)
|
|
|
|
|
m_dialog->setEditorValue(text);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 16:02:34 +02:00
|
|
|
void BindingEditor::setBackendValue(const QVariant &backendValue)
|
|
|
|
|
{
|
|
|
|
|
if (!backendValue.isNull() && backendValue.isValid()) {
|
|
|
|
|
m_backendValue = backendValue;
|
|
|
|
|
const QObject *backendValueObj = backendValue.value<QObject*>();
|
|
|
|
|
const PropertyEditorValue *propertyEditorValue = qobject_cast<const PropertyEditorValue *>(backendValueObj);
|
2019-11-27 16:05:34 +01:00
|
|
|
const ModelNode node = propertyEditorValue->modelNode();
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2019-11-27 16:05:34 +01:00
|
|
|
if (node.isValid())
|
|
|
|
|
{
|
|
|
|
|
m_backendValueTypeName = node.metaInfo().propertyTypeName(propertyEditorValue->name());
|
|
|
|
|
|
|
|
|
|
if (m_backendValueTypeName == "alias" || m_backendValueTypeName == "unknown")
|
|
|
|
|
if (QmlObjectNode::isValidQmlObjectNode(node))
|
|
|
|
|
m_backendValueTypeName = QmlObjectNode(node).instanceType(propertyEditorValue->name());
|
|
|
|
|
}
|
2019-10-10 16:02:34 +02:00
|
|
|
|
|
|
|
|
emit backendValueChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::setModelNodeBackend(const QVariant &modelNodeBackend)
|
|
|
|
|
{
|
|
|
|
|
if (!modelNodeBackend.isNull() && modelNodeBackend.isValid()) {
|
|
|
|
|
m_modelNodeBackend = modelNodeBackend;
|
|
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
const auto modelNodeBackendObject = m_modelNodeBackend.value<QObject*>();
|
|
|
|
|
|
|
|
|
|
const auto backendObjectCasted =
|
|
|
|
|
qobject_cast<const QmlDesigner::QmlModelNodeProxy *>(modelNodeBackendObject);
|
|
|
|
|
|
|
|
|
|
if (backendObjectCasted) {
|
|
|
|
|
m_modelNode = backendObjectCasted->qmlObjectNode().modelNode();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 16:02:34 +02:00
|
|
|
emit modelNodeBackendChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
void BindingEditor::setStateModelNode(const QVariant &stateModelNode)
|
2019-10-10 16:02:34 +02:00
|
|
|
{
|
2019-11-12 17:06:06 +01:00
|
|
|
if (stateModelNode.isValid())
|
|
|
|
|
{
|
|
|
|
|
m_stateModelNode = stateModelNode;
|
|
|
|
|
m_modelNode = m_stateModelNode.value<QmlDesigner::ModelNode>();
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
if (m_modelNode.isValid())
|
|
|
|
|
m_backendValueTypeName = "bool";
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
emit stateModelNodeChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2020-06-09 14:50:26 +02:00
|
|
|
void BindingEditor::setModelNode(const ModelNode &modelNode)
|
|
|
|
|
{
|
|
|
|
|
if (modelNode.isValid())
|
|
|
|
|
m_modelNode = modelNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::setBackendValueTypeName(const TypeName &backendValueTypeName)
|
|
|
|
|
{
|
|
|
|
|
m_backendValueTypeName = backendValueTypeName;
|
|
|
|
|
|
|
|
|
|
emit backendValueChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
void BindingEditor::prepareBindings()
|
|
|
|
|
{
|
|
|
|
|
if (!m_modelNode.isValid() || m_backendValueTypeName.isEmpty())
|
|
|
|
|
return;
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
const QList<QmlDesigner::ModelNode> allNodes = m_modelNode.view()->allModelNodes();
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
QList<BindingEditorDialog::BindingOption> bindings;
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2020-06-09 14:50:26 +02:00
|
|
|
const QList<TypeName> variantTypes = {"alias", "unknown", "variant", "var"};
|
|
|
|
|
const bool skipTypeFiltering = variantTypes.contains(m_backendValueTypeName);
|
|
|
|
|
|
|
|
|
|
for (const auto &objnode : allNodes) {
|
2019-11-12 17:06:06 +01:00
|
|
|
BindingEditorDialog::BindingOption binding;
|
2020-06-09 14:50:26 +02:00
|
|
|
for (const auto &propertyName : objnode.metaInfo().propertyNames())
|
2019-11-27 16:05:34 +01:00
|
|
|
{
|
|
|
|
|
TypeName propertyTypeName = objnode.metaInfo().propertyTypeName(propertyName);
|
|
|
|
|
|
2020-06-09 14:50:26 +02:00
|
|
|
if ((m_backendValueTypeName == propertyTypeName)
|
|
|
|
|
|| skipTypeFiltering
|
|
|
|
|
|| variantTypes.contains(propertyTypeName)) {
|
2019-11-12 17:06:06 +01:00
|
|
|
binding.properties.append(QString::fromUtf8(propertyName));
|
2020-06-09 14:50:26 +02:00
|
|
|
}
|
2019-11-27 16:05:34 +01:00
|
|
|
}
|
2019-10-10 16:02:34 +02:00
|
|
|
|
2020-05-29 17:02:53 +02:00
|
|
|
//dynamic properties:
|
|
|
|
|
for (const BindingProperty &bindingProperty : objnode.bindingProperties()) {
|
|
|
|
|
if (bindingProperty.isValid()) {
|
|
|
|
|
if (bindingProperty.isDynamic()) {
|
2020-06-09 14:50:26 +02:00
|
|
|
const TypeName dynamicTypeName = bindingProperty.dynamicTypeName();
|
|
|
|
|
if ((dynamicTypeName == m_backendValueTypeName) || variantTypes.contains(dynamicTypeName)) {
|
2020-05-29 17:02:53 +02:00
|
|
|
binding.properties.append(QString::fromUtf8(bindingProperty.name()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const VariantProperty &variantProperty : objnode.variantProperties()) {
|
|
|
|
|
if (variantProperty.isValid()) {
|
|
|
|
|
if (variantProperty.isDynamic()) {
|
2020-06-09 14:50:26 +02:00
|
|
|
const TypeName dynamicTypeName = variantProperty.dynamicTypeName();
|
|
|
|
|
if ((dynamicTypeName == m_backendValueTypeName) || variantTypes.contains(dynamicTypeName)) {
|
2020-05-29 17:02:53 +02:00
|
|
|
binding.properties.append(QString::fromUtf8(variantProperty.name()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
if (!binding.properties.isEmpty() && objnode.hasId()) {
|
|
|
|
|
binding.item = objnode.displayName();
|
|
|
|
|
bindings.append(binding);
|
2019-10-10 16:02:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-12 17:06:06 +01:00
|
|
|
|
|
|
|
|
if (!bindings.isEmpty() && !m_dialog.isNull())
|
|
|
|
|
m_dialog->setAllBindings(bindings);
|
2019-11-27 16:05:34 +01:00
|
|
|
|
|
|
|
|
updateWindowName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindingEditor::updateWindowName()
|
|
|
|
|
{
|
|
|
|
|
if (!m_dialog.isNull() && !m_backendValueTypeName.isEmpty())
|
|
|
|
|
{
|
|
|
|
|
m_dialog->setWindowTitle(m_dialog->defaultTitle() + " [" + m_backendValueTypeName + "]");
|
|
|
|
|
}
|
2019-10-10 16:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant BindingEditor::backendValue() const
|
|
|
|
|
{
|
|
|
|
|
return m_backendValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant BindingEditor::modelNodeBackend() const
|
|
|
|
|
{
|
|
|
|
|
return m_modelNodeBackend;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 17:06:06 +01:00
|
|
|
QVariant BindingEditor::stateModelNode() const
|
|
|
|
|
{
|
|
|
|
|
return m_stateModelNode;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 12:09:01 +01:00
|
|
|
} // QmlDesigner namespace
|