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