summaryrefslogtreecommitdiffstats
path: root/python/pyqt/examples3/listbox.py
blob: a149500efbc55a274ab9c43ffd54ddf82037aec0 (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
#!/usr/bin/env python

"""**************************************************************************
** $Id$
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
***************************************************************************"""

import sys
from qt import *

FALSE = 0
TRUE  = 1

class ListBoxDemo( QWidget ):
    def __init__( self, parent=None, name=None ):
        QWidget.__init__( self, parent, name )
        b = FALSE
        g = QGridLayout( self, 2, 2, 6 )

        g.addWidget( QLabel( "<b>Configuration:</b>", self ), 0, 0 )
        g.addWidget( QLabel( "<b>Result:</b>", self ), 0, 1 )

        self.l = QListBox( self )
        g.addWidget( self.l, 1, 1 )
        self.l.setFocusPolicy( QWidget.StrongFocus )

        v = QVBoxLayout( )
        g.addLayout( v, 1, 0 )
                
        bg = QButtonGroup( self, None )

        b = QRadioButton( "Fixed number of columns,\n"
                          "as many rows as needed.",
                          self )
        bg.insert( b )
        v.addWidget( b )
        b.setChecked( TRUE )
        self.connect( b, SIGNAL("clicked()"), self.setNumCols )
        h = QHBoxLayout()
        v.addLayout( h )
        h.addSpacing( 30 )
        h.addSpacing( 100 )
        h.addWidget( QLabel( "Columns:", self ) )
        self.columns = QSpinBox( self )
        h.addWidget( self.columns )

        v.addSpacing( 12 )

        b = QRadioButton( "As many columns as fit on-screen,\n"
                          "as many rows as needed.",
                          self )
        bg.insert( b )
        v.addWidget( b )
        self.connect( b, SIGNAL("clicked()"), self.setColsByWidth )

        v.addSpacing( 12 )

        b = QRadioButton( "Fixed number of rows,\n"
                          "as many columns as needed.",
                          self )
        bg.insert( b )
        v.addWidget( b )
        self.connect( b, SIGNAL("clicked()"), self.setNumRows )
        h = QHBoxLayout()
        v.addLayout( h )
        h.addSpacing( 30 )
        h.addSpacing( 100 )
        h.addWidget( QLabel( "Rows:", self ) )
        self.rows = QSpinBox( self )
        self.rows.setEnabled( FALSE )
        h.addWidget( self.rows )

        v.addSpacing( 12 )

        b = QRadioButton( "As many rows as fit on-screen,\n"
                          "as many columns as needed.",
                          self )
        bg.insert( b )
        v.addWidget( b )
        self.connect( b, SIGNAL("clicked()"), self.setRowsByHeight )

        v.addSpacing( 12 )

        cb = QCheckBox( "Variable-height rows", self )
        cb.setChecked( TRUE )
        self.connect( cb, SIGNAL("toggled(bool)"), self.setVariableHeight )
        v.addWidget( cb )
        v.addSpacing( 6 )
        
        cb = QCheckBox( "Variable-width columns", self )
        self.connect( cb, SIGNAL("toggled(bool)"), self.setVariableWidth )
        v.addWidget( cb )

        cb = QCheckBox( "Extended-Selection", self )
        self.connect( cb, SIGNAL("toggled(bool)"), self.setMultiSelection )
        v.addWidget( cb )

        pb = QPushButton( "Sort ascending", self )
        self.connect( pb, SIGNAL("clicked()"), self.sortAscending )
        v.addWidget( pb )

        pb = QPushButton( "Sort descending", self )
        self.connect( pb, SIGNAL("clicked()"), self.sortDescending )
        v.addWidget( pb )

        v.addStretch( 100 )

        i = 0
        while i <= 2560:
            i+=1
            self.l.insertItem( str(QString.fromLatin1( "Item " )) + str(QString.number( i )), i )
        self.columns.setRange( 1, 256 )
        self.columns.setValue( 1 )
        self.rows.setRange( 1, 256 )
        self.rows.setValue( 256 )

        self.connect( self.columns, SIGNAL("valueChanged(int)"), self.setNumCols )
        self.connect( self.rows, SIGNAL("valueChanged(int)"), self.setNumRows )

    # slots
    def setNumCols( self ):
        self.columns.setEnabled( TRUE )
        self.rows.setEnabled( FALSE )
        self.l.setColumnMode( self.columns.value() )

    def setNumRows( self ):
        self.columns.setEnabled( FALSE )
        self.rows.setEnabled( TRUE )
        self.l.setRowMode( self.rows.value() )

    def setRowsByHeight( self ):
        self.columns.setEnabled( FALSE )
        self.rows.setEnabled( FALSE )
        self.l.setRowMode( QListBox.FitToHeight )

    def setColsByWidth( self ):
        self.columns.setEnabled( FALSE )
        self.rows.setEnabled( FALSE )
        self.l.setColumnMode( QListBox.FitToWidth )

    def setVariableWidth( self, b ):
        self.l.setVariableWidth( b )

    def setVariableHeight( self, b ):
        self.l.setVariableHeight( b )

    def setMultiSelection( self, b ):
        self.l.clearSelection()
        if b :
            self.l.setSelectionMode( QListBox.Extended )
        else:
            self.l.setSelectionMode( QListBox.Single )
            
    def sortAscending( self ):
        self.l.sort( TRUE )

    def sortDescending( self ):
        self.l.sort( FALSE )

    
def main( args ):
    a = QApplication( args )
    t = ListBoxDemo()
    t.setCaption( "Qt Example - Listbox" )
    a.setMainWidget( t )
    t.show()
    
    a.exec_loop()
    
if __name__=="__main__":
    main(sys.argv)