source: sasview/sasview/wxcruft.py @ 2d88fc4

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 2d88fc4 was 2d88fc4, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

reduce number of NewIds? used for each plot. Refs #448.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import inspect
2import wx
3from wx import Timer
4import wx._misc
5
6def call_later_fix():
7    # TODO: test if we need the fix
8    wx.CallLater = CallLater
9    wx.FutureCall = FutureCall
10    wx.PyTimer = PyTimer
11
12def trace_new_id():
13    wx.NewId = NewId
14
15def NewId():
16    id = wx._misc.NewId()
17    frame = inspect.stack()[1]
18    path = frame[1]
19    index = path.find('/sas/')
20    if index == -1: index = path.find('\\sas\\')
21    path = path[index+1:]
22    print "NewId %d from %s(%d):%s"%(id, path, frame[2], frame[3])
23    return id
24
25
26# ==========================================================================
27# Hacked versions of CallLater and PyTimer so that the main GUI loop doesn't
28# eat wx ids.
29# Changed lines are marked #PAK
30# ==========================================================================
31
32# For backwards compatibility with 2.4
33class PyTimer(Timer):
34    def __init__(self, notify, *args, **kw):  #PAK
35        Timer.__init__(self, *args, **kw)     #PAK
36        self.notify = notify
37
38    def Notify(self):
39        if self.notify:
40            self.notify()
41
42
43class CallLater:
44    """
45    A convenience class for `wx.Timer`, that calls the given callable
46    object once after the given amount of milliseconds, passing any
47    positional or keyword args.  The return value of the callable is
48    availbale after it has been run with the `GetResult` method.
49
50    If you don't need to get the return value or restart the timer
51    then there is no need to hold a reference to this object.
52
53    :see: `wx.CallAfter`
54    """
55
56    __RUNNING = set()
57
58    def __init__(self, millis, callableObj, *args, **kwargs):
59        # print "=================== entering CallLater constructor"
60        assert callable(callableObj), "callableObj is not callable"
61        self.millis = millis
62        self.callable = callableObj
63        self.SetArgs(*args, **kwargs)
64        self.runCount = 0
65        self.running = False
66        self.hasRun = False
67        self.result = None
68        self.timer = None
69        self.id = wx.NewId()  # PAK
70        self.Start()
71
72
73    def Start(self, millis=None, *args, **kwargs):
74        """
75        (Re)start the timer
76        """
77        self.hasRun = False
78        if millis is not None:
79            self.millis = millis
80        if args or kwargs:
81            self.SetArgs(*args, **kwargs)
82        self.Stop()
83        self.timer = PyTimer(self.Notify, id=self.id)  # PAK
84        self.timer.Start(self.millis, wx.TIMER_ONE_SHOT)
85        self.running = True
86        self.__RUNNING.add(self)
87    Restart = Start
88
89
90    def Stop(self):
91        """
92        Stop and destroy the timer.
93        """
94        if self.timer is not None:
95            self.timer.Stop()
96            self.timer = None
97        self.__RUNNING.discard(self)
98
99
100    def GetInterval(self):
101        if self.timer is not None:
102            return self.timer.GetInterval()
103        else:
104            return 0
105
106
107    def IsRunning(self):
108        return self.timer is not None and self.timer.IsRunning()
109
110
111    def SetArgs(self, *args, **kwargs):
112        """
113        (Re)set the args passed to the callable object.  This is
114        useful in conjunction with Restart if you want to schedule a
115        new call to the same callable object but with different
116        parameters.
117        """
118        self.args = args
119        self.kwargs = kwargs
120
121
122    def HasRun(self):
123        return self.hasRun
124
125
126    def GetResult(self):
127        return self.result
128
129
130    def Notify(self):
131        """
132        The timer has expired so call the callable.
133        """
134        if self.callable and getattr(self.callable, 'im_self', True):
135            self.runCount += 1
136            self.running = False
137            self.result = self.callable(*self.args, **self.kwargs)
138        self.hasRun = True
139        if not self.running:
140            # if it wasn't restarted, then cleanup
141            wx.CallAfter(self.Stop)
142
143    Interval = property(GetInterval)
144    Result = property(GetResult)
145
146
147class FutureCall(CallLater):
148    """A compatibility alias for `CallLater`."""
Note: See TracBrowser for help on using the repository browser.