summaryrefslogtreecommitdiff
path: root/kberylsettings/lib.py
blob: 995917ace46c24dcc64cc5dad0ff00bbf18c8b6a (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" kberylsettings.lib -> library classes, functions, and attributes.

"""
from qt import PYSIGNAL, QPixmap, QImage, SIGNAL, SLOT
from kdecore import KGlobal, KIcon, KShortcut, i18n
from kdeui import KAction, KStdAction


##
# a cache for already-created icons
iconCache = {}


class Signals:
    """ class with SIGNAL and PYSIGNAL attributes

    """
    actionStatusText = SIGNAL('actionStatusText(const QString &)')
    activated = SIGNAL('activated()')
    activatedIndex = SIGNAL('activated(int)')
    berylContextChanged = PYSIGNAL('berylContextChanged')
    berylPluginEnabled = PYSIGNAL('berylPluginEnabled')
    berylSettingChanged = PYSIGNAL('berylSettingChanged')
    capturedShortcut = SIGNAL('capturedShortcut (const KShortcut &)')
    changed = SIGNAL('changed()')
    clearStatusText = SIGNAL('clearStatusText()')
    clicked = SIGNAL('clicked()')
    clickedId = SIGNAL('clicked(int)')
    colorChanged = SIGNAL('changed(const QColor &)')
    doubleValueChanged = SIGNAL('valueChanged(double)')
    fileChanged = SIGNAL('dirty(const QString&)')
    fileCreated = SIGNAL('created(const QString&)')
    iconSizeChanged = PYSIGNAL('iconSizeChanged')
    intValueChanged = SIGNAL('valueChanged(int)')
    itemClicked = SIGNAL('clicked(QListViewItem *)')
    itemSelected = SIGNAL('selectionChanged(QListViewItem *)')
    okClicked = SIGNAL('okClicked()')
    partURL = PYSIGNAL('partURL')
    quitSelected = SIGNAL('quitSelected()')
    searchInput = PYSIGNAL('searchInput')
    selectItem = PYSIGNAL('selectItem')
    selectPrevious = PYSIGNAL('selectPrevious')
    showAbout = PYSIGNAL('showAbout')
    showGroups = PYSIGNAL('showGroups')
    showSettings = PYSIGNAL('showSettings')
    someChange = PYSIGNAL('someChange')
    statusMessage = PYSIGNAL('statusMessage')
    textChanged = SIGNAL('textChanged(const QString&)')
    toggled = SIGNAL('toggled(bool)')
    viewModeChanged = PYSIGNAL('viewModeChanged')    


class Slots:
    """ class with SLOT attributes

    """
    captureShortcut = SLOT('captureShortcut()')
    clear = SLOT('clear()')
    messageText = SLOT('message( const QString & )')


def iconLoader():
    """ returns the global icon loader

    @return KIconLoader instance
    """
    return KGlobal.instance().iconLoader()


def icon(name, group=KIcon.NoGroup, size=KIcon.SizeSmall):
    """ loads pixmap by name

    This function could return the icon directly from the loadIcon
    call, but that method (for some reason) doesn't apply any icon
    effects.

    @param name icon name
    @param group KIcon group
    @param size KIcon size
    @return icon as QPixmap instance
    """
    try:
        return iconCache[(name, size)]
    except (KeyError, ):
        pass
    ico = iconLoader().loadIcon(name, group)
    img = ico.convertToImage()
    pix = iconCache[(name, size)] = QPixmap()
    pix.convertFromImage(img.smoothScale(size, size, QImage.ScaleMin))
    return pix


def iconSet(name, group=KIcon.NoGroup, size=KIcon.SizeSmall):
    """ load icon set by name
    
    @param name icon set name
    @param group KIcon group
    @param size KIcon size
    @return icon as QIconSet instance
    """
    return iconLoader().loadIconSet(name, group, size)


def action(text, iconset, cut, slot, collection, name,
           tooltip='', pluggable=None):
    """ creates and configures a KAction instance

    @param text display value for action
    @param iconset name of icon set for action
    @param cut
    @param slot
    @param collection KActionCollection instance
    @param name internal action name
    @keyparam tooltip='' value for tooltip
    @keyparam pluggable=None any pluggable object
    @return KAction instance
    """
    if not cut:
        cut = KShortcut()
    else:
        cut = KShortcut(cut)
    if iconset:
        iconset = iconSet(iconset)
        action = KAction(text, iconset, cut, slot, collection, name)
    else:
        action = KAction(text, cut, slot, collection, name)
    if tooltip:
        action.setToolTip(tooltip)
    if pluggable:
        action.plug(pluggable)
    return action


def stdAction(attr, slot, collection, name, pluggable=None):
    """ creates a named KStdAction instance

    @param attr attribute of KStdAction class
    @param slot slot for KStdAction instance
    @param collection KActionCollection instance
    @param name internal action name
    @keyparam pluggable=None any pluggable object    
    @return KStdAction instance
    """
    typ = getattr(KStdAction, attr)
    action = typ(slot, collection, name)
    if pluggable:
        action.plug(pluggable)
    return action