eventwatcher.py 15.5 KB
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
#----------------------------------------------------------------------------
# Name:        wx.lib.eventwatcher
# Purpose:     A widget that allows some or all events for a particular widget
#              to be captured and displayed.
#
# Author:      Robin Dunn
#
# Created:     21-Jan-2009
# RCS-ID:      $Id: $
# Copyright:   (c) 2009 by Total Control Software
# Licence:     wxWindows license
#----------------------------------------------------------------------------

"""
A widget and supporting classes for watching the events sent to some other widget.
"""

import wx
from wx.lib.mixins.listctrl import CheckListCtrlMixin

#----------------------------------------------------------------------------
# Helpers for building the data structures used for tracking the
# various event binders that are available

_eventBinders = None
_eventIdMap = None

def _buildModuleEventMap(module):
    count = 0
    for name in dir(module):
        if name.startswith('EVT_'):
            item = getattr(module, name)
            if isinstance(item, wx.PyEventBinder) and \
               len(item.evtType) == 1 and \
               item not in _eventBinders:
                    _eventBinders.append(item)
                    _eventIdMap[item.typeId] = name
                    count += 1
    return count


def buildWxEventMap():
    """
    Add the event binders from the main wx namespace.  This is called
    automatically from the EventWatcher.
    """
    global _eventBinders
    global _eventIdMap
    if _eventBinders is None:
        _eventBinders = list()
        _eventIdMap = dict()
        _buildModuleEventMap(wx)


def addModuleEvents(module):
    """
    Adds all the items in module that start with ``EVT_`` to the event
    data structures used by the EventWatcher.
    """
    if _eventBinders is None:
        buildWxEventMap()
    return _buildModuleEventMap(module)
    

# Events that should not be watched by default
_noWatchList = [
    wx.EVT_PAINT, 
    wx.EVT_NC_PAINT,
    wx.EVT_ERASE_BACKGROUND,
    wx.EVT_IDLE, 
    wx.EVT_UPDATE_UI,
    wx.EVT_UPDATE_UI_RANGE,
    ]
OTHER_WIDTH = 250
    

def _makeSourceString(wdgt):
    if wdgt is None:
        return "None"
    else:
        name = ''
        id = 0
        if hasattr(wdgt, 'GetName'):
            name = wdgt.GetName()
        if hasattr(wdgt, 'GetId'):
            id = wdgt.GetId()
        return '%s "%s" (%d)' % (wdgt.__class__.__name__, name, id)

def _makeAttribString(evt):
    "Find all the getters"
    attribs = ""
    for name in dir(evt):
        if (name.startswith('Get') or name.startswith('Is')) and \
               name not in [ 'GetEventObject',
                             'GetEventType',
                             'GetId',
                             'GetSkipped',
                             'GetTimestamp',
                             'GetClientData',
                             'GetClientObject',
                             ]:
            try:
                value = getattr(evt, name)()
                attribs += "%s : %s\n" % (name, value)
            except:
                pass
        
    return attribs.rstrip()
    
#----------------------------------------------------------------------------

class EventLog(wx.ListCtrl):
    """
    A virtual listctrl that displays information about the watched events.
    """
    def __init__(self, *args, **kw):
        kw['style'] = wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES
        wx.ListCtrl.__init__(self, *args, **kw)
        self.clear()
        
        if 'wxMac' in wx.PlatformInfo:
            self.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)
        
        self.InsertColumn(0, "#", format=wx.LIST_FORMAT_RIGHT, width=50)
        self.InsertColumn(1, "Event", width=200)
        self.InsertColumn(2, "Source", width=200)
        
        self.SetMinSize((450+wx.SystemSettings.GetMetric(wx.SYS_VSCROLL_X), 450))
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onItemSelected)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onItemActivated)
        self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.onItemActivated)
        
    def append(self, evt):
        evtName = _eventIdMap.get(evt.GetEventType(), None)
        if evtName is None:
            evtName = 'Unknown: %d' % evt.GetEventType()
        source = _makeSourceString(evt.GetEventObject())
        attribs = _makeAttribString(evt)

        lastIsSelected = self.currItem == len(self.data)-1
        self.data.append( (evtName, source, attribs) )
        
        count = len(self.data)
        self.SetItemCount(count)
        self.RefreshItem(count-1)
        if lastIsSelected:
            self.Select(count-1)
            self.EnsureVisible(count-1)
            
    def clear(self):
        self.data = []
        self.SetItemCount(0)
        self.currItem = -1
        self.Refresh()
                
    def OnGetItemText(self, item, col):
        if col == 0:
            val = str(item+1)
        else:
            val = self.data[item][col-1]
        return val
    
    def OnGetItemAttr(self, item):  return None
    def OnGetItemImage(self, item): return -1
    
    def onItemSelected(self, evt):
        self.currItem = evt.GetIndex()

    def onItemActivated(self, evt):
        idx = evt.GetIndex()
        text = self.data[idx][2]
        wx.CallAfter(wx.TipWindow, self, text, OTHER_WIDTH)

#----------------------------------------------------------------------------


class EventChooser(wx.Panel):
    """
    Panel with CheckListCtrl for selecting which events will be watched.
    """
    
    class EventChooserLC(wx.ListCtrl, CheckListCtrlMixin):
        def __init__(self, parent):
            wx.ListCtrl.__init__(self, parent, 
                                 style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_HRULES|wx.LC_VRULES)
            CheckListCtrlMixin.__init__(self)
            if 'wxMac' in wx.PlatformInfo:
                self.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)
            
        # this is called by the base class when an item is checked/unchecked
        def OnCheckItem(self, index, flag):
            self.Parent.OnCheckItem(index, flag)
            
            
    def __init__(self, *args, **kw):
        wx.Panel.__init__(self, *args, **kw)
        self.updateCallback = lambda: None
        self.doUpdate = True
        self._event_name_filter = wx.SearchCtrl(self)
        self._event_name_filter.ShowCancelButton(True)
        self._event_name_filter.Bind(wx.EVT_TEXT, lambda evt: self.setWatchList(self.watchList))
        self._event_name_filter.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, self._ClearEventFilter)
        self.lc = EventChooser.EventChooserLC(self)
        btn1 = wx.Button(self, -1, "All")
        btn2 = wx.Button(self, -1, "None")
        btn1.SetToolTipString("Check all events")
        btn2.SetToolTipString("Uncheck all events")

        self.Bind(wx.EVT_BUTTON, self.onCheckAll, btn1)
        self.Bind(wx.EVT_BUTTON, self.onUncheckAll, btn2)        
        
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onItemActivated, self.lc)
        self.lc.InsertColumn(0, "Binder", width=OTHER_WIDTH)        
        
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(btn1, 0, wx.ALL, 5)
        btnSizer.Add(btn2, 0, wx.ALL, 5)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self._event_name_filter, 0, wx.EXPAND|wx.ALL, 5)
        sizer.Add(self.lc, 1, wx.EXPAND)
        sizer.Add(btnSizer)
        self.SetSizer(sizer)
        
        
    def setUpdateCallback(self, func):
        self.updateCallback = func
    
    def setWatchList(self, watchList):
        self.doUpdate = False
        searched = self._event_name_filter.GetValue().lower()
        self.watchList = watchList
        self.lc.DeleteAllItems()
        count = 0
        for index, (item, flag) in enumerate(watchList):
            typeId = item.typeId
            text = _eventIdMap.get(typeId, "[Unknown]")
            if text.lower().find(searched) == -1:
                continue
            self.lc.InsertStringItem(count, text)
            self.lc.SetItemData(count, index)
            if flag:
                self.lc.CheckItem(count)
            count += 1
        self.lc.SortItems(self.sortCompare)
        self.doUpdate = True
        self.updateCallback()

        
    def OnCheckItem(self, index, flag):
        index = self.lc.GetItemData(index)
        item, f = self.watchList[index]
        self.watchList[index] = (item, flag)
        if self.doUpdate:
            self.updateCallback()
            
            
    def onItemActivated(self, evt):
        self.lc.ToggleItem(evt.m_itemIndex)

    def onCheckAll(self, evt):
        self.doUpdate = False
        for idx in range(self.lc.GetItemCount()):
            self.lc.CheckItem(idx, True)
        self.doUpdate = True
        self.updateCallback()
    
    def onUncheckAll(self, evt):
        self.doUpdate = False
        for idx in range(self.lc.GetItemCount()):
            self.lc.CheckItem(idx, False)
        self.doUpdate = True
        self.updateCallback()

    
    def sortCompare(self, data1, data2):
        item1 = self.watchList[data1][0]
        item2 = self.watchList[data2][0]
        text1 = _eventIdMap.get(item1.typeId)
        text2 = _eventIdMap.get(item2.typeId)
        return cmp(text1, text2)

    def _ClearEventFilter(self, evt):
        self._event_name_filter.SetValue("")

#----------------------------------------------------------------------------

class EventWatcher(wx.Frame):
    """
    A frame that will catch and display al events sent to some widget.
    """
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)
        self.SetTitle("EventWatcher")
        self.SetExtraStyle(wx.WS_EX_BLOCK_EVENTS)
        self._watchedWidget = None

        buildWxEventMap()
        self.buildWatchList(_noWatchList)
        
        # Make the widgets
        self.splitter = wx.SplitterWindow(self)
        panel = wx.Panel(self.splitter)
        self.splitter.Initialize(panel)
        self.log = EventLog(panel)
        clearBtn = wx.Button(panel, -1, "Clear")
        addBtn = wx.Button(panel, -1, "Add Module")
        watchBtn = wx.ToggleButton(panel, -1, "Watch")
        watchBtn.SetValue(True)
        selectBtn = wx.ToggleButton(panel, -1, ">>>")
        self.selectBtn = selectBtn
        
        clearBtn.SetToolTipString("Clear the event log")
        addBtn.SetToolTipString("Add the event binders in an additional package or module to the watcher")
        watchBtn.SetToolTipString("Toggle the watching of events")
        selectBtn.SetToolTipString("Show/hide the list of events to be logged")
        
        # Do the layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(clearBtn, 0, wx.RIGHT, 5)
        btnSizer.Add(addBtn, 0, wx.RIGHT, 5)
        btnSizer.Add((1,1), 1)
        btnSizer.Add(watchBtn, 0, wx.RIGHT, 5)
        btnSizer.Add((1,1), 1)
        btnSizer.Add(selectBtn, 0, wx.RIGHT, 5)
        sizer.Add(self.log, 1, wx.EXPAND)
        sizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 5)
        panel.SetSizer(sizer)
        self.Sizer = wx.BoxSizer()
        self.Sizer.Add(self.splitter, 1, wx.EXPAND)
        self.Fit()
        
        # Bind events
        self.Bind(wx.EVT_CLOSE, self.onCloseWindow)
        self.Bind(wx.EVT_BUTTON, self.onClear, clearBtn)
        self.Bind(wx.EVT_BUTTON, self.onAddModule, addBtn)
        self.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleWatch, watchBtn)
        self.Bind(wx.EVT_TOGGLEBUTTON, self.onToggleSelectEvents, selectBtn)
        
            
        
    def watch(self, widget):
        assert self._watchedWidget is None, "Can only watch one widget at a time"
        self.SetTitle("EventWatcher for " + _makeSourceString(widget))
        for evtBinder, flag in self._watchedEvents:
            if flag:
                widget.Bind(evtBinder, self.onWatchedEvent)
        self._watchedWidget = widget
        
                
    def unwatch(self):
        self.SetTitle("EventWatcher")
        if self._watchedWidget:
            for evtBinder, flag in self._watchedEvents:
                self._watchedWidget.Unbind(evtBinder, handler=self.onWatchedEvent)
        self._watchedWidget = None
    
        
    def updateBindings(self):
        widget = self._watchedWidget
        self.unwatch()
        self.watch(widget)
        
        
    def onWatchedEvent(self, evt):     
        if self:
            self.log.append(evt)
        evt.Skip()
        
    def buildWatchList(self, exclusions):
        # This is a list of (PyEventBinder, flag) tuples where the flag indicates
        # whether to bind that event or not. By default all execpt those in
        # the _noWatchList wil be set to be watched.
        self._watchedEvents = list()
        for item in _eventBinders:
            self._watchedEvents.append( (item, item not in exclusions) )
            
    def onCloseWindow(self, evt):
        self.unwatch()
        evt.Skip()
        
    def onClear(self, evt):
        self.log.clear()
        
    def onAddModule(self, evt):
        try:
            dlg = wx.TextEntryDialog(
                self, 
                "Enter the package or module name to be scanned for \"EVT_\" event binders.",
                "Add Module")
            if dlg.ShowModal() == wx.ID_OK:
                modname = dlg.GetValue()
                try:
                    # Passing a non-empty fromlist will cause __import__ to
                    # return the imported submodule if a dotted name is passed.
                    module = __import__(modname, fromlist=[0])
                except ImportError:
                    wx.MessageBox("Unable to import \"%s\"" % modname,
                                  "Error")
                    return
                count = addModuleEvents(module)
                wx.MessageBox("%d new event binders found" % count,
                              "Success")
                
                # Now unwatch and re-watch so we can get the new events bound
                self.updateBindings()
        finally:    
            dlg.Destroy()
    
            
    def onToggleWatch(self, evt):
        if evt.Checked():
            self.watch(self._unwatchedWidget)
            self._unwatchedWidget = None
        else:
            self._unwatchedWidget = self._watchedWidget
            self.unwatch()

    
    def onToggleSelectEvents(self, evt):
        if evt.Checked():
            self.selectBtn.SetLabel("<<<")
            self._selectList = EventChooser(self.splitter)
            self._selectList.setUpdateCallback(self.updateBindings)
            self._selectList.setWatchList(self._watchedEvents)
            
            self.SetSize(self.GetSize() + (OTHER_WIDTH,0))
            self.splitter.SplitVertically(self.splitter.GetWindow1(), 
                                          self._selectList,
                                          -OTHER_WIDTH)
        else:
            self.selectBtn.SetLabel(">>>")
            sashPos = self.splitter.GetSashPosition()
            self.splitter.Unsplit()
            self._selectList.Destroy()
            cs = self.GetClientSize()
            self.SetClientSize((sashPos, cs.height))
        
#----------------------------------------------------------------------------
        
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frm = wx.Frame(None, title="Test Frame")
    pnl = wx.Panel(frm)
    txt = wx.TextCtrl(pnl, -1, "text", pos=(20,20))
    btn = wx.Button(pnl, -1, "button", pos=(20,50))
    frm.Show()
    
    ewf=EventWatcher(frm)
    ewf.watch(frm)
    ewf.Show()
    
    #import wx.lib.inspection
    #wx.lib.inspection.InspectionTool().Show()
    
    app.MainLoop()