ToolProperties.py 9.2 KB

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