summaryrefslogtreecommitdiff
path: root/Shortcuts.py
blob: e76078f40bb4c5a33f676f53aa2b4a8658ed22a0 (plain)
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
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],
            ]