FlatCAMTool.py 5.4 KB

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