from PyQt4 import QtGui, QtCore from PyKDE4 import kdeui from clMonty import monty from clPlugin import * from misc import * import format class pluginShortcuts(Plugin): keys=None col=None actionPrefix=None def __init__(self, winMain): Plugin.__init__(self, winMain, 'Shortcuts') def _load(self): winMain=self.getWinMain() # Note people wanting to implement global shortcuts in KDE4 (and sniffing through # this code): one needs to have a KApplication running, else shortcuts will fail self.col=kdeui.KActionCollection(None) self.col.addAssociatedWidget(winMain) self.keys=[ ['toggleplay', QtCore.Qt.META+QtCore.Qt.Key_Home, self.togglePlay], ['volumeup', QtCore.Qt.META+QtCore.Qt.Key_PageDown, lambda b: monty.setVolume(monty.getVolume()-5)], ['volumedown', QtCore.Qt.META+QtCore.Qt.Key_PageUp, lambda b: monty.setVolume(monty.getVolume()+5)], ['playnext', QtCore.Qt.META+QtCore.Qt.Key_Right, monty.next], ['playprevious', QtCore.Qt.META+QtCore.Qt.Key_Left, monty.previous], ['showosd', QtCore.Qt.META+QtCore.Qt.Key_O, self.showOSD], ['togglewin', QtCore.Qt.META+QtCore.Qt.Key_P, self.toggleWinMain], ] # Note: don't use any non-alphanumerics in the prefix, as else it won't work self.actionPrefix="shortcuts" for entry in self.keys: name=entry[0] key=entry[1] callback=entry[2] self.debug("%s - %s"%(name, QtGui.QKeySequence(key).toString())) action=kdeui.KAction(winMain) # winMain needed action.setText(name) action.setObjectName(name) action.setGlobalShortcut(kdeui.KShortcut(key)) QtCore.QObject.connect(action, QtCore.SIGNAL('triggered(bool)'), callback) self.col.addAction("%s%s"%(self.actionPrefix, action.objectName()), action) def _unload(self): actions=self.col.actions() for action in actions: try: if action.objectName()[0:len(self.actionPrefix)]==self.actionPrefix: self.debug("removing %s"%(action.objectName())) self.col.removeAction(action) except Exception, e: self.important(str(e)) def getInfo(self): return "Shortcuts for mpd." def showOSD(self, b): plugins.getPlugin('notify').onSongChange(None) def togglePlay(self, btns=None, mods=None): if monty.isPlaying(): monty.pause() else: monty.resume() def toggleWinMain(self, b): w=self.getWinMain() if w.isVisible(): w.setVisible(False) else: w.setVisible(True) def _getSettings(self): txt=QtGui.QTextEdit() txt.setReadOnly(True) txt.insertPlainText("Keybindings (read-only)\n") for entry in self.keys: name=entry[0] key=entry[1] txt.insertPlainText("%s\t%s\n"%(name, QtGui.QKeySequence(key).toString())) return [ ['', 'Keybindings', 'Current keybindings', txt], ]