/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to olive/__init__.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2007-05-16 00:16:46 UTC
  • mto: (195.1.7 trunk)
  • mto: This revision was merged to the branch mainline in revision 199.
  • Revision ID: szilveszter.farkas@gmail.com-20070516001646-tj5owhwdm36ys546
Implemented sortable columns, and there are also some new columns
 - size
 - last modification

Sorting is done by the TreeModel, not "by hand" (via list.sort()).

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import os
18
18
import sys
 
19
import time
19
20
 
20
21
# gettext support
21
22
import gettext
27
28
except:
28
29
    pass
29
30
 
 
31
import gobject
30
32
import gtk
31
33
import gtk.gdk
32
34
import gtk.glade
107
109
        self.combobox_drive = gtk.combo_box_new_text()
108
110
        self.combobox_drive.connect("changed", self._refresh_drives)
109
111
        
 
112
        # Get the navigation widgets
 
113
        self.button_location_up = self.toplevel.get_widget('button_location_up')
 
114
        self.button_location_jump = self.toplevel.get_widget('button_location_jump')
 
115
        self.entry_location = self.toplevel.get_widget('entry_location')
 
116
        self.image_location_error = self.toplevel.get_widget('image_location_error')
 
117
        
110
118
        self.vbox_main_right = self.toplevel.get_widget('vbox_main_right')
111
 
 
112
119
        
113
120
        # Dictionary for signal_autoconnect
114
121
        dic = { "on_window_main_destroy": gtk.main_quit,
148
155
                "on_treeview_right_button_press_event": self.on_treeview_right_button_press_event,
149
156
                "on_treeview_right_row_activated": self.on_treeview_right_row_activated,
150
157
                "on_treeview_left_button_press_event": self.on_treeview_left_button_press_event,
151
 
                "on_treeview_left_row_activated": self.on_treeview_left_row_activated }
 
158
                "on_treeview_left_row_activated": self.on_treeview_left_row_activated,
 
159
                "on_button_location_up_clicked": self.on_button_location_up_clicked,
 
160
                "on_button_location_jump_clicked": self.on_button_location_jump_clicked,
 
161
                "on_entry_location_key_press_event": self.on_entry_location_key_press_event
 
162
            }
152
163
        
153
164
        # Connect the signals to the handlers
154
165
        self.toplevel.signal_autoconnect(dic)
200
211
            self.notbranch = True
201
212
        
202
213
        self.statusbar.push(self.context_id, path)
 
214
        self.entry_location.set_text(path)
 
215
        
 
216
        # If we're in the root, we cannot go up anymore
 
217
        if sys.platform == 'win32':
 
218
            drive, tail = os.path.splitdrive(self.path)
 
219
            if tail in ('', '/', '\\'):
 
220
                self.button_location_up.set_sensitive(False)
 
221
            else:
 
222
                self.button_location_up.set_sensitive(True)
 
223
        else:
 
224
            if self.path == '/':
 
225
                self.button_location_up.set_sensitive(False)
 
226
            else:
 
227
                self.button_location_up.set_sensitive(True)
203
228
 
204
229
    def get_path(self):
205
230
        return self.path
208
233
        from bzrlib.plugins.gtk.dialog import about
209
234
        about()
210
235
        
 
236
    def on_button_location_up_clicked(self, widget):
 
237
        """ Location Up button handler. """
 
238
        self.set_path(os.path.split(self.get_path())[0])
 
239
        self.refresh_right()
 
240
    
 
241
    def on_button_location_jump_clicked(self, widget):
 
242
        """ Location Jump button handler. """
 
243
        location = self.entry_location.get_text()
 
244
        if os.path.isdir(location):
 
245
            self.set_path(location)
 
246
            self.refresh_right()
 
247
            self.image_location_error.hide()
 
248
        else:
 
249
            self.image_location_error.show()
 
250
    
 
251
    def on_entry_location_key_press_event(self, widget, event):
 
252
        """ Key pressed handler for the location entry. """
 
253
        if event.keyval == 65293:
 
254
            # Return was hit, so we have to jump
 
255
            self.on_button_location_jump_clicked(widget)
 
256
    
211
257
    def on_menuitem_add_files_activate(self, widget):
212
258
        """ Add file(s)... menu handler. """
213
259
        from add import OliveAdd
576
622
        # Expand the tree
577
623
        self.treeview_left.expand_all()
578
624
 
579
 
    def _add_updir_to_dirlist(self, dirlist, curdir):
580
 
        """Add .. to the top of directories list if we not in root directory
581
 
 
582
 
        :param dirlist:     list of directories (modified in place)
583
 
        :param curdir:      current directory
584
 
        :return:            nothing
585
 
        """
586
 
        if curdir is None:
587
 
            curdir = self.path
588
 
 
589
 
        if sys.platform == 'win32':
590
 
            drive, tail = os.path.splitdrive(curdir)
591
 
            if tail in ('', '/', '\\'):
592
 
                return
593
 
        else:
594
 
            if curdir == '/':
595
 
                return
596
 
 
597
 
        # insert always as first element
598
 
        dirlist.insert(0, '..')
599
 
 
600
625
    def _load_right(self):
601
626
        """ Load data into the right panel. (Filelist) """
602
627
        # Create ListStore
603
 
        liststore = gtk.ListStore(str, str, str)
 
628
        # Model: [icon, dir, name, status text, status, size (int), size (human), mtime (int), mtime (local)]
 
629
        liststore = gtk.ListStore(str, gobject.TYPE_BOOLEAN, str, str, str, gobject.TYPE_INT, gobject.TYPE_STRING, gobject.TYPE_INT, gobject.TYPE_STRING)
604
630
        
605
631
        dirs = []
606
632
        files = []
614
640
                dirs.append(item)
615
641
            else:
616
642
                files.append(item)
617
 
            
618
 
        # Sort'em
619
 
        dirs.sort()
620
 
        files.sort()
621
 
 
622
 
        # add updir link to dirs
623
 
        self._add_updir_to_dirlist(dirs, self.path)
624
643
        
625
644
        if not self.notbranch:
626
645
            branch = self.wt.branch
629
648
            delta = self.wt.changes_from(tree2, want_unchanged=True)
630
649
        
631
650
        # Add'em to the ListStore
632
 
        for item in dirs:    
633
 
            liststore.append([gtk.STOCK_DIRECTORY, item, ''])
 
651
        for item in dirs:
 
652
            statinfo = os.stat(self.path + os.sep + item)
 
653
            liststore.append([gtk.STOCK_DIRECTORY, True, item, '', '', statinfo.st_size, self._format_size(statinfo.st_size), statinfo.st_mtime, self._format_date(statinfo.st_mtime)])
634
654
        for item in files:
635
655
            status = 'unknown'
636
656
            if not self.notbranch:
679
699
                st = _('ignored')
680
700
            else:
681
701
                st = _('unknown')
682
 
            liststore.append([gtk.STOCK_FILE, item, st])
 
702
            
 
703
            statinfo = os.stat(self.path + os.sep + item)
 
704
            liststore.append([gtk.STOCK_FILE, False, item, st, status, statinfo.st_size, self._format_size(statinfo.st_size), statinfo.st_mtime, self._format_date(statinfo.st_mtime)])
683
705
        
684
706
        # Create the columns and add them to the TreeView
685
707
        self.treeview_right.set_model(liststore)
686
708
        tvcolumn_filename = gtk.TreeViewColumn(_('Filename'))
687
709
        tvcolumn_status = gtk.TreeViewColumn(_('Status'))
 
710
        tvcolumn_size = gtk.TreeViewColumn(_('Size'))
 
711
        tvcolumn_mtime = gtk.TreeViewColumn(_('Last modified'))
688
712
        self.treeview_right.append_column(tvcolumn_filename)
689
713
        self.treeview_right.append_column(tvcolumn_status)
 
714
        self.treeview_right.append_column(tvcolumn_size)
 
715
        self.treeview_right.append_column(tvcolumn_mtime)
690
716
        
691
717
        # Set up the cells
692
718
        cellpb = gtk.CellRendererPixbuf()
694
720
        tvcolumn_filename.pack_start(cellpb, False)
695
721
        tvcolumn_filename.pack_start(cell, True)
696
722
        tvcolumn_filename.set_attributes(cellpb, stock_id=0)
697
 
        tvcolumn_filename.add_attribute(cell, 'text', 1)
 
723
        tvcolumn_filename.add_attribute(cell, 'text', 2)
698
724
        tvcolumn_status.pack_start(cell, True)
699
 
        tvcolumn_status.add_attribute(cell, 'text', 2)
 
725
        tvcolumn_status.add_attribute(cell, 'text', 3)
 
726
        tvcolumn_size.pack_start(cell, True)
 
727
        tvcolumn_size.add_attribute(cell, 'text', 6)
 
728
        tvcolumn_mtime.pack_start(cell, True)
 
729
        tvcolumn_mtime.add_attribute(cell, 'text', 8)
 
730
        
 
731
        # Set up the properties of the TreeView
 
732
        self.treeview_right.set_headers_visible(True)
 
733
        self.treeview_right.set_headers_clickable(True)
 
734
        self.treeview_right.set_search_column(1)
 
735
        tvcolumn_filename.set_resizable(True)
 
736
        # Set up sorting
 
737
        liststore.set_sort_func(13, self._sort_filelist_callback, None)
 
738
        liststore.set_sort_column_id(13, gtk.SORT_ASCENDING)
 
739
        tvcolumn_filename.set_sort_column_id(13)
 
740
        tvcolumn_status.set_sort_column_id(3)
 
741
        tvcolumn_size.set_sort_column_id(5)
 
742
        tvcolumn_mtime.set_sort_column_id(7)
700
743
        
701
744
        # Set sensitivity
702
745
        self.set_sensitivity()
709
752
        if iter is None:
710
753
            return None
711
754
        else:
712
 
            return model.get_value(iter, 1)
 
755
            return model.get_value(iter, 2)
713
756
    
714
757
    def get_selected_left(self):
715
758
        """ Get the selected bookmark. """
809
852
                dirs.append(item)
810
853
            else:
811
854
                files.append(item)
812
 
 
813
 
        # Sort'em
814
 
        dirs.sort()
815
 
        files.sort()
816
 
 
817
 
        # add updir link to dirs
818
 
        self._add_updir_to_dirlist(dirs, path)
819
 
 
 
855
        
820
856
        # Try to open the working tree
821
857
        notbranch = False
822
858
        try:
832
868
            
833
869
        # Add'em to the ListStore
834
870
        for item in dirs:
835
 
            liststore.append([gtk.STOCK_DIRECTORY, item, ''])
 
871
            statinfo = os.stat(self.path + os.sep + item)
 
872
            liststore.append([gtk.STOCK_DIRECTORY, True, item, '', '', statinfo.st_size, self._format_size(statinfo.st_size), statinfo.st_mtime, self._format_date(statinfo.st_mtime)])
836
873
        for item in files:
837
874
            status = 'unknown'
838
875
            if not notbranch:
881
918
                st = _('ignored')
882
919
            else:
883
920
                st = _('unknown')
884
 
            liststore.append([gtk.STOCK_FILE, item, st])
 
921
            
 
922
            statinfo = os.stat(self.path + os.sep + item)
 
923
            liststore.append([gtk.STOCK_FILE, False, item, st, status, statinfo.st_size, self._format_size(statinfo.st_size), statinfo.st_mtime, self._format_date(statinfo.st_mtime)])
885
924
 
886
925
        # Add the ListStore to the TreeView
887
926
        self.treeview_right.set_model(liststore)
 
927
        self.treeview_right.columns_autosize()
888
928
        
889
929
        # Set sensitivity
890
930
        self.set_sensitivity()
935
975
            changes = True
936
976
        
937
977
        return changes
 
978
    
 
979
    def _sort_filelist_callback(self, model, iter1, iter2, data):
 
980
        """ The sort callback for the file list, return values:
 
981
        -1: iter1 < iter2
 
982
        0: iter1 = iter2
 
983
        1: iter1 > iter2
 
984
        """
 
985
        name1 = model.get_value(iter1, 2)
 
986
        name2 = model.get_value(iter2, 2)
 
987
        
 
988
        if model.get_value(iter1, 1):
 
989
            # item1 is a directory
 
990
            if not model.get_value(iter2, 1):
 
991
                # item2 isn't
 
992
                return -1
 
993
            else:
 
994
                # both of them are directories, we compare their names
 
995
                if name1 < name2:
 
996
                    return -1
 
997
                elif name1 == name2:
 
998
                    return 0
 
999
                else:
 
1000
                    return 1
 
1001
        else:
 
1002
            # item1 is not a directory
 
1003
            if model.get_value(iter2, 1):
 
1004
                # item2 is
 
1005
                return 1
 
1006
            else:
 
1007
                # both of them are files, compare them
 
1008
                if name1 < name2:
 
1009
                    return -1
 
1010
                elif name1 == name2:
 
1011
                    return 0
 
1012
                else:
 
1013
                    return 1
 
1014
    
 
1015
    def _format_size(self, size):
 
1016
        """ Format size to a human readable format. """
 
1017
        return size
 
1018
    
 
1019
    def _format_date(self, timestamp):
 
1020
        """ Format the time (given in secs) to a human readable format. """
 
1021
        return time.ctime(timestamp)
938
1022
 
939
1023
import ConfigParser
940
1024