Draft: Improve grid behavior

This PR improves the behavior of the Draft grid:
* Options in the preferences: "Always show the grid" and "Show the grid during commands".
* The parameters are used for new views. In existing views the grid can be toggled either during a command or when no command is active.
* When switching to a different workbench all grids are hidden unless GridHideInOtherWorkbenches is set to `False`. This can be a Fine-tuning parameter IMO.

Notes:
* The defaultCameraHeight parameter becomes obsolete. It was only used in very specific circumstances.
* The code of PR #8818 becomes obsolete. It makes more sense to use `App.activeDraftCommand` instead of adding a `tool` argument to some functions. Will do some related cleanup later.
* Improved alignment for the related preference ui. The checkbox interaction has been fixed as well.
This commit is contained in:
Roy-043
2023-11-10 11:08:27 +01:00
committed by Yorik van Havre
parent 6359597341
commit 453c099873
5 changed files with 324 additions and 271 deletions

View File

@@ -53,6 +53,7 @@ import draftguitools.gui_trackers as trackers
from draftutils.init_tools import get_draft_snap_commands
from draftutils.messages import _wrn
from draftutils.translate import translate
from draftutils.todo import ToDo
__title__ = "FreeCAD Draft Snap tools"
__author__ = "Yorik van Havre"
@@ -109,7 +110,6 @@ class Snapper:
self.snapInfo = None
self.lastSnappedObject = None
self.active = True
self.forceGridOff = False
self.lastExtensions = []
# the trackers are stored in lists because there can be several views,
# each with its own set
@@ -1228,7 +1228,7 @@ class Snapper:
if self.dim2:
self.dim2.off()
if self.grid:
if not Draft.getParam("alwaysShowGrid", True):
if self.grid.show_always is False:
self.grid.off()
if self.holdTracker:
self.holdTracker.clear()
@@ -1563,44 +1563,58 @@ class Snapper:
param.SetString("snapModes",snap_modes)
def show_hide_grids(self, show=True):
"""Show the grid in all 3D views where it was previously visible, or
hide the grid in all 3D view. Used when switching to different workbenches.
Hiding the grid can be prevented by setting the GridHideInOtherWorkbenches
preference to `False`.
"""
param = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
if (not show) and (not param.GetBool("GridHideInOtherWorkbenches", True)):
return
mw = Gui.getMainWindow()
views = mw.getWindowsOfType(App.Base.TypeId.fromName("Gui::View3DInventor")) # All 3D views.
for view in views:
if view in self.trackers[0]:
i = self.trackers[0].index(view)
grid = self.trackers[1][i]
if show and grid.show_always:
grid.on()
else:
grid.off()
def show(self):
"""Show the toolbar and the grid."""
"""Show the toolbar, show the grid in all 3D views where it was
previously visible, and start the trackers for the active 3D view
if it is `tracker-less`.
"""
toolbar = self.get_snap_toolbar()
if toolbar:
if Draft.getParam("showSnapBar", True):
toolbar.show()
else:
toolbar.hide()
if Gui.ActiveDocument:
self.setTrackers()
if not App.ActiveDocument.Objects:
if Gui.ActiveDocument.ActiveView:
if Gui.ActiveDocument.ActiveView.getCameraType() == 'Orthographic':
c = Gui.ActiveDocument.ActiveView.getCameraNode()
if c.orientation.getValue().getValue() == (0.0, 0.0, 0.0, 1.0):
p = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
h = p.GetInt("defaultCameraHeight",0)
if h:
c.height.setValue(h)
self.show_hide_grids(show=True)
def hide(self):
"""Hide the toolbar."""
"""Hide the toolbar and hide the grid in all 3D views."""
if hasattr(self, "toolbar") and self.toolbar:
self.toolbar.hide()
self.toolbar.toggleViewAction().setVisible(False)
self.show_hide_grids(show=False)
def setGrid(self, tool=False):
"""Set the grid, if visible."""
self.setTrackers()
if self.grid and (not self.forceGridOff):
if self.grid.Visible:
self.grid.set(tool)
if self.grid.Visible:
self.grid.set(tool)
def setTrackers(self, tool=False):
def setTrackers(self, tool=False, update_grid=True):
"""Set the trackers."""
v = Draft.get3DView()
if v and (v != self.activeview):
@@ -1616,14 +1630,11 @@ class Snapper:
self.extLine2 = self.trackers[8][i]
self.holdTracker = self.trackers[9][i]
else:
self.grid = trackers.gridTracker()
if Draft.getParam("alwaysShowGrid", True):
self.grid.show_always = True
if Draft.getParam("grid", True):
self.grid = trackers.gridTracker()
if Draft.getParam("alwaysShowGrid", True) or tool:
self.grid.on()
else:
self.grid.off()
else:
self.grid = None
self.grid.show_during_command = True
self.tracker = trackers.snapTracker()
self.trackLine = trackers.lineTracker()
if self.snapStyle:
@@ -1651,8 +1662,14 @@ class Snapper:
self.trackers[9].append(self.holdTracker)
self.activeview = v
if tool and self.grid and (not self.forceGridOff):
self.grid.set(tool)
if not update_grid:
return
if self.grid.show_always \
or (self.grid.show_during_command \
and hasattr(App, "activeDraftCommand") \
and App.activeDraftCommand):
self.grid.set()
def addHoldPoint(self):