~cmiller/+junk/notify-friends-contacts

« back to all changes in this revision

Viewing changes to ui/widgets/categories_entry.py

  • Committer: Chad MILLER
  • Date: 2010-07-03 23:18:35 UTC
  • mfrom: (2.1.2 contacts_sync)
  • Revision ID: cmiller@hypatia-20100703231835-jqd5pmqcrlfb3avg
Merge Mandel's ui code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gtk
 
2
import gobject
 
3
import logging
 
4
from contacts_sync.ui.presenters.category import CategoryPresenter
 
5
 
 
6
logger = logging.getLogger("widgets.categories_entry")
 
7
 
 
8
categories_presentor = CategoryPresenter()
 
9
 
 
10
def categories_changed (editable, *user_data):
 
11
    """
 
12
    Function that takes care of providing which categories we can use.
 
13
    """
 
14
    comp = user_data[0]
 
15
    text = editable.get_text()
 
16
    model = comp.get_model()
 
17
    model.clear()
 
18
    for match in categories_presentor.get_rest_categories(text):
 
19
        model.append([match])
 
20
 
 
21
def match_func(completion, text, iter):
 
22
    """
 
23
    Function used by the completer to decide if a typed string matches a
 
24
    category.
 
25
    """
 
26
    model = completion.get_model()
 
27
    key = model.get_value(iter, completion.get_text_column())
 
28
    if key:
 
29
        key = key.lower()
 
30
        text = text[text.rfind(",") + 1:].strip()
 
31
        if key.startswith(text):
 
32
          return True
 
33
    return False
 
34
 
 
35
def on_completion_match(completion, model, iter, *user_data):
 
36
    """
 
37
    On completion method used to add a category without removing previous ones.
 
38
    """
 
39
    entry = user_data[0]
 
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]
 
44
    else:
 
45
        current_text = ""
 
46
    # add matching word
 
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
 
53
    return True
 
54
 
 
55
class CategoriesEntry(gtk.Entry):
 
56
    """
 
57
    This entry allows to auto-complete code with the categories that have
 
58
    been used already in the application.
 
59
    """
 
60
 
 
61
    __gtype_name__ = "CategoriesEntry"
 
62
 
 
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)