~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to release/scripts/doc_browser.py

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!BPY
2
 
 
3
 
"""
4
 
Name: 'BPy Doc Browser'
5
 
Blender: 232
6
 
Group: 'System'
7
 
Tip: 'Browse BPython (scripting API) modules doc strings.'
8
 
"""
9
 
 
10
 
__author__ = "Daniel Dunbar"
11
 
__url__ = ("blender", "elysiun")
12
 
__version__ = "1.0"
13
 
__bpydoc__ = """\
14
 
The "Doc Browser" lets users navigate the documentation strings of part of
15
 
the Blender Python API.
16
 
 
17
 
It doesn't give access yet to object method functions and variables, only to
18
 
module functions, but still it is a handy reference.
19
 
 
20
 
Hotkeys:<br>
21
 
    Page Up / Page Down: scroll 5 lines at a time;<br>
22
 
    Up / Down arrow keys or mouse wheel: scroll one line at a time.
23
 
 
24
 
Notes:<br>
25
 
    Everyone interested in the bpython api is also invited to read "The Blender
26
 
Python API Reference" doc, available online ("Python Scripting Reference"
27
 
entry in Blender's Help menu).
28
 
"""
29
 
 
30
 
 
31
 
# $Id: doc_browser.py,v 1.5 2006/12/12 05:29:42 campbellbarton Exp $
32
 
#
33
 
# --------------------------------------------------------------------------
34
 
# ***** BEGIN GPL LICENSE BLOCK *****
35
 
#
36
 
# Copyright (C) 2004:  Daniel Dunbar, ddunbar _at_ diads.com
37
 
#
38
 
# This program is free software; you can redistribute it and/or
39
 
# modify it under the terms of the GNU General Public License
40
 
# as published by the Free Software Foundation; either version 2
41
 
# of the License, or (at your option) any later version.
42
 
#
43
 
# This program is distributed in the hope that it will be useful,
44
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 
# GNU General Public License for more details.
47
 
#
48
 
# You should have received a copy of the GNU General Public License
49
 
# along with this program; if not, write to the Free Software Foundation,
50
 
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
51
 
#
52
 
# ***** END GPL LICENCE BLOCK *****
53
 
# --------------------------------------------------------------------------
54
 
 
55
 
#####
56
 
# Blender help browser
57
 
# By Daniel Dunbar, 
58
 
#
59
 
# This should function as a self-explanatory (interface, not code)
60
 
# (mostly) help browser. The code is wacky and nasty and or fun,
61
 
# but mainly bad, just cause it works doesn't mean its readable! 
62
 
63
 
# TEEHEE!
64
 
#
65
 
# The row_draw function could easily be made into a more generic
66
 
# and usefull table drawing function... 
67
 
#
68
 
 
69
 
import Blender
70
 
from types import ListType, IntType, FloatType, StringType, ModuleType
71
 
from Blender.Draw import *
72
 
from Blender.BGL import *
73
 
 
74
 
# Simple version check, since I use the
75
 
# buffer calls... DONT use this code,
76
 
# assume everyone has 1.73+, and force
77
 
# them to upgrade if they dont
78
 
try:
79
 
        a= BufList
80
 
        version= 172
81
 
except:
82
 
        version= 173
83
 
        
84
 
# I could have used the split from the string module,
85
 
# but some people might not have it
86
 
def split(str, on):
87
 
        out= [""]
88
 
        for s in str:
89
 
                if s in on: out.append("")
90
 
                else: out[-1]= out[-1]+s
91
 
 
92
 
        if out[-1]=="": del(out[-1])
93
 
 
94
 
        return out
95
 
 
96
 
last_sort= 1; direction= 1
97
 
def sort_browselist(type):
98
 
        global browselist
99
 
        global last_sort, direction
100
 
 
101
 
        if (type==last_sort): direction= -direction
102
 
        else: direction= 1
103
 
 
104
 
        last_sort= type
105
 
 
106
 
        if (direction==1):
107
 
                def byname(x, y): return cmp(x[0],y[0]);
108
 
                def bytype(x, y): return cmp(x[1],y[1])
109
 
                def bydata(x, y): return cmp(x[2],y[2])
110
 
        else:
111
 
                def byname(x, y): return cmp(y[0],x[0]);
112
 
                def bytype(x, y): return cmp(y[1],x[1])
113
 
                def bydata(x, y): return cmp(y[2],x[2])
114
 
 
115
 
        if (type==1): browselist.sort(byname)
116
 
        elif (type==2): browselist.sort(bytype)
117
 
        elif (type==3): browselist.sort(bydata)
118
 
 
119
 
selected= -1
120
 
def view_doc(num):
121
 
        global selected, selected_page
122
 
 
123
 
        if (selected==num): selected= -1
124
 
        else: selected= num
125
 
 
126
 
        selected_page= 0
127
 
 
128
 
function_filter= 0
129
 
def toggle_function_filter():
130
 
        global function_filter
131
 
 
132
 
        function_filter= not function_filter
133
 
        make_browselist()
134
 
 
135
 
def view_page(dir):
136
 
        global selected_page
137
 
 
138
 
        selected_page= selected_page + dir
139
 
 
140
 
browse_scrollstart= 0
141
 
def browse_module(num):
142
 
        global browsing, selected, browse_scrollstart
143
 
        
144
 
        # cant go back from Blender
145
 
        if browsing.val == 'Blender' and num == -1:
146
 
                return
147
 
        
148
 
        if (num>=0): newstr= browsing.val + "." + browselist[num][0]
149
 
        else:
150
 
                modules= split(browsing.val, ".")
151
 
                newstr= ""
152
 
                for m in modules[:-1]:
153
 
                        newstr= newstr+m
154
 
        try:
155
 
                browsing= Create(newstr)
156
 
                make_browselist()
157
 
        except:
158
 
                browsing= Create('Blender')
159
 
                make_browselist()
160
 
                
161
 
        browse_scrollstart= 0
162
 
        scrolling= 0
163
 
        selected= -1
164
 
 
165
 
def make_browselist():
166
 
        global browselist
167
 
        
168
 
        try:
169
 
                module= eval(browsing.val)
170
 
        except:
171
 
                Blender.Draw.PupMenu('Error%t|Module is invalid')
172
 
                browsing.val = 'Blender'
173
 
                return
174
 
        
175
 
        browselist= []
176
 
        
177
 
        
178
 
        items= dir(module)
179
 
 
180
 
        for item_name in items:
181
 
                if (item_name[:2]=='__'): continue
182
 
 
183
 
                data= [item_name, 'None', '', '']
184
 
                item= eval(item_name,module.__dict__)
185
 
                t= type(item)
186
 
 
187
 
                if (t==IntType): data[1]= 'Int'; data[2]= `item`
188
 
                elif (t==FloatType): data[1]= 'Float'; data[2]= `item`
189
 
                elif (t==StringType): data[1]= 'String'
190
 
                elif (t==ModuleType): data[1]= 'Module'
191
 
                elif (callable(item)):
192
 
                        data[1]= 'Function'
193
 
                        doc= item.__doc__
194
 
                        if (doc): data[3]= doc
195
 
 
196
 
                if (function_filter and data[1]!='Function'): continue
197
 
 
198
 
                browselist.append(data)
199
 
 
200
 
browsing= Create('Blender')
201
 
make_browselist()
202
 
 
203
 
BROWSE_EVT= 1
204
 
 
205
 
SORT_BYNAME= 2
206
 
SORT_BYTYPE= 3
207
 
SORT_BYDATA= 4
208
 
 
209
 
DOC_PAGE_UP= 5
210
 
DOC_PAGE_DOWN= 6
211
 
 
212
 
BACK_MODULE= 7
213
 
CLOSE_VIEW= 8
214
 
FILTER_DISPLAY= 9
215
 
 
216
 
#SCROLLBAR= 10
217
 
 
218
 
VIEW_DOC= 100
219
 
BROWSE_MODULE= 10000
220
 
 
221
 
scr= Create(0)
222
 
browse_scrollstart= 0
223
 
 
224
 
winrect= [0.0, 0.0, 0.0, 0.0]
225
 
def draw():
226
 
        global browsing, winrect, scr, browse_scrollstart
227
 
 
228
 
        # Blender doesn't give us direct access to
229
 
        # the window size yet, but it does set the
230
 
        # GL scissor box for it, so we can get the 
231
 
        # size from that.
232
 
 
233
 
        if (version<173):
234
 
                size= Buffer(GL_FLOAT, None, 4)
235
 
                glGetFloat(GL_SCISSOR_BOX, size)
236
 
                size= BufList(size)
237
 
        else:
238
 
                size= Buffer(GL_FLOAT, 4)
239
 
                glGetFloatv(GL_SCISSOR_BOX, size)
240
 
                size= size.list
241
 
 
242
 
        winrect= size[:]
243
 
 
244
 
        size[0]= size[1]= 0.0
245
 
        
246
 
        # Shrink the size to make a nice frame
247
 
        # (also a good technique so you can be sure you are clipping things properly)
248
 
        size[0], size[1]= int(size[0]+10), int(size[1]+10)
249
 
        size[2], size[3]= int(size[2]-12), int(size[3]-10)
250
 
 
251
 
        glClearColor(0.6, 0.5, 0.3, 0.0)
252
 
        glClear(GL_COLOR_BUFFER_BIT)
253
 
 
254
 
        # The frame
255
 
        glColor3f(0.4, 0.5, 0.2)
256
 
        glRectf(size[0], size[1], size[2], size[3])
257
 
 
258
 
        # Window header 
259
 
        glColor3f(0.2, 0.2, 0.4)
260
 
        glRectf(size[0], size[3]-25, size[2], size[3])
261
 
 
262
 
        glColor3f(0.6, 0.6, 0.6)
263
 
        glRasterPos2f(size[0]+15, size[3]-17)
264
 
        Text("Zr's Help Browser")
265
 
 
266
 
        Button("Filter", FILTER_DISPLAY, size[2]-400, size[3]-22, 45, 18)
267
 
        Button("Back", BACK_MODULE, size[2]-300, size[3]-22, 45, 18)
268
 
        browsing= String("Browse: ", BROWSE_EVT, size[2]-250, size[3]-22, 245, 18, browsing.val, 30)
269
 
 
270
 
        # The real table
271
 
        def row_draw(rect, data, cols, cell_colors, text_colors):
272
 
                if (len(data)!=len(cols)):
273
 
                        print "Must have same length data and columns"
274
 
                        return
275
 
 
276
 
                if (type(cell_colors)!=ListType): cell_colors= [cell_colors]
277
 
                if (type(text_colors)!=ListType): text_colors= [text_colors]
278
 
 
279
 
                sx= rect[0]
280
 
                for i in range(len(data)):
281
 
                        d= data[i]
282
 
                        c= cols[i]
283
 
        
284
 
                        c, align= c[0], c[1]
285
 
 
286
 
                        if (type(c)==FloatType): c= c*(rect[2]-rect[0])
287
 
                        ex= sx + c
288
 
 
289
 
                        color= cell_colors[i%len(cell_colors)]
290
 
                        apply(glColor3f, color)
291
 
                        glRectf(sx, rect[1], ex, rect[3])
292
 
 
293
 
                        color= text_colors[i%len(text_colors)]
294
 
                        apply(glColor3f, color)
295
 
 
296
 
                        if (type(d)==StringType):
297
 
                                str_width= len(d)*8
298
 
                                if (align=='left'): glRasterPos2f(sx+3, rect[1]+5)
299
 
                                elif (align=='center'): glRasterPos2f((sx+ex)/2 - str_width/2 +3, rect[1]+5)
300
 
                                elif (align=='right'): glRasterPos2f(ex - str_width -3, rect[1]+5)
301
 
 
302
 
                                Text(d)
303
 
                        else:
304
 
                                d(map(int,[sx, rect[1], ex, rect[3]]))
305
 
 
306
 
                        sx= ex
307
 
        # Some colors
308
 
        black= (0.0, 0.0, 0.0)
309
 
        white= (1.0, 1.0, 1.0)
310
 
        red= (0.8, 0.1, 0.1)
311
 
 
312
 
        gray0= (0.17, 0.17, 0.17)
313
 
        gray1= (0.25, 0.25, 0.25)
314
 
        gray2= (0.33, 0.33, 0.33)
315
 
        gray3= (0.41, 0.41, 0.41)
316
 
        gray4= (0.49, 0.49, 0.49)
317
 
        gray5= (0.57, 0.57, 0.57)
318
 
        gray6= (0.65, 0.65, 0.65)
319
 
 
320
 
        cols= [[.3, 'left'], [.2, 'left'], [.4, 'right'], [.1, 'center']]
321
 
 
322
 
        header= [size[0]+20, size[3]-60, size[2]-40, size[3]-40]
323
 
 
324
 
        def sort_byname(co): Button("Name",SORT_BYNAME, co[0]+3, co[1], co[2]-co[0]-4, 19)
325
 
        def sort_bytype(co): Button("Type",SORT_BYTYPE, co[0]+3, co[1], co[2]-co[0]-4, 19)
326
 
        def sort_bydata(co): Button("Data",SORT_BYDATA, co[0]+3, co[1], co[2]-co[0]-4, 19)
327
 
 
328
 
        row_draw(header, [sort_byname, sort_bytype, sort_bydata,'Link'], cols, [gray0, gray1], gray6)
329
 
 
330
 
        if (selected!=-1):
331
 
                table= [size[0]+20, size[1]+220, size[2]-40, size[3]-60]
332
 
        else:
333
 
                table= [size[0]+20, size[1]+20, size[2]-40, size[3]-60]
334
 
 
335
 
        row_height= 25
336
 
        items= (table[3]-table[1])/row_height
337
 
 
338
 
        items= 10
339
 
        if (items>len(browselist)): items= len(browselist)
340
 
 
341
 
        end= len(browselist)-items
342
 
        #if (end>0):
343
 
        #       scr= Scrollbar(SCROLLBAR, table[2]+5, table[1], 20, table[3]-table[1], scr.val, 0.0, end, 0, "Page Up/Down scrolls list.")
344
 
 
345
 
        row= table
346
 
        row[1]= row[3]-row_height
347
 
        start= browse_scrollstart
348
 
        if (start+items>len(browselist)): items= len(browselist)-start
349
 
        for i in range(items):
350
 
                i= start+i
351
 
                data= browselist[i][:]
352
 
 
353
 
                if (i%2): colors= [gray1, gray2]
354
 
                else: colors= [gray2, gray3]
355
 
 
356
 
                # Strange pythonic code
357
 
                def view_doc(co,num=i):
358
 
                        Button("Doc",VIEW_DOC+num, co[0]+3, co[1]+2, co[2]-co[0]-4, 19)
359
 
 
360
 
                def browse_module(co,num=i):
361
 
                        Button("Browse",BROWSE_MODULE+num, co[0]+3, co[1]+2, co[2]-co[0]-4, 19)
362
 
 
363
 
                if (data[1]=='Function'):
364
 
                        if data[3]:
365
 
                                data[3]= view_doc
366
 
                                tcolor= black
367
 
                        else:
368
 
                                tcolor= red
369
 
                                data[2]= 'NO DOC STRING'
370
 
                                data[3]= ''
371
 
                else:
372
 
                        if (data[1]=='Module'): data[3]= browse_module
373
 
                        else: data[3]= ''
374
 
 
375
 
                        tcolor= black
376
 
 
377
 
                row_draw(row, data, cols, colors, tcolor)
378
 
 
379
 
                row[1]= row[1]-row_height
380
 
                row[3]= row[3]-row_height
381
 
 
382
 
        if (selected!=-1):
383
 
                table= [size[0]+20, size[1]+20, size[2]-40, size[1]+180]
384
 
 
385
 
                apply(glColor3f, gray5)
386
 
                glRectf(table[0], table[3], table[2], table[3]+20)
387
 
                apply(glColor3f, gray2)
388
 
                glRectf(table[0], table[1], table[2], table[3])
389
 
 
390
 
                apply(glColor3f, black)
391
 
                glRasterPos2f(table[0]+3, table[3]+5)
392
 
                Text("Function: " + browsing.val + "." + browselist[selected][0])
393
 
 
394
 
                Button("Close", CLOSE_VIEW, table[2]-50, table[3], 45, 18)
395
 
 
396
 
                row_height= 20
397
 
                view_lines= int((table[3]-table[1])/row_height)-1
398
 
 
399
 
                lines= split(browselist[selected][3], "\n")
400
 
                doc_lines= len(lines)
401
 
 
402
 
                sindex= view_lines*selected_page
403
 
                eindex= view_lines*(selected_page+1)
404
 
                if (sindex>0):
405
 
                        sindex= sindex-1
406
 
                        eindex= eindex-1
407
 
 
408
 
                lines= lines[sindex:eindex]
409
 
 
410
 
                y= table[3]-20
411
 
                for line in lines:
412
 
                        glRasterPos2f(table[0]+3, y)
413
 
                        Text(line)
414
 
 
415
 
                        y= y-20
416
 
 
417
 
                if (sindex): Button("Page up", DOC_PAGE_UP, table[2]-100, table[3]-20, 90, 18)
418
 
                if (eindex<doc_lines): Button("Page down", DOC_PAGE_DOWN, table[2]-100, table[1]+5, 90, 18)
419
 
 
420
 
lmouse= [0, 0]
421
 
 
422
 
def fit_scroll():
423
 
        global browse_scrollstart, browselist
424
 
        if (browse_scrollstart<0): browse_scrollstart= 0
425
 
        elif (browse_scrollstart>=len(browselist)): browse_scrollstart= len(browselist)-1
426
 
 
427
 
def event(evt, val):
428
 
        global browse_scrollstart
429
 
 
430
 
        if (evt==QKEY or evt==ESCKEY): Exit()
431
 
        elif val:
432
 
                if (evt in [PAGEUPKEY, PAGEDOWNKEY]):
433
 
                        if (evt==PAGEUPKEY): browse_scrollstart= browse_scrollstart-5
434
 
                        else: browse_scrollstart= browse_scrollstart+5
435
 
                elif (evt in [UPARROWKEY, WHEELUPMOUSE]):
436
 
                        browse_scrollstart -= 1
437
 
                elif (evt in [DOWNARROWKEY, WHEELDOWNMOUSE]):
438
 
                        browse_scrollstart += 1
439
 
                else: return
440
 
 
441
 
                fit_scroll()
442
 
                Redraw()
443
 
 
444
 
def bevent(evt):
445
 
        if (evt==BROWSE_EVT): make_browselist()
446
 
 
447
 
        elif (evt==SORT_BYNAME): sort_browselist(1)
448
 
        elif (evt==SORT_BYTYPE): sort_browselist(2)
449
 
        elif (evt==SORT_BYDATA): sort_browselist(3)
450
 
        
451
 
        elif (evt==DOC_PAGE_UP): view_page(-1)
452
 
        elif (evt==DOC_PAGE_DOWN): view_page(1)
453
 
 
454
 
        elif (evt==BACK_MODULE): browse_module(-1)
455
 
        elif (evt==CLOSE_VIEW): view_doc(-1)
456
 
        elif (evt==FILTER_DISPLAY): toggle_function_filter()
457
 
 
458
 
        #elif (evt==SCROLLBAR):
459
 
        #       global browse_scrollstart
460
 
        #       browse_scrollstart= int(scr.val)
461
 
 
462
 
        elif (evt>=BROWSE_MODULE): browse_module(evt-BROWSE_MODULE)
463
 
        elif (evt>=VIEW_DOC): view_doc(evt-VIEW_DOC)    
464
 
 
465
 
        Redraw()
466
 
 
467
 
Register(draw, event, bevent)