def __init__(self, items=None, is_root=True, checkableMenu=False, singleCheck=False): super(Menu, self).__init__() self.items = [] if items: parseMenu(self, items) if is_root: self.managerIface = MenuManagerInterface() self.menuIface = None self.checkableMenu = checkableMenu self.singleCheck = singleCheck
class Menu(QObject): itemClicked = pyqtSignal(str, bool) menuDismissed = pyqtSignal() def __init__(self, items=None, is_root=True, checkableMenu=False, singleCheck=False): super(Menu, self).__init__() self.items = [] if items: parseMenu(self, items) if is_root: self.managerIface = MenuManagerInterface() self.menuIface = None self.checkableMenu = checkableMenu self.singleCheck = singleCheck @property def serializableItemList(self): items= [] for item in self.items: items.append(item.serializableContent) return {"items": items, "checkableMenu": self.checkableMenu, "singleCheck": self.singleCheck} def addMenuItem(self, item): self.items.append(item) def addMenuItems(self, items): for item in items: self.addMenuItem(item) def getItemById(self, id): for item in self.items: if item.hasSubMenu: if item.subMenu.getItemById(id): return item.subMenu.getItemById(id) if item.id == id: return item return None def setItemActivity(self, id, value): item = self.getItemById(id) if item: item.isActive = value if self.menuIface: self.menuIface.setItemActivity(id, value) def setItemText(self, id, value): item = self.getItemById(id) if item: item.text = value if self.menuIface: self.menuIface.setItemText(id, value) def showRectMenu(self, x, y): msg = self.managerIface.registerMenu() reply = QDBusReply(msg) self.menuIface = MenuObjectInterface(reply.value()) self.menuIface.showMenu(json.dumps({"x": x, "y": y, "isDockMenu": False, "menuJsonContent": str(self)})) self.menuIface.ItemInvoked.connect(self.itemInvokedSlot) self.menuIface.MenuUnregistered.connect(self.menuUnregisteredSlot) def showDockMenu(self, x, y, cornerDirection="down"): msg = self.managerIface.registerMenu() reply = QDBusReply(msg) self.menuIface = MenuObjectInterface(reply.value()) self.menuIface.showMenu(json.dumps({"x": x, "y": y, "isDockMenu": True, "cornerDirection": cornerDirection, "menuJsonContent": str(self)})) self.menuIface.ItemInvoked.connect(self.itemInvokedSlot) self.menuIface.MenuUnregistered.connect(self.menuUnregisteredSlot) @pyqtSlot(str, bool) def itemInvokedSlot(self, itemId, checked): self.itemClicked.emit(itemId, checked) @pyqtSlot() def menuUnregisteredSlot(self): self.menuDismissed.emit() def __str__(self): return json.dumps(self.serializableItemList)