winMain: remove useless variable
[nephilim.git] / nephilim / plugin.py
blobc993bd7773e06a3e8cdc2d22e53bd866e254ad9c
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 import logging
22 import plugins
24 class Plugin(QtCore.QObject):
25 # public, const
26 name = None
27 info = None
28 logger = None
29 mpclient = None
30 settings = None
32 # public, read-only
33 loaded = False
34 dock_widget = None
35 _parent = None
37 o = None
38 DEFAULTS = {}
40 def __init__(self, parent, mpclient, name):
41 QtCore.QObject.__init__(self)
42 self.name = name
43 self.mpclient = mpclient
44 self._parent = parent
45 self.loaded = False
46 self.settings = QtCore.QSettings()
48 self.logger = logging.getLogger(self.name)
50 #init settings
51 self.logger.debug('Initializing default settings.')
52 self.settings.beginGroup(name)
53 for key in self.DEFAULTS:
54 if not self.settings.contains(key):
55 self.settings.setValue(key, self.DEFAULTS[key])
56 self.settings.endGroup()
58 def parent(self):
59 return self._parent
61 def load(self):
62 self.logger.info('loading')
63 self._load()
64 opts = QtGui.QDockWidget.DockWidgetClosable|QtGui.QDockWidget.DockWidgetMovable
65 self.dock_widget = self._get_dock_widget()
66 if self.dock_widget:
67 self.dock_widget.setFeatures(opts)
68 QtGui.QApplication.instance().main_win.add_dock(self.dock_widget)
69 QtGui.QApplication.instance().main_win.restore_layout()
70 self.mpclient.connect_changed.connect(self.set_enabled)
71 self.loaded = True
72 def unload(self):
73 if not self.loaded:
74 return
75 self.logger.info("unloading")
76 self._unload()
77 if self.dock_widget:
78 QtGui.QApplication.instance().main_win.remove_dock(self.dock_widget)
79 self.dock_widget = None
80 self.settingsWidget = None
81 self.mpclient.connect_changed.disconnect(self.set_enabled)
82 self.loaded = False
83 def set_enabled(self, val):
84 if self.o:
85 self.o.setEnabled(val)
87 class SettingsWidget(QtGui.QWidget):
88 """ plugins should subclass this"""
89 plugin = None
90 settings = None
92 def __init__(self, plugin):
93 QtGui.QWidget.__init__(self)
94 self.plugin = plugin
95 self.settings = QtCore.QSettings()
97 def settings(self):
98 return self.settings
100 def savesettings(self):
101 """ reimplement this"""
102 pass
104 def _add_widget(self, widget, label = '', tooltip = ''):
105 """adds a widget with label"""
106 if not self.layout():
107 self.plugin.logger.error('Attempted to call add_widget with no layout set.')
108 widget.setToolTip(tooltip)
109 layout = QtGui.QHBoxLayout()
110 layout.addWidget(QtGui.QLabel(label))
111 layout.addWidget(widget)
112 self.layout().addLayout(layout)
114 def get_settings_widget(self):
115 """Should return subclassed SettingsWidget."""
116 return
118 def _get_dock_widget(self):
119 """Override this one."""
120 return None
121 def _create_dock(self, widget):
122 """Creates a QDockWidget with _parent $_parent containing widget $widget."""
123 dock=QtGui.QDockWidget(self.name, QtGui.QApplication.instance().main_win)
124 dock.setObjectName(self.name)
125 dock.setWidget(widget)
126 dock.setAllowedAreas(QtCore.Qt.AllDockWidgetAreas)
128 return dock
129 def _load(self):
130 """Override this one."""
131 return
132 def _unload(self):
133 """Override this one."""
134 return