Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

var_tree.cpp

00001 /*****************************************************************************
00002  * var_tree.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2005 VideoLAN
00005  * $Id: var_bool.hpp 9934 2005-02-15 13:55:08Z courmisch $
00006  *
00007  * Authors: Antoine Cellerier <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include "var_tree.hpp"
00025 
00026 
00027 const string VarTree::m_type = "tree";
00028 
00029 VarTree::VarTree( intf_thread_t *pIntf )
00030     : Variable( pIntf ), m_id( 0 ), m_selected( false ), m_playing( false ),
00031     m_expanded( true ), m_pData( NULL ), m_pParent( NULL )
00032 {
00033     // Create the position variable
00034     m_cPosition = VariablePtr( new VarPercent( pIntf ) );
00035     getPositionVar().set( 1.0 );
00036 }
00037 
00038 VarTree::VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
00039                   const UStringPtr &rcString, bool selected, bool playing,
00040                   bool expanded, void *pData )
00041     : Variable( pIntf ), m_id( id ), m_cString( rcString ),
00042     m_selected( selected ), m_playing( playing ), m_expanded( expanded ),
00043     m_pData( pData ), m_pParent( pParent )
00044 {
00045     // Create the position variable
00046     m_cPosition = VariablePtr( new VarPercent( pIntf ) );
00047     getPositionVar().set( 1.0 );
00048 }
00049 
00050 VarTree::~VarTree()
00051 {
00052 // TODO : check that children are deleted
00053 }
00054 
00055 void VarTree::add( int id, const UStringPtr &rcString, bool selected,
00056                    bool playing, bool expanded, void *pData )
00057 {
00058     m_children.push_back( VarTree( getIntf(), this, id, rcString, selected,
00059                                    playing, expanded, pData ) );
00060     notify();
00061 }
00062 
00063 void VarTree::delSelected()
00064 {
00065     Iterator it = begin();
00066     while( it != end() )
00067     {
00068         //dig down the tree
00069         if( size() ) it->delSelected();
00070         //stay on some level
00071         if( it->m_selected )
00072         {
00073             Iterator oldIt = it;
00074             it++;
00075             m_children.erase( oldIt );
00076         }
00077         else
00078         {
00079             it++;
00080         }
00081     }
00082     notify();
00083 }
00084 
00085 void VarTree::clear()
00086 {
00087     m_children.clear();
00088 }
00089 
00090 VarTree::Iterator VarTree::operator[]( int n )
00091 {
00092     Iterator it;
00093     int i;
00094     for( it = begin(), i = 0;
00095          i < n && it != end();
00096          it++, i++ );
00097     return it;
00098 }
00099 
00100 VarTree::ConstIterator VarTree::operator[]( int n ) const
00101 {
00102     ConstIterator it;
00103     int i;
00104     for( it = begin(), i = 0;
00105          i < n && it != end();
00106          it++, i++ );
00107     return it;
00108 }
00109 
00110 /* find iterator to next ancestor
00111  * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
00112 VarTree::Iterator VarTree::uncle()
00113 {
00114     VarTree *p_parent = parent();
00115     if( p_parent != NULL )
00116     {
00117         VarTree *p_grandparent = p_parent->parent();
00118         while( p_grandparent != NULL )
00119         {
00120             Iterator it = p_grandparent->begin();
00121             while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
00122             if( it != p_grandparent->end() )
00123             {
00124                 it++;
00125                 if( it != p_grandparent->end() )
00126                 {
00127                     return it;
00128                 }
00129             }
00130             if( p_grandparent->parent() )
00131             {
00132                 p_parent = p_grandparent;
00133                 p_grandparent = p_parent->parent();
00134             }
00135             else
00136                 p_grandparent = NULL;
00137         }
00138     }
00139 
00140     /* if we didn't return before, it means that we've reached the end */
00141     return root()->end();
00142 }
00143 
00144 void VarTree::checkParents( VarTree *pParent )
00145 {
00146     m_pParent = pParent;
00147     Iterator it = begin();
00148     while( it != end() )
00149     {
00150         it->checkParents( this );
00151         it++;
00152     }
00153 }
00154 
00155 int VarTree::visibleItems()
00156 {
00157     int i_count = size();
00158     Iterator it = begin();
00159     while( it != end() )
00160     {
00161         if( it->m_expanded )
00162         {
00163             i_count += it->visibleItems();
00164         }
00165         it++;
00166     }
00167     return i_count;
00168 }
00169 
00170 VarTree::Iterator VarTree::getVisibleItem( int n )
00171 {
00172     Iterator it = begin();
00173     while( it != end() )
00174     {
00175         n--;
00176         if( n <= 0 ) return it;
00177         if( it->m_expanded )
00178         {
00179             int i = n - it->visibleItems();
00180             if( i <= 0 ) return it->getVisibleItem( n );
00181             n = i;
00182         }
00183         it++;
00184     }
00185     return end();
00186 }
00187 
00188 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
00189 {
00190     if( it->m_expanded && it->size() )
00191     {
00192         it = it->begin();
00193     }
00194     else
00195     {
00196         VarTree::Iterator it_old = it;
00197         it++;
00198         // Was 'it' the last brother? If so, look for uncles
00199         if( it_old->parent() && it_old->parent()->end() == it )
00200         {
00201             it = it_old->uncle();
00202         }
00203     }
00204     return it;
00205 }
00206 
00207 VarTree::Iterator VarTree::findById( int id )
00208 {
00209     for (Iterator it = begin(); it != end(); ++it )
00210     {
00211         if( it->m_id == id )
00212         {
00213             return it;
00214         }
00215         Iterator result = it->findById( id );
00216         if( result != it->end() ) return result;
00217     }
00218     return end();
00219 }
00220 

Generated on Tue Dec 20 10:14:43 2005 for vlc-0.8.4a by  doxygen 1.4.2