ObjectCollection.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. def on_key(self, key):
  47. if key == QtCore.Qt.Key_Delete:
  48. self.delete_active()
  49. def on_mouse_down(self, event):
  50. print "Mouse button pressed on list"
  51. def rowCount(self, parent=QtCore.QModelIndex(), *args, **kwargs):
  52. return len(self.object_list)
  53. def columnCount(self, *args, **kwargs):
  54. return 1
  55. def data(self, index, role=Qt.Qt.DisplayRole):
  56. if not index.isValid() or not 0 <= index.row() < self.rowCount():
  57. return QtCore.QVariant()
  58. row = index.row()
  59. if role == Qt.Qt.DisplayRole:
  60. return self.object_list[row].options["name"]
  61. if role == Qt.Qt.DecorationRole:
  62. return self.icons[self.object_list[row].kind]
  63. # if role == Qt.Qt.CheckStateRole:
  64. # if row in self.checked_indexes:
  65. # return Qt.Qt.Checked
  66. # else:
  67. # return Qt.Qt.Unchecked
  68. def print_list(self):
  69. for obj in self.object_list:
  70. print obj
  71. def append(self, obj, active=False):
  72. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.append()")
  73. obj.set_ui(obj.ui_type())
  74. # Required before appending
  75. self.beginInsertRows(QtCore.QModelIndex(), len(self.object_list), len(self.object_list))
  76. # Simply append to the python list
  77. self.object_list.append(obj)
  78. # Required after appending
  79. self.endInsertRows()
  80. def get_names(self):
  81. """
  82. Gets a list of the names of all objects in the collection.
  83. :return: List of names.
  84. :rtype: list
  85. """
  86. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.get_names()")
  87. return [x.options['name'] for x in self.object_list]
  88. def get_bounds(self):
  89. """
  90. Finds coordinates bounding all objects in the collection.
  91. :return: [xmin, ymin, xmax, ymax]
  92. :rtype: list
  93. """
  94. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_bounds()")
  95. # TODO: Move the operation out of here.
  96. xmin = Inf
  97. ymin = Inf
  98. xmax = -Inf
  99. ymax = -Inf
  100. for obj in self.object_list:
  101. try:
  102. gxmin, gymin, gxmax, gymax = obj.bounds()
  103. xmin = min([xmin, gxmin])
  104. ymin = min([ymin, gymin])
  105. xmax = max([xmax, gxmax])
  106. ymax = max([ymax, gymax])
  107. except:
  108. FlatCAMApp.App.log.warning("DEV WARNING: Tried to get bounds of empty geometry.")
  109. return [xmin, ymin, xmax, ymax]
  110. def get_by_name(self, name):
  111. """
  112. Fetches the FlatCAMObj with the given `name`.
  113. :param name: The name of the object.
  114. :type name: str
  115. :return: The requested object or None if no such object.
  116. :rtype: FlatCAMObj or None
  117. """
  118. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_by_name()")
  119. for obj in self.object_list:
  120. if obj.options['name'] == name:
  121. return obj
  122. return None
  123. def delete_active(self):
  124. selections = self.view.selectedIndexes()
  125. if len(selections) == 0:
  126. return
  127. row = selections[0].row()
  128. self.beginRemoveRows(QtCore.QModelIndex(), row, row)
  129. self.object_list.pop(row)
  130. self.endRemoveRows()
  131. def get_active(self):
  132. """
  133. Returns the active object or None
  134. :return: FlatCAMObj or None
  135. """
  136. selections = self.view.selectedIndexes()
  137. if len(selections) == 0:
  138. return None
  139. row = selections[0].row()
  140. return self.object_list[row]
  141. def get_selected(self):
  142. """
  143. Returns list of objects selected in the view.
  144. :return: List of objects
  145. """
  146. return [self.object_list[sel.row()] for sel in self.view.selectedIndexes()]
  147. def set_active(self, name):
  148. """
  149. Selects object by name from the project list. This triggers the
  150. list_selection_changed event and call on_list_selection_changed.
  151. :param name: Name of the FlatCAM Object
  152. :return: None
  153. """
  154. iobj = self.createIndex(self.get_names().index(name), 0) # Column 0
  155. self.view.selectionModel().select(iobj, QtGui.QItemSelectionModel.Select)
  156. def on_list_selection_change(self, current, previous):
  157. FlatCAMApp.App.log.debug("on_list_selection_change()")
  158. FlatCAMApp.App.log.debug("Current: %s, Previous %s" % (str(current), str(previous)))
  159. try:
  160. selection_index = current.indexes()[0].row()
  161. except IndexError:
  162. FlatCAMApp.App.log.debug("on_list_selection_change(): Index Error (Nothing selected?)")
  163. return
  164. self.object_list[selection_index].build_ui()
  165. def on_item_activated(self, index):
  166. """
  167. Double-click or Enter on item.
  168. :param index: Index of the item in the list.
  169. :return: None
  170. """
  171. self.object_list[index.row()].build_ui()
  172. def delete_all(self):
  173. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.delete_all()")
  174. self.beginResetModel()
  175. self.object_list = []
  176. self.checked_indexes = []
  177. self.endResetModel()
  178. def get_list(self):
  179. return self.object_list