Start playing on add to playlist.
[nephilim.git] / plugins / Shortcuts.py
blobe76078f40bb4c5a33f676f53aa2b4a8658ed22a0
1 from PyQt4 import QtGui, QtCore
2 from PyKDE4 import kdeui
3 from clMonty import monty
4 from clPlugin import *
5 from misc import *
6 import format
8 class pluginShortcuts(Plugin):
9 keys=None
10 col=None
11 actionPrefix=None
12 def __init__(self, winMain):
13 Plugin.__init__(self, winMain, 'Shortcuts')
15 def _load(self):
16 winMain=self.getWinMain()
17 # Note people wanting to implement global shortcuts in KDE4 (and sniffing through
18 # this code): one needs to have a KApplication running, else shortcuts will fail
19 self.col=kdeui.KActionCollection(None)
20 self.col.addAssociatedWidget(winMain)
21 self.keys=[
22 ['toggleplay', QtCore.Qt.META+QtCore.Qt.Key_Home, self.togglePlay],
23 ['volumeup', QtCore.Qt.META+QtCore.Qt.Key_PageDown, lambda b: monty.setVolume(monty.getVolume()-5)],
24 ['volumedown', QtCore.Qt.META+QtCore.Qt.Key_PageUp, lambda b: monty.setVolume(monty.getVolume()+5)],
25 ['playnext', QtCore.Qt.META+QtCore.Qt.Key_Right, monty.next],
26 ['playprevious', QtCore.Qt.META+QtCore.Qt.Key_Left, monty.previous],
27 ['showosd', QtCore.Qt.META+QtCore.Qt.Key_O, self.showOSD],
28 ['togglewin', QtCore.Qt.META+QtCore.Qt.Key_P, self.toggleWinMain],
30 # Note: don't use any non-alphanumerics in the prefix, as else it won't work
31 self.actionPrefix="shortcuts"
33 for entry in self.keys:
34 name=entry[0]
35 key=entry[1]
36 callback=entry[2]
38 self.debug("%s - %s"%(name, QtGui.QKeySequence(key).toString()))
40 action=kdeui.KAction(winMain) # winMain needed
41 action.setText(name)
42 action.setObjectName(name)
43 action.setGlobalShortcut(kdeui.KShortcut(key))
44 QtCore.QObject.connect(action, QtCore.SIGNAL('triggered(bool)'), callback)
45 self.col.addAction("%s%s"%(self.actionPrefix, action.objectName()), action)
46 def _unload(self):
47 actions=self.col.actions()
48 for action in actions:
49 try:
50 if action.objectName()[0:len(self.actionPrefix)]==self.actionPrefix:
51 self.debug("removing %s"%(action.objectName()))
52 self.col.removeAction(action)
53 except Exception, e:
54 self.important(str(e))
56 def getInfo(self):
57 return "Shortcuts for mpd."
59 def showOSD(self, b):
60 plugins.getPlugin('notify').onSongChange(None)
62 def togglePlay(self, btns=None, mods=None):
63 if monty.isPlaying():
64 monty.pause()
65 else:
66 monty.resume()
68 def toggleWinMain(self, b):
69 w=self.getWinMain()
70 if w.isVisible():
71 w.setVisible(False)
72 else:
73 w.setVisible(True)
75 def _getSettings(self):
76 txt=QtGui.QTextEdit()
77 txt.setReadOnly(True)
78 txt.insertPlainText("Keybindings (read-only)\n")
79 for entry in self.keys:
80 name=entry[0]
81 key=entry[1]
83 txt.insertPlainText("%s\t%s\n"%(name, QtGui.QKeySequence(key).toString()))
84 return [
85 ['', 'Keybindings', 'Current keybindings', txt],