From 71530bb1a63be8793eb192ed3ffc3612f0e83739 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Wed, 21 Aug 2019 14:41:51 +0800 Subject: [PATCH] Gui: allow SelectionSingleton::hasSubSelection() check for sub-element This is used by relative link command activation for sub-element selection --- src/Gui/CommandLink.cpp | 10 +++++++--- src/Gui/Selection.cpp | 21 +++++++++++++-------- src/Gui/Selection.h | 14 +++++++++++++- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/Gui/CommandLink.cpp b/src/Gui/CommandLink.cpp index 0fa38a3db7..bb24481906 100644 --- a/src/Gui/CommandLink.cpp +++ b/src/Gui/CommandLink.cpp @@ -197,7 +197,7 @@ void StdCmdLinkMakeGroup::activated(int option) { //////////////////////////////////////////////////////////////////////////////////////////// -DEF_STD_CMD(StdCmdLinkMake) +DEF_STD_CMD_A(StdCmdLinkMake) StdCmdLinkMake::StdCmdLinkMake() : Command("Std_LinkMake") @@ -211,6 +211,10 @@ StdCmdLinkMake::StdCmdLinkMake() sPixmap = "Link"; } +bool StdCmdLinkMake::isActive() { + return !!App::GetApplication().getActiveDocument(); +} + void StdCmdLinkMake::activated(int) { auto doc = App::GetApplication().getActiveDocument(); if(!doc) { @@ -263,7 +267,7 @@ StdCmdLinkMakeRelative::StdCmdLinkMakeRelative() { sGroup = QT_TR_NOOP("Link"); sMenuText = QT_TR_NOOP("Make relative link"); - sToolTipText = QT_TR_NOOP("Create a relative link of two selected objects"); + sToolTipText = QT_TR_NOOP("Create a sub-object or sub-element link"); sWhatsThis = "Std_LinkMakeRelative"; sStatusTip = sToolTipText; eType = AlterDoc; @@ -271,7 +275,7 @@ StdCmdLinkMakeRelative::StdCmdLinkMakeRelative() } bool StdCmdLinkMakeRelative::isActive() { - return Selection().hasSubSelection(); + return Selection().hasSubSelection(0,true); } void StdCmdLinkMakeRelative::activated(int) { diff --git a/src/Gui/Selection.cpp b/src/Gui/Selection.cpp index 7a4e17ad0c..303161067a 100644 --- a/src/Gui/Selection.cpp +++ b/src/Gui/Selection.cpp @@ -437,7 +437,7 @@ bool SelectionSingleton::hasSelection(const char* doc, bool resolve) const return false; } -bool SelectionSingleton::hasSubSelection(const char* doc) const +bool SelectionSingleton::hasSubSelection(const char* doc, bool subElement) const { App::Document *pcDoc = 0; if(!doc || strcmp(doc,"*")!=0) { @@ -446,11 +446,14 @@ bool SelectionSingleton::hasSubSelection(const char* doc) const return false; } for(auto &sel : _SelList) { - if((!pcDoc || pcDoc == sel.pDoc) - && sel.pObject != sel.pResolvedObject) - { + if(pcDoc && pcDoc != sel.pDoc) + continue; + if(sel.SubName.empty()) + continue; + if(subElement && sel.SubName.back()!='.') + return true; + if(sel.pObject != sel.pResolvedObject) return true; - } } return false; @@ -1794,7 +1797,7 @@ PyMethodDef SelectionSingleton::Methods[] = { {"hasSelection", (PyCFunction) SelectionSingleton::sHasSelection, METH_VARARGS, "hasSelection(docName=None, resolve=False) -- check if there is any selection\n"}, {"hasSubSelection", (PyCFunction) SelectionSingleton::sHasSubSelection, METH_VARARGS, - "hasSubSelection(docName=None) -- check if there is any selection with subname\n"}, + "hasSubSelection(docName=None,subElement=False) -- check if there is any selection with subname\n"}, {"getSelectionFromStack",(PyCFunction) SelectionSingleton::sGetSelectionFromStack, METH_VARARGS, "getSelectionFromStack(docName=None,resolve=1,index=0) -- Return a list of SelectionObjects from selection stack\n" "\ndocName - document name. None means the active document, and '*' means all document" @@ -2271,11 +2274,13 @@ PyObject *SelectionSingleton::sHasSelection(PyObject * /*self*/, PyObject *args) PyObject *SelectionSingleton::sHasSubSelection(PyObject * /*self*/, PyObject *args) { const char *doc = 0; - if (!PyArg_ParseTuple(args, "|s",&doc)) + PyObject *subElement = Py_False; + if (!PyArg_ParseTuple(args, "|sO!",&doc,&PyBool_Type,&subElement)) return NULL; // NULL triggers exception PY_TRY { - return Py::new_reference_to(Py::Boolean(Selection().hasSubSelection(doc))); + return Py::new_reference_to( + Py::Boolean(Selection().hasSubSelection(doc,PyObject_IsTrue(subElement)))); } PY_CATCH; } diff --git a/src/Gui/Selection.h b/src/Gui/Selection.h index feea25b1e7..6a177294b3 100644 --- a/src/Gui/Selection.h +++ b/src/Gui/Selection.h @@ -506,7 +506,19 @@ public: std::vector getCompleteSelection(int resolve=1) const; bool hasSelection() const; bool hasSelection(const char* doc, bool resolve=true) const; - bool hasSubSelection(const char *doc=0) const; + + /** Check if there is any sub-element selection + * + * @param doc: optional document to check for selection + * @param subElement: whether to count sub-element only selection + * + * Example sub selections are face, edge or vertex. If \c resolve is false, + * then sub-object (i.e. a group child object) selection is also counted + * even if it selects the whole sub-object. + */ + bool hasSubSelection(const char *doc=0, bool subElement=false) const; + + /// Check if there is any pre-selection bool hasPreselection() const; /// Size of selected entities for all documents