Skip to content

Commit

Permalink
Fixed the crashes because of dangling Component pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Dec 14, 2016
1 parent c791818 commit 8b76c51
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 78 deletions.
93 changes: 47 additions & 46 deletions OMEdit/OMEditGUI/Component/Component.cpp
Expand Up @@ -500,12 +500,17 @@ Component::Component(LibraryTreeItem *pLibraryTreeItem, Component *pParentCompon
mIsInheritedComponent = mpParentComponent->isInheritedComponent();
mComponentType = Component::Extend;
mTransformationString = "";
mpResizerRectangle = 0;
createNonExistingComponent();
mpDefaultComponentRectangle = 0;
mpDefaultComponentText = 0;
drawInheritedComponentsAndShapes();
setDialogAnnotation(QStringList());
mpOriginItem = 0;
mpBottomLeftResizerItem = 0;
mpTopLeftResizerItem = 0;
mpTopRightResizerItem = 0;
mpBottomRightResizerItem = 0;
if (mpLibraryTreeItem) {
connect(mpLibraryTreeItem, SIGNAL(loadedForComponent()), SLOT(handleLoaded()));
connect(mpLibraryTreeItem, SIGNAL(unLoadedForComponent()), SLOT(handleUnloaded()));
Expand All @@ -524,13 +529,18 @@ Component::Component(Component *pComponent, Component *pParentComponent, Compone
mpGraphicsView = mpParentComponent->getGraphicsView();
mTransformationString = mpReferenceComponent->getTransformationString();
mDialogAnnotation = mpReferenceComponent->getDialogAnnotation();
mpResizerRectangle = 0;
createNonExistingComponent();
mpDefaultComponentRectangle = 0;
mpDefaultComponentText = 0;
drawInheritedComponentsAndShapes();
mTransformation = Transformation(mpReferenceComponent->mTransformation);
setTransform(mTransformation.getTransformationMatrix());
mpOriginItem = 0;
mpBottomLeftResizerItem = 0;
mpTopLeftResizerItem = 0;
mpTopRightResizerItem = 0;
mpBottomRightResizerItem = 0;
updateToolTip();
if (mpLibraryTreeItem) {
connect(mpLibraryTreeItem, SIGNAL(loadedForComponent()), SLOT(handleLoaded()));
Expand Down Expand Up @@ -600,6 +610,7 @@ Component::Component(ComponentInfo *pComponentInfo, Component *pParentComponent)
mpGraphicsView = mpParentComponent->getGraphicsView();
mTransformationString = "";
mDialogAnnotation.clear();
mpResizerRectangle = 0;
createNonExistingComponent();
createDefaultComponent();
mpDefaultComponentRectangle->setVisible(true);
Expand All @@ -613,6 +624,10 @@ Component::Component(ComponentInfo *pComponentInfo, Component *pParentComponent)
mTransformation.parseTransformationString(transformation, boundingRect().width(), boundingRect().height());
setTransform(mTransformation.getTransformationMatrix());
mpOriginItem = 0;
mpBottomLeftResizerItem = 0;
mpTopLeftResizerItem = 0;
mpTopRightResizerItem = 0;
mpBottomRightResizerItem = 0;
updateToolTip();
}

Expand Down Expand Up @@ -743,7 +758,8 @@ Component* Component::getRootParentComponent()
CoOrdinateSystem Component::getCoOrdinateSystem() const
{
CoOrdinateSystem coOrdinateSystem;
if (mpLibraryTreeItem && !mpLibraryTreeItem->isNonExisting() && mpLibraryTreeItem->getLibraryType() == LibraryTreeItem::Modelica) {
if (mpLibraryTreeItem && !mpLibraryTreeItem->isNonExisting() && mpLibraryTreeItem->getLibraryType() == LibraryTreeItem::Modelica
&& mpLibraryTreeItem->getModelWidget()) {
if (mpLibraryTreeItem->isConnector()) {
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
coOrdinateSystem = mpLibraryTreeItem->getModelWidget()->getIconGraphicsView()->mCoOrdinateSystem;
Expand Down Expand Up @@ -986,6 +1002,34 @@ void Component::removeConnectionDetails(LineAnnotation *pConnectorLineAnnotation
}
}

/*!
* \brief Component::removeChildren
* Removes the complete hirerchy of the Component.
*/
void Component::removeChildren()
{
foreach (Component *pInheritedComponent, mInheritedComponentsList) {
pInheritedComponent->removeChildren();
pInheritedComponent->setParentItem(0);
mpGraphicsView->removeItem(pInheritedComponent);
delete pInheritedComponent;
}
mInheritedComponentsList.clear();
foreach (Component *pComponent, mComponentsList) {
pComponent->removeChildren();
pComponent->setParentItem(0);
mpGraphicsView->removeItem(pComponent);
delete pComponent;
}
mComponentsList.clear();
foreach (ShapeAnnotation *pShapeAnnotation, mShapesList) {
pShapeAnnotation->setParentItem(0);
mpGraphicsView->removeItem(pShapeAnnotation);
delete pShapeAnnotation;
}
mShapesList.clear();
}

void Component::emitAdded()
{
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
Expand Down Expand Up @@ -1034,7 +1078,7 @@ void Component::componentParameterHasChanged()
QString Component::getParameterDisplayString(QString parameterName)
{
/* How to get the display value,
* 0. If the component is inherited component then check if the value is avaialble in the class extends modifiers.
* 0. If the component is inherited component then check if the value is available in the class extends modifiers.
* 1. Check if the value is available in component modifier.
* 2. Check if the value is available in the component's class as a parameter or variable.
* 3. Find the value in extends classes and check if the value is present in extends modifier.
Expand Down Expand Up @@ -1354,49 +1398,6 @@ void Component::createClassShapes()
}
}

/*!
* \brief Component::removeChildren
* Removes the complete hirerchy of the Component.
*/
void Component::removeChildren()
{
foreach (Component *pInheritedComponent, mInheritedComponentsList) {
pInheritedComponent->removeChildren();
if (pInheritedComponent->getLibraryTreeItem()) {
disconnect(pInheritedComponent->getLibraryTreeItem(), SIGNAL(loadedForComponent()), pInheritedComponent, SLOT(handleLoaded()));
disconnect(pInheritedComponent->getLibraryTreeItem(), SIGNAL(unLoadedForComponent()), pInheritedComponent, SLOT(handleUnloaded()));
disconnect(pInheritedComponent->getLibraryTreeItem(), SIGNAL(shapeAddedForComponent()), pInheritedComponent, SLOT(handleShapeAdded()));
disconnect(pInheritedComponent->getLibraryTreeItem(), SIGNAL(componentAddedForComponent()), pInheritedComponent, SLOT(handleComponentAdded()));
}
pInheritedComponent->setParentItem(0);
mpGraphicsView->removeItem(pInheritedComponent);
pInheritedComponent = 0;
delete pInheritedComponent;
}
mInheritedComponentsList.clear();
foreach (Component *pComponent, mComponentsList) {
pComponent->removeChildren();
if (pComponent->getLibraryTreeItem()) {
disconnect(pComponent->getLibraryTreeItem(), SIGNAL(loadedForComponent()), pComponent, SLOT(handleLoaded()));
disconnect(pComponent->getLibraryTreeItem(), SIGNAL(unLoadedForComponent()), pComponent, SLOT(handleUnloaded()));
disconnect(pComponent->getLibraryTreeItem(), SIGNAL(shapeAddedForComponent()), pComponent, SLOT(handleShapeAdded()));
disconnect(pComponent->getLibraryTreeItem(), SIGNAL(componentAddedForComponent()), pComponent, SLOT(handleComponentAdded()));
}
pComponent->setParentItem(0);
mpGraphicsView->removeItem(pComponent);
pComponent = 0;
delete pComponent;
}
mComponentsList.clear();
foreach (ShapeAnnotation *pShapeAnnotation, mShapesList) {
pShapeAnnotation->setParentItem(0);
mpGraphicsView->removeItem(pShapeAnnotation);
pShapeAnnotation = 0;
delete pShapeAnnotation;
}
mShapesList.clear();
}

void Component::createActions()
{
// Parameters Action
Expand Down Expand Up @@ -1738,7 +1739,7 @@ void Component::handleUnloaded()
{
removeChildren();
showNonExistingOrDefaultComponentIfNeeded();
emitChanged();
emitDeleted();
Component *pComponent = getRootParentComponent();
pComponent->updateConnections();
}
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Component/Component.h
Expand Up @@ -211,6 +211,7 @@ class Component : public QObject, public QGraphicsItem
void applyRotation(qreal angle);
void addConnectionDetails(LineAnnotation *pConnectorLineAnnotation);
void removeConnectionDetails(LineAnnotation *pConnectorLineAnnotation);
void removeChildren();
void emitAdded();
void emitTransformChange() {emit transformChange();}
void emitTransformHasChanged();
Expand Down Expand Up @@ -272,7 +273,6 @@ class Component : public QObject, public QGraphicsItem
void showNonExistingOrDefaultComponentIfNeeded();
void createClassInheritedComponents();
void createClassShapes();
void removeChildren();
void createActions();
void createResizerItems();
void getResizerItemsPositions(qreal *x1, qreal *y1, qreal *x2, qreal *y2);
Expand Down
16 changes: 8 additions & 8 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -909,16 +909,16 @@ void DeleteConnectionCommand::redo()
{
// Remove the start component connection details.
Component *pStartComponent = mpConnectionLineAnnotation->getStartComponent();
if (pStartComponent->getRootParentComponent()) {
if (pStartComponent && pStartComponent->getRootParentComponent()) {
pStartComponent->getRootParentComponent()->removeConnectionDetails(mpConnectionLineAnnotation);
} else {
} else if (pStartComponent) {
pStartComponent->removeConnectionDetails(mpConnectionLineAnnotation);
}
// Remove the end component connection details.
Component *pEndComponent = mpConnectionLineAnnotation->getEndComponent();
if (pEndComponent->getRootParentComponent()) {
if (pEndComponent && pEndComponent->getRootParentComponent()) {
pEndComponent->getRootParentComponent()->removeConnectionDetails(mpConnectionLineAnnotation);
} else {
} else if (pEndComponent) {
pEndComponent->removeConnectionDetails(mpConnectionLineAnnotation);
}
mpConnectionLineAnnotation->getGraphicsView()->deleteConnectionFromList(mpConnectionLineAnnotation);
Expand All @@ -935,16 +935,16 @@ void DeleteConnectionCommand::undo()
{
// Add the start component connection details.
Component *pStartComponent = mpConnectionLineAnnotation->getStartComponent();
if (pStartComponent->getRootParentComponent()) {
if (pStartComponent && pStartComponent->getRootParentComponent()) {
pStartComponent->getRootParentComponent()->addConnectionDetails(mpConnectionLineAnnotation);
} else {
} else if (pStartComponent) {
pStartComponent->addConnectionDetails(mpConnectionLineAnnotation);
}
// Add the end component connection details.
Component *pEndComponent = mpConnectionLineAnnotation->getEndComponent();
if (pEndComponent->getRootParentComponent()) {
if (pEndComponent && pEndComponent->getRootParentComponent()) {
pEndComponent->getRootParentComponent()->addConnectionDetails(mpConnectionLineAnnotation);
} else {
} else if (pEndComponent) {
pEndComponent->addConnectionDetails(mpConnectionLineAnnotation);
}
mpConnectionLineAnnotation->getGraphicsView()->addConnectionToList(mpConnectionLineAnnotation);
Expand Down
15 changes: 2 additions & 13 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1347,18 +1347,6 @@ void LibraryTreeModel::createLibraryTreeItems(QFileInfo fileInfo, LibraryTreeIte
endInsertRows();
}

/*!
* \brief LibraryTreeModel::loadNonExistingLibraryTreeItem
* \param pLibraryTreeItem
*/
void LibraryTreeModel::loadNonExistingLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem)
{
pLibraryTreeItem->emitLoaded();
for (int i = 0; i < pLibraryTreeItem->childrenSize(); i++) {
loadNonExistingLibraryTreeItem(pLibraryTreeItem->child(i));
}
}

/*!
* \brief LibraryTreeModel::checkIfAnyNonExistingClassLoaded
* Checks which non-existing classes are loaded and then call loaded for them.
Expand All @@ -1370,7 +1358,7 @@ void LibraryTreeModel::checkIfAnyNonExistingClassLoaded()
LibraryTreeItem *pLibraryTreeItem = mNonExistingLibraryTreeItemsList.at(i);
if (!pLibraryTreeItem->isNonExisting()) {
removeNonExistingLibraryTreeItem(pLibraryTreeItem);
loadNonExistingLibraryTreeItem(pLibraryTreeItem);
pLibraryTreeItem->emitLoaded();
i = 0; //Restart iteration
} else {
i++;
Expand Down Expand Up @@ -2373,6 +2361,7 @@ void LibraryTreeModel::unloadClassHelper(LibraryTreeItem *pLibraryTreeItem, Libr
pMdiSubWindow->close();
pMdiSubWindow->deleteLater();
}
pLibraryTreeItem->getModelWidget()->clearGraphicsViews();
pLibraryTreeItem->getModelWidget()->deleteLater();
pLibraryTreeItem->setModelWidget(0);
}
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.h
Expand Up @@ -240,7 +240,6 @@ class LibraryTreeModel : public QAbstractItemModel
void createLibraryTreeItems(QFileInfo fileInfo, LibraryTreeItem *pParentLibraryTreeItem);
LibraryTreeItem* createLibraryTreeItem(LibraryTreeItem::LibraryType type, QString name, QString nameStructure, QString path, bool isSaved,
LibraryTreeItem *pParentLibraryTreeItem, int row = -1);
void loadNonExistingLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
void checkIfAnyNonExistingClassLoaded();
void addNonExistingLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem) {mNonExistingLibraryTreeItemsList.append(pLibraryTreeItem);}
void removeNonExistingLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem) {mNonExistingLibraryTreeItemsList.removeOne(pLibraryTreeItem);}
Expand Down
49 changes: 40 additions & 9 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -2938,28 +2938,33 @@ Component* ModelWidget::getConnectorComponent(Component *pConnectorComponent, QS
return pConnectorComponentFound;
}

/*!
* \brief ModelWidget::reDrawModelWidget
* Redraws the ModelWidget.
*/
void ModelWidget::reDrawModelWidget()
void ModelWidget::clearGraphicsViews()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
/* remove everything from the icon view */
mpIconGraphicsView->removeAllComponents();
removeClassComponents(StringHandler::Icon);
mpIconGraphicsView->removeAllShapes();
mpIconGraphicsView->removeAllConnections();
removeInheritedClassShapes(StringHandler::Icon);
removeInheritedClassComponents(StringHandler::Icon);
mpIconGraphicsView->scene()->clear();
/* remove everything from the diagram view */
mpDiagramGraphicsView->removeAllComponents();
removeClassComponents(StringHandler::Diagram);
mpDiagramGraphicsView->removeAllShapes();
mpDiagramGraphicsView->removeAllConnections();
removeInheritedClassShapes(StringHandler::Diagram);
removeInheritedClassComponents(StringHandler::Diagram);
removeInheritedClassConnections();
mpDiagramGraphicsView->scene()->clear();
}

/*!
* \brief ModelWidget::reDrawModelWidget
* Redraws the ModelWidget.
*/
void ModelWidget::reDrawModelWidget()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
clearGraphicsViews();
/* get model components, connection and shapes. */
if (getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
// read new metamodel anem
Expand Down Expand Up @@ -3790,7 +3795,7 @@ void ModelWidget::drawModelInheritedClassComponents(ModelWidget *pModelWidget, S

/*!
* \brief ModelWidget::removeInheritedClassComponents
* Removes all the class inherited class components.
* Removes all the class inherited components.
* \param viewType
*/
void ModelWidget::removeInheritedClassComponents(StringHandler::ViewType viewType)
Expand All @@ -3802,10 +3807,36 @@ void ModelWidget::removeInheritedClassComponents(StringHandler::ViewType viewTyp
pGraphicsView = mpDiagramGraphicsView;
}
foreach (Component *pComponent, pGraphicsView->getInheritedComponentsList()) {
pComponent->removeChildren();
pGraphicsView->deleteInheritedComponentFromList(pComponent);
pGraphicsView->removeItem(pComponent->getOriginItem());
delete pComponent->getOriginItem();
pGraphicsView->removeItem(pComponent);
pComponent->emitDeleted();
delete pComponent;
}
}

/*!
* \brief ModelWidget::removeClassComponents
* Removes all the class components.
* \param viewType
*/
void ModelWidget::removeClassComponents(StringHandler::ViewType viewType)
{
GraphicsView *pGraphicsView = 0;
if (viewType == StringHandler::Icon) {
pGraphicsView = mpIconGraphicsView;
} else {
pGraphicsView = mpDiagramGraphicsView;
}
foreach (Component *pComponent, pGraphicsView->getComponentsList()) {
pComponent->removeChildren();
pGraphicsView->deleteComponentFromList(pComponent);
pGraphicsView->removeItem(pComponent->getOriginItem());
delete pComponent->getOriginItem();
pGraphicsView->removeItem(pComponent);
pComponent->emitDeleted();
delete pComponent;
}
}
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -364,6 +364,7 @@ class ModelWidget : public QWidget
void loadConnections();
void createModelWidgetComponents();
Component* getConnectorComponent(Component *pConnectorComponent, QString connectorName);
void clearGraphicsViews();
void reDrawModelWidget();
bool validateText(LibraryTreeItem **pLibraryTreeItem);
bool modelicaEditorTextChanged(LibraryTreeItem **pLibraryTreeItem);
Expand Down Expand Up @@ -416,6 +417,7 @@ class ModelWidget : public QWidget
void getModelIconDiagramShapes(StringHandler::ViewType viewType);
void drawModelInheritedClassComponents(ModelWidget *pModelWidget, StringHandler::ViewType viewType);
void removeInheritedClassComponents(StringHandler::ViewType viewType);
void removeClassComponents(StringHandler::ViewType viewType);
void getModelComponents();
void drawModelIconComponents();
void drawModelDiagramComponents();
Expand Down

0 comments on commit 8b76c51

Please sign in to comment.