source: trunk/examples/listboxcombo/listboxcombo.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/****************************************************************************
2** $Id: listboxcombo.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "listboxcombo.h"
12
13#include <qcombobox.h>
14#include <qlistbox.h>
15#include <qhbox.h>
16#include <qpushbutton.h>
17#include <qstring.h>
18#include <qpixmap.h>
19#include <qlabel.h>
20#include <qimage.h>
21#include <qpainter.h>
22#include <qstyle.h>
23
24
25class MyListBoxItem : public QListBoxItem
26{
27public:
28 MyListBoxItem()
29 : QListBoxItem()
30 {
31 setCustomHighlighting( TRUE );
32 }
33
34protected:
35 virtual void paint( QPainter * );
36 virtual int width( const QListBox* ) const { return 100; }
37 virtual int height( const QListBox* ) const { return 16; }
38
39};
40
41void MyListBoxItem::paint( QPainter *painter )
42{
43 // evil trick: find out whether we are painted onto our listbox
44 bool in_list_box = listBox() && listBox()->viewport() == painter->device();
45
46 QRect r ( 0, 0, width( listBox() ), height( listBox() ) );
47 if ( in_list_box && isSelected() )
48 painter->eraseRect( r );
49 painter->fillRect( 5, 5, width( listBox() ) - 10, height( listBox() ) - 10, Qt::red );
50 if ( in_list_box && isCurrent() )
51 listBox()->style().drawPrimitive( QStyle::PE_FocusRect, painter, r, listBox()->colorGroup() );
52}
53
54/*
55 * Constructor
56 *
57 * Creates child widgets of the ListBoxCombo widget
58 */
59
60ListBoxCombo::ListBoxCombo( QWidget *parent, const char *name )
61 : QVBox( parent, name )
62{
63 setMargin( 5 );
64 setSpacing( 5 );
65
66 unsigned int i;
67 QString str;
68
69 QHBox *row1 = new QHBox( this );
70 row1->setSpacing( 5 );
71
72 // Create a multi-selection ListBox...
73 lb1 = new QListBox( row1 );
74 lb1->setSelectionMode( QListBox::Multi );
75
76 // ...insert a pixmap item...
77 lb1->insertItem( QPixmap( "qtlogo.png" ) );
78 // ...and 100 text items
79 for ( i = 0; i < 100; i++ ) {
80 str = QString( "Listbox Item %1" ).arg( i );
81 if ( !( i % 4 ) )
82 lb1->insertItem( QPixmap( "fileopen.xpm" ), str );
83 else
84 lb1->insertItem( str );
85 }
86
87 // Create a pushbutton...
88 QPushButton *arrow1 = new QPushButton( " -> ", row1 );
89 // ...and connect the clicked SIGNAL with the SLOT slotLeft2Right
90 connect( arrow1, SIGNAL( clicked() ), this, SLOT( slotLeft2Right() ) );
91
92 // create an empty single-selection ListBox
93 lb2 = new QListBox( row1 );
94
95 QHBox *row2 = new QHBox( this );
96 row2->setSpacing( 5 );
97
98 QVBox *box1 = new QVBox( row2 );
99 box1->setSpacing( 5 );
100
101 // Create a non-editable Combobox and a label below...
102 QComboBox *cb1 = new QComboBox( FALSE, box1 );
103 label1 = new QLabel( "Current Item: Combobox Item 0", box1 );
104 label1->setMaximumHeight( label1->sizeHint().height() * 2 );
105 label1->setFrameStyle( QFrame::Panel | QFrame::Sunken );
106
107 //...and insert 50 items into the Combobox
108 for ( i = 0; i < 50; i++ ) {
109 str = QString( "Combobox Item %1" ).arg( i );
110 if ( i % 9 )
111 cb1->insertItem( str );
112 else
113 cb1->listBox()->insertItem( new MyListBoxItem );
114 }
115
116 QVBox *box2 = new QVBox( row2 );
117 box2->setSpacing( 5 );
118
119 // Create an editable Combobox and a label below...
120 QComboBox *cb2 = new QComboBox( TRUE, box2 );
121 label2 = new QLabel( "Current Item: Combobox Item 0", box2 );
122 label2->setMaximumHeight( label2->sizeHint().height() * 2 );
123 label2->setFrameStyle( QFrame::Panel | QFrame::Sunken );
124
125 // ... and insert 50 items into the Combobox
126 for ( i = 0; i < 50; i++ ) {
127 str = QString( "Combobox Item %1" ).arg( i );
128 if ( !( i % 4 ) )
129 cb2->insertItem( QPixmap( "fileopen.xpm" ), str );
130 else
131 cb2->insertItem( str );
132 }
133
134 // Connect the activated SIGNALs of the Comboboxes with SLOTs
135 connect( cb1, SIGNAL( activated( const QString & ) ), this, SLOT( slotCombo1Activated( const QString & ) ) );
136 connect( cb2, SIGNAL( activated( const QString & ) ), this, SLOT( slotCombo2Activated( const QString & ) ) );
137}
138
139/*
140 * SLOT slotLeft2Right
141 *
142 * Copies all selected items of the first ListBox into the
143 * second ListBox
144 */
145
146void ListBoxCombo::slotLeft2Right()
147{
148 // Go through all items of the first ListBox
149 for ( unsigned int i = 0; i < lb1->count(); i++ ) {
150 QListBoxItem *item = lb1->item( i );
151 // if the item is selected...
152 if ( item->isSelected() ) {
153 // ...and it is a text item...
154 if ( item->pixmap() && !item->text().isEmpty() )
155 lb2->insertItem( *item->pixmap(), item->text() );
156 else if ( !item->pixmap() )
157 lb2->insertItem( item->text() );
158 else if ( item->text().isEmpty() )
159 lb2->insertItem( *item->pixmap() );
160 }
161 }
162}
163
164/*
165 * SLOT slotCombo1Activated( const QString &s )
166 *
167 * Sets the text of the item which the user just selected
168 * in the first Combobox (and is now the value of s) to
169 * the first Label.
170 */
171
172void ListBoxCombo::slotCombo1Activated( const QString &s )
173{
174 label1->setText( QString( "Current Item: %1" ).arg( s ) );
175}
176
177/*
178 * SLOT slotCombo2Activated( const QString &s )
179 *
180 * Sets the text of the item which the user just selected
181 * in the second Combobox (and is now the value of s) to
182 * the second Label.
183 */
184
185void ListBoxCombo::slotCombo2Activated( const QString &s )
186{
187 label2->setText( QString( "Current Item: %1" ).arg( s ) );
188}
Note: See TracBrowser for help on using the repository browser.