ToolProperties.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, toggle=False):
  39. self.app.report_usage("ToolProperties()")
  40. if self.app.tool_tab_locked is True:
  41. return
  42. self.set_tool_ui()
  43. if toggle:
  44. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  45. if self.app.ui.splitter.sizes()[0] == 0:
  46. self.app.ui.splitter.setSizes([1, 1])
  47. else:
  48. try:
  49. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  50. self.app.ui.splitter.setSizes([0, 1])
  51. except AttributeError:
  52. pass
  53. FlatCAMTool.run(self)
  54. self.properties()
  55. def install(self, icon=None, separator=None, **kwargs):
  56. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  57. def set_tool_ui(self):
  58. # this reset the TreeWidget
  59. self.treeWidget.clear()
  60. self.properties_frame.show()
  61. def properties(self):
  62. obj_list = self.app.collection.get_selected()
  63. if not obj_list:
  64. self.app.inform.emit(_("[ERROR_NOTCL] Properties Tool was not displayed. No object selected."))
  65. self.app.ui.notebook.setTabText(2, _("Tools"))
  66. self.properties_frame.hide()
  67. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  68. return
  69. for obj in obj_list:
  70. self.addItems(obj)
  71. self.app.inform.emit(_("[success] Object Properties are displayed."))
  72. self.app.ui.notebook.setTabText(2, _("Properties Tool"))
  73. def addItems(self, obj):
  74. parent = self.treeWidget.invisibleRootItem()
  75. font = QtGui.QFont()
  76. font.setBold(True)
  77. obj_type = self.addParent(parent, 'TYPE', expanded=True, color=QtGui.QColor("#000000"), font=font)
  78. obj_name = self.addParent(parent, 'NAME', expanded=True, color=QtGui.QColor("#000000"), font=font)
  79. dims = self.addParent(parent, 'Dimensions', expanded=True, color=QtGui.QColor("#000000"), font=font)
  80. units = self.addParent(parent, 'Units', expanded=True, color=QtGui.QColor("#000000"), font=font)
  81. options = self.addParent(parent, 'Options', color=QtGui.QColor("#000000"), font=font)
  82. if obj.kind.lower() == 'gerber':
  83. apertures = self.addParent(parent, 'Apertures', expanded=True, color=QtGui.QColor("#000000"), font=font)
  84. else:
  85. tools = self.addParent(parent, 'Tools', expanded=True, color=QtGui.QColor("#000000"), font=font)
  86. separator = self.addParent(parent, '')
  87. self.addChild(obj_type, ['Object Type:', ('%s' % (obj.kind.capitalize()))], True)
  88. try:
  89. self.addChild(obj_type, ['Geo Type:', ('%s' % ({False: "Single-Geo", True: "Multi-Geo"}[obj.multigeo]))], True)
  90. except Exception as e:
  91. pass
  92. self.addChild(obj_name, [obj.options['name']])
  93. # calculate physical dimensions
  94. try:
  95. xmin, ymin, xmax, ymax = obj.bounds()
  96. except Exception as e:
  97. log.debug("PropertiesTool.addItems() --> %s" % str(e))
  98. return
  99. length = abs(xmax - xmin)
  100. width = abs(ymax - ymin)
  101. self.addChild(dims, ['Length:', '%.4f %s' % (
  102. length, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  103. self.addChild(dims, ['Width:', '%.4f %s' % (
  104. width, self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())], True)
  105. if self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower() == 'mm':
  106. area = (length * width) / 100
  107. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'cm2')], True)
  108. else:
  109. area = length * width
  110. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'in2')], True)
  111. self.addChild(units,
  112. ['FlatCAM units:',
  113. {
  114. 'in': 'Inch',
  115. 'mm': 'Metric'
  116. }
  117. [str(self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().lower())]], True)
  118. for option in obj.options:
  119. if option is 'name':
  120. continue
  121. self.addChild(options, [str(option), str(obj.options[option])], True)
  122. if obj.kind.lower() == 'gerber':
  123. for ap in obj.apertures:
  124. self.addChild(apertures, [str(ap), str(obj.apertures[ap])], True)
  125. elif obj.kind.lower() == 'excellon':
  126. for tool, value in obj.tools.items():
  127. self.addChild(tools, [str(tool), str(value['C'])], True)
  128. elif obj.kind.lower() == 'geometry':
  129. for tool, value in obj.tools.items():
  130. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  131. for k, v in value.items():
  132. if k == 'solid_geometry':
  133. printed_value = 'Present' if v else 'None'
  134. self.addChild(geo_tool, [str(k), printed_value], True)
  135. elif k == 'data':
  136. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  137. color=QtGui.QColor("#000000"), font=font)
  138. for data_k, data_v in v.items():
  139. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  140. else:
  141. self.addChild(geo_tool, [str(k), str(v)], True)
  142. elif obj.kind.lower() == 'cncjob':
  143. for tool, value in obj.cnc_tools.items():
  144. geo_tool = self.addParent(tools, str(tool), expanded=True, color=QtGui.QColor("#000000"), font=font)
  145. for k, v in value.items():
  146. if k == 'solid_geometry':
  147. printed_value = 'Present' if v else 'None'
  148. self.addChild(geo_tool, [str(k), printed_value], True)
  149. elif k == 'gcode':
  150. printed_value = 'Present' if v != '' else 'None'
  151. self.addChild(geo_tool, [str(k), printed_value], True)
  152. elif k == 'gcode_parsed':
  153. printed_value = 'Present' if v else 'None'
  154. self.addChild(geo_tool, [str(k), printed_value], True)
  155. elif k == 'data':
  156. tool_data = self.addParent(geo_tool, str(k).capitalize(),
  157. color=QtGui.QColor("#000000"), font=font)
  158. for data_k, data_v in v.items():
  159. self.addChild(tool_data, [str(data_k), str(data_v)], True)
  160. else:
  161. self.addChild(geo_tool, [str(k), str(v)], True)
  162. self.addChild(separator, [''])
  163. def addParent(self, parent, title, expanded=False, color=None, font=None):
  164. item = QtWidgets.QTreeWidgetItem(parent, [title])
  165. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  166. item.setExpanded(expanded)
  167. if color is not None:
  168. # item.setTextColor(0, color) # PyQt4
  169. item.setForeground(0, QtGui.QBrush(color))
  170. if font is not None:
  171. item.setFont(0, font)
  172. return item
  173. def addChild(self, parent, title, column1=None):
  174. item = QtWidgets.QTreeWidgetItem(parent)
  175. item.setText(0, str(title[0]))
  176. if column1 is not None:
  177. item.setText(1, str(title[1]))
  178. # end of file