song: make keys case-insensitive
[nephilim.git] / nephilim / plugin.py
blob37780d768d06a7948994ca7905d34ce23461f3c1
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtGui, QtCore
20 from PyQt4.QtCore import QVariant
21 import logging
23 import plugins
25 class Plugin(QtCore.QObject):
26 # public, const
27 name = None
28 info = None
29 logger = None
30 mpclient = None
31 settings = None
33 # public, read-only
34 loaded = False
35 dock_widget = None
36 _parent = None
38 o = None
39 DEFAULTS = {}
41 def __init__(self, parent, mpclient, name):
42 QtCore.QObject.__init__(self)
43 self.name = name
44 self.mpclient = mpclient
45 self._parent = parent
46 self.loaded = False
47 self.settings = QtCore.QSettings()
49 self.logger = logging.getLogger(self.name)
51 #init settings
52 self.logger.debug('Initializing default settings.')
53 self.settings.beginGroup(name)
54 for key in self.DEFAULTS:
55 if not self.settings.contains(key):
56 self.settings.setValue(key, QVariant(self.DEFAULTS[key]))
57 self.settings.endGroup()
59 def parent(self):
60 return self._parent
62 def load(self):
63 self.logger.info('loading')
64 self._load()
65 opts = QtGui.QDockWidget.DockWidgetClosable|QtGui.QDockWidget.DockWidgetMovable
66 self.dock_widget = self._get_dock_widget()
67 if self.dock_widget:
68 self.dock_widget.setFeatures(opts)
69 QtGui.QApplication.instance().main_win.add_dock(self.dock_widget)
70 QtGui.QApplication.instance().main_win.restore_layout()
71 self.mpclient.connect_changed.connect(self.set_enabled)
72 self.loaded = True
73 def unload(self):
74 if not self.loaded:
75 return
76 self.logger.info("unloading")
77 self._unload()
78 if self.dock_widget:
79 QtGui.QApplication.instance().main_win.remove_dock(self.dock_widget)
80 self.dock_widget = None
81 self.settingsWidget = None
82 self.mpclient.connect_changed.disconnect(self.set_enabled)
83 self.loaded = False
84 def set_enabled(self, val):
85 if self.o:
86 self.o.setEnabled(val)
88 class SettingsWidget(QtGui.QWidget):
89 """ plugins should subclass this"""
90 plugin = None
91 settings = None
93 def __init__(self, plugin):
94 QtGui.QWidget.__init__(self)
95 self.plugin = plugin
96 self.settings = QtCore.QSettings()
98 def settings(self):
99 return self.settings
101 def savesettings(self):
102 """ reimplement this"""
103 pass
105 def _add_widget(self, widget, label = '', tooltip = ''):
106 """adds a widget with label"""
107 if not self.layout():
108 self.plugin.logger.error('Attempted to call add_widget with no layout set.')
109 widget.setToolTip(tooltip)
110 layout = QtGui.QHBoxLayout()
111 layout.addWidget(QtGui.QLabel(label))
112 layout.addWidget(widget)
113 self.layout().addLayout(layout)
115 def get_settings_widget(self):
116 """Should return subclassed SettingsWidget."""
117 return
119 def _get_dock_widget(self):
120 """Override this one."""
121 return None
122 def _create_dock(self, widget):
123 """Creates a QDockWidget with _parent $_parent containing widget $widget."""
124 dock=QtGui.QDockWidget(self.name, QtGui.QApplication.instance().main_win)
125 dock.setObjectName(self.name)
126 dock.setWidget(widget)
127 dock.setAllowedAreas(QtCore.Qt.AllDockWidgetAreas)
129 return dock
130 def _load(self):
131 """Override this one."""
132 return
133 def _unload(self):
134 """Override this one."""
135 return