uifw/AvKon/aknhlist/src/akntreenode.cpp
changeset 0 2f259fa3e83a
child 6 9f56a4e1b8ab
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2006, 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation for CAknTreeNode class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "akntreenode.h"
       
    20 
       
    21 const TUint KDefaultNodeFlags = NULL;
       
    22 const TInt KArrayGranularity = 16;
       
    23 
       
    24 
       
    25 // ======== MEMBER FUNCTIONS ========
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Destructor.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CAknTreeNode::~CAknTreeNode()
       
    32     {
       
    33     iChild.ResetAndDestroy();
       
    34     }
       
    35 
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // Returns the number of children.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 TInt CAknTreeNode::ChildCount() const
       
    42     {
       
    43     return iChild.Count();
       
    44     }
       
    45 
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // Returns the number of descendants. Current implementation counts the value
       
    49 // by recursively calling itself to all the child nodes, but the descendant
       
    50 // count could instead be stored in each node.
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 TInt CAknTreeNode::DescendantCount() const
       
    54     {
       
    55     TInt count = iChild.Count();
       
    56     CAknTreeNode* node = NULL;
       
    57     for ( TInt ii = count - 1; ii >= 0; --ii )
       
    58         {
       
    59         node = iChild[ii]->Node();
       
    60         if ( node )
       
    61             {
       
    62             count += node->DescendantCount();
       
    63             }
       
    64         }
       
    65     return count;
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Returns the number of visible descendants.
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 TInt CAknTreeNode::VisibleDescendantCount() const
       
    74     {
       
    75     TInt count = IsExpanded() ? iChild.Count() : NULL;
       
    76     CAknTreeNode* node = NULL;
       
    77     for ( TInt ii = count - 1; ii >= 0; --ii )
       
    78         {
       
    79         node = iChild[ii]->Node();
       
    80         if ( node )
       
    81             {
       
    82             count += node->VisibleDescendantCount();
       
    83             }
       
    84         }
       
    85     return count;
       
    86     }
       
    87 
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Returns the child with the specified index.
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 CAknTreeItem* CAknTreeNode::Child( TInt aIndex ) const
       
    94     {
       
    95     // Panics if index is out of range.
       
    96     return iChild[aIndex];
       
    97     }
       
    98 
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Returns the index of the specified child. The error code KErrNotFound is
       
   102 // returned, if the item is not found.
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 TInt CAknTreeNode::Index( const CAknTreeItem* aItem ) const
       
   106     {
       
   107     __ASSERT_DEBUG( aItem, User::Invariant() );
       
   108     return iChild.Find( aItem );
       
   109     }
       
   110 
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // Adds a child to the node.
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CAknTreeNode::AddChildL( CAknTreeItem* aItem )
       
   117     {
       
   118     __ASSERT_DEBUG( aItem != this, User::Invariant() );
       
   119     __ASSERT_DEBUG( aItem != NULL, User::Invariant() );
       
   120 
       
   121     TLinearOrder<CAknTreeItem> order( CAknTreeItem::Compare );
       
   122     iChild.InsertInOrderAllowRepeatsL( aItem, order );
       
   123     }
       
   124 
       
   125 
       
   126 void CAknTreeNode::AddChildL( CAknTreeItem* aItem, TInt aPosition )
       
   127     {
       
   128     __ASSERT_DEBUG( aItem != this, User::Invariant() );
       
   129     __ASSERT_DEBUG( aItem != NULL, User::Invariant() );
       
   130 
       
   131     iChild.InsertL( aItem, aPosition );
       
   132     }
       
   133 
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // Removes the specified item from the set of children.
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 TInt CAknTreeNode::RemoveChild( CAknTreeItem* aItem )
       
   140     {
       
   141     TInt index = iChild.Find( aItem );
       
   142     if ( index >= 0 )
       
   143         {
       
   144         iChild.Remove( index );
       
   145         return KErrNone;
       
   146         }
       
   147     else
       
   148         {
       
   149         return KErrNotFound;
       
   150         }
       
   151     }
       
   152 
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // Checks whether the node is expanded.
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 TBool CAknTreeNode::IsExpanded() const
       
   159     {
       
   160     return ( iFlags & EExpanded ) ? ETrue : EFalse;
       
   161     }
       
   162 
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // Sets the node expanded.
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void CAknTreeNode::Expand()
       
   169     {
       
   170     iFlags |= EExpanded;
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // Sets the node collapsed.
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CAknTreeNode::Collapse()
       
   179     {
       
   180     iFlags &= ~EExpanded;
       
   181     }
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // Checks whether the node is empty.
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 TBool CAknTreeNode::IsEmpty() const
       
   189     {
       
   190     return ( iChild.Count() == 0 ) && !( iFlags & ENonEmpty );
       
   191     }
       
   192 
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // Changes the state of the non-empty flag.
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void CAknTreeNode::SetNonEmpty( TBool aNonEmpty )
       
   199     {
       
   200     if ( aNonEmpty )
       
   201         {
       
   202         iFlags |= ENonEmpty;
       
   203         }
       
   204     else
       
   205         {
       
   206         iFlags &= ~ENonEmpty;
       
   207         }
       
   208     }
       
   209 
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Sort.
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void CAknTreeNode::Sort()
       
   216     {
       
   217     TLinearOrder<CAknTreeItem> order( CAknTreeItem::Compare );
       
   218     iChild.Sort( order );
       
   219     }
       
   220 
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // From class CAknTreeItem.
       
   224 // Returns pointer to this node.
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 CAknTreeNode* CAknTreeNode::Node()
       
   228     {
       
   229     return this;
       
   230     }
       
   231 
       
   232 
       
   233 const CAknTreeNode* CAknTreeNode::Node() const
       
   234     {
       
   235     return this;
       
   236     }
       
   237 
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // From class CAknTreeItem.
       
   241 // Checks whether then node is set marked.
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 TBool CAknTreeNode::IsMarked() const
       
   245     {
       
   246     return ( iFlags & EMarked ) ? ETrue : EFalse;
       
   247     }
       
   248 
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // From class CAknTreeItem.
       
   252 // Changes the state of the marked flag, if marking change is not disabled.
       
   253 // ---------------------------------------------------------------------------
       
   254 //
       
   255 void CAknTreeNode::SetMarked( TBool aMarked )
       
   256     {
       
   257     if ( !( iFlags & EMarkingDisabled ) )
       
   258         {
       
   259         if ( aMarked )
       
   260             {
       
   261             iFlags |= EMarked;
       
   262             }
       
   263         else
       
   264             {
       
   265             iFlags &= ~EMarked;
       
   266             }
       
   267         }
       
   268     }
       
   269 
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // From class CAknTreeItem.
       
   273 // Checks whether marking is enabled.
       
   274 // ---------------------------------------------------------------------------
       
   275 //
       
   276 TBool CAknTreeNode::IsMarkable() const
       
   277     {
       
   278     return ( iFlags & EMarkingDisabled ) ? EFalse : ETrue;
       
   279     }
       
   280 
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // From class CAknTreeItem.
       
   284 // Changes the state of the marking enabled flag.
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CAknTreeNode::SetMarkable( TBool aMarkable )
       
   288     {
       
   289     if ( aMarkable )
       
   290         {
       
   291         iFlags &= ~EMarkingDisabled;
       
   292         }
       
   293     else
       
   294         {
       
   295         iFlags |= EMarkingDisabled;
       
   296         }
       
   297     }
       
   298 
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // From class CAknTreeItem.
       
   302 // Checks whether then node is set persistent.
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TBool CAknTreeNode::IsPersistent() const
       
   306     {
       
   307     return ( iFlags & EPersistent ) ? ETrue : EFalse;
       
   308     }
       
   309 
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // From class CAknTreeItem.
       
   313 // Changes the state of the persistent flag.
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 void CAknTreeNode::SetPersistent( TBool aPersistent )
       
   317     {
       
   318     if ( aPersistent )
       
   319         {
       
   320         iFlags |= EPersistent;
       
   321         }
       
   322     else
       
   323         {
       
   324         iFlags &= ~EPersistent;
       
   325         }
       
   326     }
       
   327 
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // From class CAknTreeItem.
       
   331 // Checks whether the node has persistent descendants.
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 TBool CAknTreeNode::HasPersistentDescendants() const
       
   335     {
       
   336     TBool persistent = EFalse;
       
   337     for ( TInt ii = 0; ii < iChild.Count() && !persistent; ++ii )
       
   338         {
       
   339         if ( iChild[ii]->IsPersistent() )
       
   340             {
       
   341             persistent = ETrue;
       
   342             }
       
   343         else if ( iChild[ii]->IsNode() )
       
   344             {
       
   345             persistent = iChild[ii]->Node()->HasPersistentDescendants();
       
   346             }
       
   347         }
       
   348     return persistent;
       
   349     }
       
   350 
       
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // From class CAknTreeItem.
       
   354 // Checks whether the item can be removed from the tree when its parent node
       
   355 // is being collapsed.
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 TBool CAknTreeNode::IsRemovableFromCollapsedNode() const
       
   359     {
       
   360     TBool removable = !( IsMarked() || IsPersistent() );
       
   361     for ( TInt ii = 0; removable && ii < iChild.Count(); ++ii )
       
   362         {
       
   363         removable = iChild[ii]->IsRemovableFromCollapsedNode();
       
   364         }
       
   365     return removable;
       
   366     }
       
   367 
       
   368 
       
   369 // ---------------------------------------------------------------------------
       
   370 // Default C++ constructor.
       
   371 // ---------------------------------------------------------------------------
       
   372 //
       
   373 CAknTreeNode::CAknTreeNode()
       
   374     : iChild( KArrayGranularity ),
       
   375       iFlags( KDefaultNodeFlags )
       
   376     {
       
   377     }
       
   378 
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 // C++ constructor.
       
   382 // ---------------------------------------------------------------------------
       
   383 //
       
   384 CAknTreeNode::CAknTreeNode( TUint32 aFlags )
       
   385     : iChild( KArrayGranularity ),
       
   386       iFlags( aFlags )
       
   387     {
       
   388     }
       
   389 
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // Returns flags set for the node.
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 TUint32 CAknTreeNode::Flags() const
       
   396     {
       
   397     return iFlags;
       
   398     }
       
   399 
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // Sets flags for the node.
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 void CAknTreeNode::SetFlags( TUint32 aFlags )
       
   406     {
       
   407     iFlags = aFlags;
       
   408     }
       
   409