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>
23 assert gtk
.pygtk_version
>= (2, 8)
28 from pysize
.ui
.gtk
.gazpacho_loader
.loader
import ObjectBuilder
30 from pysize
.ui
.gtk
.pysize_widget
import PysizeWidget
31 from pysize
.ui
.utils
import human_unit
, short_string
, sanitize_string
32 from pysize
.core
.compute_size
import size_observable
33 from pysize
.core
import history
35 def hover_changed(main_widget
, hover_node
, status_bar
):
37 node
= hover_node
or main_widget
.tree
.root
38 label
= human_unit(node
.size
) + ' | ' + node
.get_fullname()
39 status_bar
.push(0, label
)
41 def size_observer(progress_bar
):
43 if now
- size_observer
.last_pulse
> 0.04:
44 size_observer
.last_pulse
= now
45 gobject
.idle_add(lambda: progress_bar
.pulse())
46 size_observer
.last_pulse
= time
.time()
48 def hide_pbar(progress_bar
, visible
):
54 def update_action(zoom_fit_action
, main_widget
):
55 # Enable if the zoom is not already 'auto' and there is a tree
56 enable
= main_widget
.options
.min_size
!= 'auto' and \
57 not not main_widget
.tree
.root
.get_fullpaths()
58 zoom_fit_action
.set_sensitive(enable
)
60 def update_title(window
, main_widget
):
61 root
= main_widget
.tree
.root
62 window
.set_title('Pysize - %s | %s' %
63 (human_unit(root
.size
), sanitize_string(root
.get_name())))
65 def update_back_action(back_action
, next_insertion_index
, history
):
66 back_action
.set_sensitive(next_insertion_index
> 1)
68 def update_forward_action(forward_action
, next_insertion_index
, history
):
69 forward_action
.set_sensitive(next_insertion_index
< len(history
))
71 def update_hist_menu(main_widget
, hist_menu
, next_insertion_index
, hist_list
):
72 class activator(object):
73 def __init__(self
, index
):
76 def activate(self
, unused_menu_item
):
77 main_widget
.set_paths(history
.go_to_history(self
.index
))
79 for nr
, item
in enumerate(hist_menu
):
80 if nr
> 2: # Keep 'Back' and 'Forward'
81 hist_menu
.remove(item
)
82 item
= gtk
.SeparatorMenuItem()
85 for nr
, (paths
, name
) in enumerate(hist_list
):
86 # We want underscores, not mnemonics
87 name
= short_string(sanitize_string(name
), 100).replace('_', '__')
88 item
= gtk
.CheckMenuItem(name
)
89 if nr
+ 1 == next_insertion_index
:
91 item
.connect('activate', activator(nr
).activate
)
92 item
.set_draw_as_radio(True)
96 class PysizeWindow(object):
97 def __init__(self
, options
, args
):
98 create_widget
= lambda **unused_kwargs
: PysizeWidget(options
, args
)
99 glade_filename
= os
.path
.join(os
.path
.dirname(__file__
),
101 builder
= ObjectBuilder(glade_filename
,
102 custom
= {'pysize_widget': create_widget
})
104 main_widget
= builder
.get_widget('main_widget')
105 status_bar
= builder
.get_widget('status_bar')
106 main_widget
.connect('hover_changed', hover_changed
, status_bar
)
108 progress_bar
= builder
.get_widget('progress_bar')
109 size_observable
.add_observer(lambda: size_observer(progress_bar
))
110 pbar_hider
= lambda w
, building
: hide_pbar(progress_bar
, building
)
111 main_widget
.connect('building-tree-state-changed', pbar_hider
)
113 max_depth
= builder
.get_widget('max_depth')
114 spin_init
= lambda w
: max_depth
.set_value(main_widget
.options
.max_depth
)
115 main_widget
.connect('realize', spin_init
)
117 ui_manager
= builder
.get_widget('uimanager')
118 zoom_fit_action
= ui_manager
.get_action('ui/toolbar/ZoomFit')
119 action_updater
= lambda w
, building
: update_action(zoom_fit_action
, w
)
120 main_widget
.connect('building-tree-state-changed', action_updater
)
122 # Disable actions when the tree is empty: pysize launched with no arg
123 class disabler(object):
124 def __init__(self
, action
):
126 def disable(self
, main_widget
, building
):
127 sensitive
= not not main_widget
.tree
.root
.get_fullpaths()
128 self
.action
.set_sensitive(sensitive
)
129 for name
in 'ParentDirectory', 'Refresh', 'ZoomIn', 'ZoomOut':
130 action
= ui_manager
.get_action('ui/toolbar/' + name
)
131 action_disabler
= disabler(action
).disable
132 main_widget
.connect('building-tree-state-changed', action_disabler
)
134 back_action
= ui_manager
.get_action('ui/toolbar/Back')
135 back_action
.set_sensitive(False)
136 update_back
= lambda n
, h
: update_back_action(back_action
, n
, h
)
137 history
.history_observable
.add_observer(update_back
)
139 forward_action
= ui_manager
.get_action('ui/toolbar/Forward')
140 forward_action
.set_sensitive(False)
141 update_forw
= lambda n
, h
: update_forward_action(forward_action
, n
, h
)
142 history
.history_observable
.add_observer(update_forw
)
144 hist_menu_action
= ui_manager
.get_action('ui/menubar/HistoryMenu')
145 hist_menu
= hist_menu_action
.get_proxies()[0].get_submenu()
146 upd_hist
= lambda n
, h
: update_hist_menu(main_widget
, hist_menu
, n
, h
)
147 history
.history_observable
.add_observer(upd_hist
)
149 toolbar
= builder
.get_widget('toolbar')
150 toolbar_remaining
= builder
.get_widget('toolbar_remaining')
151 toolbar_remaining
.unparent()
152 tool_item
= gtk
.ToolItem()
153 tool_item
.add(toolbar_remaining
)
154 toolbar
.insert(tool_item
, -1)
156 logo_filename
= os
.path
.join(os
.path
.dirname(__file__
), 'logo.svg')
157 gtk
.window_set_default_icon_from_file(logo_filename
)
160 'quit_action': lambda w
: gtk
.main_quit(),
161 'refresh_tree_action': lambda w
: main_widget
.refresh_tree(),
162 'zoom_fit_action': lambda w
: main_widget
.zoom_fit(),
163 'zoom_in_action': lambda w
: main_widget
.zoom_in(),
164 'zoom_out_action': lambda w
: main_widget
.zoom_out(),
165 'parent_directory_action': lambda w
: main_widget
.parent_directory(),
166 'open_action': lambda w
: main_widget
.open(),
167 'back_action': lambda w
: main_widget
.set_paths(history
.back()),
168 'forw_action': lambda w
: main_widget
.set_paths(history
.forward()),
169 'max_depth_changed': main_widget
.max_depth_changed
171 builder
.signal_autoconnect(callbacks
)
173 window
= builder
.get_widget('main_window')
174 title_updater
= lambda w
, tree
: update_title(window
, main_widget
)
175 main_widget
.connect('building-tree-state-changed', title_updater
)