Merge pull request #19019 from hyarion/refactor/countObjectsOfType

Refactor countObjectsOfType in selection and document
This commit is contained in:
Chris Hennes
2025-01-14 16:05:12 -06:00
committed by GitHub
31 changed files with 194 additions and 222 deletions

View File

@@ -1698,7 +1698,7 @@ bool StdCmdAlignment::isActive()
{
if (ManualAlignment::hasInstance())
return false;
return Gui::Selection().countObjectsOfType(App::GeoFeature::getClassTypeId()) == 2;
return Gui::Selection().countObjectsOfType<App::GeoFeature>() == 2;
}
//===========================================================================

View File

@@ -526,7 +526,6 @@ std::vector<App::DocumentObject*> SelectionSingleton::getObjectsOfType(const cha
unsigned int SelectionSingleton::countObjectsOfType(const Base::Type& typeId, const char* pDocName, ResolveMode resolve) const
{
unsigned int iNbr=0;
App::Document *pcDoc = nullptr;
if(!pDocName || strcmp(pDocName,"*") != 0) {
pcDoc = getDocument(pDocName);
@@ -534,12 +533,9 @@ unsigned int SelectionSingleton::countObjectsOfType(const Base::Type& typeId, co
return 0;
}
for (auto &sel : _SelList) {
if((!pcDoc||pcDoc==sel.pDoc) && getObjectOfType(sel, typeId, resolve))
iNbr++;
}
return iNbr;
return std::count_if(_SelList.begin(), _SelList.end(), [&](auto& sel) {
return (!pcDoc || pcDoc == sel.pDoc) && getObjectOfType(sel, typeId, resolve);
});
}
unsigned int SelectionSingleton::countObjectsOfType(const char* typeName, const char* pDocName, ResolveMode resolve) const

View File

@@ -387,9 +387,11 @@ public:
* If no document name is given the active document is assumed.
*
* Set 'resolve' to true to resolve any sub object inside selection SubName
* field
* field.
*
* The typename T must be based on App::DocumentObject.
*/
unsigned int countObjectsOfType(const Base::Type& typeId=App::DocumentObject::getClassTypeId(),
template<typename T> inline unsigned int countObjectsOfType(
const char* pDocName=nullptr, ResolveMode resolve = ResolveMode::OldStyleElement) const;
/**
@@ -708,6 +710,9 @@ protected:
static App::DocumentObject *getObjectOfType(_SelObj &sel, Base::Type type,
ResolveMode resolve, const char **subelement=nullptr);
unsigned int countObjectsOfType(const Base::Type& typeId=App::DocumentObject::getClassTypeId(),
const char* pDocName=nullptr, ResolveMode resolve = ResolveMode::OldStyleElement) const;
static SelectionSingleton* _pcSingleton;
std::string DocName;
@@ -724,6 +729,15 @@ protected:
SelectionStyle selectionStyle;
};
/**
* A convenience template-based method that returns the number of objects of the given type.
*/
template<typename T>
inline unsigned int SelectionSingleton::countObjectsOfType(const char* pDocName, ResolveMode resolve) const {
static_assert(std::is_base_of<App::DocumentObject, T>::value, "Template parameter T must be derived from App::DocumentObject");
return this->countObjectsOfType(T::getClassTypeId(), pDocName, resolve);
}
/**
* A convenience template-based method that returns an array with the correct types already.
*/

View File

@@ -594,7 +594,7 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const
<< StdViews << "Separator"
<< "Std_ViewDockUndockFullscreen";
if (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId()) > 0) {
if (Gui::Selection().countObjectsOfType<App::DocumentObject>() > 0) {
*item << "Separator" << "Std_ToggleVisibility"
<< "Std_ToggleSelectability" << "Std_TreeSelection"
<< "Std_RandomColor" << "Std_ToggleTransparency" << "Separator" << "Std_Delete"
@@ -603,7 +603,7 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const
}
else if (strcmp(recipient,"Tree") == 0)
{
if (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId()) > 0) {
if (Gui::Selection().countObjectsOfType<App::DocumentObject>() > 0) {
*item << "Std_ToggleFreeze" << "Separator"
<< "Std_Placement" << "Std_ToggleVisibility" << "Std_ShowSelection" << "Std_HideSelection"
<< "Std_ToggleSelectability" << "Std_TreeSelectAllInstances" << "Separator"