Replaced 'Absolute' and 'Relative' radio buttons with one togglebutton in 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         for bsize, path, size in self.filelist:
36             self.liststore.append([size,
37                                    path.replace(self.start_path,'', 1),
38                                    bsize])
39
40         treeview = gtk.TreeView(self.liststore)
41
42         # 'Size' column
43         size_col = gtk.TreeViewColumn(_('Size'))
44         cell1 = gtk.CellRendererText()
45         cell1.set_property('width', 90)
46         size_col.pack_start(cell1, True)
47         size_col.add_attribute(cell1, 'text', 0)
48         treeview.append_column(size_col)
49
50         # 'Path' column
51         path_col = gtk.TreeViewColumn(_('Path'))
52         cell2 = gtk.CellRendererText()
53         path_col.pack_start(cell2, True)
54         path_col.add_attribute(cell2, 'markup', 1)
55         treeview.append_column(path_col)
56
57         # Column sorting
58         treeview.set_search_column(1)
59         path_col.set_sort_column_id(1)
60         size_col.set_sort_column_id(2)
61
62         # Add treeview to scrolled window
63         swin = gtk.ScrolledWindow()
64         swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
65         swin.add(treeview)
66
67         #====================
68         # Toolbar
69         #====================
70
71         toolbar = gtk.Toolbar()
72         toolbar.set_property('icon-size', 'small-toolbar')
73
74         abs_paths_tbtn = gtk.ToggleToolButton()
75         abs_paths_tbtn.set_label(_('Absolute paths'))
76         abs_paths_tbtn.connect('clicked', self._show_abspaths)
77
78         bitesizes_tbtn = gtk.ToggleToolButton()
79         bitesizes_tbtn.set_label(_('Sizes in bytes'))
80         bitesizes_tbtn.connect('clicked', self._show_bitesizes)
81
82         saveresults_tbtn = gtk.ToolButton('gtk-save')
83         saveresults_tbtn.connect('clicked', self.save_results)
84
85         copyresults_tbtn = gtk.ToolButton('gtk-copy')
86         copyresults_tbtn.connect('clicked', self.copy_results)
87
88         toolbar.insert(abs_paths_tbtn, -1)
89         toolbar.insert(bitesizes_tbtn, -1)
90         toolbar.insert(saveresults_tbtn, -1)
91         toolbar.insert(copyresults_tbtn, -1)
92
93         #====================
94         # Others
95         #====================
96
97         vbox = gtk.VBox(False, 0)
98         vbox.pack_start(swin, True, True, 0)
99         vbox.pack_start(toolbar, False, False, 0)
100
101         self.toplevel = vbox
102
103     #=== Functions ============================================================
104
105     def _show_abspaths(self, btn):
106         # Toggled mean 'absolute paths'
107         if btn.get_active():
108             # Mark absolute part of path with color 
109             for i, (bsize, path, size) in enumerate(self.filelist):
110                 self.liststore[i][1] = '<span background="lawngreen">' + \
111                                        abspath(self.start_path) + '</span>' + \
112                                        path.replace(self.start_path,'', 1)
113         else:
114             for i, (bsize, path, size) in enumerate(self.filelist):
115                 self.liststore[i][1] = path.replace(self.start_path,'', 1)
116
117     def _show_bitesizes(self, btn):
118         if btn.get_active():
119             for i, (bsize, path, size) in enumerate(self.filelist):
120                 self.liststore[i][0] = bsize
121         else:
122             for i, (bsize, path, size) in enumerate(self.filelist):
123                 self.liststore[i][0] = size
124
125     def save_results(self, btn):
126         """Show 'Save to file' dialog."""
127         dialog = gtk.FileChooserDialog(title='Save to...',
128                                        action='save',
129                                        buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
130                                                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
131         dialog.show_all()
132         response = dialog.run()
133         if response == gtk.RESPONSE_OK:
134             filepath = dialog.get_filename()
135             outfile = open(filepath, 'w')
136             # Saving results as "bite size\tabsolute path"
137             for bsize, path, size in self.filelist:
138                 outfile.write(`bsize` + '\t' + abspath(path) + '\n')
139             outfile.close()
140         dialog.destroy()
141
142     def copy_results(self, btn):
143         """Copy results to clipboard."""
144         # Form list
145         filelist = ''
146         for bsize, path, size in self.filelist:
147             filelist += `bsize` + '\t' + abspath(path) + '\n'
148
149         # Store in clipboard
150         cb = gtk.Clipboard()
151         cb.set_text(filelist)
152         cb.store()
153
154 #==============================================================================
155
156 class Hildon_Presentation(object):
157     def __init__(self, filelist):   ###
158         import gtk
159         import gobject
160         import hildon
161
162         # На таблетке не отображаються заголовки столбцов по умолчанию -
163         # след строка заставляет их отображаться принудительно
164         treeview.set_headers_visible(1)