Skip to content

Commit

Permalink
Merge pull request #9 from WandererFan/gsoc2023_measure
Browse files Browse the repository at this point in the history
Gsoc2023 measure: Support multiple subjects + multiple fixes
  • Loading branch information
hlorus authored Nov 5, 2023
2 parents 7944763 + b7791d0 commit bd6df85
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/Mod/Measure/App/MeasureAngle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void MeasureAngle::onChanged(const App::Property* prop)

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasureAngle::getSubject() const
std::vector<App::DocumentObject*> MeasureAngle::getSubject() const
{
return Element1.getValue();
return {Element1.getValue()};
}
2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasureAngle.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class MeasureExport MeasureAngle : public Measure::MeasureBaseExtendable<Measure
App::Property* getResultProp() override {return &this->Angle;}

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;


static bool getVec(App::DocumentObject& ob, std::string& subName, Base::Vector3d& vecOut);
Expand Down
8 changes: 2 additions & 6 deletions src/Mod/Measure/App/MeasureArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ Base::Placement MeasureArea::getPlacement() {

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasureArea::getSubject() const
std::vector<App::DocumentObject*> MeasureArea::getSubject() const
{
auto elements = Elements.getValues();
if (elements.empty()) {
return nullptr;
}
return Elements.getValues().front();
return Elements.getValues();
}
2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasureArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class MeasureExport MeasureArea : public Measure::MeasureBaseExtendable<MeasureA
Base::Placement getPlacement();

Check warning on line 83 in src/Mod/Measure/App/MeasureArea.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'getPlacement' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 83 in src/Mod/Measure/App/MeasureArea.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'getPlacement' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;


private:
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Measure/App/MeasureBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class AppExport MeasureBase : public App::DocumentObject
virtual QString getResultString();
virtual Base::Placement getPlacement() {return Base::Placement();}

virtual App::DocumentObject* getSubject() const = 0;
virtual std::vector<App::DocumentObject*> getSubject() const = 0;

protected:
void onDocumentRestored() override;
};
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Measure/App/MeasureDistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ void MeasureDistance::onChanged(const App::Property* prop)

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasureDistance::getSubject() const
std::vector<App::DocumentObject*> MeasureDistance::getSubject() const
{
return Element1.getValue();
return {Element1.getValue()};
}

2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasureDistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MeasureExport MeasureDistance : public Measure::MeasureBaseExtendable<Meas
bool getShape(App::PropertyLinkSub* prop, TopoDS_Shape& rShape);

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;


private:
Expand Down
8 changes: 2 additions & 6 deletions src/Mod/Measure/App/MeasureLength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@ Base::Placement MeasureLength::getPlacement() {

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasureLength::getSubject() const
std::vector<App::DocumentObject*> MeasureLength::getSubject() const
{
auto elements = Elements.getValues();
if (elements.empty()) {
return nullptr;
}
return Elements.getValues().front();
return Elements.getValues();
}

2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasureLength.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class MeasureExport MeasureLength : public Measure::MeasureBaseExtendable<Measur
Base::Placement getPlacement();

Check warning on line 83 in src/Mod/Measure/App/MeasureLength.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'getPlacement' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 83 in src/Mod/Measure/App/MeasureLength.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'getPlacement' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;


private:
Expand Down
17 changes: 11 additions & 6 deletions src/Mod/Measure/App/MeasurePosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ void MeasurePosition::recalculatePosition()
std::string obName = object->getNameInDocument();
auto result = handler(&obName, &subElement);
Position.setValue(result);
return;
}

void MeasurePosition::onChanged(const App::Property* prop)
Expand All @@ -150,18 +149,24 @@ void MeasurePosition::onChanged(const App::Property* prop)
QString MeasurePosition::getResultString() {
App::Property* prop = this->getResultProp();
if (prop == nullptr) {
return QString();
return {};
}

Base::Vector3d value = Position.getValue();
QString unit = Position.getUnit().getString();
int precision = 2;
QString text;
QTextStream(&text)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QTextStream(&text)
<< "X: " << QString::number(value.x, 'f', precision) << " " << unit << endl
<< "Y: " << QString::number(value.y, 'f', precision) << " " << unit << endl
<< "Z: " << QString::number(value.z, 'f', precision) << " " << unit;
#else
QTextStream(&text)
<< "X: " << QString::number(value.x, 'f', precision) << " " << unit << Qt::endl
<< "Y: " << QString::number(value.y, 'f', precision) << " " << unit << Qt::endl
<< "Z: " << QString::number(value.z, 'f', precision) << " " << unit;

#endif
return text;
}

Expand All @@ -175,7 +180,7 @@ Base::Placement MeasurePosition::getPlacement() {

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasurePosition::getSubject() const
std::vector<App::DocumentObject*> MeasurePosition::getSubject() const
{
return Element.getValue();
return {Element.getValue()};
}
2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasurePosition.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MeasureExport MeasurePosition : public Measure::MeasureBaseExtendable<Base
Base::Placement getPlacement();

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;

private:

Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Measure/App/MeasureRadius.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ MeasureRadiusInfo MeasureRadius::getMeasureInfoFirst() const

//! Return the object we are measuring
//! used by the viewprovider in determining visibility
App::DocumentObject* MeasureRadius::getSubject() const
std::vector<App::DocumentObject*> MeasureRadius::getSubject() const
{
return Element.getValue();
return {Element.getValue()};
}
2 changes: 1 addition & 1 deletion src/Mod/Measure/App/MeasureRadius.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class MeasureExport MeasureRadius : public Measure::MeasureBaseExtendable<Measur
Base::Vector3d getPointOnCurve() const;

// Return the object we are measuring
App::DocumentObject* getSubject() const override;
std::vector<App::DocumentObject*> getSubject() const override;


private:
Expand Down
68 changes: 49 additions & 19 deletions src/Mod/Measure/Gui/ViewProviderMeasureBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ void ViewProviderMeasureBase::connectToSubject(App::DocumentObject* subject)
subject->signalChanged.connect(bndVisibility);
}

//! connect to the subject to receive visibility updates
void ViewProviderMeasureBase::connectToSubject(std::vector<App::DocumentObject*> subject)
{
if (subject.empty()) {
return;
}

//NOLINTBEGIN
auto bndVisibility = boost::bind(&ViewProviderMeasureBase::onSubjectVisibilityChanged, this, bp::_1, bp::_2);
//NOLINTEND

// TODO: should we connect to all the subject objects when there is >1?
auto proxy = subject.front();
proxy->signalChanged.connect(bndVisibility);
}


//! retrive the feature

Check warning on line 249 in src/Mod/Measure/Gui/ViewProviderMeasureBase.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

retrive ==> retrieve
Measure::MeasureBase* ViewProviderMeasureBase::getMeasureObject()
Expand All @@ -248,41 +264,54 @@ Measure::MeasureBase* ViewProviderMeasureBase::getMeasureObject()
//! an example of an elementDirection would be the vector from the start of a line to the end.
Base::Vector3d ViewProviderMeasureBase::getTextDirection(Base::Vector3d elementDirection, double tolerance)
{
// TODO: this can fail if the active view is not a 3d view (spreadsheet, techdraw page) and something causes a measure to try to update
// we need to search throught the mdi views for a 3d view and take the direction from it (or decide that if the active view is not 3d,

Check warning on line 268 in src/Mod/Measure/Gui/ViewProviderMeasureBase.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

throught ==> thought, through, throughout
// assume we are looking from the front).
Base::Vector3d viewDirection;
Base::Vector3d upDirection;
auto view = dynamic_cast<Gui::View3DInventor*>(Gui::Application::Instance->activeDocument()->getActiveView());
// if (!view) {
// // Measure doesn't work with this kind of active view. Might be dependency graph, might be TechDraw, or ????
// // i don't know if this can even happen.
// throw Base::RuntimeError("Measure doesn't work with this kind of active view.");
// }
Gui::View3DInventorViewer* viewer = view->getViewer();
Base::Vector3d viewDirection = toVector3d(viewer->getViewDirection()).Normalize();
if (view) {
Gui::View3DInventorViewer* viewer = view->getViewer();
viewDirection = toVector3d(viewer->getViewDirection()).Normalize();
upDirection = toVector3d(viewer->getUpDirection()).Normalize();
// Measure doesn't work with this kind of active view. Might be dependency graph, might be TechDraw, or ????
//throw Base::RuntimeError("Measure doesn't work with this kind of active view.");
} else {
viewDirection = Base::Vector3d(0.0, 1.0, 0.0);
upDirection = Base::Vector3d(0.0, 0.0, 1.0);
}

Base::Vector3d textDirection = elementDirection.Cross(viewDirection);
if (textDirection.Length() < tolerance) {
// either elementDirection and viewDirection are parallel or one of them is null.
viewDirection = toVector3d(viewer->getUpDirection()).Normalize();
textDirection = elementDirection.Cross(viewDirection);
textDirection = elementDirection.Cross(upDirection);
}

return textDirection.Normalize();
}

//! true if the subject of this measurement is visible

//! true if the subject of this measurement is visible. For Measures that have multiple object subject,
//! all of the subjects must be visible.
bool ViewProviderMeasureBase::isSubjectVisible()
{
// we need these things to proceed
if (!getMeasureObject() ||
!getMeasureObject()->getSubject() ||
getMeasureObject()->getSubject().empty() ||
!Gui::Application::Instance->getDocument(getMeasureObject()->getDocument()) ) {
return false;
}

auto guiDoc = Gui::Application::Instance->getDocument(getMeasureObject()->getDocument());
Gui::ViewProvider* vp = guiDoc->getViewProvider(getMeasureObject()->getSubject());
if (vp) {
return vp->isVisible();
for (auto & obj : getMeasureObject()->getSubject()) {
Gui::ViewProvider* vp = guiDoc->getViewProvider(obj);
if (!vp || !vp->isVisible()) {
return false;
}
}

return false;
// all of the subject objects are visible
return true;
}


Expand All @@ -292,11 +321,12 @@ void ViewProviderMeasureBase::onSubjectVisibilityChanged(const App::DocumentObje
{
std::string propName = prop.getName();
if (propName == "Visibility") {
if (docObj.Visibility.getValue()) {
// show only if subject is visible
setVisible(true);
} else {
if (!docObj.Visibility.getValue()) {
// show ourselves only if subject is visible
setVisible(false);
} else {
// here, we don't know if we should be visible or not, so we have to check the whole subject
setVisible(isSubjectVisible());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Measure/Gui/ViewProviderMeasureBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class MeasureGuiExport ViewProviderMeasureBase :public Gui::ViewProviderDocument
using Connection = boost::signals2::scoped_connection;
Connection connectVisibilityChanged;
void onSubjectVisibilityChanged(const App::DocumentObject& docObj, const App::Property& prop);
void connectToSubject(App::DocumentObject* subject);
void connectToSubject(App::DocumentObject* subject);
void connectToSubject(std::vector<App::DocumentObject*> subject);

protected:
void onChanged(const App::Property* prop) override;
Expand Down

0 comments on commit bd6df85

Please sign in to comment.