diff --git a/src/Mod/TechDraw/App/DrawProjGroup.cpp b/src/Mod/TechDraw/App/DrawProjGroup.cpp index 933d5382a..0a5b70938 100644 --- a/src/Mod/TechDraw/App/DrawProjGroup.cpp +++ b/src/Mod/TechDraw/App/DrawProjGroup.cpp @@ -217,15 +217,11 @@ void DrawProjGroup::moveToCentre(void) App::DocumentObject * DrawProjGroup::getProjObj(const char *viewProjType) const { - const std::vector &views = Views.getValues(); - for(std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - - DrawView *view = dynamic_cast(*it); - if(view->getTypeId() == DrawProjGroupItem::getClassTypeId()) { - DrawProjGroupItem *projPtr = dynamic_cast(*it); - - if( strcmp(viewProjType, projPtr->Type.getValueAsString()) == 0 ) - return *it; + for( auto it : Views.getValues() ) { + auto projPtr( dynamic_cast(it) ); + if( projPtr && + strcmp(viewProjType, projPtr->Type.getValueAsString()) == 0 ) { + return it; } } @@ -234,17 +230,10 @@ App::DocumentObject * DrawProjGroup::getProjObj(const char *viewProjType) const bool DrawProjGroup::hasProjection(const char *viewProjType) const { - const std::vector &views = Views.getValues(); - - for(std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - - TechDraw::DrawView *view = dynamic_cast(*it); - if(view->getTypeId() == TechDraw::DrawProjGroupItem::getClassTypeId()) { - TechDraw::DrawProjGroupItem *projPtr = dynamic_cast(*it); - - if( strcmp(viewProjType, projPtr->Type.getValueAsString()) == 0 ) { - return true; - } + for( const auto it : Views.getValues() ) { + auto view( dynamic_cast(it) ); + if( view && strcmp(viewProjType, view->Type.getValueAsString()) == 0 ) { + return true; } } return false; @@ -269,14 +258,14 @@ bool DrawProjGroup::checkViewProjType(const char *in) App::DocumentObject * DrawProjGroup::addProjection(const char *viewProjType) { - DrawProjGroupItem *view = NULL; + DrawProjGroupItem *view( nullptr ); if ( checkViewProjType(viewProjType) && !hasProjection(viewProjType) ) { std::string FeatName = getDocument()->getUniqueObjectName("ProjItem"); - App::DocumentObject *docObj = getDocument()->addObject("TechDraw::DrawProjGroupItem", - FeatName.c_str()); + auto docObj( getDocument()->addObject( "TechDraw::DrawProjGroupItem", + FeatName.c_str() ) ); - view = dynamic_cast( docObj ); + view = static_cast( docObj ); view->Source.setValue( Source.getValue() ); view->ScaleType.setValue( ScaleType.getValue() ); view->Scale.setValue( Scale.getValue() ); @@ -351,23 +340,19 @@ void DrawProjGroup::setViewOrientation(DrawProjGroupItem *v, const char *projTyp int DrawProjGroup::removeProjection(const char *viewProjType) { if ( checkViewProjType(viewProjType) ) { - if(!hasProjection(viewProjType)) { + if( !hasProjection(viewProjType) ) { throw Base::Exception("The projection doesn't exist in the group"); } // Iterate through the child views and find the projection type - const std::vector &views = Views.getValues(); - for(std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - - TechDraw::DrawView *view = dynamic_cast(*it); - if(view->getTypeId() == TechDraw::DrawProjGroupItem::getClassTypeId()) { - TechDraw::DrawProjGroupItem *projPtr = dynamic_cast(*it); - + for( auto it : Views.getValues() ) { + auto projPtr( dynamic_cast(it) ); + if( projPtr ) { if ( strcmp(viewProjType, projPtr->Type.getValueAsString()) == 0 ) { // Remove from the document - getDocument()->remObject((*it)->getNameInDocument()); + getDocument()->remObject( it->getNameInDocument() ); moveToCentre(); - return views.size(); + return Views.getValues().size(); } } } @@ -382,7 +367,7 @@ void DrawProjGroup::arrangeViewPointers(DrawProjGroupItem *viewPtrs[10]) const viewPtrs[i] = NULL; } - DrawProjGroupItem *anchorView = dynamic_cast(Anchor.getValue()); + auto anchorView( dynamic_cast(Anchor.getValue()) ); if (!anchorView) { //TODO: Consider not requiring an anchor view, or allowing ones other than "Front" throw Base::Exception("No anchor view set in DrawProjGroup::arrangeViewPointers()"); @@ -397,8 +382,6 @@ void DrawProjGroup::arrangeViewPointers(DrawProjGroupItem *viewPtrs[10]) const } // Iterate through views and populate viewPtrs - DrawProjGroupItem* oView; - std::vector views = Views.getValues(); if ( strcmp(projType, "Third Angle") == 0 || strcmp(projType, "First Angle") == 0 ) { // Third Angle: FTL T FTRight @@ -409,10 +392,9 @@ void DrawProjGroup::arrangeViewPointers(DrawProjGroupItem *viewPtrs[10]) const // Right F L Rear // FTRight T FTL bool thirdAngle = (strcmp(projType, "Third Angle") == 0); - for (std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - if ((*it)->getTypeId().isDerivedFrom(DrawProjGroupItem::getClassTypeId())) { - oView = dynamic_cast(*it); - + for (auto it : Views.getValues()) { + auto oView( dynamic_cast(it) ); + if (oView) { const char *viewTypeCStr = oView->Type.getValueAsString(); if (strcmp(viewTypeCStr, "Front") == 0) { viewPtrs[thirdAngle ? 4 : 4] = oView; @@ -535,10 +517,9 @@ bool DrawProjGroup::distributeProjections() //!allow child DPGI's to be automatically positioned void DrawProjGroup::resetPositions(void) { - const std::vector &views = Views.getValues(); - for(std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - DrawView *view = dynamic_cast(*it); - if(view->getTypeId() == DrawProjGroupItem::getClassTypeId()) { + for( auto it : Views.getValues() ) { + auto view( dynamic_cast(it) ); + if( view ) { view->setAutoPos(true); //X,Y == 0?? } @@ -550,12 +531,11 @@ void DrawProjGroup::setFrontViewOrientation(const Base::Matrix4D &newMat) { viewOrientationMatrix.setValue(newMat); - DrawProjGroupItem *view; - std::vector views = Views.getValues(); - for (std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - if ((*it)->getTypeId().isDerivedFrom(DrawProjGroupItem::getClassTypeId())) { - view = dynamic_cast(*it); + for( auto it : Views.getValues() ) { + auto view( dynamic_cast(it) ); + if( view ) { setViewOrientation(view, view->Type.getValueAsString()); + // TODO: Seems we should ensure that modifying the view triggers this automatically? IR view->touch(); } } @@ -573,11 +553,9 @@ App::DocumentObjectExecReturn *DrawProjGroup::execute(void) Scale.setValue(autoScale); //Rebuild the DPGI's - const std::vector &views = Views.getValues(); - for(std::vector::const_iterator it = views.begin(); it != views.end(); ++it) { - App::DocumentObject *docObj = *it; - if(docObj->getTypeId().isDerivedFrom(DrawProjGroupItem::getClassTypeId())) { - DrawProjGroupItem *view = dynamic_cast(*it); + for( const auto it : Views.getValues() ) { + auto view( dynamic_cast(it) ); + if( view ) { view->ScaleType.setValue("Custom"); view->Scale.setValue(autoScale); view->Scale.setStatus(App::Property::ReadOnly,true); diff --git a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp index 3d06b374d..b0874173e 100644 --- a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp +++ b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp @@ -1012,10 +1012,16 @@ bool _isValidVertexes(Gui::Command* cmd) { //! verify that the Selection contains valid geometries for an Edge to Edge Dimension int _isValidEdgeToEdge(Gui::Command* cmd) { //TODO: can the edges be in 2 different features?? - int edgeType = isInvalid; std::vector selection = cmd->getSelection().getSelectionEx(); - TechDraw::DrawViewPart* objFeat0 = dynamic_cast(selection[0].getObject()); - //TechDraw::DrawViewPart* objFeat1 = dynamic_cast(selection[1].getObject()); + + auto objFeat0( dynamic_cast(selection[0].getObject()) ); + // getObject() can return null pointer, or dynamic_cast can fail + if ( !objFeat0 ) { + Base::Console().Error("Logic error in _isValidEdgeToEdge()\n"); + return isInvalid; + } + + int edgeType = isInvalid; const std::vector SubNames = selection[0].getSubNames(); if(SubNames.size() == 2) { //there are 2 if (TechDraw::DrawUtil::getGeomTypeFromName(SubNames[0]) == "Edge" && //they both start with "Edge" diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index 681600348..2f406a6a1 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -264,50 +264,48 @@ void MDIViewPage::attachTemplate(TechDraw::DrawTemplate *obj) } -int MDIViewPage::attachView(App::DocumentObject *obj) +bool MDIViewPage::attachView(App::DocumentObject *obj) { auto typeId(obj->getTypeId()); QGIView *qview(nullptr); if (typeId.isDerivedFrom(TechDraw::DrawViewSection::getClassTypeId()) ) { - qview = m_view->addViewSection( dynamic_cast(obj) ); + qview = m_view->addViewSection( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewPart::getClassTypeId()) ) { - qview = m_view->addViewPart( dynamic_cast(obj) ); + qview = m_view->addViewPart( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawProjGroup::getClassTypeId()) ) { - qview = m_view->addProjectionGroup( dynamic_cast(obj) ); + qview = m_view->addProjectionGroup( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewCollection::getClassTypeId()) ) { - qview = m_view->addDrawView( dynamic_cast(obj) ); + qview = m_view->addDrawView( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewDimension::getClassTypeId()) ) { - qview = m_view->addViewDimension( dynamic_cast(obj) ); + qview = m_view->addViewDimension( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewAnnotation::getClassTypeId()) ) { - qview = m_view->addDrawViewAnnotation( dynamic_cast(obj) ); + qview = m_view->addDrawViewAnnotation( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewSymbol::getClassTypeId()) ) { - qview = m_view->addDrawViewSymbol( dynamic_cast(obj) ); + qview = m_view->addDrawViewSymbol( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewClip::getClassTypeId()) ) { - qview = m_view->addDrawViewClip( dynamic_cast(obj) ); + qview = m_view->addDrawViewClip( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawViewSpreadsheet::getClassTypeId()) ) { - qview = m_view->addDrawViewSpreadsheet( dynamic_cast(obj) ); + qview = m_view->addDrawViewSpreadsheet( static_cast(obj) ); } else if (typeId.isDerivedFrom(TechDraw::DrawHatch::getClassTypeId()) ) { //Hatch is not attached like other Views (since it isn't really a View) + return true; } else { Base::Console().Log("Logic Error - Unknown view type in MDIViewPage::attachView\n"); } - if(!qview) - return -1; - else - return m_view->getViews().size(); + return (qview != nullptr); } diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.h b/src/Mod/TechDraw/Gui/MDIViewPage.h index d92c623d3..774dd611d 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.h +++ b/src/Mod/TechDraw/Gui/MDIViewPage.h @@ -92,7 +92,10 @@ protected: void findMissingViews( const std::vector &list, std::vector &missing); bool hasQView(App::DocumentObject *obj); bool orphanExists(const char *viewName, const std::vector &list); - int attachView(App::DocumentObject *obj); + + /// Attaches view of obj to m_view. Returns true on success, false otherwise + bool attachView(App::DocumentObject *obj); + void contextMenuEvent(QContextMenuEvent *event); void closeEvent(QCloseEvent*); QPrinter::PaperSize getPaperSize(int w, int h) const; diff --git a/src/Mod/TechDraw/Gui/QGIView.cpp b/src/Mod/TechDraw/Gui/QGIView.cpp index e85602d80..6e891258d 100644 --- a/src/Mod/TechDraw/Gui/QGIView.cpp +++ b/src/Mod/TechDraw/Gui/QGIView.cpp @@ -213,15 +213,19 @@ void QGIView::setPosition(qreal x, qreal y) double QGIView::getYInClip(double y) { - QGCustomClip* parentClip = dynamic_cast(parentItem()); + auto parentClip( dynamic_cast( parentItem() ) ); if (parentClip) { - QGIViewClip* parentView = dynamic_cast(parentClip->parentItem()); - TechDraw::DrawViewClip* parentFeat = dynamic_cast(parentView->getViewObject()); - double newY = parentFeat->Height.getValue() - y; - return newY; - } else { - Base::Console().Log("Logic Error - getYInClip called for child (%s) not in Clip\n",getViewName()); + auto parentView( dynamic_cast( parentClip->parentItem() ) ); + if (parentView) { + auto parentFeat( dynamic_cast(parentView->getViewObject()) ); + if (parentFeat) { + return parentFeat->Height.getValue() - y; + } + } } + + Base::Console().Log( "Logic Error - getYInClip called for child " + "(%s) not in Clip\n", getViewName() ); return 0; } diff --git a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp index 917a8be1f..498dd7df2 100644 --- a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp @@ -209,9 +209,9 @@ void QGIViewDimension::hover(bool state) void QGIViewDimension::updateView(bool update) { - if(getViewObject() == 0 || !getViewObject()->isDerivedFrom(TechDraw::DrawViewDimension::getClassTypeId())) + auto dim( dynamic_cast(getViewObject()) ); + if( dim == nullptr ) return; - TechDraw::DrawViewDimension *dim = dynamic_cast(getViewObject()); // Identify what changed to prevent complete redraw if(dim->Fontsize.isTouched() || diff --git a/src/Mod/TechDraw/Gui/QGVPage.cpp b/src/Mod/TechDraw/Gui/QGVPage.cpp index 42913bb69..fa60896eb 100644 --- a/src/Mod/TechDraw/Gui/QGVPage.cpp +++ b/src/Mod/TechDraw/Gui/QGVPage.cpp @@ -464,8 +464,7 @@ void QGVPage::toggleHatch(bool enable) int faceItemType = QGraphicsItem::UserType + 104; for (auto& c:partChildren) { if (c->type() == faceItemType) { - QGIFace* f = dynamic_cast(c); - f->toggleSvg(enable); + static_cast(c)->toggleSvg(enable); } } }