FlatCAMTool.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. ############################################################
  8. from PyQt5 import QtGui, QtCore, QtWidgets, QtWidgets
  9. from PyQt5.QtCore import Qt
  10. class FlatCAMTool(QtWidgets.QWidget):
  11. toolName = "FlatCAM Generic Tool"
  12. def __init__(self, app, parent=None):
  13. """
  14. :param app: The application this tool will run in.
  15. :type app: App
  16. :param parent: Qt Parent
  17. :return: FlatCAMTool
  18. """
  19. QtWidgets.QWidget.__init__(self, parent)
  20. # self.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
  21. self.layout = QtWidgets.QVBoxLayout()
  22. self.setLayout(self.layout)
  23. self.app = app
  24. self.menuAction = None
  25. def install(self, icon=None, separator=None, **kwargs):
  26. before = None
  27. # 'pos' is the menu where the Action has to be installed
  28. # if no 'pos' kwarg is provided then by default our Action will be installed in the menutool
  29. # as it previously was
  30. if 'pos' in kwargs:
  31. pos = kwargs['pos']
  32. else:
  33. pos = self.app.ui.menutool
  34. # 'before' is the Action in the menu stated by 'pos' kwarg, before which we want our Action to be installed
  35. # if 'before' kwarg is not provided, by default our Action will be added in the last place.
  36. if 'before' in kwargs:
  37. before = (kwargs['before'])
  38. # create the new Action
  39. self.menuAction = QtWidgets.QAction(self)
  40. # if provided, add an icon to this Action
  41. if icon is not None:
  42. self.menuAction.setIcon(icon)
  43. # set the text name of the Action, which will be displayed in the menu
  44. self.menuAction.setText(self.toolName)
  45. # add a ToolTip to the new Action
  46. # self.menuAction.setToolTip(self.toolTip) # currently not available
  47. # insert the action in the position specified by 'before' and 'pos' kwargs
  48. pos.insertAction(before, self.menuAction)
  49. # if separator parameter is True add a Separator after the newly created Action
  50. if separator is True:
  51. pos.addSeparator()
  52. self.menuAction.triggered.connect(self.run)
  53. def run(self):
  54. if self.app.tool_tab_locked is True:
  55. return
  56. # Remove anything else in the GUI
  57. self.app.ui.tool_scroll_area.takeWidget()
  58. # Put ourself in the GUI
  59. self.app.ui.tool_scroll_area.setWidget(self)
  60. # Switch notebook to tool page
  61. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  62. self.show()