Switching category over to a touch selector
[multilist] / src / libselection.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 This file is part of Multilist.
6
7 Multilist is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Multilist is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Multilist.  If not, see <http://www.gnu.org/licenses/>.
19
20 Copyright (C) 2008 Christoph Würstle
21 """
22
23
24 import logging
25
26 import gobject
27 import gtk
28
29 import gtk_toolbox
30 import hildonize
31
32 try:
33         _
34 except NameError:
35         _ = lambda x: x
36
37
38 _moduleLogger = logging.getLogger(__name__)
39
40
41 class Selection(gtk.HBox):
42
43         __gsignals__ = {
44                 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING)),
45                 #'changedCategory': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING))
46         }
47
48         def __init__(self, db, isHildon):
49                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
50
51                 self.db = db
52                 self.isHildon = isHildon
53
54                 _moduleLogger.info("libSelection, init")
55
56                 label = gtk.Label(_("List:"))
57                 self.pack_start(label, expand = False, fill = True, padding = 0)
58
59                 self.comboList = gtk.combo_box_entry_new_text()
60                 self.comboList.set_size_request(180, -1)
61                 self.pack_start(self.comboList, expand = False, fill = True, padding = 0)
62
63                 label = gtk.Label(_("  Category:"))
64                 self.pack_start(label, expand = False, fill = True, padding = 0)
65
66                 self.__categories = []
67                 self.__categoryButton = gtk.Button("")
68                 self.__categoryButton.connect("clicked", self._on_category_selector)
69                 self.pack_start(self.__categoryButton, expand = False, fill = True, padding = 0)
70
71                 self.comboList.connect("changed", self.comboList_changed, None)
72
73         def load(self):
74                 model = self.comboList.get_model()
75                 model.clear()
76                 #self.comboList.remove(0)
77
78                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
79                 rows = self.db.ladeSQL(sql)
80                 if ((rows is not None)and(len(rows)>0)):
81                         for row in rows:
82                                 self.comboList.append_text(row[0])
83                 else:
84                         self.comboList.append_text("default")
85
86                 s = self.db.ladeDirekt("comboListText")
87                 if s != "":
88                         self.comboList.get_child().set_text(s)
89                 else:
90                         self.comboList.set_active(0)
91
92         @gtk_toolbox.log_exception(_moduleLogger)
93         def _on_category_selector(self, *args):
94                 window = gtk_toolbox.find_parent_window(self)
95                 userSelection = hildonize.touch_selector_entry(
96                         window,
97                         "Categories",
98                         self.__categories,
99                         self.get_category(),
100                 )
101                 self.set_category(userSelection)
102                 self.emit("changed", "category", "")
103                 self.db.speichereDirekt("comboCategoryText"+self.comboList.get_child().get_text(), self.__categoryButton.get_label())
104                 self._update_categories()
105
106         @gtk_toolbox.log_exception(_moduleLogger)
107         def comboList_changed(self, widget = None, data = None):
108                 self._update_categories()
109
110                 s = self.db.ladeDirekt("comboCategoryText"+self.comboList.get_child().get_text())
111                 if len(s)>0:
112                         self.__categoryButton.set_label(s)
113                 else:
114                         self.__categoryButton.set_label(self.__categories[0])
115
116                 self.emit("changed", "list", "")
117                 self.db.speichereDirekt("comboListText", self.comboList.get_child().get_text())
118
119         def _update_categories(self):
120                 del self.__categories[:]
121
122                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
123                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
124
125                 self.__categories.append(_("all"))
126                 if ((rows is not None)and(len(rows)>0)):
127                         for row in rows:
128                                 if (row[0] != _("all")):
129                                         self.__categories.append(row[0])
130
131         def comboLists_check_for_update(self):
132                 if self.comboList.get_active() == -1:
133                         model = self.comboList.get_model()
134                         found = False
135                         list = self.get_list()
136                         for x in model:
137                                 if x[0] == list:
138                                         found = True
139                         if found == False:
140                                 self.comboList.append_text(self.get_list())
141                                 self.comboList.set_active(len(self.comboList.get_model())-1)
142
143         def lade(self):
144                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
145
146         def speichere(self):
147                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
148
149         def getIsHildon(self):
150                 return self.isHildon
151
152         def get_category(self, select = False):
153                 s = self.__categoryButton.get_label()
154                 if s == _("all"):
155                         if not select:
156                                 return "undefined"
157                         else:
158                                 return "%"
159                 else:
160                         return s
161
162         def set_category(self, category):
163                 self.__categoryButton.set_label(category)
164
165         def set_list(self, listname):
166                 self.comboList.get_child().set_text(listname)
167
168         def get_list(self):
169                 return self.comboList.get_child().get_text()