Gui: Improve transform UI responsivness

This introduces much faster CenterOfMassProvider#supports method that
can be used as cheap pre-check to see if the object supplied could have
the center of mass and so delay the domputation to a moment where it is
actually needed.
This commit is contained in:
Kacper Donat
2025-12-08 22:49:56 +01:00
committed by Chris Hennes
parent 5f0d99f4f3
commit 88d721b14c
5 changed files with 15 additions and 2 deletions

View File

@@ -27,4 +27,9 @@ std::optional<Base::Vector3d>
App::NullCenterOfMass::ofDocumentObject([[maybe_unused]] DocumentObject* object) const
{
return std::nullopt;
}
bool App::NullCenterOfMass::supports(DocumentObject* object) const
{
return false;
}

View File

@@ -55,6 +55,7 @@ class CenterOfMassProvider
public:
virtual ~CenterOfMassProvider() = default;
virtual bool supports(DocumentObject* object) const = 0;
virtual std::optional<Base::Vector3d> ofDocumentObject(DocumentObject* object) const = 0;
};
@@ -66,6 +67,7 @@ class NullCenterOfMass final : public CenterOfMassProvider
{
public:
std::optional<Base::Vector3d> ofDocumentObject(DocumentObject* object) const override;
bool supports(DocumentObject* object) const override;
};
}

View File

@@ -173,7 +173,7 @@ void TaskTransform::loadPlacementModeItems() const
QVariant::fromValue(PlacementMode::ObjectOrigin)
);
if (centerOfMassProvider->ofDocumentObject(vp->getObject()).has_value()) {
if (centerOfMassProvider->supports(vp->getObject())) {
ui->placementComboBox->addItem(
tr("Center of mass / centroid"),
QVariant::fromValue(PlacementMode::Centroid)

View File

@@ -45,7 +45,7 @@ Base::Placement AttacherSubObjectPlacement::calculate(
std::optional<Base::Vector3d> PartCenterOfMass::ofDocumentObject(App::DocumentObject* object) const
{
if (const auto feature = dynamic_cast<Part::Feature*>(object)) {
if (const auto* feature = freecad_cast<Part::Feature*>(object)) {
const auto shape = feature->Shape.getShape();
if (const auto cog = shape.centerOfGravity()) {
@@ -57,3 +57,8 @@ std::optional<Base::Vector3d> PartCenterOfMass::ofDocumentObject(App::DocumentOb
return {};
}
bool PartCenterOfMass::supports(App::DocumentObject* object) const
{
return object->isDerivedFrom<Part::Feature>();
}

View File

@@ -42,6 +42,7 @@ class PartCenterOfMass final: public App::CenterOfMassProvider
{
public:
std::optional<Base::Vector3d> ofDocumentObject(App::DocumentObject* object) const override;
bool supports(App::DocumentObject* object) const override;
};
#endif // PART_SERVICES_H