Improving the context menu - smart context menu

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
ABeton
Posts: 150
Joined: Tue Sep 03, 2019 6:39 pm

Improving the context menu - smart context menu

Post by ABeton »

Continuation of a discussion in this thread https://forum.freecadweb.org/viewtopic. ... 1&start=90.

My idea is to have a context menu depend on the type of the object that is selected. So for example if a line is selected, I would like to have a context menu with commands like: edit, move, rotate, stretch and maybe a couple of more. And if I select a wall or something else I would have a different context menu with some other commands. Something similar to the context menu that is used in sketcher, but with only five or six commands avalible. So it would be something like "If line is selected when you right click somewhere in the viewport you get a coressponding context menu.".

My end goal would be to have something like this in FreeCAD.
Screenshot from 2019-10-15 15-20-10.png
Screenshot from 2019-10-15 15-20-10.png (12.91 KiB) Viewed 849 times
Screenshot from 2019-10-15 15-19-50.png
Screenshot from 2019-10-15 15-19-50.png (25.65 KiB) Viewed 849 times

I looked into the code that vocx mentioned

Code: Select all

    def ContextMenu(self, recipient):
        if recipient == "View":
            if FreeCAD.activeDraftCommand is None:
                if FreeCADGui.Selection.getSelection():
                    self.appendContextMenu("Draft", self.cmdList + self.modList)
                    self.appendContextMenu("Utilities", self.treecmdList)
                else:
                    self.appendContextMenu("Draft", self.cmdList)
            else:
                if FreeCAD.activeDraftCommand.featureName == "Line":
                    self.appendContextMenu("", self.lineList)
        else:
            if FreeCADGui.Selection.getSelection():
                self.appendContextMenu("Utilities", self.treecmdList)
I added a new menu, in the Draft, Init Gui.

Code: Select all

self.linemodList = ["Draft_Edit", "Draft_Move", "Draft_Rotate", "Draft_Upgrade",
                      "Draft_Downgrade", "Draft_Stretch"]
and than appended it to the existing context menu.

Code: Select all

def ContextMenu(self, recipient):
        if recipient == "View":
            if FreeCAD.activeDraftCommand is None:
                if FreeCADGui.Selection.getSelection():
                    self.appendContextMenu("Draft", self.cmdList + self.modList)
                    self.appendContextMenu("Utilities", self.treecmdList)
                    self.appendContextMenu("Modify", self.linemodList)
                else:
                    self.appendContextMenu("Draft", self.cmdList)
            else:
                if FreeCAD.activeDraftCommand.featureName == "Line":
                    self.appendContextMenu("", self.lineList)
        else:
            if FreeCADGui.Selection.getSelection():
                self.appendContextMenu("Utilities", self.treecmdList)
My question is how would I make this context menu change depending on the object that is curently selected, something like

Code: Select all

def ContextMenu(self, recipient):
        if recipient == "View":
            if FreeCAD.activeDraftCommand is None:
                if FreeCADGui.Selection.getSelection():
                    self.appendContextMenu("Draft", self.cmdList + self.modList)
                    self.appendContextMenu("Utilities", self.treecmdList)
                    if "selection.type" == "wire object or similar":
                         self.appendContextMenu("Modify", self.linemodList)
                else:
                    self.appendContextMenu("Draft", self.cmdList)
            else:
                if FreeCAD.activeDraftCommand.featureName == "Line":
                    self.appendContextMenu("", self.lineList)
        else:
            if FreeCADGui.Selection.getSelection():
                self.appendContextMenu("Utilities", self.treecmdList)

I hope can help me with this.
ABeton
Posts: 150
Joined: Tue Sep 03, 2019 6:39 pm

Re: Improving the context menu - smart context menu

Post by ABeton »

And would it also be possible to create a context menu with two windows, one window with the existing context menu, and the other window with only icons without text, again like in the picture bellow?

Screenshot from 2019-10-15 15-19-50.png
Screenshot from 2019-10-15 15-19-50.png (25.65 KiB) Viewed 848 times
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Improving the context menu - smart context menu

Post by Kunda1 »

Bumping this thread. Any script enthusiasts want to weigh in?
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
Post Reply