QmlDesginer.statesEditor: avoid crashes during reattaching model

This commit is contained in:
Thomas Hartmann
2010-03-22 11:32:21 +01:00
parent e1e8e9fdf3
commit 6579d1d8c3
3 changed files with 18 additions and 5 deletions

View File

@@ -37,7 +37,7 @@
#include <math.h> #include <math.h>
enum { enum {
debug = false debug = true
}; };
namespace QmlDesigner { namespace QmlDesigner {
@@ -49,7 +49,8 @@ namespace Internal {
*/ */
StatesEditorView::StatesEditorView(StatesEditorModel *editorModel, QObject *parent) : StatesEditorView::StatesEditorView(StatesEditorModel *editorModel, QObject *parent) :
QmlModelView(parent), QmlModelView(parent),
m_editorModel(editorModel) m_editorModel(editorModel),
m_attachedToModel(false)
{ {
Q_ASSERT(m_editorModel); Q_ASSERT(m_editorModel);
} }
@@ -79,7 +80,9 @@ void StatesEditorView::setCurrentState(int index)
if (m_modelStates.isEmpty()) if (m_modelStates.isEmpty())
return; return;
Q_ASSERT(index >= 0 && index < m_modelStates.count()); Q_ASSERT(index < m_modelStates.count());
if (index == -1)
return;
if (m_modelStates.indexOf(currentState()) == index) if (m_modelStates.indexOf(currentState()) == index)
return; return;
@@ -174,17 +177,19 @@ void StatesEditorView::modelAttached(Model *model)
return; return;
m_modelStates.insert(0, baseState()); m_modelStates.insert(0, baseState());
m_attachedToModel = true;
m_editorModel->insertState(0, baseState().name()); m_editorModel->insertState(0, baseState().name());
// Add custom states // Add custom states
m_stateRootNode = QmlItemNode(rootModelNode()); m_stateRootNode = QmlItemNode(rootModelNode());
if (!m_stateRootNode.isValid()) if (!m_stateRootNode.isValid())
return; return;
for (int i = 0; i < m_stateRootNode.states().allStates().size(); ++i) { for (int i = 0; i < m_stateRootNode.states().allStates().size(); ++i) {
QmlModelState state = QmlItemNode(rootModelNode()).states().allStates().at(i); QmlModelState state = QmlItemNode(rootModelNode()).states().allStates().at(i);
insertModelState(i, state); insertModelState(i, state);
} }
} }
void StatesEditorView::modelAboutToBeDetached(Model *model) void StatesEditorView::modelAboutToBeDetached(Model *model)
@@ -192,6 +197,8 @@ void StatesEditorView::modelAboutToBeDetached(Model *model)
if (debug) if (debug)
qDebug() << __FUNCTION__; qDebug() << __FUNCTION__;
m_attachedToModel = false;
clearModelStates(); clearModelStates();
QmlModelView::modelAboutToBeDetached(model); QmlModelView::modelAboutToBeDetached(model);
@@ -390,6 +397,9 @@ QPixmap StatesEditorView::renderState(int i)
if (debug) if (debug)
qDebug() << __FUNCTION__ << i; qDebug() << __FUNCTION__ << i;
if (!m_attachedToModel)
return QPixmap();
Q_ASSERT(i >= 0 && i < m_modelStates.size()); Q_ASSERT(i >= 0 && i < m_modelStates.size());
nodeInstanceView()->setBlockChangeSignal(true); nodeInstanceView()->setBlockChangeSignal(true);
QmlModelState oldState = currentState(); QmlModelState oldState = currentState();

View File

@@ -53,6 +53,7 @@ public:
QPixmap renderState(int i); QPixmap renderState(int i);
QmlItemNode stateRootNode() { return m_stateRootNode; } QmlItemNode stateRootNode() { return m_stateRootNode; }
bool isAttachedToModel() const { return m_attachedToModel; }
protected: protected:
// AbstractView // AbstractView
@@ -95,6 +96,7 @@ private:
QList<int> m_updateTimerIdList; QList<int> m_updateTimerIdList;
QmlModelState m_oldRewriterAmendState; QmlModelState m_oldRewriterAmendState;
bool m_attachedToModel;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -115,7 +115,8 @@ bool StatesEditorWidgetPrivate::validStateName(const QString &name) const
void StatesEditorWidgetPrivate::currentStateChanged() void StatesEditorWidgetPrivate::currentStateChanged()
{ {
statesEditorView->setCurrentState(currentIndex()); if (statesEditorView->isAttachedToModel())
statesEditorView->setCurrentState(currentIndex());
} }
void StatesEditorWidgetPrivate::addState() void StatesEditorWidgetPrivate::addState()