A toggle button in the 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!
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

Thanks!
I will check it out.
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

Hi,

The 'checkable' button works very good and very simple to use. I think it will be perfect for PrzemoF. For me it is less perfect, I was looking for something exactly like the perspective/ortho buttons: Two buttons are shown, when I press on perspective it becomes checked, and ortho becomes unchecked, and when I press on ortho, ortho becomes checked and perspective becomes unchecked.
It is not that critical, If it is even slightly complicated task, it does not worth the work. I can do great with what I have now.
Thanks!

shai
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: A toggle button in the toolbar

Post by Jee-Bee »

shaise wrote:Two buttons are shown, when I press on perspective it becomes checked, and ortho becomes unchecked, and when I press on ortho, ortho becomes checked and perspective becomes unchecked.
That Sounds more like what they called radio buttons to me.
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

Jee-Bee wrote:
shaise wrote: That Sounds more like what they called radio buttons to me.
Indeed. A radio button is a better term for it.
Do you know if it is supported in the python interface? (for toolbar buttons, I mean)

shai
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: A toggle button in the toolbar

Post by Jee-Bee »

i don know i have only experience with LabVIEW ... just start with learning python

I saw this http://pyqt.sourceforge.net/Docs/PyQt4/ ... utton.html but that are the normal round ones...
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

Jee-Bee wrote:i don know i have only experience with LabVIEW ... just start with learning python

I saw this http://pyqt.sourceforge.net/Docs/PyQt4/ ... utton.html but that are the normal round ones...
Yes, this is for general gui use, not specific for FreeCAD toolbars.
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: A toggle button in the toolbar

Post by wmayer »

With git commit 26b424e and this example you should have want you want:

Code: Select all


class Cmd1:
    def Activated(self, index):
        pass

    def GetResources(self):
        return { 'MenuText': 'Command 1', 'Checkable': True}


class Cmd2:
    def Activated(self, index):
        pass

    def GetResources(self):
        return { 'MenuText': 'Command 2', 'Checkable': True}

class Cmd3:
    def Activated(self, index):
        pass

    def GetResources(self):
        return { 'MenuText': 'Command 3', 'Checkable': True}


class Cmd4:
    def Activated(self, index):
        pass

    def GetResources(self):
        return { 'MenuText': 'Command 4'}

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

    def Activated(self, index):
        pass

    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', 'DropDownMenu': False, 'Exclusive' : True, }
        
    def IsActive(self): # optional
        return True

FreeCADGui.addCommand('Cmd1',Cmd1())
FreeCADGui.addCommand('Cmd2',Cmd2())
FreeCADGui.addCommand('Cmd3',Cmd3())
FreeCADGui.addCommand('Cmd4',Cmd4())
FreeCADGui.addCommand('MyGroupCommand',MyGroupCommand())
The individual command must set the Checkable key to True. The command must set the Exclusive key to True and it's recommended to set DropDownMenu to False.
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

wmayer,

Thanks again!
I will give it a try!

shai
User avatar
shaise
Posts: 491
Joined: Thu Jun 11, 2015 8:11 am

Re: A toggle button in the toolbar

Post by shaise »

wmayer,

Sorry for the time it took, but I checked it and it works exactly as I wanted it to. Thanks!
There is however a slight issue, perhaps I'm doing something wrong, but initially all buttons are unchecked, and I was expecting the default button to be checked.
I marked this button as: 'Checkable': True, and in the group command I returned GetDefaultCommand() with 0.

Code: Select all

class FSMatchTypeInnerCommand:
    def Activated(self, index):
        pass

    def GetResources(self):
        return { 'Pixmap'  : os.path.join( iconPath , 'IconMatchTypeInner.svg'),
                 'MenuText': 'Match screws by inner thread diameter (Tap hole)', 
                 'Checkable': True}


class FSMatchTypeOuterCommand:
    def Activated(self, index):
        pass
        
    def GetResources(self):
        return { 'Pixmap'  : os.path.join( iconPath , 'IconMatchTypeOuter.svg'),
                 'MenuText': 'Match screws by outer thread diameter (Pass hole)', 
                 'Checkable': False}

class FSMatchTypeGroupCommand:
    def GetCommands(self):
        return ("FSMatchTypeInner", "FSMatchTypeOuter") # a tuple of command names that you want to group

    def Activated(self, index):
        if index == 0:
            FSMatchOuter = False
            FreeCAD.Console.PrintLog("Set auto diameter to match inner thread\n")
        else:
            FSMatchOuter = True
            FreeCAD.Console.PrintLog("Set auto diameter to match outer thread\n")

    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 0

    def GetResources(self):
        return { 'MenuText': 'Screw diamter matching mode', 'ToolTip': 'Screw diamter matching mode (by inner or outer thread diameter)', 'DropDownMenu': False, 'Exclusive' : True }
       
    def IsActive(self): # optional
        return True

FreeCADGui.addCommand('FSMatchTypeInner',FSMatchTypeInnerCommand())
FreeCADGui.addCommand('FSMatchTypeOuter',FSMatchTypeOuterCommand())
FreeCADGui.addCommand('FSMatchTypeGroup',FSMatchTypeGroupCommand())
Thanks again!

shai
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: A toggle button in the toolbar

Post by wmayer »

git commit cda9bf9

If GetDefaultCommand is not there the first command is toggled, otherwise you can control which other command to toggle. And if you want to have an exclusive group but no toggled command then GetDefaultCommand must be implemented to return a number out of range, e.g. -1.
Post Reply