From de8f153ca3a3b53c23f51ecf03d49e6ecb525c07 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 4 Jul 2024 15:54:19 +0200 Subject: [PATCH] Core: In dialog to add property only list types that can be instantiated Fixes #15159: Dialog for adding properties allows property types that give exceptions --- src/Base/BaseClass.h | 2 +- src/Base/Type.cpp | 5 +++++ src/Base/Type.h | 2 ++ src/Gui/DlgAddProperty.cpp | 10 ++++++++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Base/BaseClass.h b/src/Base/BaseClass.h index d1adc97602..df658b6513 100644 --- a/src/Base/BaseClass.h +++ b/src/Base/BaseClass.h @@ -128,7 +128,7 @@ private: TYPESYSTEM_SOURCE_ABSTRACT_P(_class_) \ void _class_::init(void) \ { \ - initSubclass(_class_::classTypeId, #_class_, #_parentclass_, &(_class_::create)); \ + initSubclass(_class_::classTypeId, #_class_, #_parentclass_, nullptr); \ } // NOLINTEND(cppcoreguidelines-macro-usage) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index f137ba55bf..51b2128c1e 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -65,6 +65,11 @@ void* Type::createInstance() return method ? (*method)() : nullptr; } +bool Type::canInstantiate() const +{ + instantiationMethod method = typedata[index]->instMethod; + return method != nullptr; +} void* Type::createInstanceByName(const char* TypeName, bool bLoadModule) { diff --git a/src/Base/Type.h b/src/Base/Type.h index ee120c7333..3f580724e2 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -89,6 +89,8 @@ public: /// creates a instance of this type void* createInstance(); + /// Checks whether this type can instantiate + bool canInstantiate() const; /// creates a instance of the named type static void* createInstanceByName(const char* TypeName, bool bLoadModule = false); static void importModule(const char* TypeName); diff --git a/src/Gui/DlgAddProperty.cpp b/src/Gui/DlgAddProperty.cpp index 088d9f2372..c1ff8f2472 100644 --- a/src/Gui/DlgAddProperty.cpp +++ b/src/Gui/DlgAddProperty.cpp @@ -54,9 +54,15 @@ DlgAddProperty::DlgAddProperty(QWidget* parent, if(defType.isBad()) defType = App::PropertyString::getClassTypeId(); + std::vector proptypes; std::vector types; - Base::Type::getAllDerivedFrom(Base::Type::fromName("App::Property"),types); - std::sort(types.begin(), types.end(), [](Base::Type a, Base::Type b) { return strcmp(a.getName(), b.getName()) < 0; }); + Base::Type::getAllDerivedFrom(Base::Type::fromName("App::Property"), proptypes); + std::copy_if (proptypes.begin(), proptypes.end(), std::back_inserter(types), [](const Base::Type& type) { + return type.canInstantiate(); + }); + std::sort(types.begin(), types.end(), [](Base::Type a, Base::Type b) { + return strcmp(a.getName(), b.getName()) < 0; + }); for(const auto& type : types) { ui->comboType->addItem(QString::fromLatin1(type.getName()));