Skip to content

Commit

Permalink
Part: Gui ViewProvider2D Grid Management
Browse files Browse the repository at this point in the history
========================================

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.
  • Loading branch information
abdullahtahiriyo committed May 27, 2020
1 parent da33ffc commit 1efe9c9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
45 changes: 38 additions & 7 deletions src/Mod/Part/Gui/ViewProvider2DObject.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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<std::string> ViewProvider2DObject::getDisplayModes(void) const
Expand All @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions src/Mod/Part/Gui/ViewProvider2DObject.h
Expand Up @@ -49,18 +49,20 @@ 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*);
virtual std::vector<std::string> getDisplayModes(void) const;
virtual const char* getDefaultDisplayMode() const;

/// creates the grid
SoSeparator* createGrid(void);
SoSeparator* createGrid(void);

protected:
virtual bool setEdit(int ModNum);
Expand All @@ -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<ViewProvider2DObject> ViewProvider2DObjectPython;
Expand Down

0 comments on commit 1efe9c9

Please sign in to comment.