Added 'Sizes in bytes' togglebutton to files.out_table
[findit] / src / files / out_table.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 from os.path import abspath
6
7 from misc import _
8
9 #==============================================================================
10
11 class Cli_Presentation(object):
12     def __init__(self, filelist):   ###
13         self.toplevel = self.print_results(filelist)
14
15     def print_results(self, filelist):
16         for bsize, path, size in filelist:
17             print '%10d' % bsize, path
18
19 #==============================================================================
20
21 class Gtk_Presentation(object):
22     def __init__(self, results):   ###
23         import gtk
24         global gtk  # for save_results and copy_results
25         import gobject
26
27         self.filelist, self.start_path = results
28
29         #====================
30         # Treeview
31         #====================
32
33         # Store results
34         self.liststore = gtk.ListStore(str, str, gobject.TYPE_INT64)
35         self._show_relpaths(None)
36
37         treeview = gtk.TreeView(self.liststore)
38
39         # 'Size' column
40         size_col = gtk.TreeViewColumn(_('Size'))
41         cell1 = gtk.CellRendererText()
42         cell1.set_property('width', 90)
43         size_col.pack_start(cell1, True)
44         size_col.add_attribute(cell1, 'text', 0)
45         treeview.append_column(size_col)
46
47         # 'Path' column
48         path_col = gtk.TreeViewColumn(_('Path'))
49         cell2 = gtk.CellRendererText()
50         path_col.pack_start(cell2, True)
51         path_col.add_attribute(cell2, 'markup', 1)
52         treeview.append_column(path_col)
53
54         # Column sorting
55         treeview.set_search_column(1)
56         path_col.set_sort_column_id(1)
57         size_col.set_sort_column_id(2)
58
59         # Add treeview to scrolled window
60         swin = gtk.ScrolledWindow()
61         swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
62         swin.add(treeview)
63
64         #====================
65         # Toolbar
66         #====================
67
68         toolbar = gtk.Toolbar()
69         toolbar.set_property('icon-size', 'small-toolbar')
70
71         relpaths_tbtn = gtk.RadioToolButton(None)
72         abspaths_tbtn = gtk.RadioToolButton(relpaths_tbtn)
73
74         relpaths_tbtn.set_label(_('Relative paths'))
75         abspaths_tbtn.set_label(_('Absolute paths'))
76
77         relpaths_tbtn.connect('clicked', self._show_relpaths)
78         abspaths_tbtn.connect('clicked', self._show_abspaths)
79
80         bitesizes_tbtn = gtk.ToggleToolButton()
81         bitesizes_tbtn.set_label(_('Sizes in bytes'))
82         bitesizes_tbtn.connect('clicked', self._show_bitesizes)
83
84         saveresults_tbtn = gtk.ToolButton('gtk-save')
85         saveresults_tbtn.connect('clicked', self.save_results)
86
87         copyresults_tbtn = gtk.ToolButton('gtk-copy')
88         copyresults_tbtn.connect('clicked', self.copy_results)
89
90         toolbar.insert(relpaths_tbtn, -1)
91         toolbar.insert(abspaths_tbtn, -1)
92         toolbar.insert(bitesizes_tbtn, -1)
93         toolbar.insert(saveresults_tbtn, -1)
94         toolbar.insert(copyresults_tbtn, -1)
95
96         #====================
97         # Others
98         #====================
99
100         vbox = gtk.VBox(False, 0)
101         vbox.pack_start(swin, True, True, 0)
102         vbox.pack_start(toolbar, False, False, 0)
103
104         self.toplevel = vbox
105
106     #=== Functions ============================================================
107
108     def _show_relpaths(self, btn):
109         self.liststore.clear()
110         for bsize, path, size in self.filelist:
111             self.liststore.append([size, path.replace(self.start_path,'', 1),
112                                    bsize])
113
114     def _show_abspaths(self, btn):
115         self.liststore.clear()
116         for bsize, path, size in self.filelist:
117             #self.liststore.append([abspath(path), size, bsize])
118             # Mark absolute part of path with color 
119             self.liststore.append([size,
120                 '<span background="lawngreen">' + abspath(self.start_path) + '</span>' +
121                 path.replace(self.start_path,'', 1),
122                 bsize])
123
124     def _show_bitesizes(self, btn):
125         if btn.get_active():
126             for i, (bsize, path, size) in enumerate(self.filelist):
127                 self.liststore[i][0] = bsize
128         else:
129             for i, (bsize, path, size) in enumerate(self.filelist):
130                 self.liststore[i][0] = size
131
132     def save_results(self, btn):
133         """Show 'Save to file' dialog."""
134         dialog = gtk.FileChooserDialog(title='Save to...',
135                                        action='save',
136                                        buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
137                                                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
138         dialog.show_all()
139         response = dialog.run()
140         if response == gtk.RESPONSE_OK:
141             filepath = dialog.get_filename()
142             outfile = open(filepath, 'w')
143             # Saving results as "bite size\tabsolute path"
144             for bsize, path, size in self.filelist:
145                 outfile.write(`bsize` + '\t' + abspath(path) + '\n')
146             outfile.close()
147         dialog.destroy()
148
149     def copy_results(self, btn):
150         """Copy results to clipboard."""
151         # Form list
152         filelist = ''
153         for bsize, path, size in self.filelist:
154             filelist += `bsize` + '\t' + abspath(path) + '\n'
155
156         # Store in clipboard
157         cb = gtk.Clipboard()
158         cb.set_text(filelist)
159         cb.store()
160
161 #==============================================================================
162
163 class Hildon_Presentation(object):
164     def __init__(self, filelist):   ###
165         import gtk
166         import gobject
167         import hildon
168
169         # На таблетке не отображаються заголовки столбцов по умолчанию -
170         # след строка заставляет их отображаться принудительно
171         treeview.set_headers_visible(1)