layerselectiondialog.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #
  2. # BitBake Graphical GTK User Interface
  3. #
  4. # Copyright (C) 2011-2012 Intel Corporation
  5. #
  6. # Authored by Joshua Lock <josh@linux.intel.com>
  7. # Authored by Dongxiao Xu <dongxiao.xu@intel.com>
  8. # Authored by Shane Wang <shane.wang@intel.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. import gtk
  23. import gobject
  24. import os
  25. import tempfile
  26. from bb.ui.crumbs.hobwidget import hic, HobButton, HobAltButton
  27. from bb.ui.crumbs.hig.crumbsdialog import CrumbsDialog
  28. from bb.ui.crumbs.hig.crumbsmessagedialog import CrumbsMessageDialog
  29. """
  30. The following are convenience classes for implementing GNOME HIG compliant
  31. BitBake GUI's
  32. In summary: spacing = 12px, border-width = 6px
  33. """
  34. class CellRendererPixbufActivatable(gtk.CellRendererPixbuf):
  35. """
  36. A custom CellRenderer implementation which is activatable
  37. so that we can handle user clicks
  38. """
  39. __gsignals__ = { 'clicked' : (gobject.SIGNAL_RUN_LAST,
  40. gobject.TYPE_NONE,
  41. (gobject.TYPE_STRING,)), }
  42. def __init__(self):
  43. gtk.CellRendererPixbuf.__init__(self)
  44. self.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)
  45. self.set_property('follow-state', True)
  46. """
  47. Respond to a user click on a cell
  48. """
  49. def do_activate(self, even, widget, path, background_area, cell_area, flags):
  50. self.emit('clicked', path)
  51. #
  52. # LayerSelectionDialog
  53. #
  54. class LayerSelectionDialog (CrumbsDialog):
  55. TARGETS = [
  56. ("MY_TREE_MODEL_ROW", gtk.TARGET_SAME_WIDGET, 0),
  57. ("text/plain", 0, 1),
  58. ("TEXT", 0, 2),
  59. ("STRING", 0, 3),
  60. ]
  61. def gen_label_widget(self, content):
  62. label = gtk.Label()
  63. label.set_alignment(0, 0)
  64. label.set_markup(content)
  65. label.show()
  66. return label
  67. def layer_widget_toggled_cb(self, cell, path, layer_store):
  68. name = layer_store[path][0]
  69. toggle = not layer_store[path][1]
  70. layer_store[path][1] = toggle
  71. def layer_widget_add_clicked_cb(self, action, layer_store, parent):
  72. dialog = gtk.FileChooserDialog("Add new layer", parent,
  73. gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
  74. button = dialog.add_button("Cancel", gtk.RESPONSE_NO)
  75. HobAltButton.style_button(button)
  76. button = dialog.add_button("Open", gtk.RESPONSE_YES)
  77. HobButton.style_button(button)
  78. label = gtk.Label("Select the layer you wish to add")
  79. label.show()
  80. dialog.set_extra_widget(label)
  81. response = dialog.run()
  82. path = dialog.get_filename()
  83. dialog.destroy()
  84. lbl = "<b>Error</b>\nUnable to load layer <i>%s</i> because " % path
  85. if response == gtk.RESPONSE_YES:
  86. import os
  87. import os.path
  88. layers = []
  89. it = layer_store.get_iter_first()
  90. while it:
  91. layers.append(layer_store.get_value(it, 0))
  92. it = layer_store.iter_next(it)
  93. if not path:
  94. lbl += "it is an invalid path."
  95. elif not os.path.exists(path+"/conf/layer.conf"):
  96. lbl += "there is no layer.conf inside the directory."
  97. elif path in layers:
  98. lbl += "it is already in loaded layers."
  99. else:
  100. layer_store.append([path])
  101. return
  102. dialog = CrumbsMessageDialog(parent, lbl)
  103. dialog.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_OK)
  104. response = dialog.run()
  105. dialog.destroy()
  106. def layer_widget_del_clicked_cb(self, action, tree_selection, layer_store):
  107. model, iter = tree_selection.get_selected()
  108. if iter:
  109. layer_store.remove(iter)
  110. def gen_layer_widget(self, layers, layers_avail, window, tooltip=""):
  111. hbox = gtk.HBox(False, 6)
  112. layer_tv = gtk.TreeView()
  113. layer_tv.set_rules_hint(True)
  114. layer_tv.set_headers_visible(False)
  115. tree_selection = layer_tv.get_selection()
  116. tree_selection.set_mode(gtk.SELECTION_SINGLE)
  117. # Allow enable drag and drop of rows including row move
  118. dnd_internal_target = ''
  119. dnd_targets = [(dnd_internal_target, gtk.TARGET_SAME_WIDGET, 0)]
  120. layer_tv.enable_model_drag_source( gtk.gdk.BUTTON1_MASK,
  121. dnd_targets,
  122. gtk.gdk.ACTION_MOVE)
  123. layer_tv.enable_model_drag_dest(dnd_targets,
  124. gtk.gdk.ACTION_MOVE)
  125. layer_tv.connect("drag_data_get", self.drag_data_get_cb)
  126. layer_tv.connect("drag_data_received", self.drag_data_received_cb)
  127. col0= gtk.TreeViewColumn('Path')
  128. cell0 = gtk.CellRendererText()
  129. cell0.set_padding(5,2)
  130. col0.pack_start(cell0, True)
  131. col0.set_cell_data_func(cell0, self.draw_layer_path_cb)
  132. layer_tv.append_column(col0)
  133. scroll = gtk.ScrolledWindow()
  134. scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
  135. scroll.set_shadow_type(gtk.SHADOW_IN)
  136. scroll.add(layer_tv)
  137. table_layer = gtk.Table(2, 10, False)
  138. hbox.pack_start(table_layer, expand=True, fill=True)
  139. table_layer.attach(scroll, 0, 10, 0, 1)
  140. layer_store = gtk.ListStore(gobject.TYPE_STRING)
  141. for layer in layers:
  142. layer_store.append([layer])
  143. col1 = gtk.TreeViewColumn('Enabled')
  144. layer_tv.append_column(col1)
  145. cell1 = CellRendererPixbufActivatable()
  146. cell1.set_fixed_size(-1,35)
  147. cell1.connect("clicked", self.del_cell_clicked_cb, layer_store)
  148. col1.pack_start(cell1, True)
  149. col1.set_cell_data_func(cell1, self.draw_delete_button_cb, layer_tv)
  150. add_button = gtk.Button()
  151. add_button.set_relief(gtk.RELIEF_NONE)
  152. box = gtk.HBox(False, 6)
  153. box.show()
  154. add_button.add(box)
  155. add_button.connect("enter-notify-event", self.add_hover_cb)
  156. add_button.connect("leave-notify-event", self.add_leave_cb)
  157. self.im = gtk.Image()
  158. self.im.set_from_file(hic.ICON_INDI_ADD_FILE)
  159. self.im.show()
  160. box.pack_start(self.im, expand=False, fill=False, padding=6)
  161. lbl = gtk.Label("Add layer")
  162. lbl.set_alignment(0.0, 0.5)
  163. lbl.show()
  164. box.pack_start(lbl, expand=True, fill=True, padding=6)
  165. add_button.connect("clicked", self.layer_widget_add_clicked_cb, layer_store, window)
  166. table_layer.attach(add_button, 0, 10, 1, 2, gtk.EXPAND | gtk.FILL, 0, 0, 6)
  167. layer_tv.set_model(layer_store)
  168. hbox.show_all()
  169. return hbox, layer_store
  170. def drag_data_get_cb(self, treeview, context, selection, target_id, etime):
  171. treeselection = treeview.get_selection()
  172. model, iter = treeselection.get_selected()
  173. data = model.get_value(iter, 0)
  174. selection.set(selection.target, 8, data)
  175. def drag_data_received_cb(self, treeview, context, x, y, selection, info, etime):
  176. model = treeview.get_model()
  177. data = selection.data
  178. drop_info = treeview.get_dest_row_at_pos(x, y)
  179. if drop_info:
  180. path, position = drop_info
  181. iter = model.get_iter(path)
  182. if (position == gtk.TREE_VIEW_DROP_BEFORE or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE):
  183. model.insert_before(iter, [data])
  184. else:
  185. model.insert_after(iter, [data])
  186. else:
  187. model.append([data])
  188. if context.action == gtk.gdk.ACTION_MOVE:
  189. context.finish(True, True, etime)
  190. return
  191. def add_hover_cb(self, button, event):
  192. self.im.set_from_file(hic.ICON_INDI_ADD_HOVER_FILE)
  193. def add_leave_cb(self, button, event):
  194. self.im.set_from_file(hic.ICON_INDI_ADD_FILE)
  195. def __init__(self, title, layers, layers_non_removable, all_layers, parent, flags, buttons=None):
  196. super(LayerSelectionDialog, self).__init__(title, parent, flags, buttons)
  197. # class members from other objects
  198. self.layers = layers
  199. self.layers_non_removable = layers_non_removable
  200. self.all_layers = all_layers
  201. self.layers_changed = False
  202. # icon for remove button in TreeView
  203. im = gtk.Image()
  204. im.set_from_file(hic.ICON_INDI_REMOVE_FILE)
  205. self.rem_icon = im.get_pixbuf()
  206. # class members for internal use
  207. self.layer_store = None
  208. # create visual elements on the dialog
  209. self.create_visual_elements()
  210. self.connect("response", self.response_cb)
  211. def create_visual_elements(self):
  212. layer_widget, self.layer_store = self.gen_layer_widget(self.layers, self.all_layers, self, None)
  213. layer_widget.set_size_request(450, 250)
  214. self.vbox.pack_start(layer_widget, expand=True, fill=True)
  215. self.show_all()
  216. def response_cb(self, dialog, response_id):
  217. model = self.layer_store
  218. it = model.get_iter_first()
  219. layers = []
  220. while it:
  221. layers.append(model.get_value(it, 0))
  222. it = model.iter_next(it)
  223. self.layers_changed = (self.layers != layers)
  224. self.layers = layers
  225. """
  226. A custom cell_data_func to draw a delete 'button' in the TreeView for layers
  227. other than the meta layer. The deletion of which is prevented so that the
  228. user can't shoot themselves in the foot too badly.
  229. """
  230. def draw_delete_button_cb(self, col, cell, model, it, tv):
  231. path = model.get_value(it, 0)
  232. if path in self.layers_non_removable:
  233. cell.set_sensitive(False)
  234. cell.set_property('pixbuf', None)
  235. cell.set_property('mode', gtk.CELL_RENDERER_MODE_INERT)
  236. else:
  237. cell.set_property('pixbuf', self.rem_icon)
  238. cell.set_sensitive(True)
  239. cell.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)
  240. return True
  241. """
  242. A custom cell_data_func to write an extra message into the layer path cell
  243. for the meta layer. We should inform the user that they can't remove it for
  244. their own safety.
  245. """
  246. def draw_layer_path_cb(self, col, cell, model, it):
  247. path = model.get_value(it, 0)
  248. if path in self.layers_non_removable:
  249. cell.set_property('markup', "<b>It cannot be removed</b>\n%s" % path)
  250. else:
  251. cell.set_property('text', path)
  252. def del_cell_clicked_cb(self, cell, path, model):
  253. it = model.get_iter_from_string(path)
  254. model.remove(it)