diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp index c418e5c24c..2a2282fa91 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.cpp @@ -365,6 +365,20 @@ void ViewProviderSketch::ParameterObserver::initParameters() Client.viewProviderParameters.stdCountSegments = v; }, nullptr}}, + {"SketchEdgeColor", + {[this](const std::string& string, App::Property* property) { + if (Client.AutoColor.getValue()) { + updateColorProperty(string, property, 1.0f, 1.0f, 1.0f); + } + }, + &Client.LineColor}}, + {"SketchVertexColor", + {[this](const std::string& string, App::Property* property) { + if (Client.AutoColor.getValue()) { + updateColorProperty(string, property, 1.0f, 1.0f, 1.0f); + } + }, + &Client.PointColor}}, }; for (auto& val : parameterMap) { @@ -377,8 +391,6 @@ void ViewProviderSketch::ParameterObserver::initParameters() // unsubscribed parameters which update a property on just once upon construction (and before // restore if properties are being restored from a file) - updateColorProperty("SketchEdgeColor", &Client.LineColor, 1.0f, 1.0f, 1.0f); - updateColorProperty("SketchVertexColor", &Client.PointColor, 1.0f, 1.0f, 1.0f); updateBoolProperty("ShowGrid", &Client.ShowGrid, false); updateBoolProperty("GridAuto", &Client.GridAuto, true); updateGridSize("GridSize", &Client.GridSize); @@ -539,6 +551,13 @@ ViewProviderSketch::ViewProviderSketch() (App::PropertyType)(App::Prop_ReadOnly), "Information about the Visual Representation of layers"); + ADD_PROPERTY_TYPE( + AutoColor, + (true), + "Object Style", + (App::PropertyType)(App::Prop_None), + "If true, this sketch will be colored based on user preferences. Turn it off to set color explicitly."); + // TODO: This is part of a naive minimal implementation to substitute rendering order // Three equally visual layers to enable/disable layer. std::vector layers; @@ -2895,10 +2914,58 @@ void ViewProviderSketch::onChanged(const App::Property* prop) } return; } + + if (prop == &AutoColor) { + auto usesAutomaticColors = AutoColor.getValue(); + + // when auto color is enabled don't save color information in the document + // so it does not cause unnecessary updates if multiple users use different colors + LineColor.setStatus(App::Property::Transient, usesAutomaticColors); + PointColor.setStatus(App::Property::Transient, usesAutomaticColors); + + // and mark this property as read-only hidden so it's not possible to change manually + LineColor.setStatus(App::Property::ReadOnly, usesAutomaticColors); + LineColor.setStatus(App::Property::Hidden, usesAutomaticColors); + PointColor.setStatus(App::Property::ReadOnly, usesAutomaticColors); + PointColor.setStatus(App::Property::Hidden, usesAutomaticColors); + + return; + } + // call father ViewProviderPart::onChanged(prop); } +void SketcherGui::ViewProviderSketch::startRestoring() +{ + // small hack: before restoring mark AutoColor property as non-touched + // this allows us to test if this property was restored in the finishRestoring method + AutoColor.setStatus(App::Property::Touched, false); +} + +void SketcherGui::ViewProviderSketch::finishRestoring() +{ + ViewProvider2DObject::finishRestoring(); + + // if AutoColor was not touched it means that the document is from older version of FreeCAD + // that meaans that we need to run migration strategy and come up with a proper value + if (!AutoColor.isTouched()) { + // white is the normally provided default for FreeCAD sketch colors + auto white = App::Color(1.f, 1.f, 1.f, 1.f); + + auto colorWasNeverChanged = + LineColor.getValue() == white && + PointColor.getValue() == white; + + AutoColor.setValue(colorWasNeverChanged); + } + + if (AutoColor.getValue()) { + // update colors according to current user preferences + pObserver->initParameters(); + } +} + void ViewProviderSketch::attach(App::DocumentObject* pcFeat) { ViewProviderPart::attach(pcFeat); diff --git a/src/Mod/Sketcher/Gui/ViewProviderSketch.h b/src/Mod/Sketcher/Gui/ViewProviderSketch.h index 89a9e83cd9..886151c305 100644 --- a/src/Mod/Sketcher/Gui/ViewProviderSketch.h +++ b/src/Mod/Sketcher/Gui/ViewProviderSketch.h @@ -517,6 +517,7 @@ public: App::PropertyBool RestoreCamera; App::PropertyBool ForceOrtho; App::PropertyBool SectionView; + App::PropertyBool AutoColor; App::PropertyString EditingWorkbench; SketcherGui::PropertyVisualLayerList VisualLayerList; //@} @@ -753,6 +754,10 @@ protected: void onChanged(const App::Property* prop) override; //@} + /// hook after property restoring to change some property statuses + void startRestoring() override; + void finishRestoring() override; + private: /// function to handle OCCT BSpline weight calculation singularities and representation void scaleBSplinePoleCirclesAndUpdateSolverAndSketchObjectGeometry(