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

# vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
# Copyright (C) 2002 Oleksandr Yakovlyev (yshurik) <yshurik@thekompany.com>

import sys 
from qt import *

TRUE = 1
FALSE = 0 

class CheckLists(QWidget):
	def __init__(self, *args):
		apply( QWidget.__init__, (self, ) + args )

		lay = QHBoxLayout(self)
		lay.setMargin(5)
		
		vbox1 = QVBoxLayout(lay)
		vbox1.setMargin(5)

		# First child: a Label
		vbox1.addWidget(QLabel("Check some items!", self))

		# Second child: the ListView
		self.lv1 = QListView(self)
		vbox1.addWidget(self.lv1)
		self.lv1.addColumn("Items")
		self.lv1.setRootIsDecorated(TRUE)

		# create a list with 4 ListViewItems which will be parent items of other ListViewItems

		parentList = []

    parentList.append( QListViewItem( self.lv1, "Parent Item 1" ) )
    parentList.append( QListViewItem( self.lv1, "Parent Item 2" ) )
    parentList.append( QListViewItem( self.lv1, "Parent Item 3" ) )
    parentList.append( QListViewItem( self.lv1, "Parent Item 4" ) )

    num = 0

    self.childList1 = []
    # go through the list of parent items...
    for i in range(4):
      it = parentList[i]
      it.setOpen( TRUE )
			
      # ...and create 5 checkable child ListViewItems for each parent item
      for j in range(5):
        ci = QCheckListItem( it, QString("%1. Child of Parent %2").arg( j ).arg( i ), QCheckListItem.CheckBox )
        self.childList1.append(ci)

    # Create another widget for layouting
    tmp1 = QVBoxLayout( lay )
    tmp1.setMargin( 5 )

    # create a pushbutton
    copy1 = QPushButton( "  ->  ", self )
    tmp1.addWidget( copy1 )
    copy1.setMaximumWidth( copy1.sizeHint().width() );
    # connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
    self.connect( copy1, SIGNAL('clicked()'), self.copy1to2 )

    # another widget for layouting
    vbox2 = QVBoxLayout( lay )
    vbox2.setMargin( 5 )

    # and another label
    vbox2.addWidget( QLabel( "Check one item!", self ) )

    # create the second listview
    self.lv2 = QListView( self )
    vbox2.addWidget( self.lv2 )
    self.lv2.addColumn( "Items" )
    self.lv2.setRootIsDecorated( TRUE )

    # another widget needed for layouting only
    tmp2 = QVBoxLayout( lay )
    tmp2.setMargin( 5 )

    # create another pushbutton...
    copy2 = QPushButton( "  ->  ", self )
    lay.addWidget( copy2 )
    copy2.setMaximumWidth( copy2.sizeHint().width() )
    # ...and connect its clicked() SIGNAL to the copy2to3() SLOT
    self.connect( copy2, SIGNAL('clicked()'), self.copy2to3 )

    tmp3 = QVBoxLayout( lay )
    tmp3.setMargin( 5 )

    # and create a label which will be at the right of the window
    self.label = QLabel( "No Item yet...", self )
    tmp3.addWidget( self.label )

	def copy1to2(self):
    self.lv2.clear()

    # Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
    # are inserted into a Listview, the parent item of these MUST be a controller Item!
    item = QCheckListItem( self.lv2, "Controller", QCheckListItem.Controller )
    item.setOpen( TRUE )

    self.listChild2 = []
    for it in self.childList1:
      # ...check state of childs, and...
      if it.parent() != None:
        # ...if the item is checked...
        if it.isOn():
          # ...insert a Radio-ListViewItem with the same text into the second ListView
          ri = QCheckListItem(item , it.text(0), QCheckListItem.RadioButton)
          self.listChild2.append(ri)

    if item.firstChild() != None:
      item.firstChild().setOn(TRUE)

  def copy2to3(self):
    self.label.setText( "No Item checked" )

    # iterate through the second ListView...
    for it in self.listChild2:
      # ...check state of childs, and...
      if it.parent() != None:
        # ...if the item is checked...
        if it.isOn():
          # ...set the text of the item to the label
          self.label.setText( it.text( 0 ) )

a = QApplication(sys.argv)

checkLists = CheckLists()
checkLists.resize(700, 400)
checkLists.setCaption("PyQt example - CheckLists")

a.setMainWidget(checkLists)
checkLists.show()
a.exec_loop()