Draft non standard toolbars

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Draft non standard toolbars

Post by carlopav »

chennes wrote: Sorry to take advantage of your knowledge about FC Gui and Qt.
Draft has 2 custom toolbars: Draft_Tray and Draft_Snap.
If I recall correctly those toolbars are created using PySide in the Draft Module, so they have a special handling to show or hide them when the workbench is activated or deactivated. The particular advantage (as I can understand) is that their commands can be launched also when another Draft command isActive without closing it. And that's particulary useful cause they are "status commands": they control directly user settings, change the Draft_Snap_WorkingPlane, change the construction mode or the group for Autogroup feature.

So I was asking myself, is there a way to describe a python command to behave like this from the creation, so it can be added to a standard tollbar?

In short I'd like to have certain python commands (and related actions) behave like Std_View commands. So they remain active while another command is running, but are defined and added to the workbench toolbars in the standard way...

Are you aware of any way to obtain this?
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

Perhaps we could Just create 2 empty toolbars at workbench init, and fill them with custom actions later...
follow my experiments on BIM modelling for architecture design
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Draft non standard toolbars

Post by chennes »

If the concern is just that these toolbars don't properly obey the "isMovable()" setup from the core toolbars, I think the workaround is to make the toolbar manager scan all the toolbars after a workbench is activated, and ensure they are properly configured. There's no way that I'm aware of to prevent a WB from adding its own toolbars, the best the manager can do is clean them up after the fact.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

Thx for picking up the invite :)
It's not only this, it's also the tooltips of commands that are different, but all in all is just a nitpicking of mine to understand if we can standardize it. I have been already refactoring the Snap_Toolbar so the toolbar actions rely on Snap Commands to activate the Snapper preference, and not to direct call to functions, so what remains different from a standard toolbar is just the fact that its actions remain active also when another command is running.

This goes also in the direction of making Draft a bit less special in the FC environment, and a bit more integrated, and if possible to get rid of some several lines of code.
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

chennes wrote: Mon Aug 30, 2021 12:18 am the best the manager can do is clean them up after the fact.
Yes, I agree, but I still find a bit unconvenient that a core workbench behaves so differently and need a workaround :) And this thing is becoming quite interesting to me to try to change (of course I don't want to involve you in this unuseful personal war against the Snap toolbar :twisted: )

Ok, I understood that in c++ a command is defined like this:

Code: Select all

StdCmdViewTop::StdCmdViewTop()
  : Command("Std_ViewTop")
{
    sGroup        = QT_TR_NOOP("Standard-View");
    sMenuText     = QT_TR_NOOP("Top");
    sToolTipText  = QT_TR_NOOP("Set to top view");
    sWhatsThis    = "Std_ViewTop";
    sStatusTip    = QT_TR_NOOP("Set to top view");
    sPixmap       = "view-top";
    sAccel        = "2";
    eType         = Alter3DView;
}
The eType key controls the "context" of the command. It looks like this:

Code: Select all

enum CmdType {
        AlterDoc       = 1,  /**< Command change the Document */
        Alter3DView    = 2,  /**< Command change the Gui */
        AlterSelection = 4,  /**< Command change the Selection */
        ForEdit        = 8,  /**< Command is in a special edit mode active */
        NoTransaction  = 16, /**< Do not setup auto transaction */
    };
So for example, if eType is set to 2 (Alter3DView), the command behaves exactly as I wish...
Now the Python command I want to change is defined like this:

Code: Select all

class Draft_Snap_Lock(Draft_Snap_Base):
    """GuiCommand for the Draft_Snap_Lock tool.

    Activate or deactivate all snap methods at once.
    """

    def __init__(self):
        super(Draft_Snap_Lock, self).__init__(name=translate("draft","Main toggle snap"))

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

        return {'Pixmap': 'Snap_Lock',
                'Accel': "Shift+S",
                'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Main snapping toggle On/Off"),
                'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Activates or deactivates all snap methods at once.")}
So the question become: how can I feed it the eType key, being the returned value of GetResources a dictionary?
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

And the answer is in command.cpp:

The python dictionary key is 'CmdType' and the available types are:

Code: Select all

"AlterDoc"
"Alter3DView"
"AlterSelection"
"ForEdit"
"NoTransaction"
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

Another nice find: you can produce a checkable action in this way:

Code: Select all

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

        return {'Pixmap': 'Snap_Lock',
                'Accel': "Shift+S",
                'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Main snapping toggle On/Off"),
                'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Activates or deactivates all snap methods at once."),
                'CmdType' : "NoTransaction",
                'Checkable' : True}
A problem, it works, but produce this error:

Code: Select all

 PythonCommand::isChecked(): Method GetResources() of the Python command object contains the key 'Checkable' which is not a boolean
the involved function is this one: https://github.com/FreeCAD/FreeCAD/blob ... .cpp#L1412

So a new question, how can overcome this error?
follow my experiments on BIM modelling for architecture design
User avatar
chennes
Veteran
Posts: 3906
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Draft non standard toolbars

Post by chennes »

That is a mystery to me -- can you add some code to print out what type it actually is? It certainly looks like a boolean here, I don't really see how it could be anything else!
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
Roy_043
Veteran
Posts: 8547
Joined: Thu Dec 27, 2018 12:28 pm

Re: Draft non standard toolbars

Post by Roy_043 »

carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Draft non standard toolbars

Post by carlopav »

Thanks a lot for the help!
The Snap toolbar as a standard toolbar works quite good as far as I tested it, and also cleans up a lot of code. Here is a preview:
https://github.com/FreeCAD/FreeCAD/comp ... d?expand=1

You can also add multiple times the commands in the Gui and they sync automatically without the need for extra code.
What's the usefulness of this? That I was trying also to standardize the Draft_snap_widget.
I came to another stopping point:

If I add multiple times a command to more than one toolbar it works perfectly, but it doesn't if I add it to a group... Notice in the screen recording that the dimensions button is perfect while everything in the dropdown doesn't work as expected... anybody have any idea? :oops:


SyscWwvkex.gif
SyscWwvkex.gif (450.18 KiB) Viewed 3189 times
follow my experiments on BIM modelling for architecture design
Post Reply