def populateTop(): """Populate all top menus.""" # Global menu mode if p.GetBool("Global", 0): workbench = "GlobalPanel" else: workbench = Gui.activeWorkbench().__class__.__name__ for menu in mw.findChildren(QtGui.QMenu, "NaviCube_Menu"): commands = [] # User if p.GetGroup("User").GetGroup(workbench).GetString("default"): domain = ( p.GetGroup("User").GetGroup(workbench).GetString("default")) # System elif p.GetGroup("System").GetGroup(workbench).GetString("default"): domain = ( p.GetGroup("System").GetGroup(workbench).GetString("default")) # Global default else: domain = "CPMenu.System.GlobalPanel.GlobalDefault" group = cpc.findGroup(domain) if group: commands = cpc.splitIndex(group, "commands") commands = globalDefault(commands) addActions(menu, commands)
def populateSub(domain): """Populate submenu.""" action = menuAction(domain) if action: commands = [] menu = action.menu() group = cpc.findGroup(domain) if group: commands = cpc.splitIndex(group, "commands") commands = globalDefault(commands) addActions(menu, commands)
def globalDefault(commands): """Add commands from global default menu.""" temp = [] for cmd in commands: if cmd == "CPGlobalDefault": domain = "CPMenu.System.GlobalPanel.GlobalDefault" group = cpc.findGroup(domain) if group: for cmdGlobal in cpc.splitIndex(group, "commands"): temp.append(cmdGlobal) else: temp.append(cmd) return temp
def onClose(): """Remove system presets and groups without index on FreeCAD close.""" p.RemGroup("System") for wb in Gui.listWorkbenches(): base = p.GetGroup("User").GetGroup(wb) if not cpc.splitIndex(base): p.GetGroup("User").RemGroup(wb)
def toolbarCommands(name=None): "Extract commands from toolbars." commands = [] actions = cpc.actionList() if name: toolbars = [mw.findChild(QtGui.QToolBar, name)] else: toolbars = getToolbars() for tb in toolbars: for btn in tb.findChildren(QtGui.QToolButton): if btn.menu(): menuCommands(btn.menu(), actions, commands) elif (btn.defaultAction() and btn.defaultAction().objectName() in actions): commands.append(btn.defaultAction().objectName()) else: pass return commands
def menuAction(domain): """Create action with submenu.""" action = None group = cpc.findGroup(domain) if group: for act in menuActions: if act.menu().objectName() == domain: action = act if not action: action = QtGui.QAction(mw) menu = QtGui.QMenu() menu.setObjectName(domain) action.setMenu(menu) menuActions.append(action) mapperShow.setMapping(menu, domain) menu.aboutToShow.connect(mapperShow.map) action.setText(group.GetString("name")) return action
def addActions(menu, commands): """Add actions to menu.""" actions = cpc.actionList() # Empty menu for act in menu.actions(): menu.removeAction(act) # Add actions for cmd in commands: if cmd == "CPSeparator": menu.addSeparator() elif cmd == "CPMenu": pass elif cmd.startswith("CPMenu"): ma = menuAction(cmd) if ma: menu.addAction(ma) elif cmd in actions: menu.addAction(actions[cmd]) else: pass
def addMenu(menu=None): """addMenu({menu}) Command panel API provides ability to preset commands for workbench. Settings are deleted when FreeCAD exits normally. Following is a two menus example for Start workbench. import CommandPanel as cp workbench = "StartWorkbench" # workbench = "GlobalPanel" # Global panel menuDemo = { "workbench": workbench, # Mandatory "uuid": "StartDemo", # Mandatory "name": "Demo", "commands": [ "Std_ViewFront", # Command name "CPSeparator", # Separator "Std_ViewTop", "Std_ViewRight"]} domainDemo = cp.addMenu(menuDemo) # Add menuDemo menuDefault = { "workbench": workbench, "uuid": "StartDefault", "name": "Default", "default": True, # Set as default menu "commands": [ "CPGlobalDefault", # Add global defaults domainDemo]} # Add menuDemo cp.addMenu(menuDefault)""" # Workbench if menu and "workbench" in menu: wb = menu["workbench"] if "." in wb or "," in wb: wb = None else: wb = None # UUID if wb and "uuid" in menu: uid = menu["uuid"] if "." in uid or "," in uid: uid = None else: uid = None if wb and uid: domain = ".".join(["CPMenu", "System", wb, uid]) group = cpc.findGroup(domain) if not group: group = cpc.newGroup(domain) if group: # UUID group.SetString("uuid", uid) # Name if "name" in menu: group.SetString("name", menu["name"]) # Commands if "commands" in menu: temp = [] for cmd in menu["commands"]: if cmd.startswith("CPMenu") and "," not in cmd: temp.append(cmd) elif "." not in cmd and "," not in cmd: temp.append(cmd) else: pass group.SetString("commands", ",".join(temp)) # Default if "default" in menu: base = p.GetGroup("System").GetGroup(wb) base.SetString("default", domain) else: domain = None else: domain = None return domain