summaryrefslogtreecommitdiffstats
path: root/ui/ui_pygtk.py
blob: ad1617318aeea858f13aa5e568d8a25aba726bd7 (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
# -*- coding: utf-8 -*-
# date: 17 Apr 2009

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

class ui_pygtk:
    def __init__(self, itemList):
        self.itemList = itemList
        
        # create ui
        # main window
        self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.main_window.connect('destroy', self.destroy_callback)
	
        self.main_window.set_title('ToDo')
        self.main_window.set_size_request(10, 10)
        self.main_window.resize(100, 100)
        self.main_window.move(10, 10)

        # todolist scroll window
        todo_scroll = gtk.ScrolledWindow()
        todo_scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

        liststore = gtk.ListStore(object)
        for items in self.itemList:
            liststore.append([items])

        todolist = gtk.TreeView(liststore)

        for i, column in enumerate ([['Id', 'getId'],
                                     ['ToDo', 'getTitle'],
                                     ['...', 'getDescription'],
                                     ['Erstellt am', ['getCreatedAt',
                                       (lambda x: time.strftime('%d.%m.%Y %H:%M:%S', time.localtime(x)))]]
                                     ]):
            todolist.append_column(self.create_column(i, column[0], column[1], liststore))
        
        todo_scroll.add(todolist)
        
        # statusbar
        statusbar = gtk.Statusbar()

        # join elemnts 
        # main content box
        main_content_box = gtk.VBox(False, 1)
        main_content_box.pack_start(self.get_main_menu(), False, True, 0)
        main_content_box.pack_start(todo_scroll, True, True, 0)
        main_content_box.pack_start(statusbar, False, False, 0)

        self.main_window.add(main_content_box)

        # start ui
        self.main_window.show_all()
        gtk.main()
        return

    #########################################################
    # create gui
    #########################################################

    def create_column(self, id, title, data, treemodel, cellRenderer=gtk.CellRendererText()):
        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

    def get_main_menu(self):
        if 'mainmenu' not in self.__dict__:
            quit_action = gtk.Action('Quit', '_Quit', 'Exit Todolist', gtk.STOCK_QUIT)
            quit_action.connect('activate', self.destroy_callback)

            settings_action = gtk.Action('Settings', '_Settings', 'Settings', gtk.STOCK_PREFERENCES)
            settings_action.set_property('sensitive', False)
            #settings_action.connect('activate', self.show_settings)

            #delete_action = gtk.Action('Delete', '_Delete', 'Delete a post',
            #        gtk.STOCK_DELETE)
            #delete_action.connect('activate', self.delete_tweet)

            about_action = gtk.Action('About', '_About', 'About Todolist', gtk.STOCK_ABOUT)
            about_action.connect('activate', self.about_click_callback)

            file_action = gtk.Action('File', '_File', 'File', None)
            edit_action = gtk.Action('Edit', '_Edit', 'Edit', None)
            help_action = gtk.Action('Help', '_Help', 'Help', None)

            action_group = gtk.ActionGroup('MainMenu')
            action_group.add_action_with_accel(quit_action, None) # None = default
            action_group.add_action(settings_action)
            action_group.add_action(file_action)
            action_group.add_action(edit_action)
            action_group.add_action(help_action)
            action_group.add_action(about_action)
        
            # definition of the UI
            uimanager = gtk.UIManager()
            uimanager.insert_action_group(action_group, 0)
            uimanager.add_ui_from_string('''
            <ui>
                <menubar name="MainMenu">
                    <menu action="File">
                        <menuitem action="Quit" />
                    </menu>
                    <menu action="Edit">
                        <separator />
                        <menuitem action="Settings" />
                    </menu>
                    <menu action="Help">
                        <menuitem action="About" />
                    </menu>
                </menubar>
            </ui>
            ''')
            
            self.main_window.add_accel_group(uimanager.get_accel_group())
            self.main_menu = uimanager.get_widget('/MainMenu')
            
        return self.main_menu
        
    #########################################################
    # callbacks
    #########################################################

    def item_data_callback(self, column, cell, model, iter, userdata=None):
        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 cell is not None:
            cell.set_property('text', data)
        else:
            return data

    def destroy_callback(self, widget, data=None):
        gtk.main_quit()
        return

    def about_click_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