08e147a857bf08bf2c727cd4b15c2d4faa21001a
[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 gobject
25 import logging
26
27 import gtk
28
29 try:
30         _
31 except NameError:
32         _ = lambda x: x
33
34
35 _moduleLogger = logging.getLogger(__name__)
36
37
38 class Selection(gtk.HBox):
39
40         __gsignals__ = {
41                 'changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING)),
42                 #'changedCategory': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_STRING))
43         }
44
45         def load(self):
46                 model = self.comboList.get_model()
47                 model.clear()
48                 #self.comboList.remove(0)
49
50                 sql = "SELECT DISTINCT list FROM items ORDER BY list"
51                 rows = self.db.ladeSQL(sql)
52                 if ((rows is not None)and(len(rows)>0)):
53                         for row in rows:
54                                 self.comboList.append_text(row[0])
55                 else:
56                         self.comboList.append_text("default")
57
58                 s = self.db.ladeDirekt("comboListText")
59                 if s != "":
60                         self.comboList.get_child().set_text(s)
61                 else:
62                         self.comboList.set_active(0)
63
64         def comboList_changed(self, widget = None, data = None):
65                 #self.comboCategory.set_model(None)
66                 #print "reload categories"
67                 while len(self.comboCategory.get_model())>0:
68                         self.comboCategory.remove_text(0)
69
70                 sql = "SELECT DISTINCT category FROM items WHERE list = ? ORDER BY category"
71                 rows = self.db.ladeSQL(sql, (self.get_list(), ))
72
73                 self.comboCategory.append_text(_("all"))
74                 if ((rows is not None)and(len(rows)>0)):
75                         for row in rows:
76                                 if (row[0] != _("all")):
77                                         self.comboCategory.append_text(row[0])
78
79                 s = self.db.ladeDirekt("comboCategoryText"+self.comboList.get_child().get_text())
80                 if len(s)>0:
81                         self.comboCategory.get_child().set_text(s)
82                 else:
83                         self.comboCategory.set_active(0)
84
85                 self.emit("changed", "list", "")
86                 self.db.speichereDirekt("comboListText", self.comboList.get_child().get_text())
87
88         def comboCategory_changed(self, widget = None, data = None):
89                 #_moduleLogger.info("Klasse geaendert zu ")
90                 #self.hauptRegister.set_current_page(0)
91                 self.emit("changed", "category", "")
92                 if self.comboCategory.get_active()>-1:
93                         self.db.speichereDirekt("comboCategoryText"+self.comboList.get_child().get_text(), self.comboCategory.get_child().get_text())
94
95         def radioActive_changed(self, widget, data = None):
96                 self.emit("changed", "radio", "")
97
98         def comboLists_check_for_update(self):
99                 if self.comboCategory.get_active() == -1:
100                         model = self.comboCategory.get_model()
101                         found = False
102                         cat = self.get_category()
103                         for x in model:
104                                 if x[0] == cat:
105                                         found = True
106                         if found == False:
107                                 self.comboCategory.append_text(self.get_category())
108                                 self.comboCategory.set_active(len(self.comboCategory.get_model())-1)
109                 if self.comboList.get_active() == -1:
110                         model = self.comboList.get_model()
111                         found = False
112                         list = self.get_list()
113                         for x in model:
114                                 if x[0] == list:
115                                         found = True
116                         if found == False:
117                                 self.comboList.append_text(self.get_list())
118                                 self.comboList.set_active(len(self.comboList.get_model())-1)
119
120         def lade(self):
121                 _moduleLogger.warning("Laden der aktuellen position noch nicht implementiert")
122
123         def speichere(self):
124                 _moduleLogger.warning("Speichern der aktuellen position noch nicht implementiert")
125
126         def getIsHildon(self):
127                 return self.isHildon
128
129         def get_category(self, select = False):
130                 s = self.comboCategory.get_child().get_text()
131                 if s == _("all"):
132                         if (select == False):
133                                 return "undefined"
134                         else:
135                                 return "%"
136                 else:
137                         return s
138         def set_category(self, category):
139                 self.comboCategory.get_child().set_text(category)
140
141         def set_list(self, listname):
142                 self.comboList.get_child().set_text(listname)
143
144         def get_list(self):
145                 return self.comboList.get_child().get_text()
146
147         def get_status(self):
148                 #return self.comboCategory.get_child().get_text()
149                 if self.radio_all.get_active() == True:
150                         return "-1"
151                 else:
152                         return "0"
153
154         def __init__(self, db, isHildon):
155                 gtk.HBox.__init__(self, homogeneous = False, spacing = 3)
156
157                 self.db = db
158                 self.isHildon = isHildon
159
160                 _moduleLogger.info("libSelection, init")
161
162                 label = gtk.Label(_("List:"))
163                 self.pack_start(label, expand = False, fill = True, padding = 0)
164
165                 self.comboList = gtk.combo_box_entry_new_text()
166                 self.comboList.set_size_request(180, -1)
167                 self.pack_start(self.comboList, expand = False, fill = True, padding = 0)
168
169                 label = gtk.Label(_("  Category:"))
170                 self.pack_start(label, expand = False, fill = True, padding = 0)
171
172                 self.comboCategory = gtk.combo_box_entry_new_text()
173                 self.comboCategory.set_size_request(180, -1)
174                 self.pack_start(self.comboCategory, expand = False, fill = True, padding = 0)
175
176                 self.comboList.connect("changed", self.comboList_changed, None)
177                 self.comboCategory.connect("changed", self.comboCategory_changed, None)
178
179                 label = gtk.Label(_("  View:"))
180                 self.pack_start(label, expand = False, fill = True, padding = 0)
181
182                 self.radio_all = gtk.RadioButton(group = None, label = _("All"), use_underline = True)
183                 self.pack_start(self.radio_all, expand = False, fill = True, padding = 0)
184                 self.radio_active = gtk.RadioButton(group = self.radio_all, label = _("Active"), use_underline = True)
185                 self.pack_start(self.radio_active, expand = False, fill = True, padding = 0)
186                 self.radio_all.connect("toggled", self.radioActive_changed, None)