Gui: Add getObject<T>() helpers to various classes

Obtaining specific kind of document object is very common task. This
commit introduces handy helper which makes that a lot easier.
This commit is contained in:
Kacper Donat
2024-10-27 18:45:19 +01:00
parent ad50bb9bef
commit deb15a57e4
6 changed files with 22 additions and 5 deletions

View File

@@ -71,9 +71,15 @@ public:
inline const std::vector<Base::Vector3d> getPickedPoints() const { return SelPoses; }
/// returns the selected DocumentObject or NULL if the object is already deleted
const App::DocumentObject *getObject() const;
const App::DocumentObject* getObject() const;
/// returns the selected DocumentObject or NULL if the object is already deleted
template <class T>
const T* getObject() const { return dynamic_cast<T*>(getObject()); };
/// returns the selected DocumentObject or NULL if the object is already deleted
App::DocumentObject *getObject();
/// returns the selected DocumentObject if it is of T type or null otherwise
template <class T>
T* getObject() { return dynamic_cast<T*>(getObject()); }
/// check the selected object is a special type or derived of
bool isObjectTypeOf(const Base::Type& typeId) const;

View File

@@ -92,6 +92,9 @@ public:
void updateView();
/// Get the object of this ViewProvider object
App::DocumentObject *getObject() const {return pcObject;}
/// Get the object of this ViewProvider object as specified type
template <class T>
T *getObject() const { return dynamic_cast<T*>(pcObject); }
/// Asks the view provider if the given object can be deleted.
bool canDelete(App::DocumentObject* obj) const override;
/// Ask the view provider if it accepts object deletions while in edit

View File

@@ -173,7 +173,7 @@ void ViewProviderGeometryObject::updateData(const App::Property* prop)
pcBoundingBox->maxBounds.setValue(box.MaxX, box.MaxY, box.MaxZ);
}
else if (prop->isDerivedFrom(App::PropertyPlacement::getClassTypeId())) {
auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
auto geometry = getObject<App::GeoFeature>();
if (geometry && prop == &geometry->Placement) {
const App::PropertyComplexGeoData* data = geometry->getPropertyOfGeometry();
if (data) {
@@ -185,8 +185,7 @@ void ViewProviderGeometryObject::updateData(const App::Property* prop)
}
else if (std::string(prop->getName()) == "ShapeMaterial") {
// Set the appearance from the material
auto geometry = dynamic_cast<App::GeoFeature*>(getObject());
if (geometry) {
if (auto geometry = getObject<App::GeoFeature>()) {
/*
* Change the appearance only if the appearance hasn't been set explicitly. A cached
* material appearance is used to see if the current appearance matches the last

View File

@@ -65,10 +65,15 @@ protected:
{
return m_view;
}
Fem::FemPostFunction* getObject()
Fem::FemPostFunction* getObject() const
{
return m_object;
}
template<class T>
T* getObject() const
{
return dynamic_cast<T*>(getObject());
}
bool blockObjectUpdates()
{

View File

@@ -178,6 +178,9 @@ protected:
*/
PartDesign::Transformed* getObject() const;
template <class T>
T* getObject() const { return dynamic_cast<T*>(getObject()); }
/// Get the sketch object of the first original either of the object associated with this
/// feature or with the parent feature (MultiTransform mode)
App::DocumentObject* getSketchObject() const;

View File

@@ -63,6 +63,7 @@ public:
bool operator== (const ReferenceEntry& otherRef) const;
App::DocumentObject* getObject() const;
template <class T> T* getObject() const { return dynamic_cast<T*>(getObject()); }
void setObject(App::DocumentObject* docObj) { m_object = docObj; }
std::string getSubName(bool longForm = false) const;
void setSubName(const std::string& subName) { m_subName = subName; }