ObjectCollection.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. import inspect # TODO: Remove
  9. from gi.repository import Gtk, GdkPixbuf, GLib
  10. from FlatCAMObj import *
  11. from FlatCAM_GTK 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. active = self.get_active()
  80. active.build_ui()
  81. def set_active(self, name):
  82. """
  83. Sets an object as the active object in the program. Same
  84. as `set_list_selection()`.
  85. :param name: Name of the object.
  86. :type name: str
  87. :return: None
  88. """
  89. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.set_active()")
  90. self.set_list_selection(name)
  91. def get_active(self):
  92. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_active()")
  93. try:
  94. model, treeiter = self.tree_selection.get_selected()
  95. return model[treeiter][0]
  96. except (TypeError, ValueError):
  97. return None
  98. def set_list_selection(self, name):
  99. """
  100. Sets which object should be selected in the list.
  101. :param name: Name of the object.
  102. :rtype name: str
  103. :return: None
  104. """
  105. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.set_list_selection()")
  106. iterat = self.store.get_iter_first()
  107. while iterat is not None and self.store[iterat][0].options["name"] != name:
  108. iterat = self.store.iter_next(iterat)
  109. self.tree_selection.select_iter(iterat)
  110. def append(self, obj, active=False):
  111. """
  112. Add a FlatCAMObj the the collection. This method is thread-safe.
  113. :param obj: FlatCAMObj to append
  114. :type obj: FlatCAMObj
  115. :param active: If it is to become the active object after appending
  116. :type active: bool
  117. :return: None
  118. """
  119. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.append()")
  120. def guitask():
  121. self.store.append([obj])
  122. if active:
  123. self.set_list_selection(obj.options["name"])
  124. GLib.idle_add(guitask)
  125. def get_names(self):
  126. """
  127. Gets a list of the names of all objects in the collection.
  128. :return: List of names.
  129. :rtype: list
  130. """
  131. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_names()")
  132. names = []
  133. iterat = self.store.get_iter_first()
  134. while iterat is not None:
  135. obj = self.store[iterat][0]
  136. names.append(obj.options["name"])
  137. iterat = self.store.iter_next(iterat)
  138. return names
  139. def get_bounds(self):
  140. """
  141. Finds coordinates bounding all objects in the collection.
  142. :return: [xmin, ymin, xmax, ymax]
  143. :rtype: list
  144. """
  145. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_bounds()")
  146. # TODO: Move the operation out of here.
  147. xmin = Inf
  148. ymin = Inf
  149. xmax = -Inf
  150. ymax = -Inf
  151. iterat = self.store.get_iter_first()
  152. while iterat is not None:
  153. obj = self.store[iterat][0]
  154. try:
  155. gxmin, gymin, gxmax, gymax = obj.bounds()
  156. xmin = min([xmin, gxmin])
  157. ymin = min([ymin, gymin])
  158. xmax = max([xmax, gxmax])
  159. ymax = max([ymax, gymax])
  160. except:
  161. FlatCAMApp.App.log.warning("DEV WARNING: Tried to get bounds of empty geometry.")
  162. iterat = self.store.iter_next(iterat)
  163. return [xmin, ymin, xmax, ymax]
  164. def get_list(self):
  165. """
  166. Returns a list with all FlatCAMObj.
  167. :return: List with all FlatCAMObj.
  168. :rtype: list
  169. """
  170. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_list()")
  171. collection_list = []
  172. iterat = self.store.get_iter_first()
  173. while iterat is not None:
  174. obj = self.store[iterat][0]
  175. collection_list.append(obj)
  176. iterat = self.store.iter_next(iterat)
  177. return collection_list
  178. def get_by_name(self, name):
  179. """
  180. Fetches the FlatCAMObj with the given `name`.
  181. :param name: The name of the object.
  182. :type name: str
  183. :return: The requested object or None if no such object.
  184. :rtype: FlatCAMObj or None
  185. """
  186. FlatCAMApp.App.log.debug(str(inspect.stack()[1][3]) + "--> OC.get_by_name()")
  187. iterat = self.store.get_iter_first()
  188. while iterat is not None:
  189. obj = self.store[iterat][0]
  190. if obj.options["name"] == name:
  191. return obj
  192. iterat = self.store.iter_next(iterat)
  193. return None
  194. # def change_name(self, old_name, new_name):
  195. # """
  196. # Changes the name of `FlatCAMObj` named `old_name` to `new_name`.
  197. #
  198. # :param old_name: Name of the object to change.
  199. # :type old_name: str
  200. # :param new_name: New name.
  201. # :type new_name: str
  202. # :return: True if name change succeeded, False otherwise. Will fail
  203. # if no object with `old_name` is found.
  204. # :rtype: bool
  205. # """
  206. # print inspect.stack()[1][3], "--> OC.change_name()"
  207. # iterat = self.store.get_iter_first()
  208. # while iterat is not None:
  209. # obj = self.store[iterat][0]
  210. # if obj.options["name"] == old_name:
  211. # obj.options["name"] = new_name
  212. # self.store.row_changed(0, iterat)
  213. # return True
  214. # iterat = self.store.iter_next(iterat)
  215. # return False