ObjectCollection.py 6.5 KB

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