home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / checklists / checklists.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  5KB  |  164 lines

  1. /****************************************************************************
  2. ** $Id:  qt/checklists.cpp   3.0.0   edited Jun 22 13:24 $
  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 "checklists.h"
  12.  
  13. #include <qlistview.h>
  14. #include <qvbox.h>
  15. #include <qlabel.h>
  16. #include <qvaluelist.h>
  17. #include <qstring.h>
  18. #include <qpushbutton.h>
  19. #include <qlayout.h>
  20.  
  21. /*
  22.  * Constructor
  23.  *
  24.  * Create all child widgets of the CheckList Widget
  25.  */
  26.  
  27. CheckLists::CheckLists( QWidget *parent, const char *name )
  28.     : QWidget( parent, name )
  29. {
  30.     QHBoxLayout *lay = new QHBoxLayout( this );
  31.     lay->setMargin( 5 );
  32.  
  33.     // create a widget which layouts its childs in a column
  34.     QVBoxLayout *vbox1 = new QVBoxLayout( lay );
  35.     vbox1->setMargin( 5 );
  36.  
  37.     // First child: a Label
  38.     vbox1->addWidget( new QLabel( "Check some items!", this ) );
  39.  
  40.     // Second child: the ListView
  41.     lv1 = new QListView( this );
  42.     vbox1->addWidget( lv1 );
  43.     lv1->addColumn( "Items" );
  44.     lv1->setRootIsDecorated( TRUE );
  45.  
  46.     // create a list with 4 ListViewItems which will be parent items of other ListViewItems
  47.     QValueList<QListViewItem *> parentList;
  48.  
  49.     parentList.append( new QListViewItem( lv1, "Parent Item 1" ) );
  50.     parentList.append( new QListViewItem( lv1, "Parent Item 2" ) );
  51.     parentList.append( new QListViewItem( lv1, "Parent Item 3" ) );
  52.     parentList.append( new QListViewItem( lv1, "Parent Item 4" ) );
  53.  
  54.     QListViewItem *item = 0;
  55.     unsigned int num = 1;
  56.     // go through the list of parent items...
  57.     for ( QValueList<QListViewItem*>::Iterator it = parentList.begin(); it != parentList.end();
  58.       ( *it )->setOpen( TRUE ), ++it, num++ ) {
  59.     item = *it;
  60.     // ...and create 5 checkable child ListViewItems for each parent item
  61.     for ( unsigned int i = 1; i <= 5; i++ )
  62.         (void)new QCheckListItem( item, QString( "%1. Child of Parent %2" ).arg( i ).arg( num ), QCheckListItem::CheckBox );
  63.     }
  64.  
  65.     // Create another widget for layouting
  66.     QVBoxLayout *tmp = new QVBoxLayout( lay );
  67.     tmp->setMargin( 5 );
  68.  
  69.     // create a pushbutton
  70.     QPushButton *copy1 = new QPushButton( "  ->  ", this );
  71.     tmp->addWidget( copy1 );
  72.     copy1->setMaximumWidth( copy1->sizeHint().width() );
  73.     // connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
  74.     connect( copy1, SIGNAL( clicked() ), this, SLOT( copy1to2() ) );
  75.  
  76.     // another widget for layouting
  77.     QVBoxLayout *vbox2 = new QVBoxLayout( lay );
  78.     vbox2->setMargin( 5 );
  79.  
  80.     // and another label
  81.     vbox2->addWidget( new QLabel( "Check one item!", this ) );
  82.  
  83.     // create the second listview
  84.     lv2 = new QListView( this );
  85.     vbox2->addWidget( lv2 );
  86.     lv2->addColumn( "Items" );
  87.     lv2->setRootIsDecorated( TRUE );
  88.  
  89.     // another widget needed for layouting only
  90.     tmp = new QVBoxLayout( lay );
  91.     tmp->setMargin( 5 );
  92.  
  93.     // create another pushbutton...
  94.     QPushButton *copy2 = new QPushButton( "  ->  ", this );
  95.     lay->addWidget( copy2 );
  96.     copy2->setMaximumWidth( copy2->sizeHint().width() );
  97.     // ...and connect its clicked() SIGNAL to the copy2to3() SLOT
  98.     connect( copy2, SIGNAL( clicked() ), this, SLOT( copy2to3() ) );
  99.  
  100.     tmp = new QVBoxLayout( lay );
  101.     tmp->setMargin( 5 );
  102.  
  103.     // and create a label which will be at the right of the window
  104.     label = new QLabel( "No Item yet...", this );
  105.     tmp->addWidget( label );
  106. }
  107.  
  108. /*
  109.  * SLOT copy1to2()
  110.  *
  111.  * Copies all checked ListViewItems from the first ListView to
  112.  * the second one, and inserts them as Radio-ListViewItem.
  113.  */
  114.  
  115. void CheckLists::copy1to2()
  116. {
  117.     // create an iterator which operates on the first ListView
  118.     QListViewItemIterator it( lv1 );
  119.  
  120.     lv2->clear();
  121.  
  122.     // Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
  123.     // are inserted into a Listview, the parent item of these MUST be a controller Item!
  124.     QCheckListItem *item = new QCheckListItem( lv2, "Controller", QCheckListItem::Controller );
  125.     item->setOpen( TRUE );
  126.  
  127.     // iterate through the first ListView...
  128.     for ( ; it.current(); ++it )
  129.     // ...check state of childs, and...
  130.     if ( it.current()->parent() )
  131.         // ...if the item is checked...
  132.         if ( ( (QCheckListItem*)it.current() )->isOn() )
  133.         // ...insert a Radio-ListViewItem with the same text into the second ListView
  134.         (void)new QCheckListItem( item, it.current()->text( 0 ), QCheckListItem::RadioButton );
  135.  
  136.     if ( item->firstChild() )
  137.     ( ( QCheckListItem* )item->firstChild() )->setOn( TRUE );
  138. }
  139.  
  140. /*
  141.  * SLOT copy2to3()
  142.  *
  143.  * Copies the checked item of the second ListView into the
  144.  * Label at the right.
  145.  */
  146.  
  147. void CheckLists::copy2to3()
  148. {
  149.     // create an iterator which operates on the second ListView
  150.     QListViewItemIterator it( lv2 );
  151.  
  152.     label->setText( "No Item checked" );
  153.  
  154.     // iterate through the second ListView...
  155.     for ( ; it.current(); ++it )
  156.     // ...check state of childs, and...
  157.     if ( it.current()->parent() )
  158.         // ...if the item is checked...
  159.         if ( ( (QCheckListItem*)it.current() )->isOn() )
  160.         // ...set the text of the item to the label
  161.         label->setText( it.current()->text( 0 ) );
  162. }
  163.  
  164.