Moved toolbar in files.out_table outside __init__
[findit] / src / files / out_table.py
index 375e953..ef59d46 100755 (executable)
@@ -9,7 +9,8 @@ from misc import _
 #==============================================================================
 
 class Cli_Presentation(object):
-    def __init__(self, filelist):   ###
+    def __init__(self, results):   ###
+        filelist, start_path = results
         self.toplevel = self.print_results(filelist)
 
     def print_results(self, filelist):
@@ -19,8 +20,35 @@ class Cli_Presentation(object):
 #==============================================================================
 
 class Gtk_Presentation(object):
+
+    def _create_toolbar(self):
+        toolbar = gtk.Toolbar()
+        toolbar.set_property('icon-size', 'small-toolbar')
+
+        abs_paths_tbtn = gtk.ToggleToolButton()
+        abs_paths_tbtn.set_label(_('Absolute paths'))
+        abs_paths_tbtn.connect('clicked', self._show_abspaths)
+
+        bitesizes_tbtn = gtk.ToggleToolButton()
+        bitesizes_tbtn.set_label(_('Sizes in bytes'))
+        bitesizes_tbtn.connect('clicked', self._show_bitesizes)
+
+        saveresults_tbtn = gtk.ToolButton('gtk-save')
+        saveresults_tbtn.connect('clicked', self.save_results)
+
+        copyresults_tbtn = gtk.ToolButton('gtk-copy')
+        copyresults_tbtn.connect('clicked', self.copy_results)
+
+        toolbar.insert(abs_paths_tbtn, -1)
+        toolbar.insert(bitesizes_tbtn, -1)
+        toolbar.insert(saveresults_tbtn, -1)
+        toolbar.insert(copyresults_tbtn, -1)
+
+        return toolbar
+
     def __init__(self, results):   ###
         import gtk
+        global gtk  # for save_results and copy_results
         import gobject
 
         self.filelist, self.start_path = results
@@ -31,7 +59,10 @@ class Gtk_Presentation(object):
 
         # Store results
         self.liststore = gtk.ListStore(str, str, gobject.TYPE_INT64)
-        self._show_relpaths(None)
+        for bsize, path, size in self.filelist:
+            self.liststore.append([size,
+                                   path.replace(self.start_path,'', 1),
+                                   bsize])
 
         treeview = gtk.TreeView(self.liststore)
 
@@ -40,20 +71,19 @@ class Gtk_Presentation(object):
         cell1 = gtk.CellRendererText()
         cell1.set_property('width', 90)
         size_col.pack_start(cell1, True)
-        size_col.add_attribute(cell1, 'text', 1)
+        size_col.add_attribute(cell1, 'text', 0)
         treeview.append_column(size_col)
 
         # 'Path' column
         path_col = gtk.TreeViewColumn(_('Path'))
         cell2 = gtk.CellRendererText()
         path_col.pack_start(cell2, True)
-        #path_col.add_attribute(cell2, 'text', 0)
-        path_col.add_attribute(cell2, 'markup', 0)
+        path_col.add_attribute(cell2, 'markup', 1)
         treeview.append_column(path_col)
 
         # Column sorting
         treeview.set_search_column(1)
-        path_col.set_sort_column_id(0)
+        path_col.set_sort_column_id(1)
         size_col.set_sort_column_id(2)
 
         # Add treeview to scrolled window
@@ -62,27 +92,12 @@ class Gtk_Presentation(object):
         swin.add(treeview)
 
         #====================
-        # Toolbar
+        # Packing
         #====================
 
-        toolbar = gtk.Toolbar()
-        relpaths_tbtn = gtk.RadioToolButton(None)
-        abspaths_tbtn = gtk.RadioToolButton(relpaths_tbtn)
-
-        relpaths_tbtn.set_label(_('Relative paths'))
-        abspaths_tbtn.set_label(_('Absolute paths'))
-
-        relpaths_tbtn.connect('clicked', self._show_relpaths)
-        abspaths_tbtn.connect('clicked', self._show_abspaths)
-
-        toolbar.insert(relpaths_tbtn, -1)
-        toolbar.insert(abspaths_tbtn, -1)
-
-        #====================
-        # Others
-        #====================
+        toolbar = self._create_toolbar()
 
-        vbox = gtk.VBox(False, 4)
+        vbox = gtk.VBox(False, 0)
         vbox.pack_start(swin, True, True, 0)
         vbox.pack_start(toolbar, False, False, 0)
 
@@ -90,21 +105,54 @@ class Gtk_Presentation(object):
 
     #=== Functions ============================================================
 
-    def _show_relpaths(self, btn):
-        self.liststore.clear()
-        for bsize, path, size in self.filelist:
-            self.liststore.append([path.replace(self.start_path,'', 1),
-                                   size, bsize])
-
     def _show_abspaths(self, btn):
-        self.liststore.clear()
-        for bsize, path, size in self.filelist:
-            #self.liststore.append([abspath(path), size, bsize])
+        # Toggled mean 'absolute paths'
+        if btn.get_active():
             # Mark absolute part of path with color 
-            self.liststore.append([
-                '<span background="lawngreen">' + abspath(self.start_path) + '</span>' +
-                path.replace(self.start_path,'', 1),
-                size, bsize])
+            for i, (bsize, path, size) in enumerate(self.filelist):
+                self.liststore[i][1] = '<span background="lawngreen">' + \
+                                       abspath(self.start_path) + '</span>' + \
+                                       path.replace(self.start_path,'', 1)
+        else:
+            for i, (bsize, path, size) in enumerate(self.filelist):
+                self.liststore[i][1] = path.replace(self.start_path,'', 1)
+
+    def _show_bitesizes(self, btn):
+        if btn.get_active():
+            for i, (bsize, path, size) in enumerate(self.filelist):
+                self.liststore[i][0] = bsize
+        else:
+            for i, (bsize, path, size) in enumerate(self.filelist):
+                self.liststore[i][0] = size
+
+    def save_results(self, btn):
+        """Show 'Save to file' dialog."""
+        dialog = gtk.FileChooserDialog(title='Save to...',
+                                       action='save',
+                                       buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
+                                                gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
+        dialog.show_all()
+        response = dialog.run()
+        if response == gtk.RESPONSE_OK:
+            filepath = dialog.get_filename()
+            outfile = open(filepath, 'w')
+            # Saving results as "bite size\tabsolute path"
+            for bsize, path, size in self.filelist:
+                outfile.write(`bsize` + '\t' + abspath(path) + '\n')
+            outfile.close()
+        dialog.destroy()
+
+    def copy_results(self, btn):
+        """Copy results to clipboard."""
+        # Form list
+        filelist = ''
+        for bsize, path, size in self.filelist:
+            filelist += `bsize` + '\t' + abspath(path) + '\n'
+
+        # Store in clipboard
+        cb = gtk.Clipboard()
+        cb.set_text(filelist)
+        cb.store()
 
 #==============================================================================