Do not display [] and ''
[pysize.git] / pysize / ui / gtk / pysize_widget_menu.py
blob7dccdf50336ebc868a3dabbb4177c46a97fc8704
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@yahoo.fr>
19 import pygtk
20 pygtk.require('2.0')
21 import gtk
22 assert gtk.pygtk_version >= (2, 8)
24 from pysize.core import compute_size
25 from pysize.ui.utils import human_unit
27 class PysizeWidget_Menu(object):
28 def __init__(self, options, args):
29 self.connect('popup-menu', type(self)._pop_menu)
30 self.connect('button-press-event', type(self)._menu_mouse_button)
32 @staticmethod
33 def _add_menu_item(menu, name, action):
34 item = gtk.MenuItem(name)
35 item.connect('activate', action)
36 item.show()
37 menu.append(item)
39 def _pop_menu(self, event=None):
40 if self.cursor_node:
41 menu = gtk.Menu()
43 props = lambda item: self.show_properties(self.get_selection())
44 self._add_menu_item(menu, 'Show properties...', props)
46 if event:
47 button = event.button
48 event_time = event.time
49 else:
50 button = 0
51 event_time = gtk.get_current_event_time()
52 menu.attach_to_widget(self, None)
53 menu.popup(None, None, None, button, event_time)
54 return True
56 def _menu_mouse_button(self, event):
57 if event.button == 3 and event.type == gtk.gdk.BUTTON_PRESS:
58 return self._pop_menu(event)
59 return False
61 def show_properties(self, nodes):
62 fullpaths = []
63 for node in nodes:
64 fullpaths.extend(node.get_fullpaths())
65 fullpaths.sort(key=compute_size.slow,reverse=True)
66 human_size = lambda fp: human_unit(compute_size.slow(fp))
67 size_fullpaths = [human_size(fp) + ' | ' + fp for fp in fullpaths]
68 names = ",".join([node.get_name() for node in nodes])
69 dialog = gtk.Dialog('Properties for ' + names, None, 0,
70 (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
71 label = gtk.Label('\n'.join(size_fullpaths))
72 label.set_selectable(True)
73 label.show()
74 dialog.vbox.add(label)
75 dialog.run()
76 dialog.destroy()