Gui: Update sketcher colors after file load

This commit introduces AutoColor property for sketches that when
enabled ensures that sketches are updated with the color of user
preference. It ensures that sketches from different users will be
still visible on different themes.

To ensure backward compatiblity migration strategy is to compute default
value for this property based on the color. If it is non-white (the
default value in FreeCAD) we assume that the change was inentional and
that it should be kept as-is turning automatic coloring off. In other
cases it is enabled by default.

Fixes: FreeCAD#13980
This commit is contained in:
Kacper Donat
2024-05-30 11:29:30 +02:00
committed by Chris Hennes
parent 60ec5db4b3
commit bcb4ee21cc
2 changed files with 74 additions and 2 deletions

View File

@@ -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<VisualLayer> 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);

View File

@@ -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(