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