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
25 class Plugin(QtCore
.QObject
):
41 def __init__(self
, parent
, mpclient
, name
):
42 QtCore
.QObject
.__init
__(self
)
44 self
.mpclient
= mpclient
47 self
._settings
= QtCore
.QSettings()
49 self
.logger
= logging
.getLogger(self
.name
)
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()
63 def set_status(self
, status
):
64 self
._parent
.setStatus(status
)
69 self
.logger
.info('loading')
71 opts
= QtGui
.QDockWidget
.DockWidgetClosable|QtGui
.QDockWidget
.DockWidgetMovable
72 QtGui
.QApplication
.instance().main_win
.add_dock(self
.get_dock_widget(opts
))
73 QtGui
.QApplication
.instance().main_win
.restore_layout()
74 self
.connect(self
.mpclient
, QtCore
.SIGNAL('connect_changed'), self
.set_enabled
)
79 self
.logger
.info("unloading")
81 dock_widget
= self
.get_dock_widget()
83 QtGui
.QApplication
.instance().main_win
.remove_dock(dock_widget
)
84 self
._dock
_widget
= None
85 self
._settingsWidget
= None
86 self
.disconnect(self
.mpclient
, QtCore
.SIGNAL('connect_changed'), self
.set_enabled
)
88 def set_enabled(self
, val
):
90 self
.o
.setEnabled(val
)
92 def get_dock_widget(self
, opts
= None):
93 self
._dock
_widget
= self
._get
_dock
_widget
()
94 if self
._dock
_widget
and opts
:
95 self
._dock
_widget
.setFeatures(opts
)
96 self
._dock
_widget
.setAllowedAreas(QtCore
.Qt
.AllDockWidgetAreas
)
97 return self
._dock
_widget
99 class SettingsWidget(QtGui
.QWidget
):
100 """ plugins should subclass this"""
104 def __init__(self
, plugin
):
105 QtGui
.QWidget
.__init
__(self
)
107 self
._settings
= QtCore
.QSettings()
110 return self
._settings
112 def save_settings(self
):
113 """ reimplement this"""
116 def _add_widget(self
, widget
, label
= '', tooltip
= ''):
117 """adds a widget with label"""
118 if not self
.layout():
119 self
.plugin
.logger
.error('Attempted to call add_widget with no layout set.')
120 widget
.setToolTip(tooltip
)
121 layout
= QtGui
.QHBoxLayout()
122 layout
.addWidget(QtGui
.QLabel(label
))
123 layout
.addWidget(widget
)
124 self
.layout().addLayout(layout
)
126 def get_settings_widget(self
):
127 """Should return subclassed SettingsWidget."""
130 def _get_dock_widget(self
):
131 """Override this one."""
133 def _create_dock(self
, widget
):
134 """Creates a QDockWidget with _parent $_parent containing widget $widget."""
135 dock
=QtGui
.QDockWidget(self
.name
, QtGui
.QApplication
.instance().main_win
)
136 dock
.setObjectName(self
.name
)
137 dock
.setWidget(widget
)
141 """Override this one."""
144 """Override this one."""