Views toggling macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
gflorent
Posts: 49
Joined: Fri Aug 10, 2018 10:05 am
Location: France
Contact:

Views toggling macro

Post by gflorent »

Hi,
Building on https://www.freecadweb.org/wiki/Macro_T ... Visibility, and as an exercise on my way to learn the dev side of FreeCAD, here is a new version that hides and restores all views so that the 3d view gets all the space. Run the macro again to restore the Report view, the Console view etc ....

It now deals with all 6 views instead of just 3. The .py file is used to keep visibility state info between macro calls.

vi_toggle_panels_visibility .FCMacro

Code: Select all

# coding: utf-8

r"""macro to toggle visibility of the views

The view configuration is hidden/restored on each call to this macro
The views layout is kept as is.

"""

from PySide import QtCore, QtGui

# global variables/state-keeping module
import vi_toggle_panels_visibility as state


def get_widget_by_name(widget_name, widgets):
    r"""Retrieve a view widget by its name
    
    Parameters
    ----------
    widget_name : str
        The name of the widget (e.g. 'Console view')
    widgets : list
        List of Qt widgets
    
    Returns
    -------
    The widget that corresponds to the name parameter

    """
    for dw in widgets:
        if dw.objectName() == widget_name:
            return dw

# The next 2 calls must be separated or the reference to the children disappear
# -> C++ object already deleted (must be a PySide bug)
mw = FreeCADGui.getMainWindow()
widgets_ = mw.findChildren(QtGui.QDockWidget)
widget_names = [w.objectName() for w in widgets_]

if state.calls % 2 == 0:
    state.widget_states = {w: w.isVisible() for w in
                           [get_widget_by_name(name, widgets_)
                            for name in widget_names]}

state.calls += 1

for widget, is_visible in state.widget_states.items():
    if widget.isVisible():
        widget.hide()
    else:
        if is_visible:
            widget.show()
        else:
            widget.hide()
vi_toggle_panels_visibility.py

Code: Select all

# coding: utf-8

r"""Cross macro calls global variables so that the views state can be restored
on every second call to the macro"""

calls = 0
widget_states = {}

The icon
https://www.freecadweb.org/wiki/File:Ma ... bility.png

Copy the FCMacro and the py file to the Macro folder.
If the name of the py file is changed, the FCMacro import must be changed accordingly.

Happy toggling ! I hope you find it useful.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Views toggling macro

Post by TheMarkster »

Very nice macro you and Piffpoof have put together. I put it on my toolbar. I think I will use it often.

I got to thinking at times I will want to leave the combo view visible while toggling off Python console and Report view, so I added this bit of code:

Code: Select all

if QtGui.QApplication.keyboardModifiers() == QtCore.Qt.ControlModifier:
    exceptionList=["Combo View"]
elif QtGui.QApplication.keyboardModifiers() == QtCore.Qt.ShiftModifier:
    exceptionList=["Python console", "Report view"]
elif QtGui.QApplication.keyboardModifiers() == QtCore.Qt.ShiftModifier.__or__(QtCore.Qt.ControlModifier):
    exceptionList=["Selection view"]
else:
    exceptionList=[""]
if state.calls % 2 == 0:
    state.widget_states = {w: w.isVisible() for w in
                           [get_widget_by_name(name, widgets_)
                            for name in widget_names if name not in exceptionList]}
in place of:

Code: Select all

if state.calls % 2 == 0:
    state.widget_states = {w: w.isVisible() for w in
                           [get_widget_by_name(name, widgets_)
                            for name in widget_names]}
An exception list is created (maybe "white list" would be a better name?) containing the names of views not to be toggled off. The default behavior (no keyboard modifier) is to do as the macro does now. Holding down Ctrl (Ctrl = Command on Mac OS) while executing adds "Combo View" to the exception list, meaning it doesn't get toggled off if already on, but does get toggled on if already toggled off by the macro earlier. Shift does the same thing, except it adds "Python console" and "Report view" to the exception list instead of "Combo View". Ctrl + Shift adds "Selection view" to the exception list.

Ctrl -> Combo View
Shift -> Report view / Python console
Ctrl+Shift -> Selection view
User avatar
gflorent
Posts: 49
Joined: Fri Aug 10, 2018 10:05 am
Location: France
Contact:

Re: Views toggling macro

Post by gflorent »

The 'white list' idea is a really good and useful one ! I will include it. Thanks for that.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Views toggling macro

Post by TheMarkster »

gflorent wrote: Thu Aug 23, 2018 8:34 am The 'white list' idea is a really good and useful one ! I will include it. Thanks for that.
Cool. I was thinking the way I hard coded the window names in English might be a problem, but I just tested by changing languages and it still worked as expected. I guess internally the English names are still being used, but they get translated when setting the window title.
Post Reply