4
from contacts_sync.ui.presenters.category import CategoryPresenter
6
logger = logging.getLogger("widgets.categories_entry")
8
categories_presentor = CategoryPresenter()
10
def categories_changed (editable, *user_data):
12
Function that takes care of providing which categories we can use.
15
text = editable.get_text()
16
model = comp.get_model()
18
for match in categories_presentor.get_rest_categories(text):
21
def match_func(completion, text, iter):
23
Function used by the completer to decide if a typed string matches a
26
model = completion.get_model()
27
key = model.get_value(iter, completion.get_text_column())
30
text = text[text.rfind(",") + 1:].strip()
31
if key.startswith(text):
35
def on_completion_match(completion, model, iter, *user_data):
37
On completion method used to add a category without removing previous ones.
40
current_text = entry.get_text()
41
# remove the last part coded
42
if current_text.find(",") >= 0:
43
current_text = current_text[:current_text.rfind(",") + 1]
47
current_text = "{0} {1}".format(current_text, model[iter][0]).strip()
48
# set back the whole text
49
entry.set_text(current_text)
50
# move the cursor at the end
51
entry.set_position(-1)
52
# stop the event propagation
55
class CategoriesEntry(gtk.Entry):
57
This entry allows to auto-complete code with the categories that have
58
been used already in the application.
61
__gtype_name__ = "CategoriesEntry"
63
def __init__(self, max=0):
64
gtk.Entry.__init__(self, max)
65
# set the auto complete required functions
66
completion = gtk.EntryCompletion()
67
self.set_completion(completion)
68
liststore = gtk.ListStore(gobject.TYPE_STRING)
69
completion.set_model(liststore)
70
completion.set_match_func(match_func)
71
completion.set_text_column (0)
72
completion.connect("match-selected", on_completion_match, self)
73
self.connect ("changed", categories_changed, completion)