summaryrefslogtreecommitdiffstats
path: root/python/pyqt/examples3/listboxcombo.py
blob: 4a909861d27ace1f413af1bb0584e20647904604 (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
#!/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 ListBoxCombo( QVBox ):
    # Constructor
    # Creates child widgets of the ListBoxCombo widget
    def __init__( self, parent=None, name=None ):
        QVBox.__init__( self, parent, name )
                
        self.setMargin( 5 )
        self.setSpacing( 5 )

        i = 0
        row1 = QHBox( self )
        row1.setSpacing( 5 )
 
        # Create a multi-selection ListBox...
        self.lb1 = QListBox( row1 )
        self.lb1.setSelectionMode( QListBox.Multi )
            
        # ...insert a pixmap item...
        xpm = QPixmap( "qtlogo.png" )
        txt = QString()
        QListBoxPixmap( self.lb1, xpm, txt ) 
        
        # ...and 100 text items
        for i in range(0, 100, 1) :
            xpm = QPixmap()
            txt = QString( "Listbox Item %1" ).arg( i )
            if not i % 4 :
                xpm = QPixmap( "fileopen.xpm" )
            QListBoxPixmap( self.lb1, xpm, txt ) 
                     
        # Create a pushbutton...
        arrow1 = QPushButton( " -> ", row1 )
        # ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
        self.connect( arrow1, SIGNAL("clicked()"), self.slotLeft2Right )

        # create an empty single-selection ListBox
        self.lb2 = QListBox( row1 )

        row2 = QHBox( self )
        row2.setSpacing( 5 )

        box1 = QVBox( row2 )
        box1.setSpacing( 5 )
        
        # Create a non-editable Combobox and a label below...
        cb1 = QComboBox( FALSE, box1 )
        self.label1 = QLabel( "Current Item: Combobox Item 0", box1 )
        self.label1.setMaximumHeight( self.label1.sizeHint().height() * 2 )
        self.label1.setFrameStyle( QFrame.Panel | QFrame.Sunken )

        #...and insert 50 items into the Combobox
        for i in range( 0, 50, 1 ) :
            txt = str( QString( "Combobox Item %1" ).arg( i ) )
            if i % 9 :
                cb1.insertItem( txt )
            else :
                cb1.listBox().insertItem( MyListBoxItem() )
            
        box2 = QVBox( row2 )
        box2.setSpacing( 5 )

        # Create an editable Combobox and a label below...
        cb2 = QComboBox( TRUE, box2 )
        self.label2 = QLabel( "Current Item: Combobox Item 0", box2 )
        self.label2.setMaximumHeight( self.label2.sizeHint().height() * 2 )
        self.label2.setFrameStyle( QFrame.Panel | QFrame.Sunken )

        # ... and insert 50 items into the Combobox
        for i in range(0, 50, 1 ) :
            txt = str(QString( "Combobox Item %1" ).arg( i ))
            if not i % 4 :
                cb2.insertItem( QPixmap( "fileopen.xpm" ), txt )
            else :
                cb2.insertItem( txt )
        
        # Connect the activated SIGNALs of the Comboboxes with SLOTs
        self.connect( cb1, SIGNAL("activated( const QString & )"), self.slotCombo1Activated )
        self.connect( cb2, SIGNAL("activated( const QString & )"), self.slotCombo2Activated )
    
    """ SLOT slotLeft2Right
    * Copies all selected items of the first ListBox into the second ListBox
    """
    def slotLeft2Right( self ):
        # Go through all items of the first ListBox
        for i in range( 0, self.lb1.count(), 1 ) :
            item = self.lb1.item( i )
            # if the item is selected...
            if self.lb1.isSelected( i ): #item.isSelected() :
                # ...and it is a text item...
                if item.pixmap() and not(item.text().isEmpty()):
                    self.lb2.insertItem( item.pixmap(), item.text() )
                elif not( item.pixmap() ):
                    self.lb2.insertItem( item.text() )
                elif item.text().isEmpty() :
                    self.lb2.insertItem( item.pixmap() )

    """ SLOT slotCombo1Activated( const QString &s )
    * Sets the text of the item which the user just selected in the
    * first Combobox (and is now the value of s) to the first Label.
    """
    def slotCombo1Activated( self, s ) :
        self.label1.setText( str(QString( "Current Item: %1" ).arg( s ) ) )
    
    """ SLOT slotCombo2Activated( const QString &s )
    * Sets the text of the item which the user just selected in the
    * second Combobox (and is now the value of s) to the second Label.
    """
    def slotCombo2Activated( self, s ) :
        self.label2.setText( str(QString( "Current Item: %1" ).arg( s ) ) )
    

class MyListBoxItem( QListBoxItem ):
    def __init__( self, parent=None, name=None ):
        QListBoxItem.__init__( self, parent, name )
        self.setCustomHighlighting( TRUE )
    
    def paint( self, painter ):
        # evil trick: find out whether we are painted onto our listbox
        in_list_box = 0
        if self.listBox() and self.listBox().viewport() == painter.device():
            in_list_box = 1
        r = QRect( 0, 0, self.width( self.listBox() ), self.height( self.listBox() ) )
        brush = QBrush( Qt.red, Qt.SolidPattern )
        if in_list_box and isSelected():
            painter.eraseRect( r )
        painter.fillRect( 5, 5, self.width( self.listBox() ) - 10, self.height( self.listBox() ) - 10, brush )
        if in_list_box and isCurrent():
            self.listBox().style().drawPrimitive( QStyle.PE_FocusRect, painter, r, self.listBox().colorGroup() )
            
    def width( self, QListBox ):
        return 100
    
    def height( self, QListBox ):
        return 16

    
def main( args ):
    a = QApplication( args )
    
    listboxcombo = ListBoxCombo()
    listboxcombo.resize( 400, 270 )
    listboxcombo.setCaption( "Qt Example - Listboxes and Comboboxes" )
    a.setMainWidget( listboxcombo )
    listboxcombo.show();
    
    a.exec_loop()
    
if __name__=="__main__":
    main(sys.argv)