summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/listbox/ListBoxDemo.java
blob: 60eecffcd76942cddf2505c9cb39e02d717f0823 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***************************************************************************
* $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 org.kde.qt.*;



class ListBoxDemo extends QWidget
{

private    QListBox  l;
private    QSpinBox  columns;
private    QSpinBox  rows;
private    QButtonGroup  bg;


ListBoxDemo()
{
    super( null, null );
    QGridLayout  g = new QGridLayout( this, 2, 2, 6 );

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

    l = new QListBox( this );
    g.addWidget( l, 1, 1 );
    l.setFocusPolicy( QWidget.StrongFocus );

    QVBoxLayout  v = new QVBoxLayout();
    g.addLayout( v, 1, 0 );

    QRadioButton  b = null;
    bg = new QButtonGroup( (QWidget) null );

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

    v.addSpacing( 12 );

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

    v.addSpacing( 12 );

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

    v.addSpacing( 12 );

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

    v.addSpacing( 12 );

    QCheckBox  cb = new QCheckBox( "Variable-height rows", this );
    cb.setChecked( true );
    connect( cb, SIGNAL("toggled(boolean)"), this, SLOT("setVariableHeight(boolean)") );
    v.addWidget( cb );
    v.addSpacing( 6 );

    cb = new QCheckBox( "Variable-width columns", this );
    connect( cb, SIGNAL("toggled(boolean)"), this, SLOT("setVariableWidth(boolean)") );
    v.addWidget( cb );

    cb = new QCheckBox( "Extended-Selection", this );
    connect( cb, SIGNAL("toggled(boolean)"), this, SLOT("setMultiSelection(boolean)") );
    v.addWidget( cb );

    QPushButton pb = new QPushButton( "Sort ascending", this );
    connect( pb, SIGNAL(" clicked()"), this, SLOT(" sortAscending()") );
    v.addWidget( pb );

    pb = new QPushButton( "Sort descending", this );
    connect( pb, SIGNAL(" clicked()"), this, SLOT(" sortDescending()") );
    v.addWidget( pb );

    v.addStretch( 100 );

    int i = 0;
    while( ++i <= 2560 )
        l.insertItem( "Item " + i,
                       i );
    columns.setRange( 1, 256 );
    columns.setValue( 1 );
    rows.setRange( 1, 256 );
    rows.setValue( 256 );

    connect( columns, SIGNAL("valueChanged(int)"), this, SLOT("setNumCols()") );
    connect( rows, SIGNAL("valueChanged(int)"), this, SLOT("setNumRows()") );
}


void setNumRows()
{
    columns.setEnabled( false );
    rows.setEnabled( true );
    l.setRowMode( rows.value() );
}


void setNumCols()
{
    columns.setEnabled( true );
    rows.setEnabled( false );
    l.setColumnMode( columns.value() );
}


void setRowsByHeight()
{
    columns.setEnabled( false );
    rows.setEnabled( false );
    l.setRowMode( QListBox.FitToHeight );
}


void setColsByWidth()
{
    columns.setEnabled( false );
    rows.setEnabled( false );
    l.setColumnMode( QListBox.FitToWidth );
}


void setVariableWidth( boolean b )
{
    l.setVariableWidth( b );
}


void setVariableHeight( boolean b )
{
    l.setVariableHeight( b );
}

void setMultiSelection( boolean b )
{
    l.clearSelection();
    l.setSelectionMode( b ? QListBox.Extended : QListBox.Single );
}

void sortAscending()
{
    l.sort( true );
}

void sortDescending()
{
    l.sort( false );
}
}