ToolProperties.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 us hidden, display it
  44. if self.app.ui.splitter.sizes()[0] == 0:
  45. self.app.ui.splitter.setSizes([1, 1])
  46. FlatCAMTool.run(self)
  47. self.properties()
  48. def install(self, icon=None, separator=None, **kwargs):
  49. FlatCAMTool.install(self, icon, separator, shortcut='P', **kwargs)
  50. def set_tool_ui(self):
  51. # this reset the TreeWidget
  52. self.treeWidget.clear()
  53. self.properties_frame.show()
  54. def properties(self):
  55. obj_list = self.app.collection.get_selected()
  56. if not obj_list:
  57. self.app.inform.emit("[ERROR_NOTCL] Properties Tool was not displayed. No object selected.")
  58. self.app.ui.notebook.setTabText(2, "Tools")
  59. self.properties_frame.hide()
  60. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  61. return
  62. for obj in obj_list:
  63. self.addItems(obj)
  64. self.app.inform.emit("[success] Object Properties are displayed.")
  65. self.app.ui.notebook.setTabText(2, "Properties Tool")
  66. def addItems(self, obj):
  67. parent = self.treeWidget.invisibleRootItem()
  68. font = QtGui.QFont()
  69. font.setBold(True)
  70. obj_type = self.addParent(parent, 'TYPE', expanded=True, color=QtGui.QColor("#000000"), font=font)
  71. obj_name = self.addParent(parent, 'NAME', expanded=True, color=QtGui.QColor("#000000"), font=font)
  72. dims = self.addParent(parent, 'Dimensions', expanded=True, color=QtGui.QColor("#000000"), font=font)
  73. options = self.addParent(parent, 'Options', color=QtGui.QColor("#000000"), font=font)
  74. separator = self.addParent(parent, '')
  75. self.addChild(obj_type, [obj.kind.upper()])
  76. self.addChild(obj_name, [obj.options['name']])
  77. # calculate physical dimensions
  78. xmin, ymin, xmax, ymax = obj.bounds()
  79. length = abs(xmax - xmin)
  80. width = abs(ymax - ymin)
  81. self.addChild(dims, ['Length:', '%.4f %s' % (
  82. length, self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  83. self.addChild(dims, ['Width:', '%.4f %s' % (
  84. width, self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower())], True)
  85. if self.app.ui.general_options_form.general_app_group.units_radio.get_value().lower() == 'mm':
  86. area = (length * width) / 100
  87. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'cm2')], True)
  88. else:
  89. area = length * width
  90. self.addChild(dims, ['Box Area:', '%.4f %s' % (area, 'in2')], True)
  91. for option in obj.options:
  92. if option is 'name':
  93. continue
  94. self.addChild(options, [str(option), str(obj.options[option])], True)
  95. self.addChild(separator, [''])
  96. def addParent(self, parent, title, expanded=False, color=None, font=None):
  97. item = QtWidgets.QTreeWidgetItem(parent, [title])
  98. item.setChildIndicatorPolicy(QtWidgets.QTreeWidgetItem.ShowIndicator)
  99. item.setExpanded(expanded)
  100. if color is not None:
  101. # item.setTextColor(0, color) # PyQt4
  102. item.setForeground(0, QtGui.QBrush(color))
  103. if font is not None:
  104. item.setFont(0, font)
  105. return item
  106. def addChild(self, parent, title, column1=None):
  107. item = QtWidgets.QTreeWidgetItem(parent)
  108. item.setText(0, str(title[0]))
  109. if column1 is not None:
  110. item.setText(1, str(title[1]))
  111. # end of file