SidebarCustomMenuItemsModel.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any
  4. from UM.Qt.ListModel import ListModel
  5. from PyQt5.QtCore import pyqtSlot, Qt
  6. class SidebarCustomMenuItemsModel(ListModel):
  7. name_role = Qt.UserRole + 1
  8. actions_role = Qt.UserRole + 2
  9. menu_item_role = Qt.UserRole + 3
  10. menu_item_icon_name_role = Qt.UserRole + 5
  11. def __init__(self, parent=None):
  12. super().__init__(parent)
  13. self.addRoleName(self.name_role, "name")
  14. self.addRoleName(self.actions_role, "actions")
  15. self.addRoleName(self.menu_item_role, "menu_item")
  16. self.addRoleName(self.menu_item_icon_name_role, "icon_name")
  17. self._updateExtensionList()
  18. def _updateExtensionList(self)-> None:
  19. from cura.CuraApplication import CuraApplication
  20. for menu_item in CuraApplication.getInstance().getSidebarCustomMenuItems():
  21. self.appendItem({
  22. "name": menu_item["name"],
  23. "icon_name": menu_item["icon_name"],
  24. "actions": menu_item["actions"],
  25. "menu_item": menu_item["menu_item"]
  26. })
  27. @pyqtSlot(str, "QVariantList", "QVariantMap")
  28. def callMenuItemMethod(self, menu_item_name: str, menu_item_actions: list, kwargs: Any) -> None:
  29. for item in self._items:
  30. if menu_item_name == item["name"]:
  31. for method in menu_item_actions:
  32. getattr(item["menu_item"], method)(kwargs)
  33. break