File: _widgets.py

package info (click to toggle)
python-pyo 0.7-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,512 kB
  • ctags: 19,774
  • sloc: ansic: 104,581; python: 23,599; sh: 1,091; makefile: 396; cpp: 152
file content (332 lines) | stat: -rw-r--r-- 11,693 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""
Copyright 2010 Olivier Belanger

This file is part of pyo, a python module to help digital signal
processing script creation.

pyo 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 3 of the License, or
(at your option) any later version.

pyo 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 Licensehack for OSX display
along with pyo.  If not, see <http://www.gnu.org/licenses/>.
"""
from types import ListType, FloatType, IntType
import math, sys, os, random
import __builtin__

try:
    from PIL import Image, ImageDraw, ImageTk
    WITH_PIL = True
except:
    WITH_PIL = False

try:
    if 'wx' not in sys.modules:
        # Don't try to load if already loaded.
        import wxversion
        wxversion.ensureMinimal("2.8")
        import wx
    from _wxwidgets import *
    PYO_USE_WX = True
except:
    PYO_USE_WX = False

if hasattr(__builtin__, 'EPYO_APP_OPENED'):
    PYO_USE_WX = True

PYO_USE_TK = False
if not PYO_USE_WX:
    try:
        from Tkinter import *
        from _tkwidgets import *
        PYO_USE_TK = True
        print """
WxPython is not found for the current python version. 
Pyo will use a minimal GUI toolkit written with Tkinter. 
This toolkit has limited functionnalities and is no more 
maintained or updated. If you want to use all of pyo's
GUI features, you should install WxPython, available here: 
http://www.wxpython.org/
"""
    except:
        PYO_USE_TK = False
        print """
Neither WxPython nor Tkinter are found for the current python version.
Pyo's GUI features are disabled. For a complete GUI toolkit, you should 
consider installing WxPython, available here: http://www.wxpython.org/
"""

X, Y, CURRENT_X, MAX_X, NEXT_Y = 800, 700, 30, 30, 30
WINDOWS = []
CTRLWINDOWS = []
GRAPHWINDOWS = []
DATAGRAPHWINDOWS = []
TABLEWINDOWS = []
SNDTABLEWINDOWS = []
MATRIXWINDOWS = []
SPECTRUMWINDOWS = []
WX_APP = False

def createRootWindow():
    global WX_APP
    if not PYO_USE_WX:
        if len(WINDOWS) == 0:
            root = Tk()
            root.withdraw()
            return None
        else:
            return None
    else:        
        if not WX_APP: 
            win = wx.App(False)
            WX_APP = True 
            return win
        else:
            return None

def tkCloseWindow(win):
    win.destroy()
    if win in WINDOWS: WINDOWS.remove(win)

def tkCloseWindowFromKeyboard(event):
    win = event.widget
    if not isinstance(win, ServerGUI): 
        win.destroy()
        if win in WINDOWS: WINDOWS.remove(win)
                
def tkCreateToplevelWindow():
    win = Toplevel()
    WINDOWS.append(win)
    win.protocol('WM_DELETE_WINDOW', lambda win=win: tkCloseWindow(win))
    win.bind("<Escape>", tkCloseWindowFromKeyboard)
    return win

def wxDisplayWindow(f, title):
    global CURRENT_X, MAX_X, NEXT_Y
    f.SetTitle(title)
    x, y = f.GetSize()
    if sys.platform == "linux2":
        y += 25
    if y + NEXT_Y < Y:
        px, py, NEXT_Y = CURRENT_X, NEXT_Y, NEXT_Y + y
        if x + CURRENT_X > MAX_X: MAX_X = x + CURRENT_X
        f.SetPosition((px, py))
    elif x + MAX_X < X:
        px, py, NEXT_Y, CURRENT_X = MAX_X, 50, 50 + y, MAX_X
        if x + CURRENT_X > MAX_X: MAX_X = x + CURRENT_X
        f.SetPosition((px, py))
    else:
        f.SetPosition((random.randint(250,500), random.randint(200,400)))
    f.Show()

def wxShowWindow(f, title, root):
    f.SetTitle(title)
    f.Show()
    if root != None:
        root.MainLoop()
    
def wxCreateDelayedCtrlWindows():
    for win in CTRLWINDOWS:
        f = PyoObjectControl(None, win[0], win[1])
        if win[2] == None: title = win[0].__class__.__name__
        else: title = win[2]
        wxDisplayWindow(f, title)

def wxCreateDelayedGraphWindows():
    for win in GRAPHWINDOWS:
        f = TableGrapher(None, win[0], win[1], win[2], win[3])
        if win[4] == None: title = win[0].__class__.__name__
        else: title = win[4]
        wxDisplayWindow(f, title)

def wxCreateDelayedDataGraphWindows():
    for win in DATAGRAPHWINDOWS:
        f = DataTableGrapher(None, win[0], win[1])
        if win[2] == None: title = win[0].__class__.__name__
        else: title = win[2]
        wxDisplayWindow(f, title)

def wxCreateDelayedTableWindows():
    global CURRENT_X, MAX_X, NEXT_Y
    for win in TABLEWINDOWS:
        object = win[3]
        f = ViewTable(None, win[0], win[1], object)
        if object != None:
            object._setViewFrame(f)
        wxDisplayWindow(f, win[2])

def wxCreateDelayedSndTableWindows():
    global CURRENT_X, MAX_X, NEXT_Y
    for win in SNDTABLEWINDOWS:
        f = SndViewTable(None, win[0], win[1], win[3])
        win[0]._setViewFrame(f)
        wxDisplayWindow(f, win[2])

def wxCreateDelayedMatrixWindows():
    global CURRENT_X, MAX_X, NEXT_Y
    for win in MATRIXWINDOWS:
        object = win[3]
        if WITH_PIL: f = ViewMatrix_withPIL(None, win[0], win[1], object)
        else: f = ViewMatrix_withoutPIL(None, win[0], win[1], object)
        if object != None:
            object._setViewFrame(f)
        wxDisplayWindow(f, win[2])

def wxCreateDelayedSpectrumWindows():
    for win in SPECTRUMWINDOWS:
        f = SpectrumDisplay(None, win[0])
        if win[1] == None: title = win[0].__class__.__name__
        else: title = win[1]
        if win[0] != None:
            win[0]._setViewFrame(f)
        wxDisplayWindow(f, title)
    
def createCtrlWindow(obj, map_list, title, wxnoserver=False):
    if not PYO_USE_WX:
        createRootWindow()
        win = tkCreateToplevelWindow()
        f = PyoObjectControl(win, obj, map_list)
        win.resizable(True, False)
        if title == None: title = obj.__class__.__name__
        win.title(title)
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = PyoObjectControl(None, obj, map_list)
            if title == None: title = obj.__class__.__name__
            wxShowWindow(f, title, root)
        else:
            CTRLWINDOWS.append([obj, map_list, title])

def createGraphWindow(obj, mode, xlen, yrange, title, wxnoserver=False):
    if not PYO_USE_WX:
        print "WxPython must be installed to use the 'graph()' method."
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = TableGrapher(None, obj, mode, xlen, yrange)
            if title == None: title = obj.__class__.__name__
            wxShowWindow(f, title, root)
        else:
            GRAPHWINDOWS.append([obj, mode, xlen, yrange, title])   

def createDataGraphWindow(obj, yrange, title, wxnoserver=False):
    if not PYO_USE_WX:
        print "WxPython must be installed to use the 'graph()' method."
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = DataTableGrapher(None, obj, yrange)
            if title == None: title = obj.__class__.__name__
            wxShowWindow(f, title, root)
        else:
            DATAGRAPHWINDOWS.append([obj, yrange, title])   
        
def createViewTableWindow(samples, title="Table waveform", wxnoserver=False, tableclass=None, object=None):
    if not PYO_USE_WX:
        createRootWindow()
        win = tkCreateToplevelWindow()
        if WITH_PIL: f = ViewTable_withPIL(win, samples)
        else: f = ViewTable_withoutPIL(win, samples)
        win.resizable(False, False)
        win.title(title)
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = ViewTable(None, samples, tableclass, object)
            wxShowWindow(f, title, root)
            if object != None:
                object._setViewFrame(f)
        else:
            TABLEWINDOWS.append([samples, tableclass, title, object])    

def createSndViewTableWindow(obj, title="Table waveform", wxnoserver=False, tableclass=None, mouse_callback=None):
    if not PYO_USE_WX:
        createRootWindow()
        win = tkCreateToplevelWindow()
        if WITH_PIL: f = ViewTable_withPIL(win, obj._base_objs[0].getViewTable())
        else: f = ViewTable_withoutPIL(win, obj._base_objs[0].getViewTable())
        win.resizable(False, False)
        win.title(title)
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = SndViewTable(None, obj, tableclass, mouse_callback)
            if title == None: title = obj.__class__.__name__
            wxShowWindow(f, title, root)
            obj._setViewFrame(f)
        else:
            SNDTABLEWINDOWS.append([obj, tableclass, title, mouse_callback])
        
def createViewMatrixWindow(samples, size, title="Matrix viewer", wxnoserver=False, object=None):
    if not WITH_PIL: print """The Python Imaging Library is not installed. 
It helps a lot to speed up matrix drawing!"""
    if not PYO_USE_WX:
        createRootWindow()    
        win = tkCreateToplevelWindow()
        if WITH_PIL: f = ViewMatrix_withPIL(win, samples, size)
        else: f = ViewMatrix_withoutPIL(win, samples, size)
        win.resizable(False, False)
        win.title(title)
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            if WITH_PIL: f = ViewMatrix_withPIL(None, samples, size, object)
            else: f = ViewMatrix_withoutPIL(None, samples, size, object)
            wxShowWindow(f, title, root)
            if object != None:
                object._setViewFrame(f)
        else:
            MATRIXWINDOWS.append([samples,size,title, object])    

def createSpectrumWindow(object, title, wxnoserver=False):
    if not PYO_USE_WX:
        print "WxPython must be installed to use the Spectrum display."
    else:
        if wxnoserver or WX_APP:
            root = createRootWindow()
            f = SpectrumDisplay(None, object)
            if title == None: title = object.__class__.__name__
            wxShowWindow(f, title, root)
            if object != None:
                object._setViewFrame(f)
        else:
            SPECTRUMWINDOWS.append([object, title])   
        
def createServerGUI(nchnls, start, stop, recstart, recstop, setAmp, started, locals, shutdown, meter, timer, amp, exit):
    global X, Y, MAX_X, NEXT_Y
    if not PYO_USE_WX:
        createRootWindow()
        win = tkCreateToplevelWindow()
        f = ServerGUI(win, nchnls, start, stop, recstart, recstop, setAmp, started, locals, shutdown, meter, timer, amp)
        f.master.title("pyo server")
        f.focus_set()
    else:
        win = createRootWindow()
        f = ServerGUI(None, nchnls, start, stop, recstart, recstop, setAmp, started, locals, shutdown, meter, timer, amp, exit) 
        f.SetTitle("pyo server")
        f.SetPosition((30, 30))
        f.Show()
        X,Y = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)-50, wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)-50
        if sys.platform == "linux2":
            MAX_X, NEXT_Y = f.GetSize()[0]+30, f.GetSize()[1]+55
        else:
            MAX_X, NEXT_Y = f.GetSize()[0]+30, f.GetSize()[1]+30
        wx.CallAfter(wxCreateDelayedTableWindows)
        wx.CallAfter(wxCreateDelayedGraphWindows)
        wx.CallAfter(wxCreateDelayedDataGraphWindows)
        wx.CallAfter(wxCreateDelayedSndTableWindows)
        wx.CallAfter(wxCreateDelayedMatrixWindows)
        wx.CallAfter(wxCreateDelayedCtrlWindows)
        wx.CallAfter(wxCreateDelayedSpectrumWindows)
        wx.CallAfter(f.Raise)
    return f, win