ObjectCollection.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://caram.cl/software/flatcam #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 4/20/2014 #
  6. # MIT Licence #
  7. ############################################################
  8. from FlatCAMObj import *
  9. from gi.repository import Gtk, GdkPixbuf
  10. import inspect # TODO: Remove
  11. import FlatCAMApp
  12. class ObjectCollection:
  13. classdict = {
  14. "gerber": FlatCAMGerber,
  15. "excellon": FlatCAMExcellon,
  16. "cncjob": FlatCAMCNCjob,
  17. "geometry": FlatCAMGeometry
  18. }
  19. icon_files = {
  20. "gerber": "share/flatcam_icon16.png",
  21. "excellon": "share/drill16.png",
  22. "cncjob": "share/cnc16.png",
  23. "geometry": "share/geometry16.png"
  24. }
  25. def __init__(self):
  26. ### Icons for the list view
  27. self.icons = {}
  28. for kind in ObjectCollection.icon_files:
  29. self.icons[kind] = GdkPixbuf.Pixbuf.new_from_file(ObjectCollection.icon_files[kind])
  30. ### GUI List components
  31. ## Model
  32. self.store = Gtk.ListStore(FlatCAMObj)
  33. ## View
  34. self.view = Gtk.TreeView(model=self.store)
  35. #self.view.connect("row_activated", self.on_row_activated)
  36. self.tree_selection = self.view.get_selection()
  37. self.change_subscription = self.tree_selection.connect("changed", self.on_list_selection_change)
  38. ## Renderers
  39. # Icon
  40. renderer_pixbuf = Gtk.CellRendererPixbuf()
  41. column_pixbuf = Gtk.TreeViewColumn("Type", renderer_pixbuf)
  42. def _set_cell_icon(column, cell, model, it, data):
  43. obj = model.get_value(it, 0)
  44. cell.set_property('pixbuf', self.icons[obj.kind])
  45. column_pixbuf.set_cell_data_func(renderer_pixbuf, _set_cell_icon)
  46. self.view.append_column(column_pixbuf)
  47. # Name
  48. renderer_text = Gtk.CellRendererText()
  49. column_text = Gtk.TreeViewColumn("Name", renderer_text)
  50. def _set_cell_text(column, cell, model, it, data):
  51. obj = model.get_value(it, 0)
  52. cell.set_property('text', obj.options["name"])
  53. column_text.set_cell_data_func(renderer_text, _set_cell_text)
  54. self.view.append_column(column_text)
  55. def print_list(self):
  56. iterat = self.store.get_iter_first()
  57. while iterat is not None:
  58. obj = self.store[iterat][0]
  59. print obj
  60. iterat = self.store.iter_next(iterat)
  61. def delete_all(self):
  62. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.delete_all()")
  63. self.store.clear()
  64. def delete_active(self):
  65. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.delete_active()")
  66. try:
  67. model, treeiter = self.tree_selection.get_selected()
  68. self.store.remove(treeiter)
  69. except:
  70. pass
  71. def on_list_selection_change(self, selection):
  72. """
  73. Callback for change in selection on the objects' list.
  74. Instructs the new selection to build the UI for its options.
  75. :param selection: Ignored.
  76. :return: None
  77. """
  78. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.on_list_selection_change()")
  79. try:
  80. self.get_active().build_ui()
  81. except AttributeError: # For None being active
  82. pass
  83. def set_active(self, name):
  84. """
  85. Sets an object as the active object in the program. Same
  86. as `set_list_selection()`.
  87. :param name: Name of the object.
  88. :type name: str
  89. :return: None
  90. """
  91. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.set_active()")
  92. self.set_list_selection(name)
  93. def get_active(self):
  94. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_active()")
  95. try:
  96. model, treeiter = self.tree_selection.get_selected()
  97. return model[treeiter][0]
  98. except (TypeError, ValueError):
  99. return None
  100. def set_list_selection(self, name):
  101. """
  102. Sets which object should be selected in the list.
  103. :param name: Name of the object.
  104. :rtype name: str
  105. :return: None
  106. """
  107. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.set_list_selection()")
  108. iterat = self.store.get_iter_first()
  109. while iterat is not None and self.store[iterat][0].options["name"] != name:
  110. iterat = self.store.iter_next(iterat)
  111. self.tree_selection.select_iter(iterat)
  112. def append(self, obj, active=False):
  113. """
  114. Add a FlatCAMObj the the collection. This method is thread-safe.
  115. :param obj: FlatCAMObj to append
  116. :type obj: FlatCAMObj
  117. :param active: If it is to become the active object after appending
  118. :type active: bool
  119. :return: None
  120. """
  121. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.append()")
  122. def guitask():
  123. self.store.append([obj])
  124. if active:
  125. self.set_list_selection(obj.options["name"])
  126. GLib.idle_add(guitask)
  127. def get_names(self):
  128. """
  129. Gets a list of the names of all objects in the collection.
  130. :return: List of names.
  131. :rtype: list
  132. """
  133. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_names()")
  134. names = []
  135. iterat = self.store.get_iter_first()
  136. while iterat is not None:
  137. obj = self.store[iterat][0]
  138. names.append(obj.options["name"])
  139. iterat = self.store.iter_next(iterat)
  140. return names
  141. def get_bounds(self):
  142. """
  143. Finds coordinates bounding all objects in the collection.
  144. :return: [xmin, ymin, xmax, ymax]
  145. :rtype: list
  146. """
  147. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_bounds()")
  148. # TODO: Move the operation out of here.
  149. xmin = Inf
  150. ymin = Inf
  151. xmax = -Inf
  152. ymax = -Inf
  153. iterat = self.store.get_iter_first()
  154. while iterat is not None:
  155. obj = self.store[iterat][0]
  156. try:
  157. gxmin, gymin, gxmax, gymax = obj.bounds()
  158. xmin = min([xmin, gxmin])
  159. ymin = min([ymin, gymin])
  160. xmax = max([xmax, gxmax])
  161. ymax = max([ymax, gymax])
  162. except:
  163. FlatCAMApp.App.log.waring("DEV WARNING: Tried to get bounds of empty geometry.")
  164. iterat = self.store.iter_next(iterat)
  165. return [xmin, ymin, xmax, ymax]
  166. def get_list(self):
  167. """
  168. Returns a list with all FlatCAMObj.
  169. :return: List with all FlatCAMObj.
  170. :rtype: list
  171. """
  172. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_list()")
  173. collection_list = []
  174. iterat = self.store.get_iter_first()
  175. while iterat is not None:
  176. obj = self.store[iterat][0]
  177. collection_list.append(obj)
  178. iterat = self.store.iter_next(iterat)
  179. return collection_list
  180. def get_by_name(self, name):
  181. """
  182. Fetches the FlatCAMObj with the given `name`.
  183. :param name: The name of the object.
  184. :type name: str
  185. :return: The requested object or None if no such object.
  186. :rtype: FlatCAMObj or None
  187. """
  188. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_by_name()")
  189. iterat = self.store.get_iter_first()
  190. while iterat is not None:
  191. obj = self.store[iterat][0]
  192. if obj.options["name"] == name:
  193. return obj
  194. iterat = self.store.iter_next(iterat)
  195. return None
  196. # def change_name(self, old_name, new_name):
  197. # """
  198. # Changes the name of `FlatCAMObj` named `old_name` to `new_name`.
  199. #
  200. # :param old_name: Name of the object to change.
  201. # :type old_name: str
  202. # :param new_name: New name.
  203. # :type new_name: str
  204. # :return: True if name change succeeded, False otherwise. Will fail
  205. # if no object with `old_name` is found.
  206. # :rtype: bool
  207. # """
  208. # print inspect.stack()[1][3], "--> OC.change_name()"
  209. # iterat = self.store.get_iter_first()
  210. # while iterat is not None:
  211. # obj = self.store[iterat][0]
  212. # if obj.options["name"] == old_name:
  213. # obj.options["name"] = new_name
  214. # self.store.row_changed(0, iterat)
  215. # return True
  216. # iterat = self.store.iter_next(iterat)
  217. # return False