FlatCAMTool.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. from shapely.geometry import Polygon
  11. class FlatCAMTool(QtWidgets.QWidget):
  12. toolName = "FlatCAM Generic Tool"
  13. def __init__(self, app, parent=None):
  14. """
  15. :param app: The application this tool will run in.
  16. :type app: App
  17. :param parent: Qt Parent
  18. :return: FlatCAMTool
  19. """
  20. QtWidgets.QWidget.__init__(self, parent)
  21. # self.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
  22. self.layout = QtWidgets.QVBoxLayout()
  23. self.setLayout(self.layout)
  24. self.app = app
  25. self.menuAction = None
  26. def install(self, icon=None, separator=None, shortcut=None, **kwargs):
  27. before = None
  28. # 'pos' is the menu where the Action has to be installed
  29. # if no 'pos' kwarg is provided then by default our Action will be installed in the menutool
  30. # as it previously was
  31. if 'pos' in kwargs:
  32. pos = kwargs['pos']
  33. else:
  34. pos = self.app.ui.menutool
  35. # 'before' is the Action in the menu stated by 'pos' kwarg, before which we want our Action to be installed
  36. # if 'before' kwarg is not provided, by default our Action will be added in the last place.
  37. if 'before' in kwargs:
  38. before = (kwargs['before'])
  39. # create the new Action
  40. self.menuAction = QtWidgets.QAction(self)
  41. # if provided, add an icon to this Action
  42. if icon is not None:
  43. self.menuAction.setIcon(icon)
  44. # set the text name of the Action, which will be displayed in the menu
  45. if shortcut is None:
  46. self.menuAction.setText(self.toolName)
  47. else:
  48. self.menuAction.setText(self.toolName + '\t%s' % shortcut)
  49. # add a ToolTip to the new Action
  50. # self.menuAction.setToolTip(self.toolTip) # currently not available
  51. # insert the action in the position specified by 'before' and 'pos' kwargs
  52. pos.insertAction(before, self.menuAction)
  53. # if separator parameter is True add a Separator after the newly created Action
  54. if separator is True:
  55. pos.addSeparator()
  56. self.menuAction.triggered.connect(self.run)
  57. def run(self):
  58. if self.app.tool_tab_locked is True:
  59. return
  60. # Remove anything else in the GUI
  61. self.app.ui.tool_scroll_area.takeWidget()
  62. # Put ourself in the GUI
  63. self.app.ui.tool_scroll_area.setWidget(self)
  64. # Switch notebook to tool page
  65. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  66. # Set the tool name as the widget object name
  67. self.app.ui.tool_scroll_area.widget().setObjectName(self.toolName)
  68. self.show()
  69. def draw_tool_selection_shape(self, old_coords, coords, **kwargs):
  70. """
  71. :param old_coords: old coordinates
  72. :param coords: new coordinates
  73. :return:
  74. """
  75. if 'color' in kwargs:
  76. color = kwargs['color']
  77. else:
  78. color = self.app.defaults['global_sel_line']
  79. if 'face_color' in kwargs:
  80. face_color = kwargs['face_color']
  81. else:
  82. face_color = self.app.defaults['global_sel_fill']
  83. if 'face_alpha' in kwargs:
  84. face_alpha = kwargs['face_alpha']
  85. else:
  86. face_alpha = 0.3
  87. x0, y0 = old_coords
  88. x1, y1 = coords
  89. pt1 = (x0, y0)
  90. pt2 = (x1, y0)
  91. pt3 = (x1, y1)
  92. pt4 = (x0, y1)
  93. sel_rect = Polygon([pt1, pt2, pt3, pt4])
  94. # color_t = Color(face_color)
  95. # color_t.alpha = face_alpha
  96. color_t = face_color[:-2] + str(hex(int(face_alpha * 255)))[2:]
  97. self.app.tool_shapes.add(sel_rect, color=color, face_color=color_t, update=True,
  98. layer=0, tolerance=None)
  99. if self.app.is_legacy is True:
  100. self.app.tool_shapes.redraw()
  101. def delete_tool_selection_shape(self):
  102. self.app.tool_shapes.clear()
  103. self.app.tool_shapes.redraw()