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
|
#!/usr/bin/env python
"""
This is a rough Python translation of the ideas presented in this KMDI tutorial:
http://web.tiscali.it/andreabergia/kmditutorial.html
What does work:
IDEAlMode - yay!
Adding and closing child views
Two-way syncing between a tool widget and a matching child view
All is not rosy, however:
Instances of the KmdiExample maintain a dictionary of child views. Values
cannot be deleted from this dictionary during a window close (causes an
immediate segfault).
Child views created after initialization aren't numbered correctly; given
the first problem, it's harder to do this than it's really worth.
The example segfaults at shutdown if the tool (on the left) is is open but
is not in overlap-mode.
"""
import os
import sys
from qt import SIGNAL, QVBoxLayout, QLabel
from kdecore import i18n, KAboutData, KApplication, KGlobal, KIcon, KCmdLineArgs
from kdeui import KDockWidget, KListBox, KStdAction
try:
from kmdi import KMdi, KMdiMainFrm, KMdiChildView
except (ImportError, ):
print 'Exception importing KMDI; check your PyKDE installation'
sys.exit(1)
sigChildCloseRequest = SIGNAL('childWindowCloseRequest(KMdiChildView *)')
sigChildViewActivated = SIGNAL('viewActivated(KMdiChildView *)')
sigBoxSelectionChanged = SIGNAL('selectionChanged(QListBoxItem *)')
def getIcon(name, group=KIcon.NoGroup, size=KIcon.SizeSmall):
""" returns a kde icon by name
"""
return KGlobal.instance().iconLoader().loadIcon(name, group, size)
class KmdiExample(KMdiMainFrm):
""" KmdiExample(parent=None) -> an example KMdiMainFrm window
"""
uifilebase = 'uikmdi.rc'
viewIcons = ('network', 'email', 'stop', 'back', 'forward', )
toolIcons = ('view_icon', 'configure')
def __init__(self, parent=None):
KMdiMainFrm.__init__(self, parent, 'KmdiExample', KMdi.IDEAlMode)
xmlfile = os.path.join('.', self.uifilebase)
self.setXMLFile(os.path.abspath(xmlfile))
actions = self.actionCollection()
self.openNewAction = KStdAction.openNew(self.newView, actions)
self.quitAction = KStdAction.quit(self.close, actions)
self.closeAction = KStdAction.close(self.closeActiveChild, actions)
self.createGUI(None)
self.statusBar()
self.resize(400, 300)
self.tools = {}
for idx, ico in enumerate(self.toolIcons):
wid = KListBox(self, 'list%s' % idx)
self.makeTool(wid, 'Tool %s' % idx, ico)
## smells
self.mainToolWidget = maintool = self.tools['Tool 0'][0]
self.childs = {}
for idx, ico in enumerate(self.viewIcons):
self.makeView('View %s' % idx, ico, ico)
self.connect(self, sigChildViewActivated, self.activatedMessage)
self.connect(self, sigChildViewActivated, self.syncFromChildView)
self.connect(maintool, sigBoxSelectionChanged, self.syncFromMainTool)
self.syncFromChildView(self.activeWindow())
def syncFromMainTool(self, item):
""" activate the view that matches the item text
"""
try:
self.activateView(self.findWindow(item.text()))
except (RuntimeError, ):
pass
def syncFromChildView(self, child):
""" sync the main tool to the indicated child
"""
maintool = self.mainToolWidget
item = maintool.findItem(child.tabCaption())
if item:
maintool.setSelected(item, True)
def makeTool(self, widget, caption, icon, percent=50):
""" makes a tool from the widget
"""
tip = i18n('%s Tool Tip' % caption)
dock = KDockWidget.DockLeft
maindock = self.getMainDockWidget()
widget.setIcon(getIcon(icon))
tool = self.addToolWindow(widget, dock, maindock, percent, tip, caption)
self.tools[caption] = (widget, tool)
def makeView(self, label, icon, text):
""" makes a child view with a text label and a pixmap label
"""
view = KMdiChildView(label, self)
self.childs[label] = view
view.setIcon(getIcon(icon))
layout = QVBoxLayout(view)
layout.setAutoAdd(True)
lbl = i18n('Label for a view with an icon named %s' % text)
lbl = QLabel(lbl, view)
pxm = QLabel('', view)
pxm.setPixmap(getIcon(icon, size=KIcon.SizeLarge))
self.addWindow(view)
self.mainToolWidget.insertItem(label)
self.connect(view, sigChildCloseRequest, self.closeChild)
def removeMainToolItem(self, view):
""" remove item from the main list tool that corresponds to the view
"""
maintool = self.mainToolWidget
maintool.takeItem(maintool.findItem(view.tabCaption(), 0))
def newView(self):
""" make a view when the user invokes the new action
"""
self.makeView('View %s' % len(self.childs), 'network', 'A Fresh View')
self.syncFromChildView(self.activeWindow())
def closeActiveChild(self):
""" close the current view
"""
self.removeMainToolItem(self.activeWindow())
self.closeActiveView()
self.syncFromChildView(self.activeWindow())
def closeChild(self, which):
""" called to close a view from its tab close button
"""
try:
caption = which.tabCaption()
except (AttributeError, ):
## probably None; bug in kmdi?
return
self.removeMainToolItem(which)
which.close()
self.statusBar().message(i18n('%s closed' % caption))
self.syncFromChildView(self.activeWindow())
def activatedMessage(self, view):
""" updates the status bar with the caption of the current view
"""
try:
self.statusBar().message(i18n('%s activated' % view.tabCaption()))
except (RuntimeError, ):
## sometimes the status bar or the current object is already gone...
pass
if __name__ == '__main__':
aname = 'PyKDE KMDI Sample'
desc = 'A Simple PyKDE KMDI Sample'
ver = '1.0'
lic = KAboutData.License_GPL
author = 'Troy Melhase'
authormail = 'troy@gci.net'
about = KAboutData(aname, aname, ver, desc, lic, '%s (c) 2004' % authormail)
about.addAuthor(author, 'hi, mom!', authormail)
about.addAuthor ('Jim Bublitz', 'For PyKDE', 'jbublitz@nwinternet.com')
KCmdLineArgs.init(sys.argv, about)
app = KApplication()
mainWindow = KmdiExample()
mainWindow.show()
app.exec_loop()
|