ObjectCollection.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #from PyQt4.QtCore import QModelIndex
  2. from FlatCAMObj import *
  3. import inspect # TODO: Remove
  4. import FlatCAMApp
  5. from PyQt4 import Qt, QtGui, QtCore
  6. class KeySensitiveListView(QtGui.QListView):
  7. keyPressed = QtCore.pyqtSignal(int)
  8. def keyPressEvent(self, event):
  9. super(KeySensitiveListView, self).keyPressEvent(event)
  10. self.keyPressed.emit(event.key())
  11. class ObjectCollection(QtCore.QAbstractListModel):
  12. """
  13. Object storage and management.
  14. """
  15. classdict = {
  16. "gerber": FlatCAMGerber,
  17. "excellon": FlatCAMExcellon,
  18. "cncjob": FlatCAMCNCjob,
  19. "geometry": FlatCAMGeometry
  20. }
  21. icon_files = {
  22. "gerber": "share/flatcam_icon16.png",
  23. "excellon": "share/drill16.png",
  24. "cncjob": "share/cnc16.png",
  25. "geometry": "share/geometry16.png"
  26. }
  27. def __init__(self, parent=None):
  28. QtCore.QAbstractListModel.__init__(self, parent=parent)
  29. ### Icons for the list view
  30. self.icons = {}
  31. for kind in ObjectCollection.icon_files:
  32. self.icons[kind] = QtGui.QPixmap(ObjectCollection.icon_files[kind])
  33. ### Data ###
  34. self.object_list = []
  35. self.checked_indexes = []
  36. ### View
  37. #self.view = QtGui.QListView()
  38. self.view = KeySensitiveListView()
  39. self.view.setSelectionMode(Qt.QAbstractItemView.ExtendedSelection)
  40. self.view.setModel(self)
  41. self.click_modifier = None
  42. ## GUI Events
  43. self.view.selectionModel().selectionChanged.connect(self.on_list_selection_change)
  44. self.view.activated.connect(self.on_item_activated)
  45. self.view.keyPressed.connect(self.on_key)
  46. self.view.clicked.connect(self.on_mouse_down)
  47. def on_key(self, key):
  48. # Delete
  49. if key == QtCore.Qt.Key_Delete:
  50. self.delete_active()
  51. def on_mouse_down(self, event):
  52. FlatCAMApp.App.log.debug("Mouse button pressed on list")
  53. def rowCount(self, parent=QtCore.QModelIndex(), *args, **kwargs):
  54. return len(self.object_list)
  55. def columnCount(self, *args, **kwargs):
  56. return 1
  57. def data(self, index, role=Qt.Qt.DisplayRole):
  58. if not index.isValid() or not 0 <= index.row() < self.rowCount():
  59. return QtCore.QVariant()
  60. row = index.row()
  61. if role == Qt.Qt.DisplayRole:
  62. return self.object_list[row].options["name"]
  63. if role == Qt.Qt.DecorationRole:
  64. return self.icons[self.object_list[row].kind]
  65. # if role == Qt.Qt.CheckStateRole:
  66. # if row in self.checked_indexes:
  67. # return Qt.Qt.Checked
  68. # else:
  69. # return Qt.Qt.Unchecked
  70. def print_list(self):
  71. for obj in self.object_list:
  72. print obj
  73. def append(self, obj, active=False):
  74. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.append()")
  75. obj.set_ui(obj.ui_type())
  76. # Required before appending
  77. self.beginInsertRows(QtCore.QModelIndex(), len(self.object_list), len(self.object_list))
  78. # Simply append to the python list
  79. self.object_list.append(obj)
  80. # Required after appending
  81. self.endInsertRows()
  82. def get_names(self):
  83. """
  84. Gets a list of the names of all objects in the collection.
  85. :return: List of names.
  86. :rtype: list
  87. """
  88. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.get_names()")
  89. return [x.options['name'] for x in self.object_list]
  90. def get_bounds(self):
  91. """
  92. Finds coordinates bounding all objects in the collection.
  93. :return: [xmin, ymin, xmax, ymax]
  94. :rtype: list
  95. """
  96. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_bounds()")
  97. # TODO: Move the operation out of here.
  98. xmin = Inf
  99. ymin = Inf
  100. xmax = -Inf
  101. ymax = -Inf
  102. for obj in self.object_list:
  103. try:
  104. gxmin, gymin, gxmax, gymax = obj.bounds()
  105. xmin = min([xmin, gxmin])
  106. ymin = min([ymin, gymin])
  107. xmax = max([xmax, gxmax])
  108. ymax = max([ymax, gymax])
  109. except:
  110. FlatCAMApp.App.log.warning("DEV WARNING: Tried to get bounds of empty geometry.")
  111. return [xmin, ymin, xmax, ymax]
  112. def get_by_name(self, name):
  113. """
  114. Fetches the FlatCAMObj with the given `name`.
  115. :param name: The name of the object.
  116. :type name: str
  117. :return: The requested object or None if no such object.
  118. :rtype: FlatCAMObj or None
  119. """
  120. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_by_name()")
  121. for obj in self.object_list:
  122. if obj.options['name'] == name:
  123. return obj
  124. return None
  125. def delete_active(self):
  126. selections = self.view.selectedIndexes()
  127. if len(selections) == 0:
  128. return
  129. row = selections[0].row()
  130. self.beginRemoveRows(QtCore.QModelIndex(), row, row)
  131. self.object_list.pop(row)
  132. self.endRemoveRows()
  133. def get_active(self):
  134. """
  135. Returns the active object or None
  136. :return: FlatCAMObj or None
  137. """
  138. selections = self.view.selectedIndexes()
  139. if len(selections) == 0:
  140. return None
  141. row = selections[0].row()
  142. return self.object_list[row]
  143. def get_selected(self):
  144. """
  145. Returns list of objects selected in the view.
  146. :return: List of objects
  147. """
  148. return [self.object_list[sel.row()] for sel in self.view.selectedIndexes()]
  149. def set_active(self, name):
  150. """
  151. Selects object by name from the project list. This triggers the
  152. list_selection_changed event and call on_list_selection_changed.
  153. :param name: Name of the FlatCAM Object
  154. :return: None
  155. """
  156. iobj = self.createIndex(self.get_names().index(name), 0) # Column 0
  157. self.view.selectionModel().select(iobj, QtGui.QItemSelectionModel.Select)
  158. def on_list_selection_change(self, current, previous):
  159. FlatCAMApp.App.log.debug("on_list_selection_change()")
  160. FlatCAMApp.App.log.debug("Current: %s, Previous %s" % (str(current), str(previous)))
  161. try:
  162. selection_index = current.indexes()[0].row()
  163. except IndexError:
  164. FlatCAMApp.App.log.debug("on_list_selection_change(): Index Error (Nothing selected?)")
  165. return
  166. self.object_list[selection_index].build_ui()
  167. def on_item_activated(self, index):
  168. """
  169. Double-click or Enter on item.
  170. :param index: Index of the item in the list.
  171. :return: None
  172. """
  173. self.object_list[index.row()].build_ui()
  174. def delete_all(self):
  175. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.delete_all()")
  176. self.beginResetModel()
  177. self.object_list = []
  178. self.checked_indexes = []
  179. self.endResetModel()
  180. def get_list(self):
  181. return self.object_list