Skip to content

Commit

Permalink
Measure: Add optional priority callback to measure types
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorus committed Aug 20, 2023
1 parent ddf3c71 commit cb11524
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/App/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ typedef struct MeasureElementInfo {

using MeasureSelection = std::vector<std::tuple<std::string, std::string>>;
using MeasureValidateMethod = std::function<bool(const MeasureSelection&)>;
using MeasurePrioritizeMethod = std::function<bool(const MeasureSelection&)>;
using MeasureInfoMethod = std::function<MeasureElementInfo(const char*, const char*)>;
using MeasureTypeMethod = std::function<App::MeasureElementType (const char*, const char*)>;

typedef struct MeasureType {
std::string identifier;
std::string label;
std::string measureObject;

// Checks if the measurement works with a given selection
MeasureValidateMethod validatorCb;

// Allows to prioritize this over other measurement types when the measurement type is picked implicitly from the selection.
// Gets called only when validatorCb returned true for the given selection
MeasurePrioritizeMethod prioritizeCb;
} MeasureType;

typedef struct MeasureHandler {
Expand Down
4 changes: 2 additions & 2 deletions src/App/Measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PROPERTY_SOURCE_ABSTRACT(App::MeasurementBase, App::DocumentObject)
void App::Measure::initialize(){
App::Application& app = App::GetApplication();

app.addMeasureType(new MeasureType{"LENGTH", "Length", "App::MeasureLength", App::MeasureLength::isValidSelection});
app.addMeasureType(new MeasureType{"ANGLE", "Angle", "App::MeasureAngle", App::MeasureAngle::isValidSelection});
app.addMeasureType(new MeasureType{"LENGTH", "Length", "App::MeasureLength", App::MeasureLength::isValidSelection, nullptr});
app.addMeasureType(new MeasureType{"ANGLE", "Angle", "App::MeasureAngle", App::MeasureAngle::isValidSelection, App::MeasureAngle::isPrioritizedSelection});

}

Check warning on line 40 in src/App/Measure.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]
8 changes: 8 additions & 0 deletions src/App/MeasureAngle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ bool MeasureAngle::isValidSelection(const App::MeasureSelection& selection){
return true;
}

bool MeasureAngle::isPrioritizedSelection(const App::MeasureSelection& selection) {
if (selection.size() == 2) {
return true;
}

return false;
}


void MeasureAngle::parseSelection(const App::MeasureSelection& selection) {

Expand Down
1 change: 1 addition & 0 deletions src/App/MeasureAngle.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class AppExport MeasureAngle : public App::MeasurementBaseExtendable<MeasureAngl
// }

static bool isValidSelection(const App::MeasureSelection& selection);
static bool isPrioritizedSelection(const App::MeasureSelection& selection);
void parseSelection(const App::MeasureSelection& selection);

Check warning on line 72 in src/App/MeasureAngle.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

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

Check warning on line 72 in src/App/MeasureAngle.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'parseSelection' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
Base::Quantity result() {return Angle.getQuantityValue();}

Check warning on line 73 in src/App/MeasureAngle.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

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

Expand Down
2 changes: 1 addition & 1 deletion src/App/MeasureLength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ MeasureLength::~MeasureLength() = default;

bool MeasureLength::isValidSelection(const App::MeasureSelection& selection){

if (selection.size() != 1) {
if (selection.size() < 1) {
return false;
}

Expand Down
16 changes: 12 additions & 4 deletions src/Gui/TaskMeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ void TaskMeasure::update(){
continue;
}

isValid = true;
measureType = mType;
setModeSilent(mType);
// Check if the measurement type prioritizes the given selection
bool isPriority = (mType->prioritizeCb != nullptr && mType->prioritizeCb(selection));

break;
if (!isValid || isPriority) {
isValid = true;
measureType = mType;

}


// break;
}

if (!isValid) {
Expand All @@ -190,6 +196,8 @@ void TaskMeasure::update(){
return;
}

// Update tool mode display
setModeSilent(measureType);

if (!_mMeasureObject || measureType->measureObject != _mMeasureObject->getTypeId().getName()) {

Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Part/App/Measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void Measure::initialize() {
"DISTANCEPOINTS",
"Distance Points",
"Part::MeasureDistancePoints",
Part::MeasureDistancePoints::isValidSelection
Part::MeasureDistancePoints::isValidSelection,
nullptr,
});


Expand Down

0 comments on commit cb11524

Please sign in to comment.