summaryrefslogtreecommitdiff
path: root/kberylsettings/beryl.py
blob: 828465c84d14d4bf11efc3a249cf2be553ec8af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" kberylsettings.beryl -> wrappers around berylsettings objects.

"""
import berylsettings

from kdecore import KIcon, locate
from kberylsettings.lib import Signals, icon, iconLoader
from qt import QImage, QPixmap, QObject


class Context(QObject):
    """ Context -> wraps berylsetting.Context instances with extra
    methods and properties.

    This class inherits QObject so that instances can emit Qt signals.
    """

    def __init__(self, context=None):
        QObject.__init__(self)
        if context is None:
            context = berylsettings.Context()
            context.Read()
        self.context = context

    def __getattr__(self, value):
        """ x.__getattr__('name') <==> x.name

        """
        return getattr(self.context, value)

    def __repr__(self):
        """ x.__repr__() <==> repr(x)

        """
        args = (self.__module__, self.__class__.__name__, hex(abs(id(self))))
        return "<%s.%s instance at %s>" % args
    __str__ = __repr__

    def getCategories(self):
        """ sorted categories sequence

        @return sorted list of Category instances
        """
        seq = [Category(c) for c in self.context.Categories]
        seq.sort()
        return seq
    categories = property(getCategories)

    def getPlugins(self):
        """ sorted plugins sequence

        @return sequence of Plugin wrapper instances
        """
        seq = [Plugin(p) for p in self.context.Plugins]
        seq.sort()
        return seq
    plugins = property(getPlugins)

    def plugin(self, value):
        """ locates plugin by name, description, or index

        @param value plugin name, short description, or index
        @return Plugin wrapper instance
        """
        try:
            value + 0
        except (TypeError, ):
            for plugin in self.plugins:
                if value == plugin.ShortDesc:
                    return Plugin(plugin)
                if value == plugin.Name:
                    return Plugin(plugin)
        else:
            return list(self.plugins)[value]

    def getGeneral(self):
        """ general plugin instance

        @return Plugin wrapper instance
        """
        return Plugin(self.context.Plugin(Plugin.generalName))
    general = property(getGeneral)

    def write(self):
        """ saves the beryl context

        @return None
        """
        self.emit(Signals.statusMessage, ('Saving Beryl settings....', ))
        self.context.Write()
        self.reload()

    def reload(self):
        """ messages the extension to reload the new settings

        @return None
        """
        berylsettings.send_reload()
        self.emit(Signals.statusMessage, ('Beryl settings reloaded.', ))    


class Category(object):
    """ Category -> wraps berylsetting.Category instances with extra
    methods and properties.

    """
    iconNameMap = {
        'wm' : 'kwin',
        'effects' : 'style',
        'misc' : 'package_toys',
        'settings' : 'configure',
        'imageformat' : 'icons',
        'devel' : 'source',
        'desktop' : 'desktop',
        'accessibility' : 'access',
    }
    
    def __init__(self, category):
        self.category = category

    def __cmp__(self, other):
        """ x.__cmp__(y) <==> cmp(x, y)

        """
        return cmp(self.ShortDesc, other.ShortDesc)

    def __getattr__(self, value):
        """ x.__getattr__('name') <==> x.name

        """
        return getattr(self.category, value)

    def icon(self, size):
        """ icon for this category

        @param size KIcon size
        @return icon instance
        """
        name = self.iconNameMap.get(self.Name, self.Name)
        return icon(name, KIcon.NoGroup, size)

    def plugins(self):
        """ sequence of plugin instances in this category

        @return sequence of Plugin instances
        """
        seq = [Plugin(p) for p in self.category.Plugins]
        seq.sort()
        return seq
    plugins = property(plugins)


class Plugin(object):
    """ Plugin -> wraps berylsetting.Plugin instances with extra
    methods and properties.

    """
    generalName = '_'
    iconCache = {}
    
    def __init__(self, plugin):
        self.plugin = plugin

    def __cmp__(self, other):
        """ x.__cmp__(y) <==> cmp(x, y)

        """
        if other is None:
            return -1
        if not isinstance(other, Plugin):
            other = Plugin(other)
        if self.isGeneral:
            return -1
        if other.isGeneral:
            return 1
        return cmp(self.plugin.ShortDesc, other.plugin.ShortDesc)

    def __getattr__(self, value):
        """ x.__getattr__('name') <==> x.name

        """
        return getattr(self.plugin, value)

    def __repr__(self):
        """ x.__repr__() <==> repr(x)

        """
        args = (self.__module__, self.__class__.__name__,
                self.Name,
                hex(abs(id(self))))
        return "<%s.%s instance name='%s' at %s>" % args
    __str__ = __repr__

    def getEnabled(self):
        """ true if this plugin is enabled

        """
        return self.plugin.Enabled

    def setEnabled(self, value):
        """ set the enabled flag for this plugin

        """
        self.plugin.Enabled = value
    enabled = property(getEnabled, setEnabled)
    
    def isGeneral(self):
        """ true if this plugin is a general plugin

        """
        return self.plugin.Name == self.generalName
    isGeneral = property(isGeneral)

    def icon(self, size, loader):
        """ icon for this plugin

        @param size KIcon size
        @param loader KIconLoader instance
        @return icon instance
        """
        name = self.plugin.Name
        try:
            pix = self.iconCache[(name, size)]
        except (KeyError, ):
            path = locate('icon', 'beryl-settings-section-%s.svg' % name)
            if not path:
                path = 'unknown'
            ico = loader.loadIcon(path, KIcon.NoGroup)
            img = ico.convertToImage()
            pix = self.iconCache[(name, size)] = QPixmap()
            pix.convertFromImage(img.smoothScale(size, size, QImage.ScaleMin))
        return pix

    def iconPath(self):
        """ path to icon for this plugin

        @return icon path
        """
        name = self.plugin.Name        
        return locate('icon', 'beryl-settings-section-%s.png' % name)
            
    altType = {
        'Int' : 'Values',
        'Float' : 'Values',
        'Color' : 'Colors',
        'Bool' : 'Choices',
        'String' : 'Choices',
        'Binding' : 'Bindings',
        'List of String' : 'Items',
        }

    altLong = {
        'Values' : 'Configure numeric values',
        'Colors' : 'Choose colors',
        'Choices' : 'Turn things on or off',
        'Bindings' : 'Connect keyboard, mouse, edges and the system bell',
        'Items' : 'Select items from (and into) lists of strings',
        }

    def settingTypeDesc(self, key):
        """ lookup a long description for alternate type name

        @param key alternate type name (keys of altType dict)
        @return string description or key
        """
        return self.altLong.get(key, key)
    
    def settings(self):
        """ mapping of plugin settings

        """
        settingtype = {}
        for setting in [Setting(s) for s in self.plugin.Settings]:
            typ = setting.Type
            key = self.altType.get(typ, typ)
            settingtype.setdefault(key, []).append(setting)
        return settingtype
    settings = property(settings)

    def groups(self):
        """ sorted groups sequence

        """
        groups = [Group(g) for g in self.plugin.Groups]
        groups.sort()
        return groups
    groups = property(groups)


class Group(object):
    """ Group -> wraps berylsetting.Group instances with extra
    methods and properties.

    """
    def __init__(self, group):
        self.group = group

    def __getattr__(self, value):
        """ x.__getattr__('name') <==> x.name

        """
        return getattr(self.group, value)

    def __cmp__(self, other):
        """ x.__cmp__(y) <==> cmp(x, y)

        """
        if not isinstance(other, Group):
            other = Group(other)
        return cmp(self.label, other.label)

    def label(self):
        """ label for this group

        """
        return self.Name or self.Desc or 'Uncategorized'
    label = property(label)

    def settings(self, plugin):
        """ sequence of settings in this group for plugin

        """
        group = self.Name
        return [Setting(s) for s in plugin.Settings if s.Group==group]


class Setting(object):
    """ Setting -> wraps berylsetting.Setting instances with extra
    methods and properties.

    """
    iconNameMap = {
        'Bindings' : 'mouse',
        'Choices': 'apply',        
        'Colors' : 'colorize',
        'Items': 'edit',
        'Values' : 'exec',
        'Back' : 'back',
        'None' : 'back',
    }

    def __init__(self, setting):
        self.setting = setting

    def __getattr__(self, value):
        """ x.__getattr__('name') <==> x.name

        """
        return getattr(self.setting, value)

    def __repr__(self):
        """ x.__repr__() <==> repr(x)

        """
        args = (self.__module__, self.__class__.__name__,
                self.Name,
                hex(abs(id(self))))
        return "<%s.%s instance name='%s' at %s>" % args
    __str__ = __repr__
    
    def set(self, value):
        """ set the setting's value

        @param value new value for setting
        @return None
        """
        self.setting.Value = value
        
    def icon(cls, value, size):
        """ icon for setting type

        @param value setting type name
        @param size KIcon size
        @return icon instance
        """
        name = cls.iconNameMap.get(str(value), cls.iconNameMap['Choices'])
        return icon(name, size=size)
    icon = classmethod(icon)

    def iconPath(cls, value):
        """ path to icon for setting type

        @param value setting type name
        @return icon path
        """
        name = cls.iconNameMap.get(str(value), cls.iconNameMap['Choices'])
        ico = iconLoader().iconPath(name, KIcon.NoGroup)
        return ico
    iconPath = classmethod(iconPath)
    
    def getValue(self):
        """ get value of setting, explicitly silencing errors

        @return setting value
        """
        try:
            return self.setting.Value
        except (IndexError, ):
            return ''

    def setValue(self, value):
        """ set value of setting

        @param new value for setting
        @return None
        """
        self.set(value)
    Value = property(getValue, setValue)