Commit 562d0c5f authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesigner: Refactor states editor

Reviewed-By: Thomas Hartmann
parent fabf76d2
...@@ -151,7 +151,7 @@ void ComponentView::nodeRemoved(const ModelNode & /*removedNode*/, const NodeAbs ...@@ -151,7 +151,7 @@ void ComponentView::nodeRemoved(const ModelNode & /*removedNode*/, const NodeAbs
// } // }
// } // }
//} //}
void ComponentView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
void ComponentView::nodeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {} void ComponentView::nodeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
void ComponentView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {} void ComponentView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {}
void ComponentView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {} void ComponentView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
......
...@@ -59,6 +59,7 @@ public: ...@@ -59,6 +59,7 @@ public:
void nodeCreated(const ModelNode &createdNode); void nodeCreated(const ModelNode &createdNode);
void nodeAboutToBeRemoved(const ModelNode &removedNode); void nodeAboutToBeRemoved(const ModelNode &removedNode);
void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange); void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange);
void nodeAboutToBeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange); void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId); void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId);
void propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList); void propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList);
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#include <allpropertiesbox.h> #include <allpropertiesbox.h>
#include <itemlibrary.h> #include <itemlibrary.h>
#include <navigatorview.h> #include <navigatorview.h>
#include <stateseditorwidget.h> #include <stateseditorview.h>
#include <formeditorview.h> #include <formeditorview.h>
#include <formeditorwidget.h> #include <formeditorwidget.h>
#include <basetexteditmodifier.h> #include <basetexteditmodifier.h>
...@@ -87,7 +87,7 @@ public: ...@@ -87,7 +87,7 @@ public:
QWeakPointer<ItemLibrary> itemLibrary; QWeakPointer<ItemLibrary> itemLibrary;
QWeakPointer<NavigatorView> navigator; QWeakPointer<NavigatorView> navigator;
QWeakPointer<AllPropertiesBox> allPropertiesBox; QWeakPointer<AllPropertiesBox> allPropertiesBox;
QWeakPointer<StatesEditorWidget> statesEditorWidget; QWeakPointer<StatesEditorView> statesEditorView;
QWeakPointer<QStackedWidget> stackedWidget; QWeakPointer<QStackedWidget> stackedWidget;
QWeakPointer<NodeInstanceView> nodeInstanceView; QWeakPointer<NodeInstanceView> nodeInstanceView;
...@@ -205,9 +205,9 @@ void DesignDocumentController::setAllPropertiesBox(AllPropertiesBox* allProperti ...@@ -205,9 +205,9 @@ void DesignDocumentController::setAllPropertiesBox(AllPropertiesBox* allProperti
m_d->allPropertiesBox = allPropertiesBox; m_d->allPropertiesBox = allPropertiesBox;
} }
void DesignDocumentController::setStatesEditorWidget(StatesEditorWidget* statesEditorWidget) void DesignDocumentController::setStatesEditorView(StatesEditorView* statesEditorView)
{ {
m_d->statesEditorWidget = statesEditorWidget; m_d->statesEditorView = statesEditorView;
} }
void DesignDocumentController::setFormEditorView(FormEditorView *formEditorView) void DesignDocumentController::setFormEditorView(FormEditorView *formEditorView)
...@@ -375,7 +375,7 @@ void DesignDocumentController::loadCurrentModel() ...@@ -375,7 +375,7 @@ void DesignDocumentController::loadCurrentModel()
m_d->stackedWidget->addWidget(m_d->textEdit.data()); m_d->stackedWidget->addWidget(m_d->textEdit.data());
// Will call setCurrentState (formEditorView etc has to be constructed first) // Will call setCurrentState (formEditorView etc has to be constructed first)
m_d->statesEditorWidget->setup(m_d->model.data()); m_d->model->attachView(m_d->statesEditorView.data());
m_d->allPropertiesBox->setModel(m_d->model.data()); m_d->allPropertiesBox->setModel(m_d->model.data());
......
...@@ -54,7 +54,7 @@ class RewriterView; ...@@ -54,7 +54,7 @@ class RewriterView;
class ItemLibrary; class ItemLibrary;
class NavigatorView; class NavigatorView;
class AllPropertiesBox; class AllPropertiesBox;
class StatesEditorWidget; class StatesEditorView;
class FormEditorView; class FormEditorView;
class DesignDocumentController: public QObject class DesignDocumentController: public QObject
...@@ -92,7 +92,7 @@ public: ...@@ -92,7 +92,7 @@ public:
void setItemLibrary(ItemLibrary* itemLibrary); void setItemLibrary(ItemLibrary* itemLibrary);
void setNavigator(NavigatorView* navigatorView); void setNavigator(NavigatorView* navigatorView);
void setAllPropertiesBox(AllPropertiesBox* allPropertiesBox); void setAllPropertiesBox(AllPropertiesBox* allPropertiesBox);
void setStatesEditorWidget(StatesEditorWidget* statesEditorWidget); void setStatesEditorView(StatesEditorView* statesEditorView);
void setFormEditorView(FormEditorView *formEditorView); void setFormEditorView(FormEditorView *formEditorView);
void setNodeInstanceView(NodeInstanceView *nodeInstanceView); void setNodeInstanceView(NodeInstanceView *nodeInstanceView);
......
...@@ -43,6 +43,7 @@ namespace QmlDesigner { ...@@ -43,6 +43,7 @@ namespace QmlDesigner {
void DesignDocumentControllerView::nodeCreated(const ModelNode & /*createdNode*/) {}; void DesignDocumentControllerView::nodeCreated(const ModelNode & /*createdNode*/) {};
void DesignDocumentControllerView::nodeAboutToBeRemoved(const ModelNode & /*removedNode*/) {}; void DesignDocumentControllerView::nodeAboutToBeRemoved(const ModelNode & /*removedNode*/) {};
void DesignDocumentControllerView::nodeRemoved(const ModelNode & /*removedNode*/, const NodeAbstractProperty & /*parentProperty*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}; void DesignDocumentControllerView::nodeRemoved(const ModelNode & /*removedNode*/, const NodeAbstractProperty & /*parentProperty*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {};
void DesignDocumentControllerView::nodeAboutToBeReparented(const ModelNode & /*node*/, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {};
void DesignDocumentControllerView::nodeReparented(const ModelNode & /*node*/, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}; void DesignDocumentControllerView::nodeReparented(const ModelNode & /*node*/, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {};
void DesignDocumentControllerView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {}; void DesignDocumentControllerView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {};
void DesignDocumentControllerView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {}; void DesignDocumentControllerView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {};
......
...@@ -45,6 +45,7 @@ public: ...@@ -45,6 +45,7 @@ public:
virtual void nodeCreated(const ModelNode &createdNode); virtual void nodeCreated(const ModelNode &createdNode);
virtual void nodeAboutToBeRemoved(const ModelNode &removedNode); virtual void nodeAboutToBeRemoved(const ModelNode &removedNode);
virtual void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange); virtual void nodeRemoved(const ModelNode &removedNode, const NodeAbstractProperty &parentProperty, PropertyChangeFlags propertyChange);
virtual void nodeAboutToBeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
virtual void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange); virtual void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
virtual void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId); virtual void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId);
virtual void propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList); virtual void propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList);
......
...@@ -130,6 +130,10 @@ void NavigatorView::nodeAboutToBeRemoved(const ModelNode &removedNode) ...@@ -130,6 +130,10 @@ void NavigatorView::nodeAboutToBeRemoved(const ModelNode &removedNode)
m_treeModel->removeSubTree(removedNode); m_treeModel->removeSubTree(removedNode);
} }
void NavigatorView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
}
void NavigatorView::nodeReparented(const ModelNode &node, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) void NavigatorView::nodeReparented(const ModelNode &node, const NodeAbstractProperty & /*newPropertyParent*/, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{ {
bool blocked = blockSelectionChangedSignal(true); bool blocked = blockSelectionChangedSignal(true);
......
...@@ -69,9 +69,9 @@ public: ...@@ -69,9 +69,9 @@ public:
void bindingPropertiesChanged(const QList<BindingProperty> &propertyList, PropertyChangeFlags propertyChange); void bindingPropertiesChanged(const QList<BindingProperty> &propertyList, PropertyChangeFlags propertyChange);
void nodeAboutToBeRemoved(const ModelNode &removedNode); void nodeAboutToBeRemoved(const ModelNode &removedNode);
void nodeReparented(const ModelNode &node, const ModelNode &oldParent, const ModelNode &newParent);
void nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &movedNode, int oldIndex); void nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &movedNode, int oldIndex);
void nodeAboutToBeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange); void nodeReparented(const ModelNode &node, const NodeAbstractProperty &newPropertyParent, const NodeAbstractProperty &oldPropertyParent, AbstractView::PropertyChangeFlags propertyChange);
void rootNodeTypeChanged(const QString &type, int majorVersion, int minorVersion); void rootNodeTypeChanged(const QString &type, int majorVersion, int minorVersion);
void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId); void nodeIdChanged(const ModelNode& node, const QString& newId, const QString& oldId);
......
...@@ -33,107 +33,143 @@ ...@@ -33,107 +33,143 @@
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <nodelistproperty.h>
#include <modelnode.h>
#include <variantproperty.h>
enum { enum {
debug = false debug = false
}; };
namespace QmlDesigner { namespace QmlDesigner {
namespace Internal {
StatesEditorModel::StatesEditorModel(QObject *parent) : StatesEditorModel::StatesEditorModel(StatesEditorView *view)
QAbstractListModel(parent), : QAbstractListModel(view),
m_updateCounter(0) m_statesEditorView(view),
m_updateCounter(0)
{ {
QHash<int, QByteArray> roleNames; QHash<int, QByteArray> roleNames;
roleNames.insert(StateNameRole, "stateName"); roleNames.insert(StateNameRole, "stateName");
roleNames.insert(StateImageSourceRole, "stateImageSource"); roleNames.insert(StateImageSourceRole, "stateImageSource");
roleNames.insert(NodeId, "nodeId");
setRoleNames(roleNames); setRoleNames(roleNames);
} }
int StatesEditorModel::count() const int StatesEditorModel::count() const
{ {
return m_stateNames.count(); return rowCount();
}
QModelIndex StatesEditorModel::index(int row, int column, const QModelIndex &parent) const
{
if (m_statesEditorView.isNull())
return QModelIndex();
int internalId = 0;
if (row > 0)
internalId = m_statesEditorView->rootModelNode().nodeListProperty("states").at(row - 1).internalId();
return hasIndex(row, column, parent) ? createIndex(row, column, internalId) : QModelIndex();
} }
int StatesEditorModel::rowCount(const QModelIndex &parent) const int StatesEditorModel::rowCount(const QModelIndex &parent) const
{ {
if (parent.isValid()) if (parent.isValid() || m_statesEditorView.isNull() || !m_statesEditorView->model())
return 0; return 0;
return m_stateNames.count();
if (!m_statesEditorView->rootModelNode().hasNodeListProperty("states"))
return 1;
return m_statesEditorView->rootModelNode().nodeListProperty("states").count() + 1;
}
void StatesEditorModel::reset()
{
QAbstractListModel::reset();
} }
QVariant StatesEditorModel::data(const QModelIndex &index, int role) const QVariant StatesEditorModel::data(const QModelIndex &index, int role) const
{ {
if (index.parent().isValid() || index.column() != 0) if (index.parent().isValid() || index.column() != 0 || m_statesEditorView.isNull() || !m_statesEditorView->hasModelNodeForInternalId(index.internalId()))
return QVariant(); return QVariant();
QVariant result; ModelNode stateNode;
if (index.internalId() > 0)
stateNode = m_statesEditorView->modelNodeForInternalId(index.internalId());
switch (role) { switch (role) {
case StateNameRole: { case StateNameRole: {
if (index.row()==0) if (index.row() == 0) {
result = QString(tr("base state", "Implicit default state")); return QString(tr("base state", "Implicit default state"));
else } else {
result = m_stateNames.at(index.row()); if (stateNode.hasVariantProperty("name")) {
break; return stateNode.variantProperty("name").value();
} } else {
case StateImageSourceRole: { return QVariant();
if (!m_statesView.isNull()) }
return QString("image://qmldesigner_stateseditor/%1-%2").arg(index.row()).arg(m_updateCounter); }
break;
} }
case StateImageSourceRole: return QString("image://qmldesigner_stateseditor/%1").arg(index.internalId());
case NodeId : return index.internalId();
} }
return result;
return QVariant();
} }
void StatesEditorModel::insertState(int i, const QString &name) void StatesEditorModel::insertState(int stateIndex)
{ {
beginInsertRows(QModelIndex(), i, i); if (stateIndex >= 0) {
m_stateNames.insert(i, name); const int index = stateIndex + 1;
beginInsertRows(QModelIndex(), index, index);
endInsertRows(); endInsertRows();
emit dataChanged(createIndex(i, 0), createIndex(i, 0)); emit dataChanged(createIndex(index, 0), createIndex(index, 0));
emit countChanged(); emit countChanged();
}
} }
void StatesEditorModel::removeState(int i) void StatesEditorModel::updateState(int stateIndex)
{ {
beginRemoveRows(QModelIndex(), i, i); if (stateIndex >= 0) {
const int index = stateIndex + 1;
m_stateNames.removeAt(i); emit dataChanged(createIndex(index, 0), createIndex(index, 0));
}
endRemoveRows();
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
emit countChanged();
} }
void StatesEditorModel::renameState(int i, const QString &newName) void StatesEditorModel::removeState(int stateIndex)
{ {
Q_ASSERT(i > 0 && i < m_stateNames.count()); if (stateIndex >= 0) {
const int index = stateIndex + 1;
if (m_stateNames[i] != newName) { beginRemoveRows(QModelIndex(), index, index);
if (m_stateNames.contains(newName) || newName.isEmpty()) {
QMessageBox::warning(0, tr("Invalid state name"),
newName.isEmpty() ? endRemoveRows();
tr("The empty string as a name is reserved for the base state.") :
tr("Name already used in another state")); emit dataChanged(createIndex(index, 0), createIndex(index, 0));
} else { emit countChanged();
m_stateNames.replace(i, newName);
m_statesView->renameState(i,newName);
emit dataChanged(createIndex(i, 0), createIndex(i, 0));
}
} }
} }
void StatesEditorModel::setStatesEditorView(StatesEditorView *statesView) void StatesEditorModel::renameState(int nodeId, const QString &newName)
{ {
m_statesView = statesView; if (newName.isEmpty() ||! m_statesEditorView->validStateName(newName)) {
QMessageBox::warning(0, tr("Invalid state name"),
newName.isEmpty() ?
tr("The empty string as a name is reserved for the base state.") :
tr("Name already used in another state"));
} else {
m_statesEditorView->renameState(nodeId, newName);
}
} }
void StatesEditorModel::emitChangedToState(int n) void StatesEditorModel::emitChangedToState(int n)
...@@ -141,5 +177,4 @@ void StatesEditorModel::emitChangedToState(int n) ...@@ -141,5 +177,4 @@ void StatesEditorModel::emitChangedToState(int n)
emit changedToState(n); emit changedToState(n);
} }
} // namespace Internal
} // namespace QmlDesigner } // namespace QmlDesigner
...@@ -33,11 +33,10 @@ ...@@ -33,11 +33,10 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QWeakPointer> #include <QWeakPointer>
#include <stateseditorview.h>
namespace QmlDesigner { namespace QmlDesigner {
namespace Internal { class StatesEditorView;
class StatesEditorModel : public QAbstractListModel class StatesEditorModel : public QAbstractListModel
{ {
...@@ -48,32 +47,34 @@ class StatesEditorModel : public QAbstractListModel ...@@ -48,32 +47,34 @@ class StatesEditorModel : public QAbstractListModel
enum { enum {
StateNameRole = Qt::DisplayRole, StateNameRole = Qt::DisplayRole,
StateImageSourceRole = Qt::UserRole, StateImageSourceRole = Qt::UserRole,
NodeId
}; };
public: public:
StatesEditorModel(QObject *parent); StatesEditorModel(StatesEditorView *view);
int count() const; int count() const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void insertState(int i, const QString &name); void insertState(int stateIndex);
void removeState(int i); void removeState(int stateIndex);
Q_INVOKABLE void renameState(int i, const QString &newName); void updateState(int stateIndex);
void setStatesEditorView(StatesEditorView *statesView); Q_INVOKABLE void renameState(int nodeId, const QString &newName);
void emitChangedToState(int n); void emitChangedToState(int n);
void reset();
signals: signals:
void countChanged(); void countChanged();
void changedToState(int n); void changedToState(int n);
private: private:
QList<QString> m_stateNames; QWeakPointer<StatesEditorView> m_statesEditorView;
QWeakPointer<StatesEditorView> m_statesView;
int m_updateCounter; int m_updateCounter;
}; };
} // namespace Itnernal
} // namespace QmlDesigner } // namespace QmlDesigner
#endif // STATESEDITORMODEL_H #endif // STATESEDITORMODEL_H
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
**************************************************************************/ **************************************************************************/
#include "stateseditorview.h" #include "stateseditorview.h"
#include "stateseditorwidget.h"
#include "stateseditormodel.h" #include "stateseditormodel.h"
#include <customnotifications.h> #include <customnotifications.h>
#include <rewritingexception.h> #include <rewritingexception.h>
...@@ -38,6 +39,8 @@ ...@@ -38,6 +39,8 @@
#include <QDebug> #include <QDebug>
#include <math.h> #include <math.h>
#include <nodemetainfo.h>
#include <variantproperty.h> #include <variantproperty.h>
#include <nodelistproperty.h> #include <nodelistproperty.h>
...@@ -46,108 +49,124 @@ enum { ...@@ -46,108 +49,124 @@ enum {
}; };
namespace QmlDesigner { namespace QmlDesigner {
namespace Internal {
/** /**
We always have 'one' current state, where we get updates from (see sceneChanged()). In case We always have 'one' current state, where we get updates from (see sceneChanged()). In case
the current state is the base state, we render the base state + all other states. the current state is the base state, we render the base state + all other states.
*/ */
StatesEditorView::StatesEditorView(StatesEditorModel *editorModel, QObject *parent) : StatesEditorView::StatesEditorView(QObject *parent) :
QmlModelView(parent), QmlModelView(parent),
m_editorModel(editorModel) m_statesEditorModel(new StatesEditorModel(this)),
m_lastIndex(-1)
{ {