summaryrefslogtreecommitdiffstats
path: root/examples3/listboxcombo.py
blob: 1a39310e1473ff07170f933c4142b6b762a088e7 (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: listboxcombo.py,v 1.1 2003/05/30 17:47:57 phil Exp $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
***************************************************************************"""

import sys
from qt import *

FALSE = 0
TRUE  = 1

class ListBoxCombo( TQVBox ):
    # Constructor
    # Creates child widgets of the ListBoxCombo widget
    def __init__( self, parent=None, name=None ):
        TQVBox.__init__( self, parent, name )
                
        self.setMargin( 5 )
        self.setSpacing( 5 )

        i = 0
        row1 = TQHBox( self )
        row1.setSpacing( 5 )
 
        # Create a multi-selection ListBox...
        self.lb1 = TQListBox( row1 )
        self.lb1.setSelectionMode( TQListBox.Multi )
            
        # ...insert a pixmap item...
        xpm = TQPixmap( "qtlogo.png" )
        txt = TQString()
        TQListBoxPixmap( self.lb1, xpm, txt ) 
        
        # ...and 100 text items
        for i in range(0, 100, 1) :
            xpm = TQPixmap()
            txt = TQString( "Listbox Item %1" ).arg( i )
            if not i % 4 :
                xpm = TQPixmap( "fileopen.xpm" )
            TQListBoxPixmap( self.lb1, xpm, txt ) 
                     
        # Create a pushbutton...
        arrow1 = TQPushButton( " -> ", 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 = TQListBox( row1 )

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

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

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

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

        # ... and insert 50 items into the Combobox
        for i in range(0, 50, 1 ) :
            txt = str(TQString( "Combobox Item %1" ).arg( i ))
            if not i % 4 :
                cb2.insertItem( TQPixmap( "fileopen.xpm" ), txt )
            else :
                cb2.insertItem( txt )
        
        # Connect the activated SIGNALs of the Comboboxes with SLOTs
        self.connect( cb1, SIGNAL("activated( const TQString & )"), self.slotCombo1Activated )
        self.connect( cb2, SIGNAL("activated( const TQString & )"), 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 TQString &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(TQString( "Current Item: %1" ).arg( s ) ) )
    
    """ SLOT slotCombo2Activated( const TQString &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(TQString( "Current Item: %1" ).arg( s ) ) )
    

class MyListBoxItem( TQListBoxItem ):
    def __init__( self, parent=None, name=None ):
        TQListBoxItem.__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 = TQRect( 0, 0, self.width( self.listBox() ), self.height( self.listBox() ) )
        brush = TQBrush( TQt.red, TQt.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( TQStyle.PE_FocusRect, painter, r, self.listBox().colorGroup() )
            
    def width( self, TQListBox ):
        return 100
    
    def height( self, TQListBox ):
        return 16

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