source: trunk/examples/addressbook/centralwidget.cpp@ 7

Last change on this file since 7 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: 10.0 KB
Line 
1/****************************************************************************
2** $Id: centralwidget.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 "centralwidget.h"
12
13#include <qtabwidget.h>
14#include <qlistview.h>
15#include <qlayout.h>
16#include <qwidget.h>
17#include <qlabel.h>
18#include <qpushbutton.h>
19#include <qlineedit.h>
20#include <qlabel.h>
21#include <qcheckbox.h>
22#include <qfile.h>
23#include <qtextstream.h>
24
25ABCentralWidget::ABCentralWidget( QWidget *parent, const char *name )
26 : QWidget( parent, name )
27{
28 mainGrid = new QGridLayout( this, 2, 1, 5, 5 );
29
30 setupTabWidget();
31 setupListView();
32
33 mainGrid->setRowStretch( 0, 0 );
34 mainGrid->setRowStretch( 1, 1 );
35}
36
37void ABCentralWidget::save( const QString &filename )
38{
39 if ( !listView->firstChild() )
40 return;
41
42 QFile f( filename );
43 if ( !f.open( IO_WriteOnly ) )
44 return;
45
46 QTextStream t( &f );
47
48 QListViewItemIterator it( listView );
49
50 for ( ; it.current(); ++it )
51 for ( unsigned int i = 0; i < 4; i++ )
52 t << it.current()->text( i ) << "\n";
53
54 f.close();
55}
56
57void ABCentralWidget::load( const QString &filename )
58{
59 listView->clear();
60
61 QFile f( filename );
62 if ( !f.open( IO_ReadOnly ) )
63 return;
64
65 QTextStream t( &f );
66
67 while ( !t.atEnd() ) {
68 QListViewItem *item = new QListViewItem( listView );
69 for ( unsigned int i = 0; i < 4; i++ )
70 item->setText( i, t.readLine() );
71 }
72
73 f.close();
74}
75
76void ABCentralWidget::setupTabWidget()
77{
78 tabWidget = new QTabWidget( this );
79
80 QWidget *input = new QWidget( tabWidget );
81 QGridLayout *grid1 = new QGridLayout( input, 2, 5, 5, 5 );
82
83 QLabel *liFirstName = new QLabel( "First &Name", input );
84 liFirstName->resize( liFirstName->sizeHint() );
85 grid1->addWidget( liFirstName, 0, 0 );
86
87 QLabel *liLastName = new QLabel( "&Last Name", input );
88 liLastName->resize( liLastName->sizeHint() );
89 grid1->addWidget( liLastName, 0, 1 );
90
91 QLabel *liAddress = new QLabel( "Add&ress", input );
92 liAddress->resize( liAddress->sizeHint() );
93 grid1->addWidget( liAddress, 0, 2 );
94
95 QLabel *liEMail = new QLabel( "&E-Mail", input );
96 liEMail->resize( liEMail->sizeHint() );
97 grid1->addWidget( liEMail, 0, 3 );
98
99 add = new QPushButton( "A&dd", input );
100 add->resize( add->sizeHint() );
101 grid1->addWidget( add, 0, 4 );
102 connect( add, SIGNAL( clicked() ), this, SLOT( addEntry() ) );
103
104 iFirstName = new QLineEdit( input );
105 iFirstName->resize( iFirstName->sizeHint() );
106 grid1->addWidget( iFirstName, 1, 0 );
107 liFirstName->setBuddy( iFirstName );
108
109 iLastName = new QLineEdit( input );
110 iLastName->resize( iLastName->sizeHint() );
111 grid1->addWidget( iLastName, 1, 1 );
112 liLastName->setBuddy( iLastName );
113
114 iAddress = new QLineEdit( input );
115 iAddress->resize( iAddress->sizeHint() );
116 grid1->addWidget( iAddress, 1, 2 );
117 liAddress->setBuddy( iAddress );
118
119 iEMail = new QLineEdit( input );
120 iEMail->resize( iEMail->sizeHint() );
121 grid1->addWidget( iEMail, 1, 3 );
122 liEMail->setBuddy( iEMail );
123
124 change = new QPushButton( "&Change", input );
125 change->resize( change->sizeHint() );
126 grid1->addWidget( change, 1, 4 );
127 connect( change, SIGNAL( clicked() ), this, SLOT( changeEntry() ) );
128
129 tabWidget->addTab( input, "&Add/Change Entry" );
130
131 // --------------------------------------
132
133 QWidget *search = new QWidget( this );
134 QGridLayout *grid2 = new QGridLayout( search, 2, 5, 5, 5 );
135
136 cFirstName = new QCheckBox( "First &Name", search );
137 cFirstName->resize( cFirstName->sizeHint() );
138 grid2->addWidget( cFirstName, 0, 0 );
139 connect( cFirstName, SIGNAL( clicked() ), this, SLOT( toggleFirstName() ) );
140
141 cLastName = new QCheckBox( "&Last Name", search );
142 cLastName->resize( cLastName->sizeHint() );
143 grid2->addWidget( cLastName, 0, 1 );
144 connect( cLastName, SIGNAL( clicked() ), this, SLOT( toggleLastName() ) );
145
146 cAddress = new QCheckBox( "Add&ress", search );
147 cAddress->resize( cAddress->sizeHint() );
148 grid2->addWidget( cAddress, 0, 2 );
149 connect( cAddress, SIGNAL( clicked() ), this, SLOT( toggleAddress() ) );
150
151 cEMail = new QCheckBox( "&E-Mail", search );
152 cEMail->resize( cEMail->sizeHint() );
153 grid2->addWidget( cEMail, 0, 3 );
154 connect( cEMail, SIGNAL( clicked() ), this, SLOT( toggleEMail() ) );
155
156 sFirstName = new QLineEdit( search );
157 sFirstName->resize( sFirstName->sizeHint() );
158 grid2->addWidget( sFirstName, 1, 0 );
159
160 sLastName = new QLineEdit( search );
161 sLastName->resize( sLastName->sizeHint() );
162 grid2->addWidget( sLastName, 1, 1 );
163
164 sAddress = new QLineEdit( search );
165 sAddress->resize( sAddress->sizeHint() );
166 grid2->addWidget( sAddress, 1, 2 );
167
168 sEMail = new QLineEdit( search );
169 sEMail->resize( sEMail->sizeHint() );
170 grid2->addWidget( sEMail, 1, 3 );
171
172 find = new QPushButton( "F&ind", search );
173 find->resize( find->sizeHint() );
174 grid2->addWidget( find, 1, 4 );
175 connect( find, SIGNAL( clicked() ), this, SLOT( findEntries() ) );
176
177 cFirstName->setChecked( TRUE );
178 sFirstName->setEnabled( TRUE );
179 sLastName->setEnabled( FALSE );
180 sAddress->setEnabled( FALSE );
181 sEMail->setEnabled( FALSE );
182
183 tabWidget->addTab( search, "&Search" );
184
185 mainGrid->addWidget( tabWidget, 0, 0 );
186}
187
188void ABCentralWidget::setupListView()
189{
190 listView = new QListView( this );
191 listView->addColumn( "First Name" );
192 listView->addColumn( "Last Name" );
193 listView->addColumn( "Address" );
194 listView->addColumn( "E-Mail" );
195
196 listView->setSelectionMode( QListView::Single );
197
198 connect( listView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( itemSelected( QListViewItem* ) ) );
199
200 mainGrid->addWidget( listView, 1, 0 );
201 listView->setAllColumnsShowFocus( TRUE );
202}
203
204void ABCentralWidget::addEntry()
205{
206 if ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
207 !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) {
208 QListViewItem *item = new QListViewItem( listView );
209 item->setText( 0, iFirstName->text() );
210 item->setText( 1, iLastName->text() );
211 item->setText( 2, iAddress->text() );
212 item->setText( 3, iEMail->text() );
213 }
214
215 iFirstName->setText( "" );
216 iLastName->setText( "" );
217 iAddress->setText( "" );
218 iEMail->setText( "" );
219}
220
221void ABCentralWidget::changeEntry()
222{
223 QListViewItem *item = listView->currentItem();
224
225 if ( item &&
226 ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
227 !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) ) {
228 item->setText( 0, iFirstName->text() );
229 item->setText( 1, iLastName->text() );
230 item->setText( 2, iAddress->text() );
231 item->setText( 3, iEMail->text() );
232 }
233}
234
235void ABCentralWidget::selectionChanged()
236{
237 iFirstName->setText( "" );
238 iLastName->setText( "" );
239 iAddress->setText( "" );
240 iEMail->setText( "" );
241}
242
243void ABCentralWidget::itemSelected( QListViewItem *item )
244{
245 if ( !item )
246 return;
247 item->setSelected( TRUE );
248 item->repaint();
249
250 iFirstName->setText( item->text( 0 ) );
251 iLastName->setText( item->text( 1 ) );
252 iAddress->setText( item->text( 2 ) );
253 iEMail->setText( item->text( 3 ) );
254}
255
256void ABCentralWidget::toggleFirstName()
257{
258 sFirstName->setText( "" );
259
260 if ( cFirstName->isChecked() ) {
261 sFirstName->setEnabled( TRUE );
262 sFirstName->setFocus();
263 }
264 else
265 sFirstName->setEnabled( FALSE );
266}
267
268void ABCentralWidget::toggleLastName()
269{
270 sLastName->setText( "" );
271
272 if ( cLastName->isChecked() ) {
273 sLastName->setEnabled( TRUE );
274 sLastName->setFocus();
275 }
276 else
277 sLastName->setEnabled( FALSE );
278}
279
280void ABCentralWidget::toggleAddress()
281{
282 sAddress->setText( "" );
283
284 if ( cAddress->isChecked() ) {
285 sAddress->setEnabled( TRUE );
286 sAddress->setFocus();
287 }
288 else
289 sAddress->setEnabled( FALSE );
290}
291
292void ABCentralWidget::toggleEMail()
293{
294 sEMail->setText( "" );
295
296 if ( cEMail->isChecked() ) {
297 sEMail->setEnabled( TRUE );
298 sEMail->setFocus();
299 }
300 else
301 sEMail->setEnabled( FALSE );
302}
303
304void ABCentralWidget::findEntries()
305{
306 if ( !cFirstName->isChecked() &&
307 !cLastName->isChecked() &&
308 !cAddress->isChecked() &&
309 !cEMail->isChecked() ) {
310 listView->clearSelection();
311 return;
312 }
313
314 QListViewItemIterator it( listView );
315
316 for ( ; it.current(); ++it ) {
317 bool select = TRUE;
318
319 if ( cFirstName->isChecked() ) {
320 if ( select && it.current()->text( 0 ).contains( sFirstName->text() ) )
321 select = TRUE;
322 else
323 select = FALSE;
324 }
325 if ( cLastName->isChecked() ) {
326 if ( select && it.current()->text( 1 ).contains( sLastName->text() ) )
327 select = TRUE;
328 else
329 select = FALSE;
330 }
331 if ( cAddress->isChecked() ) {
332 if ( select && it.current()->text( 2 ).contains( sAddress->text() ) )
333 select = TRUE;
334 else
335 select = FALSE;
336 }
337 if ( cEMail->isChecked() ) {
338 if ( select && it.current()->text( 3 ).contains( sEMail->text() ) )
339 select = TRUE;
340 else
341 select = FALSE;
342 }
343
344 if ( select )
345 it.current()->setSelected( TRUE );
346 else
347 it.current()->setSelected( FALSE );
348 it.current()->repaint();
349 }
350}
Note: See TracBrowser for help on using the repository browser.