ToolProperties.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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("<font size=4><b>&nbsp;%s</b></font>" % self.toolName)
  19. self.properties_box.addWidget(title_label)
  20. # self.layout.setMargin(0) # PyQt4
  21. self.properties_box.setContentsMargins(0, 0, 0, 0) # PyQt5
  22. self.vlay = QtWidgets.QVBoxLayout()
  23. self.properties_box.addLayout(self.vlay)
  24. self.treeWidget = QtWidgets.QTreeWidget()
  25. self.treeWidget.setColumnCount(2)
  26. self.treeWidget.setHeaderHidden(True)
  27. self.treeWidget.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  28. self.treeWidget.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Expanding)
  29. self.vlay.addWidget(self.treeWidget)
  30. self.vlay.setStretch(0,0)
  31. def run(self):
  32. self.app.report_usage("ToolProperties()")
  33. if self.app.tool_tab_locked is True:
  34. return
  35. self.set_tool_ui()
  36. # if the splitter us hidden, display it
  37. if self.app.ui.splitter.sizes()[0] == 0:
  38. self.app.ui.splitter.setSizes([1, 1])
  39. FlatCAMTool.run(self)
  40. self.properties()
  41. def install(self, icon=None, separator=None, **kwargs):
  42. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  43. def set_tool_ui(self):
  44. # this reset the TreeWidget
  45. self.treeWidget.clear()
  46. self.properties_frame.show()
  47. def properties(self):
  48. obj_list = self.app.collection.get_selected()
  49. if not obj_list:
  50. self.app.inform.emit("[ERROR_NOTCL] Properties Tool was not displayed. No object selected.")
  51. self.app.ui.notebook.setTabText(2, "Tools")
  52. self.properties_frame.hide()
  53. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  54. return
  55. for obj in obj_list:
  56. self.addItems(obj)
  57. self.app.inform.emit("[success] Object Properties are displayed.")
  58. self.app.ui.notebook.setTabText(2, "Properties Tool")
  59. def addItems(self, obj):
  60. parent = self.treeWidget.invisibleRootItem()
  61. font = QtGui.QFont()
  62. font.setBold(True)
  63. obj_type = self.addParent(parent, 'TYPE', expanded=True, color=QtGui.QColor("#000000"), font=font)
  64. obj_name = self.addParent(parent, 'NAME', expanded=True, color=QtGui.QColor("#000000"), font=font)
  65. dims = self.addParent(parent, 'Dimensions', expanded=True, color=QtGui.QColor("#000000"), font=font)
  66. options = self.addParent(parent, 'Options', color=QtGui.QColor("#000000"), font=font)
  67. separator = self.addParent(parent, '')
  68. self.addChild(obj_type, [obj.kind.upper()])
  69. self.addChild(obj_name, [obj.options['name']])
  70. # calculate physical dimensions
  71. xmin, ymin, xmax, ymax = obj.bounds()
  72. length = abs(xmax - xmin)
  73. width = abs(ymax - ymin)
  74. self.addChild(dims, ['Length:', '%.4f %s' % (
  75. length, self.app.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  76. self.addChild(dims, ['Width:', '%.4f %s' % (
  77. width, self.app.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  78. if self.app.general_options_form.general_app_group.units_radio.get_value().lower() == 'mm':
  79. area = (length * width) / 100
  80. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'cm2')], True)
  81. else:
  82. area = length * width
  83. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'in2')], True)
  84. for option in obj.options:
  85. if option is 'name':
  86. continue
  87. self.addChild(options, [str(option), str(obj.options[option])], True)
  88. self.addChild(separator, [''])
  89. def addParent(self, parent, title, expanded=False, color=None, font=None):
  90. item = QtWidgets.QTreeWidgetItem(parent, [title])
  91. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  92. item.setExpanded(expanded)
  93. if color is not None:
  94. # item.setTextColor(0, color) # PyQt4
  95. item.setForeground(0, QtGui.QBrush(color))
  96. if font is not None:
  97. item.setFont(0, font)
  98. return item
  99. def addChild(self, parent, title, column1=None):
  100. item = QtWidgets.QTreeWidgetItem(parent)
  101. item.setText(0, str(title[0]))
  102. if column1 is not None:
  103. item.setText(1, str(title[1]))
  104. # end of file