00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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