Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Examples  

qschildlist.h

00001 /***************************************************************************
00002                           qschildlist.h  -  description
00003                              -------------------
00004     begin                : Thu Aug 23 2001
00005     copyright            : (C) 2001 by kamil
00006     email                : kamil@localhost.localdomain
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef QSCHILDLIST_H
00019 #define QSCHILDLIST_H
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include<deque.h>
00026 
00035 template<class CHILD_OBJECT>
00036 class QSChildList {
00037   public:
00038      QSChildList( bool autoDelete = true ) {
00039         m_index = 0;
00040         m_auto_delete = autoDelete;
00041         }
00042     ~QSChildList() {
00043         while( count() ) del(0);
00044         }
00045      void setPos( int index ) {
00046         m_index = index;
00047         }
00048      int pos() const {
00049         return m_index;
00050         }
00051      CHILD_OBJECT *current() const {
00052         return operator[]( m_index );
00053         }
00054      bool isValidPos() const  {
00055         return valid(m_index);
00056         }
00057      int count() const {
00058         return (int )m_child_list.size();
00059         }
00060      void add( CHILD_OBJECT *object ) {
00061         insert( count(), object );
00062         }
00063      void insert( int before_pos, CHILD_OBJECT *object ) {
00064         before_pos = QMIN( before_pos, count() );
00065         before_pos = QMAX( before_pos, 0 );
00066         if ( object ) m_child_list.insert( m_child_list.begin()+before_pos, object );
00067         }
00068      CHILD_OBJECT *remove( int index ) {
00069         if ( valid(index) ) {   
00070             CHILD_OBJECT *object = m_child_list[index];
00071             m_child_list.erase( m_child_list.begin() + index );
00072             return object;
00073             }
00074         return NULL;
00075         }
00076      void del( int index ) {
00077         if ( m_auto_delete ) delete remove(index); else remove(index);
00078         }
00079      void toFront( int index ) {
00080         if ( valid(index) ) m_child_list.push_back(  remove(index) );
00081         }
00082      void toBack( int index ) {
00083         if ( valid(index) ) m_child_list.push_front( remove(index) );
00084         }
00085      void raise( int index ) {
00086         if ( index<count()-1 ) reorder( index+1, index );
00087         }
00088      void lower( int index ) {
00089                 if ( index>0 ) reorder( index-1, index );
00090         }
00091      void reorder( int position, int index ) {
00092         position = QMIN( position, count() );
00093         position = QMAX( position, 0 );
00094         if ( valid(index) ) {
00095              CHILD_OBJECT *object = remove(index);
00096              m_child_list.insert( m_child_list.begin() + position, object  );           
00097             }       
00098         }
00099      int find( CHILD_OBJECT *object ) const {
00100         for( int i=0; i<count(); i++ ) if ( m_child_list[i] == object ) return i;
00101         return -1;
00102         }
00103      CHILD_OBJECT *operator[]( int index ) const  {
00104         return valid(index) ? m_child_list[index] : NULL;
00105         }
00106      inline bool valid( int index ) const {
00107         return ( index<count() && index>=0 );
00108         }
00109      inline bool autoDelete() const {
00110         return m_auto_delete;
00111         }
00112   private:
00113      int m_index;
00114      bool m_auto_delete;
00115      deque<CHILD_OBJECT*> m_child_list;
00116  };
00117 
00118 #endif
00119 

Generated on Mon Mar 18 19:16:31 2002 for KMatplot library by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002