summaryrefslogtreecommitdiffstats
path: root/ui/ui_pygtk.py
blob: 7f257f1f8f309454f22303979fead0821e2a6460 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
# date: 17 Apr 2009

import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import gobject
import item
import time
import inspect
import os
from license import version, gpl_3

class ui_pygtk:
    def __init__(self, itemList):
        self.itemList = itemList
        
        # load ui from glade file
        c=inspect.currentframe()
        glade = gtk.glade.XML (os.path.dirname(c.f_code.co_filename) + "/todolist.glade")
        self.main_window = glade.get_widget('main_window')
        self.main_window.todolist = glade.get_widget('todolist')

        self.edit_dialog = glade.get_widget('edit_dialog')
        self.edit_dialog.title_field = glade.get_widget('title_field')
        self.edit_dialog.priority_field = glade.get_widget('priority_field')
        self.edit_dialog.due_field = glade.get_widget('due_field')
        self.edit_dialog.desc_field = glade.get_widget('description_field')

        self.date_select_dialog = glade.get_widget('date_select_dialog')
        self.date_select_dialog.calendar = glade.get_widget('calendar')
        self.date_select_dialog.hour_field = glade.get_widget('hour_field')
        self.date_select_dialog.minute_field = glade.get_widget('minute_field')
        
        # connect the defined signales
        dic = {"on_menu_about_clicked"          : self.about_clicked_callback,
               "on_menu_quit_clicked"           : gtk.main_quit,
               "on_main_window_destroy"         : gtk.main_quit,
               "on_treeview_row_activated"      : self.item_double_clicked_callback,
               "on_edit_clicked"                : self.edit_clicked_callback,
               "on_remove_clicked"              : self.remove_clicked_callback,
               "on_add_clicked"                 : self.add_clicked_callback,
               "on_select_date_clicked"         : self.select_date_clicked_callback
               }
        glade.signal_autoconnect(dic)

        # fill the treeview with data
        liststore = gtk.ListStore(object)
        for items in self.itemList:
            liststore.append([items])

        self.main_window.todolist.set_model(liststore)
        self.main_window.todolist.append_column(self.create_column(0, 'Finished', 'getCompleted', liststore, gtk.CellRendererToggle()))
        # magic column
        # self.main_window.todolist.append_column(self.create_column(1, 'Todo', 'getCompleted', liststore, gtk.CellRendererText()))
        for column in ([['Id', 'getId'],
                        ['ToDo', 'getTitle'],
                        ['...', 'getDescription'],
                        ['Erstellt am', ['getCreatedAt',
                         (lambda x: time.strftime('%d.%m.%Y %H:%M:%S', time.localtime(x)))]]
                        ]):
            self.main_window.todolist.append_column(self.create_column(len(self.main_window.todolist.get_columns())+1, column[0], column[1], liststore))
        
        # start ui
        self.main_window.show_all()
        gtk.main()
        return

    def create_column(self, id, title, data, treemodel, cellRenderer=gtk.CellRendererText()):
        # abstract mmethod to create a column for the data
        # from the item objects in the treeview 
        column = gtk.TreeViewColumn(title, cellRenderer)
        
        column.set_cell_data_func(cellRenderer, self.item_data_callback, data)
        column.set_reorderable(True)
        column.set_resizable(True)
        column.set_sort_column_id(id)

        treemodel.set_sort_func(id, (lambda model, iter1, iter2, userdata=None:
                                     apply(cmp,
                                            map((lambda x: self.item_data_callback(column, None, model, x, data)),
                                                [iter1, iter2]))))
        return column

    #########################################################
    # callbacks
    #########################################################

    def item_data_callback(self, column, cell, model, iter, userdata=None):
        # method to get the data from the item object for one column
        data=''
        if (userdata == None):
            # try to convert the object to string
            data =  model.get_value(iter, 0)
        else:
            if type(userdata) is type([]):
                data = getattr(model.get_value(iter, 0), userdata[0])()

                # modify data with the given functions
                for i in xrange (1, len(userdata)):
                    data = userdata[i](data)
            else:
                data = getattr(model.get_value(iter, 0), userdata)()

        # set the data as cell content
        if isinstance(cell, gtk.CellRendererText) :
            cell.set_property('text', data)
        elif isinstance(cell, gtk.CellRendererToggle) :
            cell.set_property('active', data)
        else:
            return data

    def about_clicked_callback(self, widget, data=None):
        # show the about dialog
        about_window = gtk.AboutDialog()
        about_window.set_name('Todolist')
        about_window.set_version(version)
        about_window.set_copyright('2009 Alexander Sulfrian')
        about_window.set_license(gpl_3)
        about_window.set_website('http://git.animux.de/todolist.git')
        about_window.run()
        about_window.hide()
        return

    def edit_clicked_callback(self, widget, data=None):
        # edit btn click
        treeview = self.main_window.todolist
        path = treeview.get_model().get_path(treeview.get_selection().get_selected()[1])
        self.item_double_clicked_callback(treeview, path, 0)

    def item_double_clicked_callback(self, treeview, path, view_column, user_data=None):
        # dbl click on treeview row to edit item
        model = treeview.get_model() 
        item = model.get_value(model.get_iter(path), 0)

        if self.edit_item(item):
            treeview.get_model().row_changed(path, model.get_iter(path))
            sort = list(model.get_sort_column_id())
            if sort[0]:
                model.set_sort_column_id(0, gtk.SORT_ASCENDING)
                apply(model.set_sort_column_id, sort)
        return

    def remove_clicked_callback(self, widget, data=None):
        # remove the selected element
        treeview = self.main_window.todolist
        if (treeview.get_selection().count_selected_rows() == 1):
            iter = treeview.get_selection().get_selected()[1]
            path = treeview.get_model().get_path(iter)
            
            item = treeview.get_model().get_value(iter, 0)
            self.main_window.todolist.get_model().remove(iter)
            self.itemList.remove(item)
            treeview.get_model().row_deleted(path)
        return

    def add_clicked_callback(self, widget, data=None):
        # add a new element
        new_item = item.item()
        if self.edit_item(new_item):
            iter = self.main_window.todolist.get_model().append([new_item])
            self.itemList.append(new_item)
            self.main_window.todolist.get_model().row_inserted(self.main_window.todolist.get_model().get_path(iter), iter)
        return

    def select_date_clicked_callback(self, widget, data=None):
        # display the dialog to select a date after clicked on the
        # button in the edit dialog
        try:
            date = time.strptime(self.edit_dialog.due_field.get_text(), '%d.%m.%Y %H:%M')
        except:
            date = time.localtime()
            
        self.date_select_dialog.calendar.select_month(date[1] - 1, date[0])
        self.date_select_dialog.calendar.select_day(date[2])
        self.date_select_dialog.hour_field.set_value(date[3])
        self.date_select_dialog.minute_field.set_value(date[4])
        
        if self.date_select_dialog.run():
            date = self.date_select_dialog.calendar.get_date() 
            date = (date[0], date[1] + 1, date[2],
                    int(self.date_select_dialog.hour_field.get_value()),
                    int(self.date_select_dialog.minute_field.get_value()),
                    0, 0, 0, 0)
            
            self.edit_dialog.due_field.set_text(time.strftime(
                '%d.%m.%Y %H:%M', date))
        self.date_select_dialog.hide()
        return
    
    #########################################################
    # helper methods
    #########################################################

    def edit_item(self, item):
        # helper method to edit item
        self.edit_dialog.title_field.set_text(item.getTitle())
        self.edit_dialog.priority_field.set_text(item.getPriority().__str__())

        if item.getDue() != -1:
            self.edit_dialog.due_field.set_text(time.strftime('%d.%m.%Y %H:%M', time.localtime(item.getDue())))
        else:
            self.edit_dialog.due_field.set_text('')
        
        text_buffer = gtk.TextBuffer()
        if item.getDescription() != None:
            text_buffer.set_text(item.getDescription())
        self.edit_dialog.desc_field.set_buffer(text_buffer)
                
        successfull = self.edit_dialog.run()

        if (successfull):                
            item.title = self.edit_dialog.title_field.get_text()
            item.priority = self.edit_dialog.priority_field.get_text()
            item.description = text_buffer.get_text(text_buffer.get_start_iter(), text_buffer.get_end_iter())

            try:
                item.due = time.strftime('%s', time.strptime(self.edit_dialog.due_field.get_text(), '%d.%m.%Y %H:%M'))
            except:
                item.due = -1


        self.edit_dialog.hide()
        return successfull