From 570ca6350777acc69429300bea4cc8f8eb97267e Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 23 Apr 2009 06:02:37 +0200 Subject: gui extended, database layout 0.2 sortable and reorderable listview in gui db-version 0.2 with description field (BLOB --- item.py | 8 +++- storage/sqlite.py | 23 +++++++--- ui/ui_pygtk.py | 134 +++++++++++++++++++++++++++++++++++------------------- 3 files changed, 110 insertions(+), 55 deletions(-) diff --git a/item.py b/item.py index c39a1ea..b15fb8e 100644 --- a/item.py +++ b/item.py @@ -3,14 +3,15 @@ class item: readOnly = ['id'] - def __init__(self, title=None, created=None, priority=None, row=None): + def __init__(self, title=None, created=None, priority=None, description=None, row=None): if row == None: self.__dict__['id'] = -1 self.title = title self.created = created self.priority = priority + self.description = description else: - self.__init__(row[1], row[2], row[3]) + self.__init__(row[1], row[2], row[3], row[4]) self.__dict__['id'] = row[0] def setId(self, id): @@ -28,6 +29,9 @@ class item: def getPriority(self): return self.priority + + def getDescription(self): + return self.description def __setattr__(self, name, value): if name not in item.readOnly: diff --git a/storage/sqlite.py b/storage/sqlite.py index 5b21054..09e55fc 100644 --- a/storage/sqlite.py +++ b/storage/sqlite.py @@ -6,7 +6,7 @@ from item import item from pysqlite2 import dbapi2 as sqliteBackend class sqlite(storageBase): - dbVersion = '0.1' + dbVersion = '0.2' def __init__(self): self.con = sqliteBackend.connect(self.getConfigDir() + '/data.sqlite') @@ -22,7 +22,7 @@ class sqlite(storageBase): version = self.cur.execute("select value from control where setting='db-version'").fetchone() if (sqlite.dbVersion,) != version: # TODO: update not init - self.init_db() + self.update_db(version) @@ -47,6 +47,17 @@ class sqlite(storageBase): self.cur.execute("insert into control (setting, value) VALUES ('db-version', ?)", (sqlite.dbVersion,)) self.con.commit() + def update_db(self, updateFrom): + print 'updating todo table...' + if updateFrom == '0.1': + self.cur.execute('''alter table todo + add desc BLOB''') + self.con.commit() + updateFrom = '0.2' + + self.cur.execute("update control set value = ? where setting = 'db-version'", (sqlite.dbVersion,)) + self.con.commit() + def __del__(self): self.con.close() @@ -60,11 +71,11 @@ class sqlite(storageBase): def notifyChange(self, sender): if sender.getId() >= 0: - self.cur.execute('update todo set title=?, createdAt=?, priority=? where id=?', - (sender.getTitle(), sender.getCreatedAt(), sender.getPriority(), sender.getId())) + self.cur.execute('update todo set title=?, createdAt=?, priority=?, desc=? where id=?', + (sender.getTitle(), sender.getCreatedAt(), sender.getPriority(), sender.getId(), sender.getDescription())) self.con.commit() else: - self.cur.execute('insert into todo (title, createdAt, priority) VALUES (?, ?, ?)', - (sender.getTitle(), sender.getCreatedAt(), sender.getPriority())) + self.cur.execute('insert into todo (title, createdAt, priority, desc) VALUES (?, ?, ?, ?)', + (sender.getTitle(), sender.getCreatedAt(), sender.getPriority(), sender.getDescription())) self.con.commit() sender.setId(self.cur.execute('select last_insert_rowid()').fetchone()[0]) diff --git a/ui/ui_pygtk.py b/ui/ui_pygtk.py index 4dd4b53..ad16173 100644 --- a/ui/ui_pygtk.py +++ b/ui/ui_pygtk.py @@ -5,18 +5,58 @@ 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 - self.get_main_window(100, 100, 10, 10) + # 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.get_main_window().show_all() + self.main_window.show_all() gtk.main() return @@ -24,28 +64,20 @@ class ui_pygtk: # create gui ######################################################### - def get_main_window(self, initial_width=None, initial_height=None, - initial_pos_x=None, initial_pos_y=None): - if 'main_window' not in self.__dict__: - 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) # very small minimal size - self.main_window.resize(initial_width, initial_height) - self.main_window.move(initial_pos_x, initial_pos_y) - - self.main_window.add(self.get_main_content_box()) - return self.main_window - - def get_main_content_box(self): - if 'main_content_box' not in self.__dict__: - self.main_content_box = gtk.VBox(False, 1) - self.main_content_box.pack_start(self.get_main_menu(), False, True, 0) - self.main_content_box.pack_start(self.get_todo_list(), True, True, 0) - self.main_content_box.pack_start(self.get_statusbar(), False, False, 0) - return self.main_content_box - + 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) @@ -66,17 +98,17 @@ class ui_pygtk: edit_action = gtk.Action('Edit', '_Edit', 'Edit', None) help_action = gtk.Action('Help', '_Help', 'Help', None) - self.action_group = gtk.ActionGroup('MainMenu') - self.action_group.add_action_with_accel(quit_action, None) # None = default - self.action_group.add_action(settings_action) - self.action_group.add_action(file_action) - self.action_group.add_action(edit_action) - self.action_group.add_action(help_action) - self.action_group.add_action(about_action) + 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(self.action_group, 0) + uimanager.insert_action_group(action_group, 0) uimanager.add_ui_from_string(''' @@ -98,24 +130,32 @@ class ui_pygtk: self.main_menu = uimanager.get_widget('/MainMenu') return self.main_menu - - def get_todo_list(self): - if 'todo_list' not in self.__dict__: - self.todo_list = gtk.ScrolledWindow() - self.todo_list.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) - - return self.todo_list - - def get_statusbar(self): - if 'statusbar' not in self.__dict__: - self.statusbar = gtk.Statusbar() - self.statusbar.show() - return self.statusbar ######################################################### # 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 -- cgit v1.2.3