ObjectCollection.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. self.object_list = []
  26. self.view = QtGui.QListView()
  27. self.view.setModel(self)
  28. self.view.selectionModel().selectionChanged.connect(self.on_list_selection_change)
  29. self.view.activated.connect(self.on_item_activated)
  30. def rowCount(self, parent=QtCore.QModelIndex(), *args, **kwargs):
  31. return len(self.object_list)
  32. def columnCount(self, *args, **kwargs):
  33. return 1
  34. def data(self, index, role=Qt.Qt.DisplayRole):
  35. if not index.isValid() or not 0 <= index.row() < self.rowCount():
  36. return QtCore.QVariant()
  37. row = index.row()
  38. if role == Qt.Qt.DisplayRole:
  39. return self.object_list[row].options["name"]
  40. if role == Qt.Qt.DecorationRole:
  41. return self.icons[self.object_list[row].kind]
  42. def print_list(self):
  43. for obj in self.object_list:
  44. print obj
  45. def append(self, obj, active=False):
  46. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.append()")
  47. obj.set_ui(obj.ui_type())
  48. # Required before appending
  49. self.beginInsertRows(QtCore.QModelIndex(), len(self.object_list), len(self.object_list))
  50. self.object_list.append(obj)
  51. # Required after appending
  52. self.endInsertRows()
  53. def get_names(self):
  54. """
  55. Gets a list of the names of all objects in the collection.
  56. :return: List of names.
  57. :rtype: list
  58. """
  59. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + " --> OC.get_names()")
  60. return [x.options['name'] for x in self.object_list]
  61. def get_bounds(self):
  62. """
  63. Finds coordinates bounding all objects in the collection.
  64. :return: [xmin, ymin, xmax, ymax]
  65. :rtype: list
  66. """
  67. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_bounds()")
  68. # TODO: Move the operation out of here.
  69. xmin = Inf
  70. ymin = Inf
  71. xmax = -Inf
  72. ymax = -Inf
  73. for obj in self.object_list:
  74. try:
  75. gxmin, gymin, gxmax, gymax = obj.bounds()
  76. xmin = min([xmin, gxmin])
  77. ymin = min([ymin, gymin])
  78. xmax = max([xmax, gxmax])
  79. ymax = max([ymax, gymax])
  80. except:
  81. FlatCAMApp.App.log.warning("DEV WARNING: Tried to get bounds of empty geometry.")
  82. return [xmin, ymin, xmax, ymax]
  83. def get_by_name(self, name):
  84. """
  85. Fetches the FlatCAMObj with the given `name`.
  86. :param name: The name of the object.
  87. :type name: str
  88. :return: The requested object or None if no such object.
  89. :rtype: FlatCAMObj or None
  90. """
  91. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_by_name()")
  92. for obj in self.object_list:
  93. if obj.options['name'] == name:
  94. return obj
  95. return None
  96. def delete_active(self):
  97. selections = self.view.selectedIndexes()
  98. if len(selections) == 0:
  99. return
  100. row = selections[0].row()
  101. self.beginRemoveRows(QtCore.QModelIndex(), row, row)
  102. self.object_list.pop(row)
  103. self.endRemoveRows()
  104. def get_active(self):
  105. selections = self.view.selectedIndexes()
  106. if len(selections) == 0:
  107. return None
  108. row = selections[0].row()
  109. return self.object_list[row]
  110. def set_active(self, name):
  111. iobj = self.createIndex(self.get_names().index(name))
  112. self.view.selectionModel().select(iobj, QtGui.QItemSelectionModel)
  113. def on_list_selection_change(self, current, previous):
  114. FlatCAMApp.App.log.debug("on_list_selection_change()")
  115. FlatCAMApp.App.log.debug("Current: %s, Previous %s" % (str(current), str(previous)))
  116. try:
  117. selection_index = current.indexes()[0].row()
  118. except IndexError:
  119. FlatCAMApp.App.log.debug("on_list_selection_change(): Index Error (Nothing selected?)")
  120. return
  121. self.object_list[selection_index].build_ui()
  122. def on_item_activated(self, index):
  123. self.object_list[index.row()].build_ui()
  124. def delete_all(self):
  125. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.delete_all()")
  126. self.beginResetModel()
  127. self.object_list = []
  128. self.endResetModel()
  129. def get_list(self):
  130. return self.object_list