aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/stateseditor
diff options
context:
space:
mode:
authorThomas Hartmann <Thomas.Hartmann@theqtcompany.com>2016-09-19 17:17:13 +0200
committerTim Jenssen <tim.jenssen@qt.io>2016-09-20 10:31:16 +0000
commitd7a23f97163b2d103f71642e110790e4215b3153 (patch)
tree11b939d602e0020c7d49865ec1fbf52767bac8c3 /src/plugins/qmldesigner/components/stateseditor
parentb225a5284214dbe0d8630f690d630bd30c8ef68b (diff)
QmlDesigner: Allow to edit when condition in states editor
The when condition for each state can now be edited. Change-Id: If569e3f22e2eee34b9333b3e139da59768649811 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Diffstat (limited to 'src/plugins/qmldesigner/components/stateseditor')
-rw-r--r--src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp36
-rw-r--r--src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h8
-rw-r--r--src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp59
-rw-r--r--src/plugins/qmldesigner/components/stateseditor/stateseditorview.h4
4 files changed, 104 insertions, 3 deletions
diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp
index 4eba23b2f3e..e02fbf6438b 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp
@@ -30,7 +30,9 @@
#include <nodelistproperty.h>
#include <modelnode.h>
+#include <bindingproperty.h>
#include <variantproperty.h>
+#include <rewriterview.h>
#include <coreplugin/icore.h>
#include <coreplugin/messagebox.h>
@@ -115,9 +117,18 @@ QVariant StatesEditorModel::data(const QModelIndex &index, int role) const
else
return QString("image://qmldesigner_stateseditor/%1-%2").arg(index.internalId()).arg(randomNumber);
}
- case InternalNodeId : return index.internalId();
+ case InternalNodeId: return index.internalId();
+
+ case HasWhenCondition: return stateNode.isValid() && stateNode.hasProperty("when");
+
+ case WhenConditionString: {
+ if (stateNode.isValid() && stateNode.hasBindingProperty("when"))
+ return stateNode.bindingProperty("when").expression();
+ else
+ return QString();
}
+ }
return QVariant();
}
@@ -127,7 +138,9 @@ QHash<int, QByteArray> StatesEditorModel::roleNames() const
static QHash<int, QByteArray> roleNames{
{StateNameRole, "stateName"},
{StateImageSourceRole, "stateImageSource"},
- {InternalNodeId, "internalNodeId"}
+ {InternalNodeId, "internalNodeId"},
+ {HasWhenCondition, "hasWhenCondition"},
+ {WhenConditionString, "whenConditionString"}
};
return roleNames;
}
@@ -182,4 +195,23 @@ void StatesEditorModel::renameState(int internalNodeId, const QString &newName)
}
+void StatesEditorModel::setWhenCondition(int internalNodeId, const QString &condition)
+{
+ m_statesEditorView->setWhenCondition(internalNodeId, condition);
+}
+
+void StatesEditorModel::resetWhenCondition(int internalNodeId)
+{
+ m_statesEditorView->resetWhenCondition(internalNodeId);
+}
+
+QStringList StatesEditorModel::autoComplete(const QString &text, int pos, bool explicitComplete)
+{
+ Model *model = m_statesEditorView->model();
+ if (model && model->rewriterView())
+ return model->rewriterView()->autoComplete(text, pos, explicitComplete);
+
+ return QStringList();
+}
+
} // namespace QmlDesigner
diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h
index 0dbc8c09252..614e106e45d 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h
@@ -42,7 +42,9 @@ class StatesEditorModel : public QAbstractListModel
enum {
StateNameRole = Qt::DisplayRole,
StateImageSourceRole = Qt::UserRole,
- InternalNodeId
+ InternalNodeId,
+ HasWhenCondition,
+ WhenConditionString
};
public:
@@ -58,9 +60,13 @@ public:
void removeState(int stateIndex);
void updateState(int beginIndex, int endIndex);
Q_INVOKABLE void renameState(int internalNodeId, const QString &newName);
+ Q_INVOKABLE void setWhenCondition(int internalNodeId, const QString &condition);
+ Q_INVOKABLE void resetWhenCondition(int internalNodeId);
+ Q_INVOKABLE QStringList autoComplete(const QString &text, int pos, bool explicitComplete);
void reset();
+
signals:
void countChanged();
void changedToState(int n);
diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
index 493fbf3bd76..8e404fbfe43 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp
@@ -33,10 +33,12 @@
#include <nodemetainfo.h>
+#include <bindingproperty.h>
#include <variantproperty.h>
#include <nodelistproperty.h>
#include <qmlitemnode.h>
+#include <qmlstate.h>
namespace QmlDesigner {
@@ -102,6 +104,10 @@ void StatesEditorView::synchonizeCurrentStateFromWidget()
{
if (!model())
return;
+
+ if (m_block)
+ return;
+
int internalId = m_statesEditorWidget->currentStateInternalId();
if (internalId > 0 && hasModelNodeForInternalId(internalId)) {
@@ -245,6 +251,48 @@ void StatesEditorView::renameState(int internalNodeId, const QString &newName)
}
}
+void StatesEditorView::setWhenCondition(int internalNodeId, const QString &condition)
+{
+ if (m_block)
+ return;
+
+ m_block = true;
+
+ if (hasModelNodeForInternalId(internalNodeId)) {
+ QmlModelState state(modelNodeForInternalId(internalNodeId));
+ try {
+ if (state.isValid())
+ state.modelNode().bindingProperty("when").setExpression(condition);
+
+ } catch (const RewritingException &e) {
+ e.showException();
+ }
+ }
+
+ m_block = false;
+}
+
+void StatesEditorView::resetWhenCondition(int internalNodeId)
+{
+ if (m_block)
+ return;
+
+ m_block = true;
+
+ if (hasModelNodeForInternalId(internalNodeId)) {
+ QmlModelState state(modelNodeForInternalId(internalNodeId));
+ try {
+ if (state.isValid() && state.modelNode().hasProperty("when"))
+ state.modelNode().removeProperty("when");
+
+ } catch (const RewritingException &e) {
+ e.showException();
+ }
+ }
+
+ m_block = false;
+}
+
void StatesEditorView::modelAttached(Model *model)
{
if (model == AbstractView::model())
@@ -272,6 +320,8 @@ void StatesEditorView::propertiesRemoved(const QList<AbstractProperty>& property
foreach (const AbstractProperty &property, propertyList) {
if (property.name() == "states" && property.parentModelNode().isRootNode())
resetModel();
+ if (property.name() == "when" && QmlModelState::isValidQmlModelState(property.parentModelNode()))
+ resetModel();
}
}
@@ -320,6 +370,15 @@ void StatesEditorView::nodeOrderChanged(const NodeListProperty &listProperty, co
resetModel();
}
+void StatesEditorView::bindingPropertiesChanged(const QList<BindingProperty> &propertyList, AbstractView::PropertyChangeFlags propertyChange)
+{
+ foreach (const BindingProperty &property, propertyList) {
+ if (property.name() == "when" && QmlModelState::isValidQmlModelState(property.parentModelNode()))
+ resetModel();
+ }
+
+}
+
void StatesEditorView::currentStateChanged(const ModelNode &node)
{
QmlModelState newQmlModelState(node);
diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h
index c7215874b68..a17a8d80162 100644
--- a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h
+++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h
@@ -43,6 +43,8 @@ public:
~StatesEditorView();
void renameState(int internalNodeId,const QString &newName);
+ void setWhenCondition(int internalNodeId, const QString &condition);
+ void resetWhenCondition(int internalNodeId);
bool validStateName(const QString &name) const;
QString currentStateName() const;
void setCurrentState(const QmlModelState &state);
@@ -66,6 +68,7 @@ public:
const NodeAbstractProperty &oldPropertyParent,
AbstractView::PropertyChangeFlags propertyChange) override;
void nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &movedNode, int oldIndex) override;
+ void bindingPropertiesChanged(const QList<BindingProperty>& propertyList, PropertyChangeFlags propertyChange) override;
// AbstractView
@@ -93,6 +96,7 @@ private:
QPointer<StatesEditorModel> m_statesEditorModel;
QPointer<StatesEditorWidget> m_statesEditorWidget;
int m_lastIndex;
+ bool m_block = false;
};
} // namespace QmlDesigner