ObjectCollection.py 8.0 KB

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