Drop-down button in python workbenches

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!
User avatar
shaise
Posts: 470
Joined: Thu Jun 11, 2015 8:11 am

Drop-down button in python workbenches

Post by shaise »

Hi,

I have been working on the last week on a fasteners workbench that lets the user easily add screws, bolts, nuts etc to the design. https://github.com/shaise/FreeCAD_FastenersWB
The problem is, as the number of different types grow, the toolbar becomes more and more crowded:
ftolbar.jpg
ftolbar.jpg (10.39 KiB) Viewed 3694 times
My question: is it possible to group several functions into a drop-down button like this:
bdropdown.jpg
bdropdown.jpg (2.53 KiB) Viewed 3694 times
Can it be done in pure python workbench?

Thanks!
shai
Last edited by shaise on Fri Jun 26, 2015 11:24 am, edited 1 time in total.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Drop-down button in python workbenches

Post by wmayer »

Can it be done in pure python workbench?
Not yet but I have it on my to-do list.
User avatar
shaise
Posts: 470
Joined: Thu Jun 11, 2015 8:11 am

Re: Drop-down button in python workbenches

Post by shaise »

Thanks for the quick reply.
For the meantime I think I will just separate to several toolbars so they can be moved around.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Drop-down button in python workbenches

Post by wmayer »

git commit 882ecd3 implements this feature now.

How does it work? You write a Python command class with this interface:

Code: Select all

class MyGroupCommand:
    def GetCommands(self):
        return ("Cmd1", "Cmd2", "Cmd3", "Cmd4") # a tuple of command names that you want to group

    def GetDefaultCommand(self): # return the index of the tuple of the default command. This method is optional and when not implemented '0' is used  
        return 2

    def GetResources(self):
        return { 'MenuText': 'Group command', 'ToolTip': 'Example group command'}
        
    def IsActive(self): # optional
        return True
        
FreeCADGui.addCommand('MyGroupCommand',MyGroupCommand())
If for any reason the group command class must manage the activation of a clicked command you can additionally implement the method Activated.

Code: Select all

class MyGroupCommand:
...
    def Activated(self, index): # index is an int in the range [0, len(GetCommands)[
        if index == 0:
           ...
        elif index == 1:
            ...
        elif index == 2:
           ...
User avatar
shaise
Posts: 470
Joined: Thu Jun 11, 2015 8:11 am

Re: Drop-down button in python workbenches

Post by shaise »

That was fast! Thanks!
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Drop-down button in python workbenches

Post by yorik »

Awesome Werner, like always, much better implementation than what I was thinking :)
hamish
Posts: 72
Joined: Wed Nov 12, 2014 7:08 am

Re: Drop-down button in python workbenches

Post by hamish »

Hi wmayer,

Looks great, I plan to make use of the GroupCommand Feature for the drawing dimension workbench.
Before I do so however i would like add support for backwards compatibility with regard to FreeCAD 0.15 users.
There i would like add a check to see if the user's FreeCAD has the GroupCommand Feature.

I suppose something along the lines of

Code: Select all

if int( FreeCAD.Version()[1] > 15 ) and  int( FreeCAD.Version()[2].split()[0] ) > 5165 then
   GroupCommand Feature  
else 
    ...
would work, but is there a nicer way of doing this check?(which is backwards compilable upto FreeCAD 0.15 )
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Drop-down button in python workbenches

Post by wmayer »

would work, but is there a nicer way of doing this check?(which is backwards compilable upto FreeCAD 0.15 )
I think that's the only way to go.
hamish
Posts: 72
Joined: Wed Nov 12, 2014 7:08 am

Re: Drop-down button in python workbenches

Post by hamish »

Implemented the Group Command Feature and its working as expected.
The feature saves alot of toolbar space which is great, so thanks wmayer!

Is there an option to change the drop-list from a text list (containing each command's menu text), to an icon list?
If so, i would prefer this icon based alternative.
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: Drop-down button in python workbenches

Post by triplus »

hamish wrote:Implemented the Group Command Feature and its working as expected.
The feature saves alot of toolbar space which is great, so thanks wmayer!

Is there an option to change the drop-list from a text list (containing each command's menu text), to an icon list?
If so, i would prefer this icon based alternative.
Looks good.

P.S. Just a side note probably this new toolbar should be set to ON by default.
Post Reply