Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/editor.py
blob: 42cc7188c8f3555d1fdffea254584004b86d2d94 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# Copyright (C) 2009, Tutorius.org
# Greatly influenced by sugar/activity/namingalert.py
#
# 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
""" Tutorial Editor Module
""" 

import gtk
import gobject
#import hippo
#import gconf

from gettext import gettext as _

from sugar.tutorius.gtkutils import register_signals_numbered, get_children

class WidgetIdentifier(gtk.Window):
    """
    Tool that allows identifying widgets.

    """
    __gtype_name__ = 'TutoriusWidgetIdentifier'

    def __init__(self, activity):
        gtk.Window.__init__(self)
        
        self._activity = activity
        self._handlers = {}
        # dict of signals to register on the widgets.
        # key : signal name
        # value : initial checkbox status
        signals = {
            "focus":True,
            "button-press-event":True,
            "enter-notify-event":False,
            "leave-notify-event":False,
            "key-press-event":True,
            "text-selected":True,
            "clicked":True,
        }

        self.set_decorated(False)
        self.set_resizable(False)
        self.set_modal(False)

        self.connect('realize', self.__realize_cb)

        self._expander = gtk.Expander(_("Widget Identifier"))
        self._expander.set_expanded(True)
        self.add(self._expander)
        self._expander.connect("notify::expanded", self.__expander_cb)

        self._expander.show()

        nbk = gtk.Notebook()
        self._expander.add(nbk)
        nbk.show()

        ###############################
        # Event log viewer page
        ###############################
        self.logview = gtk.TextView()
        self.logview.set_editable(False)
        self.logview.set_cursor_visible(False)
        self.logview.set_wrap_mode(gtk.WRAP_NONE)
        self._textbuffer = self.logview.get_buffer()

        swd = gtk.ScrolledWindow()
        swd.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        swd.add(self.logview)
        self.logview.show()

        nbk.append_page(swd, gtk.Label(_("Log")))
        swd.show()
        
        ###############################
        # Filters page
        ###############################
        filters = gtk.Table( (len(signals)+1)/2, 2)

        xpos, ypos = 0, 0
        for key, active in signals.items():
            cbtn = gtk.CheckButton(label=key)
            filters.attach(cbtn, xpos, xpos+1, ypos, ypos+1)
            cbtn.show()
            cbtn.set_active(active)
            if active:
                self._handlers[key] = register_signals_numbered( \
                    self._activity, self._handle_events, events=(key,))
            else:
                self._handlers[key] = []
            
            cbtn.connect("toggled", self.__filter_toggle_cb, key)

            #Follow lines then columns
            xpos, ypos = (xpos+1)%2, ypos+(xpos%2)

        nbk.append_page(filters, gtk.Label(_("Events")))
        filters.show()

        ###############################
        # Explorer Page
        ###############################
        tree = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        explorer = gtk.TreeView(tree)

        pathrendr = gtk.CellRendererText()
        pathrendr.set_properties(background="#ffffff", foreground="#000000")
        pathcol = gtk.TreeViewColumn(_("Path"), pathrendr, text=0, background=0, foreground=0)
        explorer.append_column(pathcol)

        typerendr = gtk.CellRendererText()
        typerendr.set_properties(background="#ffffff", foreground="#000000")
        typecol = gtk.TreeViewColumn(_("Widget"), typerendr, text=1, background=1, foreground=1)
        explorer.append_column(typecol)

        self.__populate_treestore( 
            tree, #tree
            tree.append(None, ["0",self._activity.get_name()]), #parent
            self._activity, #widget
            "0" #path
        )

        explorer.set_expander_column(typecol)

        swd2 = gtk.ScrolledWindow()
        swd2.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        swd2.add(explorer)
        explorer.show()
        nbk.append_page(swd2, gtk.Label(_("Explorer")))
        swd2.show()

        ###############################
        # GObject Explorer Page
        ###############################
        tree2 = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
        explorer2 = gtk.TreeView(tree2)

        pathrendr2 = gtk.CellRendererText()
        pathrendr2.set_properties(background="#ffffff", foreground="#000000")
        pathcol2 = gtk.TreeViewColumn(_("Path"), pathrendr2, text=0, background=0, foreground=0)
        explorer2.append_column(pathcol2)

        typerendr2 = gtk.CellRendererText()
        typerendr2.set_properties(background="#ffffff", foreground="#000000")
        typecol2 = gtk.TreeViewColumn(_("Widget"), typerendr2, text=1, background=1, foreground=1)
        explorer2.append_column(typecol2)

        self.__populate_gobject_treestore( 
            tree2, #tree
            tree2.append(None, ["activity",self._activity.get_name()]), #parent
            self._activity, #widget
            "activity" #path
        )

        explorer2.set_expander_column(typecol2)

        swd3 = gtk.ScrolledWindow()
        swd3.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        swd3.add(explorer2)
        explorer2.show()
        nbk.append_page(swd3, gtk.Label(_("GObject Explorer")))
        swd3.show()

    def __populate_treestore(self, tree, parent, widget, path):
        """Populates the treestore with the widget's children recursively
        @param tree gtk.TreeStore to populate
        @param parent gtk.TreeIter to append to
        @param widget gtk.Widget to check for children
        @param path treeish of the widget
        """
        #DEBUG: show parameters in log window gehehe
        #self._handle_events((path,str(type(widget))))
        children = get_children(widget)
        for i in xrange(len(children)):
            childpath = ".".join([path, str(i)])
            child = children[i]
            self.__populate_treestore( 
                tree, #tree
                tree.append(parent, [childpath, child.get_name()]), #parent
                child, #widget
                childpath #path
            )


    def __populate_gobject_treestore(self, tree, parent, widget, path, listed=None):
        """Populates the treestore with the widget's children recursively
        @param tree gtk.TreeStore to populate
        @param parent gtk.TreeIter to append to
        @param widget gtk.Widget to check for children
        @param path treeish of the widget
        """
        listed = listed or []
        if widget in listed:
            return
        listed.append(widget)
        #DEBUG: show parameters in log window gehehe
        #self._handle_events((path,str(type(widget))))
        #Add a child node
        children = tree.append(parent, ["","children"])
        for i in dir(widget):
            #Add if a gobject
            try:
                child = getattr(widget, i)
            except:
                continue
            if isinstance(child,gobject.GObject):
                childpath = ".".join([path, i])
                child = getattr(widget, i)
                self.__populate_gobject_treestore( 
                    tree, #tree
                    tree.append(children, [childpath, i]), #parent
                    child, #widget
                    path + "." + i, #path,
                    listed
                )
        widgets = tree.append(parent, ["","widgets"])
        wchildren = get_children(widget)
        for i in xrange(len(wchildren)):
            childpath = ".".join([path, str(i)])
            child = wchildren[i]
            self.__populate_gobject_treestore( 
                tree, #tree
                tree.append(widgets, [childpath, (hasattr(child,"get_name") and child.get_name()) or i]), #parent
                child, #widget
                childpath, #path,
                listed
            )
        
        #Add signals and attributes nodes
        signals = tree.append(parent, ["","signals"])
        for signame in gobject.signal_list_names(widget):
            tree.append(signals, ["",signame])

        attributes = tree.append(parent, ["","properties"])
        for prop in gobject.list_properties(widget):
            tree.append(attributes, ["",prop])

    def __filter_toggle_cb(self, btn, eventname):
        """Callback for signal name checkbuttons' toggling"""
        #Disconnect existing handlers on key
        self.__disconnect_handlers(eventname)
        if btn.get_active():
            #if checked, reconnect
            self._handlers[eventname] = register_signals_numbered( \
                self._activity, self._handle_events, events=(eventname,))


    def __expander_cb(self, *args):
        """Callback for the window expander toggling"""
        if self._expander.get_expanded():
            self.__move_expanded()
        else:
            self.__move_collapsed()

    def __move_expanded(self):
        """Move the window to it's expanded position"""
        width = 500
        height = 300
        swidth = gtk.gdk.screen_width()
        sheight = gtk.gdk.screen_height()

        self.set_size_request(width, height)
        self.move((swidth-width)/2, sheight-height)

    def __move_collapsed(self):
        """Move the window to it's collapsed position"""
        width = 150
        height = 40
        swidth = gtk.gdk.screen_width()
        sheight = gtk.gdk.screen_height()

        self.set_size_request(width, height)
        self.move((swidth-width)/2, sheight-height)

    def __realize_cb(self, widget):
        """Callback for realize"""
        self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
        self.window.set_accept_focus(True)
        self.__move_expanded()

    def _disconnect_handlers(self):
        """ Disconnect all event handlers """
        for key in self._handlers:
            self.__disconnect_handlers(key)

    def __disconnect_handlers(self, key):
        """ Disconnect event handlers associated to signal name "key" """
        if self._handlers.has_key(key):
            for widget, handlerid in self._handlers[key]:
                widget.handler_disconnect(handlerid)
            del self._handlers[key]

    def _handle_events(self, *args):
        """ Event handler for subscribed widget events.
        Accepts variable length argument list. Last must be
        a two-tuple containing (event name, widget name) """
        sig, name = args[-1]
        text = "\r\n".join(
            (["%s event received from %s" % (sig, name)] + 
             self._textbuffer.get_text(*(self._textbuffer.get_bounds())
             ).split("\r\n"))[:80]
        )
        self._textbuffer.set_text(text)