2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2019-12-05 14:33:42 +01:00
|
|
|
|
|
|
|
|
#include "actioneditor.h"
|
|
|
|
|
|
|
|
|
|
#include <qmldesignerplugin.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2020-08-18 17:52:44 +02:00
|
|
|
#include <bindingeditor/actioneditordialog.h>
|
2019-12-05 14:33:42 +01:00
|
|
|
|
|
|
|
|
#include <metainfo.h>
|
|
|
|
|
#include <qmlmodelnodeproxy.h>
|
|
|
|
|
#include <nodeabstractproperty.h>
|
|
|
|
|
#include <nodelistproperty.h>
|
|
|
|
|
#include <propertyeditorvalue.h>
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
#include <bindingproperty.h>
|
|
|
|
|
#include <variantproperty.h>
|
|
|
|
|
|
|
|
|
|
#include <qmljs/qmljsscopechain.h>
|
|
|
|
|
#include <qmljs/qmljsvalueowner.h>
|
|
|
|
|
|
2022-06-30 18:57:07 +02:00
|
|
|
#include <tuple>
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
static Q_LOGGING_CATEGORY(ceLog, "qtc.qmldesigner.connectioneditor", QtWarningMsg)
|
|
|
|
|
|
2019-12-05 14:33:42 +01:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
|
|
static ActionEditor *s_lastActionEditor = nullptr;
|
|
|
|
|
|
|
|
|
|
ActionEditor::ActionEditor(QObject *)
|
|
|
|
|
: m_index(QModelIndex())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActionEditor::~ActionEditor()
|
|
|
|
|
{
|
|
|
|
|
hideWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::registerDeclarativeType()
|
|
|
|
|
{
|
|
|
|
|
qmlRegisterType<ActionEditor>("HelperWidgets", 2, 0, "ActionEditor");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 18:51:56 +02:00
|
|
|
void ActionEditor::prepareDialog()
|
2019-12-05 14:33:42 +01:00
|
|
|
{
|
|
|
|
|
if (s_lastActionEditor)
|
|
|
|
|
s_lastActionEditor->hideWidget();
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
s_lastActionEditor = this;
|
2019-12-05 14:33:42 +01:00
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
m_dialog = new ActionEditorDialog(Core::ICore::dialogParent());
|
2019-12-05 14:33:42 +01:00
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
QObject::connect(m_dialog, &AbstractEditorDialog::accepted,
|
2019-12-05 14:33:42 +01:00
|
|
|
this, &ActionEditor::accepted);
|
2020-08-18 17:52:44 +02:00
|
|
|
QObject::connect(m_dialog, &AbstractEditorDialog::rejected,
|
2019-12-05 14:33:42 +01:00
|
|
|
this, &ActionEditor::rejected);
|
|
|
|
|
|
|
|
|
|
m_dialog->setAttribute(Qt::WA_DeleteOnClose);
|
2020-06-17 18:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::showWidget()
|
|
|
|
|
{
|
|
|
|
|
prepareDialog();
|
|
|
|
|
m_dialog->showWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::showWidget(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
prepareDialog();
|
2019-12-05 14:33:42 +01:00
|
|
|
m_dialog->showWidget(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::hideWidget()
|
|
|
|
|
{
|
|
|
|
|
if (s_lastActionEditor == this)
|
|
|
|
|
s_lastActionEditor = nullptr;
|
2020-08-18 17:52:44 +02:00
|
|
|
|
|
|
|
|
if (m_dialog) {
|
|
|
|
|
m_dialog->unregisterAutoCompletion(); // we have to do it separately, otherwise we have an autocompletion action override
|
2019-12-05 14:33:42 +01:00
|
|
|
m_dialog->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
QString ActionEditor::connectionValue() const
|
2019-12-05 14:33:42 +01:00
|
|
|
{
|
|
|
|
|
if (!m_dialog)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return m_dialog->editorValue();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
void ActionEditor::setConnectionValue(const QString &text)
|
2019-12-05 14:33:42 +01:00
|
|
|
{
|
|
|
|
|
if (m_dialog)
|
|
|
|
|
m_dialog->setEditorValue(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ActionEditor::hasModelIndex() const
|
|
|
|
|
{
|
|
|
|
|
return m_index.isValid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::resetModelIndex()
|
|
|
|
|
{
|
|
|
|
|
m_index = QModelIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex ActionEditor::modelIndex() const
|
|
|
|
|
{
|
|
|
|
|
return m_index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ActionEditor::setModelIndex(const QModelIndex &index)
|
|
|
|
|
{
|
|
|
|
|
m_index = index;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
void ActionEditor::setModelNode(const ModelNode &modelNode)
|
|
|
|
|
{
|
|
|
|
|
if (modelNode.isValid())
|
|
|
|
|
m_modelNode = modelNode;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 17:03:42 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2020-08-18 17:52:44 +02:00
|
|
|
bool isLiteral(QmlJS::AST::Node *ast)
|
|
|
|
|
{
|
|
|
|
|
if (QmlJS::AST::cast<QmlJS::AST::StringLiteral *>(ast)
|
|
|
|
|
|| QmlJS::AST::cast<QmlJS::AST::NumericLiteral *>(ast)
|
|
|
|
|
|| QmlJS::AST::cast<QmlJS::AST::TrueLiteral *>(ast)
|
|
|
|
|
|| QmlJS::AST::cast<QmlJS::AST::FalseLiteral *>(ast))
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 17:03:42 +02:00
|
|
|
TypeName skipCpp(TypeName typeName)
|
|
|
|
|
{
|
|
|
|
|
// TODO remove after project storage introduction
|
|
|
|
|
|
|
|
|
|
if (typeName.contains("<cpp>."))
|
|
|
|
|
typeName.remove(0, 6);
|
|
|
|
|
|
|
|
|
|
return typeName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
2020-08-18 17:52:44 +02:00
|
|
|
void ActionEditor::prepareConnections()
|
|
|
|
|
{
|
|
|
|
|
if (!m_modelNode.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
BindingEditorWidget *bindingEditorWidget = m_dialog->bindingEditorWidget();
|
|
|
|
|
|
|
|
|
|
if (!bindingEditorWidget) {
|
|
|
|
|
qCInfo(ceLog) << Q_FUNC_INFO << "BindingEditorWidget is missing!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!bindingEditorWidget->qmlJsEditorDocument()) {
|
|
|
|
|
qCInfo(ceLog) << Q_FUNC_INFO << "QmlJsEditorDocument is missing!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare objects for analysing slots
|
|
|
|
|
const QmlJSTools::SemanticInfo &semanticInfo = bindingEditorWidget->qmljsdocument->semanticInfo();
|
|
|
|
|
const QList<QmlJS::AST::Node *> path = semanticInfo.rangePath(0);
|
|
|
|
|
const QmlJS::ContextPtr &context = semanticInfo.context;
|
|
|
|
|
const QmlJS::ScopeChain &scopeChain = semanticInfo.scopeChain(path);
|
|
|
|
|
|
2022-06-30 18:57:07 +02:00
|
|
|
constexpr auto typeWhiteList = std::make_tuple(
|
|
|
|
|
"string", "real", "int", "double", "bool", "QColor", "color", "QtQuick.Item", "QQuickItem");
|
2020-08-18 17:52:44 +02:00
|
|
|
|
2022-08-23 16:43:44 +02:00
|
|
|
auto isSkippedType = [](auto &&type) {
|
|
|
|
|
return !(type.isString() || type.isInteger() || type.isBool() || type.isColor()
|
|
|
|
|
|| type.isFloat() || type.isQmlItem());
|
|
|
|
|
};
|
2020-08-18 17:52:44 +02:00
|
|
|
static QList<PropertyName> methodBlackList({"toString", "destroy"});
|
|
|
|
|
|
|
|
|
|
QList<ActionEditorDialog::ConnectionOption> connections;
|
|
|
|
|
QList<ActionEditorDialog::SingletonOption> singletons;
|
|
|
|
|
QStringList states;
|
|
|
|
|
|
|
|
|
|
const QList<QmlDesigner::ModelNode> allNodes = m_modelNode.view()->allModelNodes();
|
|
|
|
|
for (const auto &modelNode : allNodes) {
|
|
|
|
|
// Skip nodes without specified id
|
|
|
|
|
if (!modelNode.hasId())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ActionEditorDialog::ConnectionOption connection(modelNode.id());
|
|
|
|
|
|
2022-06-09 17:03:42 +02:00
|
|
|
for (const auto &property : modelNode.metaInfo().properties()) {
|
2022-08-23 16:43:44 +02:00
|
|
|
if (isSkippedType(property.propertyType()))
|
2020-08-18 17:52:44 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-06-09 17:03:42 +02:00
|
|
|
connection.properties.append(
|
|
|
|
|
ActionEditorDialog::PropertyOption(QString::fromUtf8(property.name()),
|
2022-08-23 16:43:44 +02:00
|
|
|
skipCpp(property.propertyType().typeName()),
|
2022-06-09 17:03:42 +02:00
|
|
|
property.isWritable()));
|
2020-08-18 17:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const VariantProperty &variantProperty : modelNode.variantProperties()) {
|
2021-12-08 15:22:08 +01:00
|
|
|
if (variantProperty.isValid() && variantProperty.isDynamic()) {
|
2022-06-30 18:57:07 +02:00
|
|
|
if (!variantProperty.hasDynamicTypeName(typeWhiteList))
|
2021-12-08 15:22:08 +01:00
|
|
|
continue;
|
2020-08-18 17:52:44 +02:00
|
|
|
|
2021-12-08 15:22:08 +01:00
|
|
|
const QString name = QString::fromUtf8(variantProperty.name());
|
2022-06-09 17:03:42 +02:00
|
|
|
const bool writeable = modelNode.metaInfo().property(variantProperty.name()).isWritable();
|
2020-08-18 17:52:44 +02:00
|
|
|
|
2022-06-09 17:03:42 +02:00
|
|
|
connection.properties.append(
|
|
|
|
|
ActionEditorDialog::PropertyOption(name,
|
|
|
|
|
skipCpp(variantProperty.dynamicTypeName()),
|
|
|
|
|
writeable));
|
2020-08-18 17:52:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const auto &slotName : modelNode.metaInfo().slotNames()) {
|
2021-12-07 10:40:09 +01:00
|
|
|
if (slotName.startsWith("q_") || slotName.startsWith("_q_"))
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-06-20 12:35:13 +02:00
|
|
|
QmlJS::Document::MutablePtr newDoc
|
|
|
|
|
= QmlJS::Document::create(Utils::FilePath::fromString("<expression>"),
|
|
|
|
|
QmlJS::Dialect::JavaScript);
|
2021-12-07 10:40:09 +01:00
|
|
|
newDoc->setSource(modelNode.id() + "." + QLatin1String(slotName));
|
2020-08-18 17:52:44 +02:00
|
|
|
newDoc->parseExpression();
|
|
|
|
|
|
|
|
|
|
QmlJS::AST::ExpressionNode *expression = newDoc->expression();
|
|
|
|
|
|
|
|
|
|
if (expression && !isLiteral(expression)) {
|
|
|
|
|
QmlJS::ValueOwner *interp = context->valueOwner();
|
|
|
|
|
const QmlJS::Value *value = interp->convertToObject(scopeChain.evaluate(expression));
|
|
|
|
|
|
|
|
|
|
if (const QmlJS::FunctionValue *f = value->asFunctionValue()) {
|
|
|
|
|
// Only add slots with zero arguments
|
|
|
|
|
if (f->namedArgumentCount() == 0 && !methodBlackList.contains(slotName))
|
|
|
|
|
connection.methods.append(QString::fromUtf8(slotName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connection.methods.removeDuplicates();
|
|
|
|
|
connections.append(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Singletons
|
|
|
|
|
if (RewriterView *rv = m_modelNode.view()->rewriterView()) {
|
|
|
|
|
for (const QmlTypeData &data : rv->getQMLTypes()) {
|
|
|
|
|
if (!data.typeName.isEmpty()) {
|
|
|
|
|
NodeMetaInfo metaInfo = m_modelNode.view()->model()->metaInfo(data.typeName.toUtf8());
|
|
|
|
|
if (metaInfo.isValid()) {
|
|
|
|
|
ActionEditorDialog::SingletonOption singelton;
|
2022-06-09 17:03:42 +02:00
|
|
|
for (const auto &property : metaInfo.properties()) {
|
2022-08-23 16:43:44 +02:00
|
|
|
if (isSkippedType(property.propertyType()))
|
2020-08-18 17:52:44 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-08-23 16:43:44 +02:00
|
|
|
singelton.properties.append(ActionEditorDialog::PropertyOption(
|
|
|
|
|
QString::fromUtf8(property.name()),
|
|
|
|
|
skipCpp(property.propertyType().typeName()),
|
|
|
|
|
property.isWritable()));
|
2020-08-18 17:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!singelton.properties.isEmpty()) {
|
|
|
|
|
singelton.item = data.typeName;
|
|
|
|
|
singletons.append(singelton);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// States
|
2020-11-13 10:55:23 +01:00
|
|
|
for (const QmlModelState &state : QmlItemNode(m_modelNode.view()->rootModelNode()).states().allStates())
|
2020-08-18 17:52:44 +02:00
|
|
|
states.append(state.name());
|
|
|
|
|
|
|
|
|
|
if (!connections.isEmpty() && !m_dialog.isNull())
|
|
|
|
|
m_dialog->setAllConnections(connections, singletons, states);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 18:06:26 +02:00
|
|
|
void ActionEditor::updateWindowName(const QString &targetName)
|
2019-12-05 14:33:42 +01:00
|
|
|
{
|
2020-08-18 17:52:44 +02:00
|
|
|
if (!m_dialog.isNull()) {
|
2021-09-10 18:06:26 +02:00
|
|
|
if (targetName.isEmpty())
|
|
|
|
|
m_dialog->setWindowTitle(m_dialog->defaultTitle());
|
|
|
|
|
else
|
|
|
|
|
m_dialog->setWindowTitle(m_dialog->defaultTitle() + " [" + targetName + "]");
|
2019-12-05 14:33:42 +01:00
|
|
|
m_dialog->raise();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // QmlDesigner namespace
|