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
|
#!/usr/bin/env python
""" The PyKDE application sampler
This module defines the top-level widgets for displaying the sampler
application.
"""
import inspect
import os
import sys
from qt import SIGNAL, SLOT, PYSIGNAL, Qt
from qt import QVBoxLayout, QLabel, QPixmap, QSplitter, QFrame, QDialog
from qt import QSizePolicy, QHBoxLayout, QSpacerItem, QPushButton
from kdecore import i18n, KAboutData, KApplication, KCmdLineArgs, KGlobal
from kdecore import KGlobalSettings, KWin, KWinModule, KURL, KIcon
from kdeui import KComboBox, KListView, KListViewItem, KTabWidget, KTextEdit
from kdeui import KMainWindow, KPushButton, KSplashScreen, KStdAction
from kdeui import KKeyDialog, KEditToolbar
from kio import KTrader
from kparts import createReadOnlyPart, createReadWritePart
from khtml import KHTMLPart
import about
import lib
try:
__file__
except (NameError, ):
__file__ = sys.argv[0]
sigDoubleClicked = SIGNAL('doubleClicked(QListViewItem *)')
sigViewItemSelected = SIGNAL('selectionChanged(QListViewItem *)')
sigSampleSelected = PYSIGNAL('sample selected')
blank = KURL('about:blank')
def appConfig(group=None):
""" appConfig(group=None) -> returns the application KConfig
"""
config = KGlobal.instance().config()
if group is not None:
config.setGroup(group)
return config
def getIcon(name, group=KIcon.NoGroup, size=KIcon.SizeSmall):
""" returns a kde icon by name
"""
return KGlobal.instance().iconLoader().loadIcon(name, group, size)
def getIconSet(name, group=KIcon.NoGroup, size=KIcon.SizeSmall):
""" returns a kde icon set by name
"""
return KGlobal.instance().iconLoader().loadIconSet(name, group, size)
def buildPart(parent, query, constraint, writable=False):
""" builds the first available offered part on the parent
"""
offers = KTrader.self().query(query, constraint)
for ptr in offers:
if writable:
builder = createReadWritePart
else:
builder = createReadOnlyPart
part = builder(ptr.library(), parent, ptr.name())
if part:
break
return part
class CommonFrame(QFrame):
""" provides a modicum of reuse
"""
def __init__(self, parent):
QFrame.__init__(self, parent)
layout = QVBoxLayout(self)
layout.setAutoAdd(True)
layout.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
class SamplerFrame(CommonFrame):
""" frame type that swaps out old widgets for new when told to do so
"""
def __init__(self, parent):
CommonFrame.__init__(self, parent)
self.widget = None
def setWidget(self, widget):
self.layout().deleteAllItems()
previous = self.widget
if previous:
previous.close()
delattr(self, 'widget')
self.widget = widget
def showSample(self, item, module):
try:
frameType = module.builder()
except (AttributeError, ):
print 'No sample callable defined in %s' % (module.name(), )
else:
frame = frameType(self)
self.setWidget(frame)
frame.show()
class SourceFrame(CommonFrame):
""" frame with part for displaying python source
"""
def __init__(self, parent):
CommonFrame.__init__(self, parent)
query = ''
self.part = buildPart(self, 'application/x-python', query, False)
def showModuleSource(self, item, module):
if not self.part:
print 'No part available for displaying python source.'
return
try:
modulefile = inspect.getabsfile(module.module)
except:
return
self.part.openURL(blank)
if os.path.splitext(modulefile)[-1] == '.py':
self.part.openURL(KURL('file://%s' % modulefile))
class WebFrame(CommonFrame):
""" frame with part for viewing web pages
"""
docBase = 'http://www.riverbankcomputing.com/Docs/PyKDE3/classref/'
def __init__(self, parent):
CommonFrame.__init__(self, parent)
self.part = part = buildPart(self, 'text/html', "Type == 'Service'")
#part.connect(part, SIGNAL('khtmlMousePressEvent(a)'), self.onURL)
def onURL(self, a):
print '****', a
def showDocs(self, item, module):
try:
mod, cls = module.module.docParts
except (AttributeError, ):
url = blank
else:
url = KURL(self.docUrl(mod, cls))
self.part.openURL(url)
def docUrl(self, module, klass):
""" docUrl(name) -> return a doc url given a name from the kde libs
"""
return '%s/%s/%s.html' % (self.docBase, module, klass, )
class OutputFrame(KTextEdit):
""" text widget that acts (just enough) like a file
"""
def __init__(self, parent, filehandle):
KTextEdit.__init__(self, parent)
self.filehandle = filehandle
self.setReadOnly(True)
self.setFont(KGlobalSettings.fixedFont())
def write(self, text):
self.insert(text)
def clear(self):
self.setText('')
def __getattr__(self, name):
return getattr(self.filehandle, name)
class SamplerListView(KListView):
""" the main list view of samples
"""
def __init__(self, parent):
KListView.__init__(self, parent)
self.addColumn(i18n('Sample'))
self.setRootIsDecorated(True)
modules = lib.listmodules()
modules.sort(lambda a, b: cmp(a[0], b[0]))
modmap = dict(modules)
modules = [(name.split('.'), name, mod) for name, mod in modules]
roots, cache = {}, {}
for names, modname, module in modules:
topname, subnames = names[0], names[1:]
item = roots.get(topname, None)
if item is None:
roots[topname] = item = KListViewItem(self, module.labelText())
item.module = module
item.setPixmap(0, getIcon(module.icon()))
bname = ''
subitem = item
for subname in subnames:
bname = '%s.%s' % (bname, subname, )
item = cache.get(bname, None)
if item is None:
subitem = cache[bname] = \
KListViewItem(subitem, module.labelText())
subitem.module = module
subitem.setPixmap(0, getIcon(module.icon()))
subitem = item
for root in roots.values():
self.setOpen(root, True)
class SamplerMainWindow(KMainWindow):
""" the main window
"""
def __init__(self, *args):
KMainWindow.__init__(self, *args)
self.hSplitter = hSplit = QSplitter(Qt.Horizontal, self)
self.samplesList = samplesList = SamplerListView(hSplit)
self.vSplitter = vSplit = QSplitter(Qt.Vertical, hSplit)
self.setCentralWidget(hSplit)
self.setIcon(getIcon('kmail'))
hSplit.setOpaqueResize(True)
vSplit.setOpaqueResize(True)
self.contentTabs = cTabs = KTabWidget(vSplit)
self.outputTabs = oTabs = KTabWidget(vSplit)
self.sampleFrame = SamplerFrame(cTabs)
self.sourceFrame = SourceFrame(cTabs)
self.webFrame = WebFrame(cTabs)
cTabs.insertTab(self.sampleFrame, getIconSet('exec'), i18n('Sample'))
cTabs.insertTab(self.sourceFrame, getIconSet('source'), i18n('Source'))
cTabs.insertTab(self.webFrame, getIconSet('help'), i18n('Docs'))
sys.stdout = self.stdoutFrame = OutputFrame(oTabs, sys.stdout)
sys.stderr = self.stderrFrame = OutputFrame(oTabs, sys.stderr)
termIcons = getIconSet('terminal')
oTabs.insertTab(self.stdoutFrame, termIcons, i18n('stdout'))
oTabs.insertTab(self.stderrFrame, termIcons, i18n('stderr'))
self.resize(640, 480)
height, width = self.height(), self.width()
hSplit.setSizes([width * 0.35, width * 0.65])
vSplit.setSizes([height * 0.80, height * 0.20])
self.xmlRcFileName = os.path.abspath(os.path.join(os.path.dirname(__file__), 'sampler.rc'))
self.setXMLFile(self.xmlRcFileName)
config = appConfig()
actions = self.actionCollection()
actions.readShortcutSettings("", config)
self.quitAction = KStdAction.quit(self.close, actions)
self.toggleMenubarAction = \
KStdAction.showMenubar(self.showMenubar, actions)
self.toggleToolbarAction = \
KStdAction.showToolbar(self.showToolbar, actions)
self.toggleStatusbarAction = \
KStdAction.showStatusbar(self.showStatusbar, actions)
self.configureKeysAction = \
KStdAction.keyBindings(self.showConfigureKeys, actions)
self.configureToolbarAction = \
KStdAction.configureToolbars(self.showConfigureToolbars, actions)
self.configureAppAction = \
KStdAction.preferences(self.showConfiguration, actions)
connect = self.connect
connect(samplesList, sigViewItemSelected, self.sampleSelected)
connect(self, sigSampleSelected, self.reloadModule)
connect(self, sigSampleSelected, self.sourceFrame.showModuleSource)
connect(self, sigSampleSelected, self.sampleFrame.showSample)
connect(self, sigSampleSelected, self.webFrame.showDocs)
self.restoreWindowSize(config)
self.createGUI(self.xmlRcFileName, 0)
self.sourceFrame.part.openURL(KURL('file://%s' % os.path.abspath(__file__)))
def showConfiguration(self):
""" showConfiguration() -> display the config dialog
"""
return
## not yet implemented
dlg = configdialog.ConfigurationDialog(self)
for obj in (self.stderrFrame, self.stdoutFrame, self.pythonShell):
call = getattr(obj, 'configChanged', None)
if call:
self.connect(dlg, util.sigConfigChanged, call)
dlg.show()
def senderCheckShow(self, widget):
""" senderCheckShow(widget) -> show or hide widget if sender is checked
"""
if self.sender().isChecked():
widget.show()
else:
widget.hide()
def showMenubar(self):
""" showMenuBar() -> toggle the menu bar
"""
self.senderCheckShow(self.menuBar())
def showToolbar(self):
""" showToolbar() -> toggle the tool bar
"""
self.senderCheckShow(self.toolBar())
def showStatusbar(self):
""" showStatusbar() -> toggle the status bar
"""
self.senderCheckShow(self.statusBar())
def showConfigureKeys(self):
""" showConfigureKeys() -> show the shortcut keys dialog
"""
ret = KKeyDialog.configure(self.actionCollection(), self)
print ret
if ret == QDialog.Accepted:
actions = self.actionCollection()
actions.writeShortcutSettings(None, appConfig())
def showConfigureToolbars(self):
""" showConfigureToolbars() -> broken
"""
dlg = KEditToolbar(self.actionCollection(), self.xmlRcFileName)
self.connect(dlg, SIGNAL('newToolbarConfig()'), self.rebuildGui)
#connect(self, sigSampleSelected, self.sourceFrame.showModuleSource)
dlg.exec_loop()
def rebuildGui(self):
""" rebuildGui() -> recreate the gui and refresh the palette
"""
self.createGUI(self.xmlRcFileName, 0)
for widget in (self.toolBar(), self.menuBar(), ):
widget.setPalette(self.palette())
def sampleSelected(self):
""" sampleSelected() -> emit the current item and its module
"""
self.stdoutFrame.clear()
self.stderrFrame.clear()
item = self.sender().currentItem()
self.emit(sigSampleSelected, (item, item.module))
def setSplashPixmap(self, pixmap):
""" setSplashPixmap(pixmap) -> assimilate the splash screen pixmap
"""
target = self.sampleFrame
label = QLabel(target)
label.setPixmap(pixmap)
target.setWidget(label)
def reloadModule(self, item, module):
print >> sys.__stdout__, 'reload: ', reload(module.module)
if __name__ == '__main__':
aboutdata = about.about()
KCmdLineArgs.init(sys.argv, aboutdata)
app = KApplication()
splashpix = QPixmap(os.path.join(lib.samplerpath, 'aboutkde.png'))
splash = KSplashScreen(splashpix)
splash.resize(splashpix.size())
splash.show()
mainWindow = SamplerMainWindow()
mainWindow.setSplashPixmap(splashpix)
mainWindow.show()
splash.finish(mainWindow)
app.exec_loop()
|