ObjectCollection.py 6.4 KB

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