summaryrefslogtreecommitdiff
path: root/kberylsettings/widget.py
blob: d6a60f903dfa6ba081d41733bc64b7c755902bba (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" kberylsettings.widget -> various common widgets.

"""
from qt import Qt, QFrame, QGroupBox, QListViewItem, QListViewItemIterator, \
     QSizePolicy, QSplitter, QVBoxLayout, QWidgetStack
from kdeui import KListView, KPopupMenu, KPushButton, KStdGuiItem


class Frame(QFrame):
    """ Frame -> QFrame with QVBoxLayout

    """
    def __init__(self, parent, margin=0, spacing=-1, autoAdd=True):
        QFrame.__init__(self, parent)
        layout = QVBoxLayout(self, margin, spacing)
        layout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        layout.setAutoAdd(autoAdd)


class Popup(KPopupMenu):
    """ Popup -> KPopupMenu always set to checkable

    """
    def __init__(self, parent):
        KPopupMenu.__init__(self, parent)
        self.setCheckable(True)


class Splitter(QSplitter):
    """ Splitter -> QSplitter always set to opaque resize

    """
    def __init__(self, parent, style):
        QSplitter.__init__(self, style, parent)
        self.setOpaqueResize(True)
        self.setChildrenCollapsible(False)


class SmallPushButton(KPushButton):
    """ SmallPushButton -> KPushButton with fixed sizes

    """
    def __init__(self, icon, text, parent):
        KPushButton.__init__(self, icon, text, parent)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed))        


class ValueListViewItem(QListViewItem):
    """ ValueListViewItem -> QListViewItem with extra 'value' attribute

    """
    def __init__(self, parent, text, value=None):
        QListViewItem.__init__(self, parent, text)
        self.value = value


class ListView(KListView):
    """ ListView -> KListView that is directly iterable

    """
    def __init__(self, parent):
        KListView.__init__(self, parent)
        self.configList()

    def configList(self):
        self.setSorting(-1)
        
    def __iter__(self):
        """ iterate over QListView items

        """
        it = QListViewItemIterator(self)
        item = it.current()
        while item:
            yield item
            it += 1            
            item = it.current()


class ShortListView(ListView):
    """ ShortListView -> KListView with height set equal to a number of items

    """
    def __init__(self, parent, text, lines):
        ListView.__init__(self, parent)
        self.addColumn(text)
        self.setResizeMode(self.LastColumn)
        self.setHeightLines(lines)
        
    def setHeightLines(self, num):
        height = self.fontMetrics().lineSpacing() * num
        height += self.header().height()
        self.setMaximumHeight(height)


class BasicListView(ListView):
    """ BasicListView -> KListView configured for a single column, no
        sorting, hidden header and no horizontal scrollbar.

    """
    def configList(self):
        self.addColumn('')
        self.setSorting(-1)
        self.header().hide()
        self.setHScrollBarMode(self.AlwaysOff)


class WidgetStack(QWidgetStack):
    """ WidgetStack -> QWidgetStack with QVBoxLayout

    """
    def __init__(self, parent):
        QWidgetStack.__init__(self, parent)
        layout = QVBoxLayout(self)
        layout.setAutoAdd(True)


class GroupBox(QGroupBox):
    """ GroupBox -> QGroupBox with nicer parameters

    """
    def __init__(self, strips, orient, text, parent, margin=0, spacing=0):
        QGroupBox.__init__(self, strips, orient, text, parent)
        self.setFlat(True)
        self.setInsideMargin(margin)
        self.setInsideSpacing(spacing)    

    def addSpaces(self, count, size=0):
        """ adds a number of spaces to the groupbox

        @param count number of spaces to add
        @param size size of each space
        @return None
        """
        for i in range(count):
            self.addSpace(size)


def guiButton(parent, name, layout=None, enable=True):
    """ builds a standard push button by name

    @param parent parent of button
    @param name name of standard item; refer to KStdGuiItem static members
    @keyparam layout=None if given, new button is added to this layout
    @keyparam enable=True initial enabled state of button
    @return KPushButton instance
    """
    gui = getattr(KStdGuiItem, name)()
    button = KPushButton(gui.iconSet(), gui.text(), parent)
    if layout:
        layout.addWidget(button)
    button.setEnabled(enable)
    return button