summaryrefslogtreecommitdiff
path: root/kberylsettings/beryl.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/beryl.py
downloadkberylsettings-e9c4bd69ccee487f8d32e8d2e2072dc46fb36f09.tar.gz
kberylsettings-e9c4bd69ccee487f8d32e8d2e2072dc46fb36f09.tar.bz2
racarr: Add Troy Mellhase's kberylsettings manager to branches
Diffstat (limited to 'kberylsettings/beryl.py')
-rw-r--r--kberylsettings/beryl.py209
1 files changed, 209 insertions, 0 deletions
diff --git a/kberylsettings/beryl.py b/kberylsettings/beryl.py
new file mode 100644
index 0000000..ed0f5ae
--- /dev/null
+++ b/kberylsettings/beryl.py
@@ -0,0 +1,209 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import berylsettings
+import re
+from os.path import abspath, exists
+from qt import QImage, QPixmap, QObject
+from kdecore import KIcon
+from kberylsettings.lib import appDebug, appBase, iconCache, icon, Signals
+
+
+class Context(QObject):
+ activePluginsSettingName = 'active_plugins'
+
+ def __init__(self, context=None):
+ QObject.__init__(self)
+ if context is None:
+ context = berylsettings.Context()
+ context.read()
+ self.context = context
+
+
+ def plugins(self):
+ seq = [Plugin(p) for p in self.context.Plugins]
+ seq.sort(reverse=True)
+ return iter(seq)
+
+ plugins = property(plugins)
+
+
+ def plugin(self, value):
+ try:
+ value + 0
+ except (TypeError, ):
+ for plugin in self.plugins:
+ if value == plugin.ShortDesc:
+ return plugin
+ else:
+ return list(self.plugins)[value]
+
+
+ def getActive(self):
+ act = self.general.Setting(self.activePluginsSettingName).Value
+ return act + [Plugin.generalName, ]
+
+ def setActive(self, active):
+ self.general.Setting(self.activePluginsSettingName).Value = active
+
+ active = property(getActive, setActive)
+
+
+ def general(self):
+ return self.context.Plugin(Plugin.generalName)
+
+ general = property(general)
+
+
+ def write(self):
+ self.emit(Signals.statusMessage, ('Saving Beryl settings....', ))
+ self.context.write()
+ berylsettings.send_reload()
+ self.emit(Signals.statusMessage, ('Beryl settings reloaded.', ))
+
+
+class Plugin:
+ generalName = '_'
+
+ def __init__(self, plugin):
+ self.plugin = plugin
+
+
+ def __getattr__(self, value):
+ return getattr(self.plugin, value)
+
+
+ def __cmp__(self, other):
+ if self.isGeneral:
+ return -1
+ return cmp(self.plugin.ShortDesc,
+ getattr(other.plugin, 'ShortDesc', None))
+
+
+ def isGeneral(self):
+ return self.plugin.Name == self.generalName
+ isGeneral = property(isGeneral)
+
+
+ def icon(self, size, loader):
+ if appDebug:
+ return loader.loadIcon('empty', KIcon.NoGroup, size)
+ name = self.plugin.Name
+ try:
+ pix = iconCache[(name, size)]
+ except (KeyError, ):
+ path = abspath(appBase + '/pixmaps/beryl-settings-section-%s.svg' % name)
+ if not exists(path):
+ path = 'unknown'
+ ico = loader.loadIcon(path, KIcon.NoGroup)
+ img = ico.convertToImage()
+ pix = iconCache[(name, size)] = QPixmap()
+ pix.convertFromImage(img.smoothScale(size, size, QImage.ScaleMin))
+ return pix
+
+
+ def nativeSettingsGroups(self):
+ settingsMap = {}
+ for setting in self.plugin.Settings:
+ seq = settingsMap.setdefault(setting.Type, [])
+ seq.append(setting)
+ return settingsMap
+
+
+ def regroupSettings(self):
+ mapping = self.nativeSettingsGroups()
+ remap = {}
+ for typ, values in mapping.items():
+ other = self.alternateGroup(typ, values, mapping)
+ seq = remap.setdefault(other, [])
+ values.sort()
+ seq.extend(values)
+ return remap
+
+
+ def alternateGroup(self, typ, val, mapping):
+ if typ in ('Int', 'Float'):
+ return 'Numeric Values'
+ if typ in ('Bool', 'String'):
+ return 'Choices'
+ if typ in ('Binding', ):
+ return 'Bindings'
+ return typ
+
+ def settings(self):
+ mapping = {}
+ settings = [Setting(s) for s in self.plugin.Settings]
+ for setting in settings:
+ key = self.alternateGroup(setting.Type, None, None)
+ seq = mapping.setdefault(key, [])
+ seq.append(setting)
+ for seq in mapping.values():
+ seq.sort()
+ return mapping
+
+ settings = property(settings)
+
+
+class Setting:
+ iconNameMap = {
+ 'Binding' : 'mouse',
+ 'Bool' : 'apply',
+ 'Int' : 'configure',
+ 'List of String' : 'view_text',
+ 'String' : 'view_detailed',
+ 'Back':'back',
+ }
+
+ labelMap = {
+ 'Binding' : 'Keyboard/Mouse',
+ 'Bool' : 'Choices',
+ 'Int' : 'Values',
+ 'List of String' : 'Files',
+ 'String' : 'Values',
+ }
+
+ def __init__(self, setting):
+ self.setting = setting
+
+ def __cmp__(self, other):
+ try:
+ a = int(self.ShortDesc.split()[-1])
+ except:
+ a = self.ShortDesc
+ try:
+ b = int(other.ShortDesc.split()[-1])
+ except:
+ b = getattr(other, 'ShortDesc', None)
+ return cmp(a, b)
+
+ def __getattr__(self, value):
+ return getattr(self.setting, value)
+
+ def icon(cls, value, size):
+ name = cls.iconNameMap.get(str(value), cls.iconNameMap['Bool'])
+ return icon(name, size=size)
+ icon = classmethod(icon)
+
+ def label(self):
+ value = self.ShortDesc
+ value = value.title()
+ for pattern, repl in self.fixes:
+ value = re.sub(pattern, repl, value)
+ return value
+ return settingLabelMap.get(value, value)
+ label = property(label)
+
+ fixes = [
+ (' A ', ' a '),
+ (' And ', ' and '),
+ (' For ', ' for '),
+ (' In ', ' in '),
+ (' The ', ' the '),
+ (' To ', ' to '),
+ (' Of ', ' of '),
+ (' On ', ' on '),
+ (' Fsp', ' FSP'),
+ (' Svg', ' SVG'),
+ (' Vblank', ' VBlank'),
+ ]
+
+