FlatCAMTool.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, shortcut=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. if shortcut is None:
  45. self.menuAction.setText(self.toolName)
  46. else:
  47. self.menuAction.setText(self.toolName + '\t%s' % shortcut)
  48. # add a ToolTip to the new Action
  49. # self.menuAction.setToolTip(self.toolTip) # currently not available
  50. # insert the action in the position specified by 'before' and 'pos' kwargs
  51. pos.insertAction(before, self.menuAction)
  52. # if separator parameter is True add a Separator after the newly created Action
  53. if separator is True:
  54. pos.addSeparator()
  55. self.menuAction.triggered.connect(self.run)
  56. def run(self):
  57. if self.app.tool_tab_locked is True:
  58. return
  59. # Remove anything else in the GUI
  60. self.app.ui.tool_scroll_area.takeWidget()
  61. # Put ourself in the GUI
  62. self.app.ui.tool_scroll_area.setWidget(self)
  63. # Switch notebook to tool page
  64. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  65. # Set the tool name as the widget object name
  66. self.app.ui.tool_scroll_area.widget().setObjectName(self.toolName)
  67. self.show()