[Guide] Add Command Group to Toolbar

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

[Guide] Add Command Group to Toolbar

Post by HakanSeven12 »

Submenus.png
Submenus.png (121.04 KiB) Viewed 1089 times


If you want to create command groups firstly add a class to your initgui.py.

Code: Select all

class CommandGroup:
    def __init__(self, cmdlist, menu, TypeId=None, tooltip=None):
        self.cmdlist = cmdlist
        self.menu = menu
        self.TypeId = TypeId
        if tooltip is None:
            self.tooltip = menu
        else:
            self.tooltip = tooltip

    def GetCommands(self):
        return tuple(self.cmdlist)

    def GetResources(self):
        return {'MenuText': self.menu, 'ToolTip': self.tooltip}
Secondly you need to create a list which contains your commands.

Code: Select all

    commandList= ['Command1', 'Command2', 'Command3', 'Command4',
               'Command5', 'Command6', 'Command7', 'Command8', 
               'Command9'
               ]
Thirdly send your list to new class.

Code: Select all

    Gui.addCommand('Command Group Name', 
                   CommandGroup(commandList, 'Command Group Description'))
Finally you can use 'Command Group Name' to add it to toolbar you want :)
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: [Guide] Add Command Group to Toolbar

Post by Kunda1 »

Where should we add this to in the wiki ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
HakanSeven12
Veteran
Posts: 1481
Joined: Wed Feb 06, 2019 10:30 pm

Re: [Guide] Add Command Group to Toolbar

Post by HakanSeven12 »

Kunda1 wrote: Sun Mar 29, 2020 1:05 pm Where should we add this to in the wiki ?
I think you can add it here
https://wiki.freecadweb.org/Workbench_c ... orkbenches
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: [Guide] Add Command Group to Toolbar

Post by Kunda1 »

HakanSeven12 wrote: Sun Mar 29, 2020 1:08 pm
Kunda1 wrote: Sun Mar 29, 2020 1:05 pm Where should we add this to in the wiki ?
I think you can add it here
https://wiki.freecadweb.org/Workbench_c ... orkbenches
vocx wrote::bell:
@vocx do you mind weighing in here?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: [Guide] Add Command Group to Toolbar

Post by vocx »

Kunda1 wrote: Mon Mar 30, 2020 6:00 pm @vocx do you mind weighing in here?
It's fine. As I have mentioned other times, the information is probably already there, however, since the power user's documentation is not very well organized, it's probably difficult to find.

Now, I honestly don't like Hakan's method exactly like that, because he is adding a few arguments and variables to facilitate the creation of the menu. That's fine, but in my opinion we should present the most basic way of creating the group first, and then we can show Hakan's way of using inputs.

This is the most basic way.

Code: Select all

class GroupCommand:
    """Gui command for the group of tools."""

    def GetCommands(self):
        """Tuple of already existing commands.
        
        These command names should have been added with `Gui.addCommand` previously.
        """
        return ("Command_One",
                "Command_Two", "Command_Three")

    def GetResources(self):
        """Set icon, menu and tooltip."""

        return {'MenuText': "Grouped commands",
                'ToolTip': "Information on what these commands do"}

Gui.addCommand('Grop_Command', GroupCommand())
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
fc_tofu
Posts: 653
Joined: Sun Jan 05, 2020 4:56 pm

Re: [Guide] Add Command Group to Toolbar

Post by fc_tofu »

What I am imagining the usage of command group/list is to enhance current "Customize >Toolbars" ability.

Use current "Customize >Toolbars" feature, I spend about 10 minutes in customizing my PartWB. Most time is used in arranging tools display order to be consistent with their original workbench.
If command goup/list can be used, it will take less than 30 seconds, I guess.

The reason that I prefer horizontal toolbar is both for saving mouse click and saving memory (in human brain), so far as total toolbars cover no more than 3 columns.
fsc_2020-04-01_020920b.jpg
fsc_2020-04-01_020920b.jpg (33.54 KiB) Viewed 926 times
fsc_2020-04-01_015824c.jpg
fsc_2020-04-01_015824c.jpg (62.92 KiB) Viewed 926 times
(above, mocked UI)
fsc_2020-03-31_180702c.jpg
fsc_2020-03-31_180702c.jpg (122.21 KiB) Viewed 926 times
There are 2 bugs (unfixed till now) I found must be considered during toolbar customization.
1. Pref data loss when linking external workbench commands (there is a workaround)
https://forum.freecadweb.org/viewtopic. ... 05#p364205

2. Command group conflict between workbenches (no workaround yet)
https://forum.freecadweb.org/viewtopic.php?f=34&t=42799
Post Reply