summaryrefslogtreecommitdiff
path: root/kberylsettings/pluginframe.py
diff options
context:
space:
mode:
authorracarr <racarr>2006-12-15 19:45:12 +0000
committerracarr <racarr>2006-12-15 19:45:12 +0000
commite9c4bd69ccee487f8d32e8d2e2072dc46fb36f09 (patch)
tree22e638e15060218ba4d180d007cf3e3d60a52df1 /kberylsettings/pluginframe.py
downloadkberylsettings-e9c4bd69ccee487f8d32e8d2e2072dc46fb36f09.tar.gz
kberylsettings-e9c4bd69ccee487f8d32e8d2e2072dc46fb36f09.tar.bz2
racarr: Add Troy Mellhase's kberylsettings manager to branches
Diffstat (limited to 'kberylsettings/pluginframe.py')
-rw-r--r--kberylsettings/pluginframe.py296
1 files changed, 296 insertions, 0 deletions
diff --git a/kberylsettings/pluginframe.py b/kberylsettings/pluginframe.py
new file mode 100644
index 0000000..9ce1264
--- /dev/null
+++ b/kberylsettings/pluginframe.py
@@ -0,0 +1,296 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from qt import Qt, QFrame, QListViewItem, QListViewItemIterator, QVBoxLayout
+from kdecore import KIconLoader, i18n
+from kdeui import KListView
+
+from kberylsettings.beryl import Setting
+from kberylsettings.lib import Signals, icon
+from kberylsettings.widget import Popup
+
+
+class PluginFrame(QFrame):
+ """ PluginFrame -> encapsulates an icon ListView and a tree
+ ListView and swaps between them.
+
+ """
+ def __init__(self, parent):
+ QFrame.__init__(self, parent)
+ layout = QVBoxLayout(self)
+ layout.setAutoAdd(True)
+ self.iconView = PluginIconView(self)
+ self.treeView = PluginTreeView(self)
+
+ root = self.topLevelWidget()
+ connect = self.connect
+ connect(root, Signals.viewModeChanged, self.onViewMode)
+ connect(root, Signals.iconSizeChanged, self.iconView.onIconSize)
+
+ views = (self.iconView, self.treeView)
+ for view in views:
+ connect(view, Signals.showSettings, Signals.showSettings)
+ connect(view, Signals.berylSettingChanged, root.settingsChanged)
+ connect(view, Signals.pluginAbout, root.showAboutPlugin)
+ connect(view, Signals.statusMessage, root.showMessage)
+ connect(root, Signals.berylContextChanged, view.onBerylContext)
+ for receiver in views:
+ connect(view, Signals.berylPluginEnabled,
+ receiver.enablePluginItem)
+
+ def onViewMode(self, value):
+ """ user requested view mode change
+
+ @param value view mode; 0 or 1
+ @return None
+ """
+ items = [self.treeView, self.iconView, ]
+ if value:
+ items.reverse()
+
+ last = items[0].selectedItem()
+ if last:
+ for item in items[1]:
+ item.setSelected(item.text(0) == last.text(0))
+ items[0].hide()
+ items[1].show()
+
+
+class PluginView(KListView):
+ """ PluginView -> mixin for shared view functionality
+
+ """
+ def __init__(self, parent):
+ KListView.__init__(self, parent)
+ self.addColumn('')
+ self.setSorting(-1)
+ self.header().hide()
+ self.loader = KIconLoader(self.__class__.__name__)
+ self.connect(self, Signals.contextMenuRequest, self.onContextMenu)
+
+ def __iter__(self):
+ """ iterate over QListView items
+
+ """
+ it = QListViewItemIterator(self)
+ item = it.current()
+ while item:
+ yield item
+ it += 1
+ item = it.current()
+
+ def addPlugins(self):
+ """ adds list view items for every plugin in context
+
+ @return None
+ """
+ active = self.context.active
+ for plugin in self.context.plugins:
+ self.listItem(plugin, active)
+
+ def listItem(self, plugin, active):
+ """ create a list view item for a plugin
+
+ @param plugin beryl settings Plugin object
+ @param active sequence of active plugin names
+ @return QListViewItem instance
+ """
+ item = self.findItem(plugin.ShortDesc, 0)
+ if not item:
+ item = QListViewItem(self, plugin.ShortDesc)
+ item.setVisible(True)
+ item.setEnabled(plugin.Name in active or plugin.isGeneral)
+ item.setPixmap(0, plugin.icon(self.iconSize, self.loader))
+ item.setText(0, plugin.ShortDesc)
+ return item
+
+ def onBerylContext(self, context):
+ """ beryl settings context has changed
+
+ @param context
+ @return None
+ """
+ self.context = context
+ self.addPlugins()
+
+ def onContextMenu(self, item, point, value):
+ """ display a context menu
+
+ @param item QListViewItem instance
+ @param point QPoint at click
+ @param value ignored
+ @return None
+ """
+ if not item:
+ return
+
+ plugins = list(self.context.plugins)
+ plugin = [p for p in plugins if p.ShortDesc==item.text(0)]
+ if not plugin:
+ return
+ else:
+ plugin = plugin[0]
+
+ pop = Popup(self)
+ pop.insertItem(plugin.ShortDesc)
+ pop.insertSeparator()
+ if plugin.Name in self.context.active:
+ menuid = pop.insertItem(i18n('&Disable'))
+ else:
+ menuid = pop.insertItem(i18n('&Enable'))
+ pop.setItemEnabled(menuid, not plugin.isGeneral)
+ pop.setItemParameter(menuid, plugins.index(plugin))
+ pop.connectItem(menuid, self.enablePlugin)
+ pop.popup(point)
+
+ def enablePlugin(self, index):
+ """ toggle the active state of a plugin
+
+ @param index position of plugin within plugins list
+ @return None
+ """
+ plugin = self.context.plugin(index)
+ active = self.context.active
+ if plugin.Name in active:
+ active.remove(plugin.Name)
+ else:
+ active.append(plugin.Name)
+ self.context.active = active
+ self.emit(Signals.berylPluginEnabled, (plugin, plugin.Name in active))
+ self.emit(Signals.berylSettingChanged, ())
+
+ def enablePluginItem(self, plugin, enable):
+ """ toggle the enabled state of a plugin list view item
+
+ @param index position of plugin within plugins list
+ @return None
+ """
+ for item in self:
+ if item.text(0) == plugin.ShortDesc:
+ item.setOpen(enable)
+ item.setEnabled(enable)
+
+
+class PluginIconView(PluginView):
+ """ PluginIconView -> icon list view of plugins
+
+ """
+ iconSize = 32
+
+ def __init__(self, parent):
+ PluginView.__init__(self, parent)
+ self.connect(self, Signals.itemClicked, self.onSelected)
+ self.selectedPlugin = self.selectedSetting = None
+
+ def onIconSize(self, size):
+ """ update the icon size
+
+ @param size icon size as integer
+ @return None
+ """
+ self.iconSize = size
+ for item in self:
+ plugin = self.context.plugin(item.text(0))
+ if plugin:
+ item.setPixmap(0, plugin.icon(size, self.loader))
+ else:
+ item.setPixmap(0, Setting.icon(item.text(0), size))
+
+ def onSelected(self, item):
+ """ emit a signal if a plugin item is selected
+
+ @param item QListViewItem instance
+ @return None
+ """
+ plugin = self.context.plugin(item.text(0))
+ text = item.text(0)
+ if plugin:
+ self.navToSettings(plugin)
+ self.emit(Signals.pluginAbout, (plugin, ))
+ elif item == self.firstChild():
+ for item in self:
+ plugin = self.context.plugin(item.text(0))
+ if plugin:
+ item.setVisible(True)
+ else:
+ item.setVisible(False)
+ elif self.selectedSetting != text:
+ self.selectedSetting = text
+ plugin = self.selectedPlugin
+ name = str(text)
+ print '***', name
+ self.emit(Signals.showSettings, (plugin, name))
+
+ def navToSettings(self, plugin):
+ self.emit(Signals.statusMessage, ('%s.navToSettings(%r)' % (self.__class__.__name__, plugin), ))
+ self.selectedPlugin = plugin
+ groups = plugin.settings
+
+ for k, v in groups.items():
+ print k
+ for a in v:
+ print ' ', a.ShortDesc
+
+ keys = groups.keys()
+ keys.sort()
+
+ for key in keys:
+ label = key #Setting.label(key)
+ sub = self.findItem(label, 0)
+ if not sub:
+ sub = QListViewItem(self, self.lastItem(), label, )
+ sub.setPixmap(0, Setting.icon(key, self.iconSize))
+
+ back = self.findItem(i18n('Back'), 0)
+ if not back:
+ back = QListViewItem(self, i18n('Back'))
+ back.setPixmap(0, icon('back', size=self.iconSize))
+
+ for item in self:
+ plugin = self.context.plugin(item.text(0))
+ if plugin or (item.text(0) not in keys and item != back):
+ item.setVisible(False)
+ else:
+ item.setVisible(True)
+
+
+class PluginTreeView(PluginView):
+ """ PluginTreeView -> tree list view of plugins
+
+ """
+ iconSize = 16
+
+ def __init__(self, parent):
+ PluginView.__init__(self, parent)
+ self.setRootIsDecorated(True)
+ self.connect(self, Signals.itemSelected, self.onSelected)
+
+ def listItem(self, plugin, active):
+ """ create an item for the plugin and sub items for its settings
+
+ @param plugin beryl settings Plugin object
+ @param active sequence of active plugin names
+ @param size icon size
+ @return QListViewItem instance with children
+ """
+ item = PluginView.listItem(self, plugin, active)
+ groups = plugin.settings
+ keys = groups.keys()
+ keys.sort()
+ for key in keys:
+ sub = QListViewItem(item, key)
+ sub.setPixmap(0, Setting.icon(key, self.iconSize))
+ return item
+
+ def onSelected(self, item):
+ """ emit a signal if a plugin item is selected
+
+ @param item QListViewItem instance
+ @return None
+ """
+ plugin = self.context.plugin(item.text(0))
+ if plugin:
+ self.emit(Signals.pluginAbout, (plugin, ))
+ else:
+ plugin = self.context.plugin(item.parent().text(0))
+ name = str(item.text(0))
+ self.emit(Signals.showSettings, (plugin, name))