"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/ui/gtk/pysize_widget_menu.py" (11 Mar 2007, 3167 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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.
    5 #
    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.
   10 #
   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.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   19 import os
   20 import pygtk
   21 pygtk.require('2.0')
   22 import gtk
   23 assert gtk.pygtk_version >= (2, 8)
   24 
   25 from pysize.core import compute_size
   26 from pysize.ui.utils import human_unit
   27 
   28 class PysizeWidget_Menu(object):
   29     def __init__(self, options, args):
   30         self.connect('popup-menu', type(self)._pop_menu)
   31         self.connect('button-press-event', type(self)._menu_mouse_button)
   32 
   33     @staticmethod
   34     def _add_menu_item(menu, name, action):
   35         item = gtk.MenuItem(name)
   36         item.connect('activate', action)
   37         item.show()
   38         menu.append(item)
   39 
   40     def _pop_menu(self, event=None):
   41         if self.cursor_node:
   42             menu = gtk.Menu()
   43 
   44             props = lambda item: self.show_properties(self.get_selected_nodes())
   45             self._add_menu_item(menu, 'Show properties...', props)
   46             dele = lambda item: self.schedule_delete(self.get_selected_nodes())
   47             self._add_menu_item(menu, 'Schedule for deletion', dele)
   48 
   49             if event:
   50                 button = event.button
   51                 event_time = event.time
   52             else:
   53                 button = 0
   54                 event_time = gtk.get_current_event_time()
   55             menu.attach_to_widget(self, None)
   56             menu.popup(None, None, None, button, event_time)
   57             return True
   58 
   59     def _menu_mouse_button(self, event):
   60         if event.button == 3 and event.type == gtk.gdk.BUTTON_PRESS:
   61             return self._pop_menu(event)
   62         return False
   63 
   64     def show_properties(self, nodes):
   65         fullpaths = []
   66         for node in nodes:
   67             fullpaths.extend(node.get_fullpaths())
   68         fullpaths.sort(key=compute_size.slow,reverse=True)
   69         total_size = 0
   70         text = ''
   71         for path in fullpaths:
   72             size = compute_size.slow(path)
   73             total_size += size
   74             if os.path.isdir(path) and path != '/':
   75                 path += '/'
   76             text += '%s | %s\n' % (human_unit(size), path)
   77         text += human_unit(total_size)
   78         names = ','.join([node.get_name() for node in nodes])
   79         dialog = gtk.Dialog('Properties for ' + names, None, 0,
   80                             (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
   81         label = gtk.Label(text)
   82         label.set_selectable(True)
   83         label.show()
   84         dialog.vbox.add(label)
   85         dialog.run()
   86         dialog.destroy()