paco_projects Paco's Projects
The little Paco's projects joined in a single project

 

Browse the code

Differences between 1 and 2 on /.
Number of edited files: 1 (0 added, 0 deleted and 1 modified)
Author: Paco
Log message: Add a few options, and fixing bugs
Date: 2011-07-17 14:08:54

Added file(s) Deleted file(s) Modified file(s)

 

Old New Code
1  
#!/usr/bin/python
2 1
# -*- coding: utf-8 -*-
3 2

                                        
4 3
#  Copyright (C) 2010  Ribémont François e-mail address :
19 18
#  You should have received a copy of the GNU General  Public License
20 19
#  along with SimpleSheet. If not, see <http://www.gnu.org/licenses/>.
21 20

                                        
22  
from sys import exit
  21
import gtk
23 22

                                        
24  
from gtk import ListStore, TreeViewColumn
25  
from gtk import CellRendererText, TreeView, Window, WINDOW_TOPLEVEL
26  
from gtk import main_quit, main
27 23

                                        
28 24

                                        
29  
class SimpleSheet(TreeView):
  25
class SimpleSheet(gtk.TreeView):
30 26
    """A pseudo-widget that you permit to create a sheet easily
31 27
    With the creation of a SimpleSheet, you have a widget who configure a Sheet
32 28
    for you. But if you want, you can modify the different attribute :
59 55
        self.cell_text_list = []
60 56
        self.number_columns = len(columns_names)
61 57
        self.number_lines = len(lines_names)
  58
        self.has_lines_names = lines_names != ()
62 59

                                        
63  
        self.cell_text_list.append(CellRendererText())
64  
        for i in range(1, self.number_columns+1):
65  
            self.cell_text_list.append(CellRendererText())
66  
            # We allow to edit the cases of the cell_text
67  
            self.cell_text_list[i].set_property('editable', True)
  60
        self.cell_text_list.append(gtk.CellRendererText())
68 61

                                        
  62
        if self.has_lines_names:
  63
            for i in range(1, self.number_columns+1):
  64
                self.cell_text_list.append(gtk.CellRendererText())
  65
                # We allow to edit the cases of the cell_text
  66
                self.cell_text_list[i].set_property('editable', True)
  67
        else:
  68
            for i in range(0, self.number_columns+1):
  69
                self.cell_text_list.append(gtk.CellRendererText())
  70
                # We allow to edit the cases of the cell_text
  71
                self.cell_text_list[i].set_property('editable', True)
  72

                                    
  73

                                    
69 74
        tuples_strings = ()
70 75

                                        
71  
        # We modify the background color
72  
        #of the CellRenderer of the first column
73  
        self.cell_text_list[0].set_property('cell-background', color)
  76
        if self.has_lines_names:
  77
            # We modify the background color
  78
            #of the CellRenderer of the first column
  79
            self.cell_text_list[0].set_property('cell-background', color)
74 80

                                        
75  
        self.columns_list.append(TreeViewColumn('', self.cell_text_list[0],
  81
        if lines_names != ():
  82
            self.columns_list.append(gtk.TreeViewColumn('',
  83
                                                        self.cell_text_list[0],
76 84
            text=0))
  85

                                    
  86

                                    
  87

                                    
77 88
        for i in range(self.number_columns):
78  
            self.columns_list.append(TreeViewColumn(columns_names[i],
79  
                    self.cell_text_list[i+1], text=i+1))
  89
            if self.has_lines_names:
  90
                self.columns_list.append(gtk.TreeViewColumn(columns_names[i],
  91
                        self.cell_text_list[i+1], text=i+1))
  92
            else:
  93
                self.columns_list.append(gtk.TreeViewColumn(columns_names[i],
  94
                        self.cell_text_list[i], text=i))
80 95

                                        
81 96
            # tuples_strings is under the form : (str, str, str, ...)
82 97
            # It used for the creation of the ListStore
83 98
            tuples_strings = tuples_strings + (str, )
84 99

                                        
85  
        # For the column without name
86  
        tuples_strings = tuples_strings + (str, )
  100
        if self.has_lines_names:
  101
            # For the column without name
  102
            tuples_strings = tuples_strings + (str, )
87 103

                                        
88 104

                                        
89  
        self.liststore = ListStore(*tuples_strings)
  105
        self.liststore = gtk.ListStore(*tuples_strings)
90 106

                                        
91  
        TreeView.__init__(self, self.liststore)
92  
        # We do +1 because there is an other column which contains no title
93  
        for i in range(self.number_columns+1):
94  
            self.append_column(self.columns_list[i])
  107
        gtk.TreeView.__init__(self, self.liststore)
95 108

                                        
  109
        if self.has_lines_names:
  110
            # We do +1 because there is an other column which contains no title
  111
            for i in range(self.number_columns+1):
  112
                self.append_column(self.columns_list[i])
  113
        else:
  114
            for i in range(self.number_columns):
  115
                self.append_column(self.columns_list[i])
  116

                                    
  117

                                    
96 118
        for column in self.columns_list:
97 119
            column.set_resizable(True)
98 120

                                        
108 130

                                        
109 131
            self.iter.append(self.liststore.append(default_value_sheet))
110 132

                                        
  133
    def add_line(self, name=''):
  134
        ''' add an empty line to the sheet at the end
  135
           Parameters :
  136
            name  : a string that represents the name of the line '''
  137
        if not self.has_lines_names and name != '':
  138
            raise NameError('Name shouldn\'t be given, because the sheet has '
  139
                            'no name for the lines')
  140
        if not self.has_lines_names:
  141
            default_value_sheet = ()
  142
            # We create the different cases whith no text
  143
            # So args_empty_string contains an empty line
  144
            for i in range(self.number_columns):
  145
                default_value_sheet += ('', )
  146

                                    
  147
            self.iter.append(self.liststore.append(default_value_sheet))
  148

                                    
111 149
    def connect(self, detailed_signal, handler, kwargs={}):
112  
        for i in range(1, self.number_columns+1):
113  
            self.cell_text_list[i].connect(detailed_signal, self.cell_edition,
114  
                                           i, handler, kwargs)
  150
        if self.has_lines_names:
  151
            for i in range(1, self.number_columns+1):
  152
                self.cell_text_list[i].connect(detailed_signal, self.cell_edition,
  153
                                               i, handler, kwargs)
  154
        else:
  155
            for i in range(0, self.number_columns+1):
  156
                self.cell_text_list[i].connect(detailed_signal, self.cell_edition,
  157
                                               i, handler, kwargs)
115 158

                                        
116 159

                                        
117 160
    def cell_edition(self, cellrenderer, path, new_text, column_cell,
158 201
        # We have to return the value, is it a good way ?
159 202
        return float(param)
160 203

                                        
161  
    a = SimpleSheet(('column 0', 'column 1', 'column 2', 'column 3'),
162  
                    ['Line 1', 'Line2'], 'blue',)
  204
    a = SimpleSheet(('column 0', 'column 1', 'column 2', 'column 3'))
163 205
    a.connect('edited', test)
  206
    a.add_line()
164 207

                                        
165  
    fenetre = Window(WINDOW_TOPLEVEL)
166  
    fenetre.connect('delete_event', main_quit)
  208
    fenetre = gtk.Window(gtk.WINDOW_TOPLEVEL)
  209
    fenetre.connect('delete_event', gtk.main_quit)
167 210
    fenetre.add(a)
168 211
    fenetre.show_all()
169  
    main()
  212
    gtk.main()
170 213