# -*- 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(''' ''') 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