From dfe02d40991c3f9e91aaaef1d28ca6630254068e Mon Sep 17 00:00:00 2001 From: mwganson Date: Mon, 6 Sep 2021 13:20:29 -0500 Subject: [PATCH] [TechDraw] prevent crash where user duplicates page without also duplicating dependencies --- src/Mod/TechDraw/App/DrawView.cpp | 20 ++++++++++++++++++++ src/Mod/TechDraw/App/DrawView.h | 1 + src/Mod/TechDraw/Gui/MDIViewPage.cpp | 6 +++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Mod/TechDraw/App/DrawView.cpp b/src/Mod/TechDraw/App/DrawView.cpp index e6eea03205..3386511412 100644 --- a/src/Mod/TechDraw/App/DrawView.cpp +++ b/src/Mod/TechDraw/App/DrawView.cpp @@ -239,6 +239,26 @@ void DrawView::onDocumentRestored() handleXYLock(); DrawView::execute(); } +/** + * @brief DrawView::countParentPages + * Fixes a crash in TechDraw when user creates duplicate page without dependencies + * In fixOrphans() we check how many parent pages an object has before deleting + * in case it is also a child of another duplicate page + * @return + */ +int DrawView::countParentPages() const +{ + int count = 0; + + std::vector parent = getInList(); + for (std::vector::iterator it = parent.begin(); it != parent.end(); ++it) { + if ((*it)->getTypeId().isDerivedFrom(DrawPage::getClassTypeId())) { + //page = static_cast(*it); + count++; + } + } + return count; +} DrawPage* DrawView::findParentPage() const { diff --git a/src/Mod/TechDraw/App/DrawView.h b/src/Mod/TechDraw/App/DrawView.h index 1768e06f8d..83c26e4576 100644 --- a/src/Mod/TechDraw/App/DrawView.h +++ b/src/Mod/TechDraw/App/DrawView.h @@ -85,6 +85,7 @@ public: virtual PyObject *getPyObject(void) override; virtual DrawPage* findParentPage() const; + virtual int countParentPages() const; virtual QRectF getRect() const; //must be overridden by derived class virtual double autoScale(void) const; virtual double autoScale(double w, double h) const; diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index 287242806d..13ae474eeb 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -481,7 +481,11 @@ void MDIViewPage::fixOrphans(bool force) m_view->removeQView(qv); } else { TechDraw::DrawPage* pp = qv->getViewObject()->findParentPage(); - if (thisPage != pp) { + /** avoid crash where a view might have more than one parent page + * if the user duplicated the page without duplicating dependencies + */ + int numParentPages = qv->getViewObject()->countParentPages(); + if (thisPage != pp && numParentPages == 1) { m_view->removeQView(qv); } }