Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/addons/gtkwidgettypefilter.py
blob: 816a7547a4919946abf0ee42338e8525ccafbb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from sugar.tutorius.filters import *
from sugar.tutorius.properties import *
from sugar.tutorius.services import ObjectStore
from sugar.tutorius.gtkutils import find_widget

import logging
logger = logging.getLogger("GtkWidgetTypeFilter")

class GtkWidgetTypeFilter(EventFilter):
    """
    Event Filter that listens for keystrokes on a widget
    """
    object_id = TStringProperty("")
    text = TStringProperty("")
    strokes = TArrayProperty([])

    def __init__(self, object_id, text=None, strokes=None):
        """Constructor
        @param next_state default EventFilter param, passed on to EventFilter
        @param object_id object tree-ish identifier
        @param text resulting text expected
        @param strokes list of strokes expected

        At least one of text or strokes must be supplied
        """
        super(GtkWidgetTypeFilter, self).__init__()
        self.object_id = object_id
        self.text = text
        self._captext = ""
        self.strokes = strokes
        self._capstrokes = []
        self._widget = None
        self._handler_id = None

    def install_handlers(self, callback, **kwargs):
        """install handlers
        @param callback default EventFilter callback arg
        """
        super(GtkWidgetTypeFilter, self).install_handlers(callback, **kwargs)
        logger.debug("~~~GtkWidgetTypeFilter install")
        activity = ObjectStore().activity
        if activity is None:
            logger.error("No activity")
            raise RuntimeWarning("no activity in the objectstore")

        self._widget = find_widget(activity, self.object_id)
        if self._widget:
            self._handler_id= self._widget.connect("key-press-event",self.__keypress_cb)
            logger.debug("~~~Connected handler %d on %s" % (self._handler_id,self.object_id) )

    def remove_handlers(self):
        """remove handlers"""
        super(GtkWidgetTypeFilter, self).remove_handlers()
        #if an event was connected, disconnect it
        if self._handler_id:
            self._widget.handler_disconnect(self._handler_id)
            self._handler_id=None

    def __keypress_cb(self, widget, event, *args):
        """keypress callback"""
        logger.debug("~~~keypressed!")
        key = event.keyval
        keystr = event.string
        logger.debug("~~~Got key: " + str(key) + ":"+ keystr)
        self._capstrokes += [key]
        #TODO Treat other stuff, such as arrows
        if key == gtk.keysyms.BackSpace:
            self._captext = self._captext[:-1]
        else:
            self._captext = self._captext + keystr

        logger.debug("~~~Current state: " + str(self._capstrokes) + ":" + str(self._captext))
        if not self.strokes is None and self.strokes in self._capstrokes:
            self.do_callback()
        if not self.text is None and self.text in self._captext:
            self.do_callback()

__event__ = {
    'name' : 'GtkWidgetTypeFilter',
    'display_name' : 'Widget Filter',
    'icon' : '',
    'class' : GtkWidgetTypeFilter,
    'mandatory_props' : ['next_state', 'object_id']
}