diff --git a/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.cpp b/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.cpp index cf166dfc3ae..7f0db656005 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.cpp +++ b/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.cpp @@ -81,6 +81,7 @@ void ConnectionModel::resetModel() addModelNode(modelNode); } endResetModel(); + m_delegate->update(); } SignalHandlerProperty ConnectionModel::signalHandlerPropertyForRow(int rowNumber) const @@ -332,6 +333,8 @@ void ConnectionModel::addConnection() } newNode.signalHandlerProperty("onClicked").setSource(source); + + selectProperty(newNode.signalHandlerProperty("onClicked")); }); } } @@ -393,6 +396,32 @@ void ConnectionModel::remove(int row) deleteConnectionByRow(row); } +void ConnectionModel::setCurrentIndex(int i) +{ + if (m_currentIndex != i) { + m_currentIndex = i; + emit currentIndexChanged(); + } + m_delegate->setCurrentRow(i); +} + +int ConnectionModel::currentIndex() const +{ + return m_currentIndex; +} + +void ConnectionModel::selectProperty(const SignalHandlerProperty &property) +{ + for (int i = 0; i < rowCount(); i++) { + auto otherProperty = signalHandlerPropertyForRow(i); + + if (property == otherProperty) { + setCurrentIndex(i); + return; + } + } +} + void ConnectionModel::handleException() { QMessageBox::warning(nullptr, tr("Error"), m_exceptionError); @@ -559,13 +588,10 @@ QHash ConnectionModel::roleNames() const } ConnectionModelBackendDelegate::ConnectionModelBackendDelegate(ConnectionModel *parent) - : QObject(parent) - , m_signalDelegate(parent->connectionView()) - , m_okStatementDelegate(parent) - , m_koStatementDelegate(parent) - , m_conditionListModel(parent) - , m_propertyTreeModel(parent->connectionView()) - , m_propertyListProxyModel(&m_propertyTreeModel) + : QObject(parent), m_signalDelegate(parent->connectionView()), m_okStatementDelegate(parent), + m_koStatementDelegate(parent), m_conditionListModel(parent), + m_propertyTreeModel(parent->connectionView()), m_propertyListProxyModel(&m_propertyTreeModel) + { connect(&m_signalDelegate, &PropertyTreeModelDelegate::commitData, this, [this]() { handleTargetChanged(); @@ -760,16 +786,17 @@ void ConnectionModelBackendDelegate::setCurrentRow(int i) m_currentRow = i; - m_propertyTreeModel.resetModel(); - m_propertyListProxyModel.setRowAndInternalId(0, internalRootIndex); - - //setup - update(); } void ConnectionModelBackendDelegate::update() { + if (m_blockReflection) + return; + + m_propertyTreeModel.resetModel(); + m_propertyListProxyModel.setRowAndInternalId(0, internalRootIndex); + ConnectionModel *model = qobject_cast(parent()); QTC_ASSERT(model, return ); @@ -797,9 +824,6 @@ void ConnectionModelBackendDelegate::update() setSource(signalHandlerProperty.source()); - qDebug() << Q_FUNC_INFO - << removeOnFromSignalName(QString::fromUtf8(signalHandlerProperty.name())); - m_signalDelegate.setup(targetNodeName, removeOnFromSignalName(QString::fromUtf8(signalHandlerProperty.name()))); @@ -970,14 +994,14 @@ void ConnectionModelBackendDelegate::handleTargetChanged() const PropertyName handlerName = addOnToSignalName(m_signalDelegate.name()).toUtf8(); - qDebug() << Q_FUNC_INFO << m_signalDelegate.id() << handlerName; - const auto parentModelNode = signalHandlerProperty.parentModelNode(); QTC_ASSERT(parentModelNode.isValid(), return ); const auto newId = m_signalDelegate.id(); + const int internalId = signalHandlerProperty.parentModelNode().internalId(); + model->connectionView() ->executeInTransaction("ConnectionModelBackendDelegate::handleTargetChanged", [&]() { const auto oldTargetNodeName @@ -996,8 +1020,15 @@ void ConnectionModelBackendDelegate::handleTargetChanged() if (parent.isValid() && QmlItemNode::isValidQmlVisualNode(parent)) parent.nodeListProperty("data").reparentHere(parentModelNode); + else + model->connectionView()->rootModelNode().nodeListProperty("data").reparentHere( + parentModelNode); } }); + + model->selectProperty(model->connectionView() + ->modelNodeForInternalId(internalId) + .signalHandlerProperty(handlerName)); } void ConnectionModelBackendDelegate::handleOkStatementChanged() @@ -1051,12 +1082,14 @@ void ConnectionModelBackendDelegate::commitNewSource(const QString &source) SignalHandlerProperty signalHandlerProperty = model->signalHandlerPropertyForRow(currentRow()); + m_blockReflection = true; model->connectionView()->executeInTransaction("ConnectionModelBackendDelegate::commitNewSource", [&]() { signalHandlerProperty.setSource(source); }); setSource(signalHandlerProperty.source()); + m_blockReflection = false; } static ConnectionEditorStatements::MatchedStatement emptyStatement; @@ -1637,6 +1670,7 @@ void ConditionListModel::appendToken(const QString &value) void ConditionListModel::removeToken(int index) { + QTC_ASSERT(index < m_tokens.count(), return ); beginRemoveRows({}, index, index); m_tokens.remove(index, 1); diff --git a/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.h b/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.h index 706fceed2ff..bef5b79637d 100644 --- a/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.h +++ b/src/plugins/qmldesigner/components/connectioneditor/connectionmodel.h @@ -27,6 +27,9 @@ class ConnectionModel : public QStandardItemModel Q_PROPERTY(ConnectionModelBackendDelegate *delegate READ delegate CONSTANT) +public: + Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) + public: enum ColumnRoles { TargetModelNodeRow = 0, @@ -63,6 +66,14 @@ public: Q_INVOKABLE void add(); Q_INVOKABLE void remove(int row); + void setCurrentIndex(int i); + int currentIndex() const; + + void selectProperty(const SignalHandlerProperty &property); + +signals: + void currentIndexChanged(); + protected: void addModelNode(const ModelNode &modelNode); void addConnection(const ModelNode &modelNode); @@ -87,6 +98,7 @@ private: bool m_lock = false; QString m_exceptionError; ConnectionModelBackendDelegate *m_delegate = nullptr; + int m_currentIndex = -1; }; class ConditionListModel : public QAbstractListModel @@ -272,6 +284,7 @@ public: Q_INVOKABLE void addElse(); Q_INVOKABLE void removeElse(); + void setCurrentRow(int i); void update(); signals: @@ -283,8 +296,6 @@ signals: private: int currentRow() const; - void setCurrentRow(int i); - void handleException(); bool hasCondition() const; bool hasElse() const; @@ -324,6 +335,7 @@ private: QString m_source; PropertyTreeModel m_propertyTreeModel; PropertyListProxyModel m_propertyListProxyModel; + bool m_blockReflection = false; }; } // namespace QmlDesigner