ToolProperties.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from PyQt5 import QtGui, QtCore, QtWidgets
  2. from PyQt5.QtCore import Qt
  3. from FlatCAMTool import FlatCAMTool
  4. from FlatCAMObj import *
  5. class Properties(FlatCAMTool):
  6. toolName = "Properties"
  7. def __init__(self, app):
  8. FlatCAMTool.__init__(self, app)
  9. self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
  10. # this way I can hide/show the frame
  11. self.properties_frame = QtWidgets.QFrame()
  12. self.properties_frame.setContentsMargins(0, 0, 0, 0)
  13. self.layout.addWidget(self.properties_frame)
  14. self.properties_box = QtWidgets.QVBoxLayout()
  15. self.properties_box.setContentsMargins(0, 0, 0, 0)
  16. self.properties_frame.setLayout(self.properties_box)
  17. ## Title
  18. title_label = QtWidgets.QLabel("%s" % self.toolName)
  19. title_label.setStyleSheet("""
  20. QLabel
  21. {
  22. font-size: 16px;
  23. font-weight: bold;
  24. }
  25. """)
  26. self.properties_box.addWidget(title_label)
  27. # self.layout.setMargin(0) # PyQt4
  28. self.properties_box.setContentsMargins(0, 0, 0, 0) # PyQt5
  29. self.vlay = QtWidgets.QVBoxLayout()
  30. self.properties_box.addLayout(self.vlay)
  31. self.treeWidget = QtWidgets.QTreeWidget()
  32. self.treeWidget.setColumnCount(2)
  33. self.treeWidget.setHeaderHidden(True)
  34. self.treeWidget.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  35. self.treeWidget.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Expanding)
  36. self.vlay.addWidget(self.treeWidget)
  37. self.vlay.setStretch(0,0)
  38. def run(self):
  39. self.app.report_usage("ToolProperties()")
  40. if self.app.tool_tab_locked is True:
  41. return
  42. self.set_tool_ui()
  43. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  44. if self.app.ui.splitter.sizes()[0] == 0:
  45. self.app.ui.splitter.setSizes([1, 1])
  46. else:
  47. try:
  48. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  49. self.app.ui.splitter.setSizes([0, 1])
  50. except AttributeError:
  51. pass
  52. FlatCAMTool.run(self)
  53. self.properties()
  54. def install(self, icon=None, separator=None, **kwargs):
  55. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  56. def set_tool_ui(self):
  57. # this reset the TreeWidget
  58. self.treeWidget.clear()
  59. self.properties_frame.show()
  60. def properties(self):
  61. obj_list = self.app.collection.get_selected()
  62. if not obj_list:
  63. self.app.inform.emit("[ERROR_NOTCL] Properties Tool was not displayed. No object selected.")
  64. self.app.ui.notebook.setTabText(2, "Tools")
  65. self.properties_frame.hide()
  66. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  67. return
  68. for obj in obj_list:
  69. self.addItems(obj)
  70. self.app.inform.emit("[success] Object Properties are displayed.")
  71. self.app.ui.notebook.setTabText(2, "Properties Tool")
  72. def addItems(self, obj):
  73. parent = self.treeWidget.invisibleRootItem()
  74. font = QtGui.QFont()
  75. font.setBold(True)
  76. obj_type = self.addParent(parent, 'TYPE', expanded=True, color=QtGui.QColor("#000000"), font=font)
  77. obj_name = self.addParent(parent, 'NAME', expanded=True, color=QtGui.QColor("#000000"), font=font)
  78. dims = self.addParent(parent, 'Dimensions', expanded=True, color=QtGui.QColor("#000000"), font=font)
  79. units = self.addParent(parent, 'Units', expanded=True, color=QtGui.QColor("#000000"), font=font)
  80. options = self.addParent(parent, 'Options', color=QtGui.QColor("#000000"), font=font)
  81. if obj.kind.lower() == 'gerber':
  82. apertures = self.addParent(parent, 'Apertures', expanded=True, color=QtGui.QColor("#000000"), font=font)
  83. else:
  84. tools = self.addParent(parent, 'Tools', expanded=True, color=QtGui.QColor("#000000"), font=font)
  85. separator = self.addParent(parent, '')
  86. self.addChild(obj_type, ['Object Type:', ('%s' % (obj.kind.capitalize()))], True)
  87. try:
  88. self.addChild(obj_type, ['Geo Type:', ('%s' % ({False: "Single-Geo", True: "Multi-Geo"}[obj.multigeo]))], True)
  89. except Exception as e:
  90. pass
  91. self.addChild(obj_name, [obj.options['name']])
  92. # calculate physical dimensions
  93. try:
  94. xmin, ymin, xmax, ymax = obj.bounds()
  95. except Exception as e:
  96. log.debug("PropertiesTool.addItems() --> %s" % str(e))
  97. return
  98. length = abs(xmax - xmin)
  99. width = abs(ymax - ymin)
  100. self.addChild(dims, ['Length:', '%.4f %s' % (
  101. length, self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  102. self.addChild(dims, ['Width:', '%.4f %s' % (
  103. width, self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  104. if self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower() == 'mm':
  105. area = (length * width) / 100
  106. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'cm2')], True)
  107. else:
  108. area = length * width
  109. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'in2')], True)
  110. self.addChild(units,
  111. ['FlatCAM units:',
  112. {
  113. 'in': 'Inch',
  114. 'mm': 'Metric'
  115. }
  116. [str(self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower())]], True)
  117. for option in obj.options:
  118. if option is 'name':
  119. continue
  120. self.addChild(options, [str(option), str(obj.options[option])], True)
  121. if obj.kind.lower() == 'gerber':
  122. for ap in obj.apertures:
  123. self.addChild(apertures, [str(ap), str(obj.apertures[ap])], True)
  124. elif obj.kind.lower() == 'excellon':
  125. for tool, value in obj.tools.items():
  126. self.addChild(tools, [str(tool), str(value['C'])], True)
  127. elif obj.kind.lower() == 'geometry':
  128. for tool, value in obj.tools.items():
  129. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  130. for k, v in value.items():
  131. if k == 'solid_geometry':
  132. printed_value = 'Present' if v else 'None'
  133. self.addChild(geo_tool, [str(k), printed_value], True)
  134. elif k == 'data':
  135. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  136. color=QtGui.QColor("#000000"), font=font)
  137. for data_k, data_v in v.items():
  138. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  139. else:
  140. self.addChild(geo_tool, [str(k), str(v)], True)
  141. elif obj.kind.lower() == 'cncjob':
  142. for tool, value in obj.cnc_tools.items():
  143. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  144. for k, v in value.items():
  145. if k == 'solid_geometry':
  146. printed_value = 'Present' if v else 'None'
  147. self.addChild(geo_tool, [str(k), printed_value], True)
  148. elif k == 'gcode':
  149. printed_value = 'Present' if v != '' else 'None'
  150. self.addChild(geo_tool, [str(k), printed_value], True)
  151. elif k == 'gcode_parsed':
  152. printed_value = 'Present' if v else 'None'
  153. self.addChild(geo_tool, [str(k), printed_value], True)
  154. elif k == 'data':
  155. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  156. color=QtGui.QColor("#000000"), font=font)
  157. for data_k, data_v in v.items():
  158. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  159. else:
  160. self.addChild(geo_tool, [str(k), str(v)], True)
  161. self.addChild(separator, [''])
  162. def addParent(self, parent, title, expanded=False, color=None, font=None):
  163. item = QtWidgets.QTreeWidgetItem(parent, [title])
  164. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  165. item.setExpanded(expanded)
  166. if color is not None:
  167. # item.setTextColor(0, color) # PyQt4
  168. item.setForeground(0, QtGui.QBrush(color))
  169. if font is not None:
  170. item.setFont(0, font)
  171. return item
  172. def addChild(self, parent, title, column1=None):
  173. item = QtWidgets.QTreeWidgetItem(parent)
  174. item.setText(0, str(title[0]))
  175. if column1 is not None:
  176. item.setText(1, str(title[1]))
  177. # end of file