From f230967146eaec3b74933c8e552ea596dd3327ae Mon Sep 17 00:00:00 2001 From: WandererFan Date: Tue, 13 Sep 2016 13:05:23 -0400 Subject: [PATCH] Only enable Commands when appropriate Remove dupl code --- src/Mod/TechDraw/Gui/CMakeLists.txt | 2 + src/Mod/TechDraw/Gui/Command.cpp | 155 +++++++++------------ src/Mod/TechDraw/Gui/CommandCreateDims.cpp | 112 +++++++-------- src/Mod/TechDraw/Gui/CommandDecorate.cpp | 69 +++------ src/Mod/TechDraw/Gui/DrawGuiUtil.cpp | 135 ++++++++++++++++++ src/Mod/TechDraw/Gui/DrawGuiUtil.h | 40 ++++++ 6 files changed, 317 insertions(+), 196 deletions(-) create mode 100644 src/Mod/TechDraw/Gui/DrawGuiUtil.cpp create mode 100644 src/Mod/TechDraw/Gui/DrawGuiUtil.h diff --git a/src/Mod/TechDraw/Gui/CMakeLists.txt b/src/Mod/TechDraw/Gui/CMakeLists.txt index 852d15f2f..4282c54d7 100644 --- a/src/Mod/TechDraw/Gui/CMakeLists.txt +++ b/src/Mod/TechDraw/Gui/CMakeLists.txt @@ -81,6 +81,8 @@ SET(TechDrawGui_SRCS TaskSectionView.ui TaskSectionView.cpp TaskSectionView.h + DrawGuiUtil.cpp + DrawGuiUtil.h ) SET(TechDrawGuiView_SRCS MDIViewPage.cpp diff --git a/src/Mod/TechDraw/Gui/Command.cpp b/src/Mod/TechDraw/Gui/Command.cpp index 06ca5a760..a6d619ca7 100644 --- a/src/Mod/TechDraw/Gui/Command.cpp +++ b/src/Mod/TechDraw/Gui/Command.cpp @@ -65,6 +65,7 @@ #include #include +#include "DrawGuiUtil.h" #include "MDIViewPage.h" #include "TaskProjGroup.h" #include "TaskSectionView.h" @@ -74,60 +75,6 @@ using namespace TechDrawGui; using namespace std; -//=========================================================================== -// utility routines -//=========================================================================== - -//! find a DrawPage in Selection or Document -//TODO: code is duplicated in CommandCreateDims and CommandDecorate -TechDraw::DrawPage* _findPage(Gui::Command* cmd) -{ - //check if a DrawPage is currently displayed - auto mdiView( Gui::getMainWindow()->activeWindow() ); - auto mvp( dynamic_cast(mdiView) ); - if (mvp) { - return mvp->getQGVPage()->getDrawPage(); - } else { - TechDraw::DrawPage* page(nullptr); - - //DrawPage not displayed, check Selection and/or Document for a DrawPage - auto drawPageType( TechDraw::DrawPage::getClassTypeId() ); - auto selPages( cmd->getSelection().getObjectsOfType(drawPageType) ); - if (selPages.empty()) { //no page in selection - selPages = cmd->getDocument()->getObjectsOfType(drawPageType); - if (selPages.empty()) { //no page in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page found"), - QObject::tr("Create a page first.")); - return page; - } else if (selPages.size() > 1) { //multiple pages in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Can not determine correct page.")); - return page; - } else { //use only page in document - page = static_cast(selPages.front()); - } - } else if (selPages.size() > 1) { //multiple pages in selection - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Select exactly 1 page.")); - return page; - } else { //use only page in selection - page = static_cast(selPages.front()); - } - - return page; - } -} - -bool isDrawingPageActive(Gui::Document *doc) -{ - if (doc) - // checks if a DrawPage Viewprovider is in Edit and is in no special mode - if (doc->getInEdit() && doc->getInEdit()->isDerivedFrom(TechDrawGui::ViewProviderPage::getClassTypeId())) - return true; - return false; -} - - //=========================================================================== // TechDraw_NewPageDef (default template) //=========================================================================== @@ -299,7 +246,7 @@ CmdTechDrawNewView::CmdTechDrawNewView() void CmdTechDrawNewView::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -350,8 +297,7 @@ void CmdTechDrawNewView::activated(int iMsg) bool CmdTechDrawNewView::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } //=========================================================================== @@ -375,8 +321,7 @@ CmdTechDrawNewViewSection::CmdTechDrawNewViewSection() void CmdTechDrawNewViewSection::activated(int iMsg) { Q_UNUSED(iMsg); - //TODO: should just use BaseView's page - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -420,8 +365,13 @@ void CmdTechDrawNewViewSection::activated(int iMsg) bool CmdTechDrawNewViewSection::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + bool taskInProgress = false; + if (havePage) { + taskInProgress = Gui::Control().activeDialog(); + } + return (havePage && haveView && !taskInProgress); } //=========================================================================== @@ -445,7 +395,7 @@ CmdTechDrawProjGroup::CmdTechDrawProjGroup() void CmdTechDrawProjGroup::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -480,18 +430,18 @@ void CmdTechDrawProjGroup::activated(int iMsg) // create the rest of the desired views Gui::Control().showDialog(new TaskDlgProjGroup(multiView,true)); -// // add the multiView to the page -// doCommand(Doc,"App.activeDocument().%s.addView(App.activeDocument().%s)",PageName.c_str(),multiViewName.c_str()); - updateActive(); commitCommand(); } bool CmdTechDrawProjGroup::isActive(void) { - if ( !hasActiveDocument() || Gui::Control().activeDialog()) - return false; - return true; + bool havePage = DrawGuiUtil::needPage(this); + bool taskInProgress = false; + if (havePage) { + taskInProgress = Gui::Control().activeDialog(); + } + return (havePage && !taskInProgress); } @@ -516,7 +466,7 @@ CmdTechDrawAnnotation::CmdTechDrawAnnotation() void CmdTechDrawAnnotation::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -532,7 +482,7 @@ void CmdTechDrawAnnotation::activated(int iMsg) bool CmdTechDrawAnnotation::isActive(void) { - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } @@ -557,7 +507,7 @@ CmdTechDrawClip::CmdTechDrawClip() void CmdTechDrawClip::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -577,7 +527,7 @@ void CmdTechDrawClip::activated(int iMsg) bool CmdTechDrawClip::isActive(void) { - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } //=========================================================================== @@ -589,7 +539,6 @@ DEF_STD_CMD_A(CmdTechDrawClipPlus); CmdTechDrawClipPlus::CmdTechDrawClipPlus() : Command("TechDraw_ClipPlus") { - // seting the sGroup = QT_TR_NOOP("TechDraw"); sMenuText = QT_TR_NOOP("&ClipPlus"); sToolTipText = QT_TR_NOOP("Add a View to a clip group in the active drawing"); @@ -601,12 +550,12 @@ CmdTechDrawClipPlus::CmdTechDrawClipPlus() void CmdTechDrawClipPlus::activated(int iMsg) { Q_UNUSED(iMsg); - std::vector selection = getSelection().getSelectionEx(); - if (selection.size() != 2) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Select 1 DrawViewClip and 1 DrawView.")); - return; - } + std::vector selection = getSelection().getSelectionEx(); + if (selection.size() != 2) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Select 1 DrawViewClip and 1 DrawView.")); + return; + } TechDraw::DrawViewClip* clip = 0; TechDraw::DrawView* view = 0; @@ -657,7 +606,16 @@ void CmdTechDrawClipPlus::activated(int iMsg) bool CmdTechDrawClipPlus::isActive(void) { - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveClip = false; + if (havePage) { + auto drawClipType( TechDraw::DrawViewClip::getClassTypeId() ); + auto selClips = getDocument()->getObjectsOfType(drawClipType); + if (!selClips.empty()) { + haveClip = true; + } + } + return (havePage && haveClip); } //=========================================================================== @@ -721,7 +679,16 @@ void CmdTechDrawClipMinus::activated(int iMsg) bool CmdTechDrawClipMinus::isActive(void) { - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveClip = false; + if (havePage) { + auto drawClipType( TechDraw::DrawViewClip::getClassTypeId() ); + auto selClips = getDocument()->getObjectsOfType(drawClipType); + if (!selClips.empty()) { + haveClip = true; + } + } + return (havePage && haveClip); } @@ -746,7 +713,7 @@ CmdTechDrawSymbol::CmdTechDrawSymbol() void CmdTechDrawSymbol::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -772,7 +739,7 @@ void CmdTechDrawSymbol::activated(int iMsg) bool CmdTechDrawSymbol::isActive(void) { - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } //=========================================================================== @@ -796,7 +763,7 @@ CmdTechDrawDraftView::CmdTechDrawDraftView() void CmdTechDrawDraftView::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -823,7 +790,7 @@ void CmdTechDrawDraftView::activated(int iMsg) bool CmdTechDrawDraftView::isActive(void) { - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } //=========================================================================== @@ -855,7 +822,7 @@ void CmdTechDrawSpreadsheet::activated(int iMsg) } std::string SpreadName = spreads.front()->getNameInDocument(); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -872,7 +839,17 @@ void CmdTechDrawSpreadsheet::activated(int iMsg) bool CmdTechDrawSpreadsheet::isActive(void) { - return (getActiveGuiDocument() ? true : false); + //need a Page and a SpreadSheet::Sheet + bool havePage = DrawGuiUtil::needPage(this); + bool haveSheet = false; + if (havePage) { + auto spreadSheetType( Spreadsheet::Sheet::getClassTypeId() ); + auto selSheets = getDocument()->getObjectsOfType(spreadSheetType); + if (!selSheets.empty()) { + haveSheet = true; + } + } + return (havePage && haveSheet); } @@ -896,7 +873,7 @@ CmdTechDrawExportPage::CmdTechDrawExportPage() void CmdTechDrawExportPage::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPage(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -917,7 +894,7 @@ void CmdTechDrawExportPage::activated(int iMsg) bool CmdTechDrawExportPage::isActive(void) { - return hasActiveDocument(); + return DrawGuiUtil::needPage(this); } diff --git a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp index d83048a9d..34e678873 100644 --- a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp +++ b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp @@ -34,6 +34,8 @@ # include # include +#include +#include # include # include # include @@ -58,8 +60,9 @@ #include -# include "MDIViewPage.h" -# include "ViewProviderPage.h" +#include "DrawGuiUtil.h" +#include "MDIViewPage.h" +#include "ViewProviderPage.h" #include "TaskLinkDim.h" using namespace TechDrawGui; @@ -69,44 +72,6 @@ using namespace std; // utility routines //=========================================================================== -//TODO: still need this as separate routine? only used in LinkDimension now -//TODO: code is duplicated in Command and CommandDecorate -TechDraw::DrawPage* _findPageCCD(Gui::Command* cmd) -{ - TechDraw::DrawPage* page = 0; - //check if a DrawPage is currently displayed - Gui::MainWindow* w = Gui::getMainWindow(); - Gui::MDIView* mv = w->activeWindow(); - MDIViewPage* mvp = dynamic_cast(mv); - if (mvp) { - QGVPage* qp = mvp->getQGVPage(); - page = qp->getDrawPage(); - } else { - //DrawPage not displayed, check Selection and/or Document for a DrawPage - std::vector selPages = cmd->getSelection().getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); - if (selPages.empty()) { //no page in selection - selPages = cmd->getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); - if (selPages.empty()) { //no page in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page found"), - QObject::tr("Create a page first.")); - return page; - } else if (selPages.size() > 1) { //multiple pages in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Can not determine correct page.")); - return page; - } else { //use only page in document - page = static_cast(selPages.front()); - } - } else if (selPages.size() > 1) { //multiple pages in selection - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Select exactly 1 page.")); - return page; - } else { //use only page in selection - page = static_cast(selPages.front()); - } - } - return page; -} //internal functions bool _checkSelection(Gui::Command* cmd, unsigned maxObjs = 2); @@ -116,6 +81,7 @@ int _isValidSingleEdge(Gui::Command* cmd); bool _isValidVertexes(Gui::Command* cmd); int _isValidEdgeToEdge(Gui::Command* cmd); bool _isValidVertexToEdge(Gui::Command* cmd); +//bool _checkActive(Gui::Command* cmd, Base::Type classType, bool needSubs); enum EdgeType{ isInvalid, @@ -260,8 +226,9 @@ void CmdTechDrawNewDimension::activated(int iMsg) bool CmdTechDrawNewDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -348,8 +315,9 @@ void CmdTechDrawNewRadiusDimension::activated(int iMsg) bool CmdTechDrawNewRadiusDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -438,8 +406,9 @@ void CmdTechDrawNewDiameterDimension::activated(int iMsg) bool CmdTechDrawNewDiameterDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -546,8 +515,9 @@ void CmdTechDrawNewLengthDimension::activated(int iMsg) bool CmdTechDrawNewLengthDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -652,8 +622,9 @@ void CmdTechDrawNewDistanceXDimension::activated(int iMsg) bool CmdTechDrawNewDistanceXDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -757,8 +728,9 @@ void CmdTechDrawNewDistanceYDimension::activated(int iMsg) bool CmdTechDrawNewDistanceYDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -844,8 +816,9 @@ void CmdTechDrawNewAngleDimension::activated(int iMsg) bool CmdTechDrawNewAngleDimension::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //! link 3D geometry to Dimension(s) on a Page @@ -871,7 +844,7 @@ CmdTechDrawLinkDimension::CmdTechDrawLinkDimension() void CmdTechDrawLinkDimension::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPageCCD(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -907,7 +880,13 @@ void CmdTechDrawLinkDimension::activated(int iMsg) bool CmdTechDrawLinkDimension::isActive(void) { - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + bool taskInProgress = false; + if (havePage) { + taskInProgress = Gui::Control().activeDialog(); + } + return (havePage && haveView && !taskInProgress); } void CreateTechDrawCommandsDims(void) @@ -1133,3 +1112,24 @@ bool _isValidVertexToEdge(Gui::Command* cmd) { } return result; } +//bool _checkActive(Gui::Command* cmd, Base::Type classType, bool needSubs) +//{ +// //need a page, a selected classType and [a subelement] +// bool active = false; +// if (cmd->hasActiveDocument()) { +// auto drawPageType( TechDraw::DrawPage::getClassTypeId() ); +// auto selPages = cmd->getDocument()->getObjectsOfType(drawPageType); +// if (!selPages.empty()) { +// std::vector selection = cmd->getSelection().getSelectionEx(); +// for (auto& s:selection) { +// if (s.getObject()->isDerivedFrom(classType)) { +// if (needSubs && !(s.getSubNames().empty())) { +// active = true; +// break; +// } +// } +// } +// } +// } +// return active; +//} diff --git a/src/Mod/TechDraw/Gui/CommandDecorate.cpp b/src/Mod/TechDraw/Gui/CommandDecorate.cpp index 16fcf9b7d..60e3cb366 100644 --- a/src/Mod/TechDraw/Gui/CommandDecorate.cpp +++ b/src/Mod/TechDraw/Gui/CommandDecorate.cpp @@ -53,53 +53,13 @@ #include #include -# include "MDIViewPage.h" -# include "ViewProviderPage.h" +#include "DrawGuiUtil.h" +#include "MDIViewPage.h" +#include "ViewProviderPage.h" using namespace TechDrawGui; using namespace std; -//=========================================================================== -// utility routines -//=========================================================================== - -//TODO: code is duplicated in Command and CommandCreateDims -TechDraw::DrawPage* _findPageCD(Gui::Command* cmd) -{ - TechDraw::DrawPage* page = 0; - //check if a DrawPage is currently displayed - Gui::MainWindow* w = Gui::getMainWindow(); - Gui::MDIView* mv = w->activeWindow(); - MDIViewPage* mvp = dynamic_cast(mv); - if (mvp) { - QGVPage* qp = mvp->getQGVPage(); - page = qp->getDrawPage(); - } else { - //DrawPage not displayed, check Selection and/or Document for a DrawPage - std::vector selPages = cmd->getSelection().getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); - if (selPages.empty()) { //no page in selection - selPages = cmd->getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); - if (selPages.empty()) { //no page in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page found"), - QObject::tr("Create a page first.")); - return page; - } else if (selPages.size() > 1) { //multiple pages in document - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Can not determine correct page.")); - return page; - } else { //use only page in document - page = dynamic_cast(selPages.front()); - } - } else if (selPages.size() > 1) { //multiple pages in selection - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), - QObject::tr("Select exactly 1 page.")); - return page; - } else { //use only page in selection - page = dynamic_cast(selPages.front()); - } - } - return page; -} //internal functions bool _checkSelectionHatch(Gui::Command* cmd); @@ -161,8 +121,9 @@ void CmdTechDrawNewHatch::activated(int iMsg) bool CmdTechDrawNewHatch::isActive(void) { - // TODO: Also ensure that there's a part selected? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } //=========================================================================== @@ -186,7 +147,7 @@ CmdTechDrawToggleFrame::CmdTechDrawToggleFrame() void CmdTechDrawToggleFrame::activated(int iMsg) { Q_UNUSED(iMsg); - TechDraw::DrawPage* page = _findPageCD(this); + TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; } @@ -207,8 +168,9 @@ void CmdTechDrawToggleFrame::activated(int iMsg) bool CmdTechDrawToggleFrame::isActive(void) { - // TODO: Also ensure that there's a page displayed? - return hasActiveDocument(); + bool havePage = DrawGuiUtil::needPage(this); + bool haveView = DrawGuiUtil::needView(this); + return (havePage && haveView); } void CreateTechDrawCommandsDecorate(void) @@ -227,14 +189,14 @@ bool _checkSelectionHatch(Gui::Command* cmd) { std::vector selection = cmd->getSelection().getSelectionEx(); if (selection.size() == 0) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"), - QObject::tr("Select an object first")); + QObject::tr("Select a Face first")); return false; } TechDraw::DrawViewPart * objFeat = dynamic_cast(selection[0].getObject()); if(!objFeat) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect selection"), - QObject::tr("No Feature in selection")); + QObject::tr("No TechDraw object in selection")); return false; } @@ -246,10 +208,15 @@ bool _checkSelectionHatch(Gui::Command* cmd) { } const std::vector &SubNames = selection[0].getSubNames(); + if (SubNames.empty()) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"), + QObject::tr("Can't make a Hatched area from this selection")); + return false; + } std::string gType = TechDraw::DrawUtil::getGeomTypeFromName(SubNames.at(0)); if (!(gType == "Face")) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Incorrect Selection"), - QObject::tr("Can't make a Hatched area from this selection")); + QObject::tr("No Face in this selection")); return false; } diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp new file mode 100644 index 000000000..0e42243b7 --- /dev/null +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp @@ -0,0 +1,135 @@ +/*************************************************************************** + * Copyright (c) 2016 WandererFan * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#include "PreCompiled.h" + +#ifndef _PreComp_ +# include +# include +# include +# include +# include +# include +# include +# include +# include + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "QGVPage.h" +#include "MDIViewPage.h" +#include "ViewProviderPage.h" +#include "DrawGuiUtil.h" + +using namespace TechDrawGui; + +//=========================================================================== +// validate helper routines +//=========================================================================== +TechDraw::DrawPage* DrawGuiUtil::findPage(Gui::Command* cmd) +{ + TechDraw::DrawPage* page = 0; + //check if a DrawPage is currently displayed + Gui::MainWindow* w = Gui::getMainWindow(); + Gui::MDIView* mv = w->activeWindow(); + MDIViewPage* mvp = dynamic_cast(mv); + if (mvp) { + QGVPage* qp = mvp->getQGVPage(); + page = qp->getDrawPage(); + } else { + //DrawPage not displayed, check Selection and/or Document for a DrawPage + std::vector selPages = cmd->getSelection().getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); + if (selPages.empty()) { //no page in selection + selPages = cmd->getDocument()->getObjectsOfType(TechDraw::DrawPage::getClassTypeId()); + if (selPages.empty()) { //no page in document + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No page found"), + QObject::tr("Create a page first.")); + return page; + } else if (selPages.size() > 1) { //multiple pages in document + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), + QObject::tr("Can not determine correct page.")); + return page; + } else { //use only page in document + page = static_cast(selPages.front()); + } + } else if (selPages.size() > 1) { //multiple pages in selection + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Too many pages"), + QObject::tr("Select exactly 1 page.")); + return page; + } else { //use only page in selection + page = static_cast(selPages.front()); + } + } + return page; +} + +bool DrawGuiUtil::needPage(Gui::Command* cmd) +{ + //need a Document and a Page + bool active = false; + if (cmd->hasActiveDocument()) { + auto drawPageType( TechDraw::DrawPage::getClassTypeId() ); + auto selPages = cmd->getDocument()->getObjectsOfType(drawPageType); + if (!selPages.empty()) { + active = true; + } + } + return active; +} + +bool DrawGuiUtil::needView(Gui::Command* cmd) +{ + bool haveView = false; + if (cmd->hasActiveDocument()) { + auto drawPartType (TechDraw::DrawViewPart::getClassTypeId()); + auto selParts = cmd->getDocument()->getObjectsOfType(drawPartType); + if (!selParts.empty()) { + haveView = true; + } + } + return haveView; +} diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.h b/src/Mod/TechDraw/Gui/DrawGuiUtil.h new file mode 100644 index 000000000..40bc0a7c6 --- /dev/null +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.h @@ -0,0 +1,40 @@ +/*************************************************************************** + * Copyright (c) 2016 WandererFan * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + +#ifndef _DrawGuiUtil_h_ +#define _DrawGuiUtil_h_ + +#include + +namespace TechDrawGui +{ + +/// Convenient utility functions for TechDraw Gui Module +class TechDrawExport DrawGuiUtil { + public: + static TechDraw::DrawPage* findPage(Gui::Command* cmd); + static bool needPage(Gui::Command* cmd); + static bool needView(Gui::Command* cmd); +}; + +} //end namespace TechDrawGui +#endif