From 1efe9c920874571e37f94f1cee8c2e1af0ee4e7b Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Wed, 27 May 2020 16:47:21 +0200 Subject: [PATCH] Part: Gui ViewProvider2D Grid Management ======================================== 1. The Grid has a default maximum of 10000 lines that is controlled via a new property. The grid is not created (is empty) if a higher number of lines is necessary. This event is show as a Warning in the Report view. 2. The Grid now checks a new property ShowOnlyInEditMode before deciding whether to show the grid or not. This is a new mode for ViewProvider2D and derived objects. If the property is set to true (and showGrid is true), the grid is only shown if the object is in edit mode. If the property is set to false, the grid is shown regardless of whether it is in edit mode or not (provided that showGrid is true). 3. Grid limits are now encapsulated (private). They can be set via a new function updateGridExtent. --- src/Mod/Part/Gui/ViewProvider2DObject.cpp | 45 +++++++++++++++++++---- src/Mod/Part/Gui/ViewProvider2DObject.h | 12 ++++-- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/Mod/Part/Gui/ViewProvider2DObject.cpp b/src/Mod/Part/Gui/ViewProvider2DObject.cpp index 134d73466f35..2e7228129b0a 100644 --- a/src/Mod/Part/Gui/ViewProvider2DObject.cpp +++ b/src/Mod/Part/Gui/ViewProvider2DObject.cpp @@ -65,10 +65,12 @@ PROPERTY_SOURCE(PartGui::ViewProvider2DObject, PartGui::ViewProviderPart) ViewProvider2DObject::ViewProvider2DObject() { ADD_PROPERTY_TYPE(ShowGrid,(false),"Grid",(App::PropertyType)(App::Prop_None),"Switch the grid on/off"); + ADD_PROPERTY_TYPE(ShowOnlyInEditMode,(true),"Grid",(App::PropertyType)(App::Prop_None),"Show only while in edit mode"); ADD_PROPERTY_TYPE(GridSize,(10.0),"Grid",(App::PropertyType)(App::Prop_None),"Gap size of the grid"); ADD_PROPERTY_TYPE(GridStyle,((long)0),"Grid",(App::PropertyType)(App::Prop_None),"Appearance style of the grid"); ADD_PROPERTY_TYPE(TightGrid,(true),"Grid",(App::PropertyType)(App::Prop_None),"Switch the tight grid mode on/off"); ADD_PROPERTY_TYPE(GridSnap,(false),"Grid",(App::PropertyType)(App::Prop_None),"Switch the grid snap on/off"); + ADD_PROPERTY_TYPE(maxNumberOfLines,(10000),"Grid",(App::PropertyType)(App::Prop_None),"Maximum Number of Lines in grid"); GridRoot = new SoAnnotation(); GridRoot->ref(); @@ -199,6 +201,13 @@ SoSeparator* ViewProvider2DObject::createGrid(void) int lines = vlines + hlines; + if( lines > maxNumberOfLines.getValue() ) { // If + Base::Console().Warning("Grid Disabled: Requested number of lines %d is larger than the maximum configured of %d\n.", lines, maxNumberOfLines.getValue()); + parent->addChild(vts); + parent->addChild(grid); + return GridRoot; + } + // set the grid indices grid->numVertices.setNum(lines); int32_t* vertices = grid->numVertices.startEditing(); @@ -247,9 +256,12 @@ void ViewProvider2DObject::updateData(const App::Property* prop) this->MaxX = bbox2d.MaxX; this->MinY = bbox2d.MinY; this->MaxY = bbox2d.MaxY; - if (ShowGrid.getValue()) { + if (ShowGrid.getValue() && !(ShowOnlyInEditMode.getValue() && !this->isEditing()) ) { createGrid(); } + else { + Gui::coinRemoveAllChildren(GridRoot); + } } } @@ -258,15 +270,14 @@ void ViewProvider2DObject::onChanged(const App::Property* prop) // call father ViewProviderPart::onChanged(prop); - if (prop == &ShowGrid || prop == &Visibility) { - if (ShowGrid.getValue() && Visibility.getValue()) + if (prop == &ShowGrid || prop == &ShowOnlyInEditMode || prop == &Visibility) { + if (ShowGrid.getValue() && Visibility.getValue() && !(ShowOnlyInEditMode.getValue() && !this->isEditing())) createGrid(); else Gui::coinRemoveAllChildren(GridRoot); } if ((prop == &GridSize) || (prop == &GridStyle) || (prop == &TightGrid)) { - if (ShowGrid.getValue()) { - Gui::coinRemoveAllChildren(GridRoot); + if (ShowGrid.getValue() && !(ShowOnlyInEditMode.getValue() && !this->isEditing())) { createGrid(); } } @@ -296,18 +307,22 @@ void ViewProvider2DObject::attach(App::DocumentObject *pcFeat) { ViewProviderPart::attach(pcFeat); - if (ShowGrid.getValue()) + if (ShowGrid.getValue() && !(ShowOnlyInEditMode.getValue() && !this->isEditing())) createGrid(); } bool ViewProvider2DObject::setEdit(int) { + if (ShowGrid.getValue()) + createGrid(); + return false; } void ViewProvider2DObject::unsetEdit(int) { - + if (ShowGrid.getValue() && ShowOnlyInEditMode.getValue()) + Gui::coinRemoveAllChildren(GridRoot); } std::vector ViewProvider2DObject::getDisplayModes(void) const @@ -329,6 +344,22 @@ const char* ViewProvider2DObject::getDefaultDisplayMode() const return "Wireframe"; } +void ViewProvider2DObject::updateGridExtent(float minx, float maxx, float miny, float maxy) +{ + bool redraw = false; + + if( minx < MinX || maxx > MaxX || miny < MinY || maxy > MaxY) + redraw = true; + + MinX = minx; + MaxX = maxx; + MinY = miny; + MaxY = maxy; + + if(redraw && ShowGrid.getValue() && !(ShowOnlyInEditMode.getValue() && !this->isEditing())) + createGrid(); +} + // ----------------------------------------------------------------------- namespace Gui { diff --git a/src/Mod/Part/Gui/ViewProvider2DObject.h b/src/Mod/Part/Gui/ViewProvider2DObject.h index f6662c771dfb..877ce17470c0 100644 --- a/src/Mod/Part/Gui/ViewProvider2DObject.h +++ b/src/Mod/Part/Gui/ViewProvider2DObject.h @@ -49,10 +49,12 @@ class PartGuiExport ViewProvider2DObject: public PartGui::ViewProviderPart /// Property to switch the grid on and off App::PropertyBool ShowGrid; + App::PropertyBool ShowOnlyInEditMode; App::PropertyLength GridSize; App::PropertyEnumeration GridStyle; App::PropertyBool TightGrid; App::PropertyBool GridSnap; + App::PropertyInteger maxNumberOfLines; virtual void attach(App::DocumentObject *); virtual void updateData(const App::Property*); @@ -60,7 +62,7 @@ class PartGuiExport ViewProvider2DObject: public PartGui::ViewProviderPart virtual const char* getDefaultDisplayMode() const; /// creates the grid - SoSeparator* createGrid(void); + SoSeparator* createGrid(void); protected: virtual bool setEdit(int ModNum); @@ -72,12 +74,16 @@ class PartGuiExport ViewProvider2DObject: public PartGui::ViewProviderPart SoSeparator *GridRoot; + void updateGridExtent(float minx, float maxx, float miny, float maxy); + + static const char* GridStyleEnums[]; + static App::PropertyQuantityConstraint::Constraints GridSizeRange; + +private: float MinX; float MaxX; float MinY; float MaxY; - static const char* GridStyleEnums[]; - static App::PropertyQuantityConstraint::Constraints GridSizeRange; }; typedef Gui::ViewProviderPythonFeatureT ViewProvider2DObjectPython;