mulwidgets/muldatamodel/inc/multree.h
branchRCL_3
changeset 20 0e9bb658ef58
parent 0 e83bab7cf002
equal deleted inserted replaced
19:4ea6f81c838a 20:0e9bb658ef58
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  Header for MulTree
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef MULTREE_H_
       
    20 #define MULTREE_H_
       
    21 
       
    22 #include <memory>
       
    23 #include <vector>
       
    24 #include <osn/osnnew.h>
       
    25 
       
    26 namespace Alf
       
    27     {
       
    28 //Forward declaration
       
    29 class MulDataPath;
       
    30 	
       
    31 class MulTreeNode
       
    32 	{
       
    33 public:
       
    34 
       
    35 	/**
       
    36 	 * Constuctor
       
    37 	 * 
       
    38 	 * @param aIndex Index of this node
       
    39 	 */
       
    40 	MulTreeNode( int aIndex )
       
    41 		{
       
    42 		mParent = NULL;
       
    43 		mExpandedState = false;
       
    44 		mIndex = aIndex;
       
    45 		}
       
    46 	
       
    47 	/**
       
    48 	 * Constuctor
       
    49 	 * 
       
    50 	 * @param aParent Parent node for this node. Node dosent take ownership of aParent
       
    51 	 * @param aIndex Index of this node
       
    52 	 */
       
    53 	MulTreeNode( MulTreeNode* aParent, int aIndex )
       
    54 		{
       
    55 		mParent = aParent;
       
    56 		mExpandedState = false;
       
    57 		mIndex = aIndex;
       
    58 		}
       
    59 
       
    60 	/**
       
    61 	 * Destructor
       
    62 	 */
       
    63 	~MulTreeNode()
       
    64 		{
       
    65 		int count = mChilds.size();
       
    66 		for(int i = 0 ; i < count ; ++i )
       
    67 			{
       
    68 			delete Child(i);
       
    69 			}
       
    70 		mChilds.clear();
       
    71 		}
       
    72 	
       
    73 	/**
       
    74 	 * Insert child node in this node at given index
       
    75 	 * 
       
    76 	 * @param aChild Child node.Takes ownership of aChild
       
    77 	 * @param aIndex index at which child node to be inserted
       
    78 	 */
       
    79 	void InsertChild( std::auto_ptr<MulTreeNode> aChild, int aIndex )
       
    80 		{
       
    81 		aChild->mParent = this;
       
    82 		mChilds.insert( mChilds.begin() + aIndex, aChild.get() );
       
    83 		aChild.release();
       
    84 		}
       
    85 	
       
    86 	/**
       
    87 	 * Remove child node from this node.
       
    88 	 * 
       
    89 	 * @param aIndex Index of child that need to be removed
       
    90 	 */
       
    91 	void RemoveChild( int aIndex )
       
    92 		{
       
    93 		MulTreeNode* childNode = Child(aIndex);
       
    94 		delete childNode;
       
    95 		childNode = NULL;
       
    96 		mChilds.erase( mChilds.begin() + aIndex );
       
    97 		}
       
    98 	
       
    99 	/**
       
   100 	 * Number of child node that this node contains
       
   101 	 * 
       
   102 	 * @return Number of childs
       
   103 	 */
       
   104 	int ChildCount() const
       
   105 		{
       
   106 		return mChilds.size();
       
   107 		}
       
   108 	
       
   109 	/**
       
   110 	 * Return child node at given index
       
   111 	 * 
       
   112 	 * @param aIndex index of child node
       
   113 	 * @return Child node at given index
       
   114 	 */
       
   115 	MulTreeNode* Child( int aIndex )
       
   116 		{
       
   117 		//At checks for out of bound condition we dont need to check again
       
   118 		return mChilds.at(aIndex);
       
   119 		}
       
   120 	
       
   121 	/**
       
   122 	 * Check that weather this node is parent node and has any childs
       
   123 	 * 
       
   124 	 * @retrun true if node has cilds false otherwise
       
   125 	 */
       
   126 	bool HasChild()	const		
       
   127 		{	
       
   128 		return ( ChildCount() > 0 );	
       
   129 		}
       
   130 	
       
   131 	/**
       
   132 	 * Return parent of this node
       
   133 	 * 
       
   134 	 * @return Parent of node
       
   135 	 */
       
   136 	MulTreeNode* Parent() const
       
   137 		{
       
   138 		return mParent;
       
   139 		}
       
   140 	
       
   141 	/**
       
   142 	 * Set Index of node to current path
       
   143 	 * 
       
   144 	 * @param aIndex index of node
       
   145 	 */
       
   146 	void SetIndex( int aIndex )
       
   147 		{
       
   148 		mIndex = aIndex;
       
   149 		}
       
   150 
       
   151 	/**
       
   152 	 * Return index of node
       
   153 	 * 
       
   154 	 * @return index of node.
       
   155 	 */
       
   156 	int Index() const
       
   157 		{
       
   158 		return mIndex;
       
   159 		}
       
   160 	
       
   161 	/**
       
   162 	 * Expand or unexpand node.
       
   163 	 * 
       
   164 	 * @param aExpendedState New state of node
       
   165 	 */
       
   166 	void SetExpanded( bool aExpandedState )
       
   167 		{
       
   168 		mExpandedState = aExpandedState;
       
   169 		}
       
   170 	
       
   171 	/**
       
   172 	 * Return weather this node is expended or not
       
   173 	 * 
       
   174 	 * @return true if expended, false otherwise
       
   175 	 */
       
   176 	bool IsExpanded() const
       
   177 		{
       
   178 		return mExpandedState;
       
   179 		}
       
   180 	
       
   181 private:
       
   182 
       
   183 	MulTreeNode* mParent; //not own
       
   184 	int mIndex;
       
   185 	bool mExpandedState;
       
   186 	std::vector<MulTreeNode*> mChilds; //owns all children	
       
   187 	};
       
   188 
       
   189 class MulTree 
       
   190 	{
       
   191 public: //Constructor and Destructor
       
   192 
       
   193 	/**
       
   194 	 * C++ Constructor
       
   195 	 */
       
   196 	MulTree()
       
   197 		{
       
   198 		//create root node for tree
       
   199 		mRootNode.reset( new (EMM) MulTreeNode(0) );
       
   200 		mRootNode->SetExpanded( true );
       
   201 		mRootNode->SetIndex(0);
       
   202 		}
       
   203 
       
   204 	/**
       
   205 	 * Destructor
       
   206 	 */
       
   207 	~MulTree()
       
   208 		{
       
   209 		}
       
   210 	
       
   211 public: //New method
       
   212 	
       
   213 	/**
       
   214 	 * Create new node at specified index
       
   215 	 * 
       
   216 	 * @param aPath Path to parent node
       
   217 	 * @param aIndex Position at which node to be inserted in parent node
       
   218 	 */
       
   219 	void AddNode( const MulDataPath& aPath, int aIndex );
       
   220 	
       
   221 	/**
       
   222 	 * Remove node from specified index
       
   223 	 * 
       
   224 	 * @param aPath Path to parent node
       
   225 	 * @param aIndex Posiotin at which node to be remove from parent node
       
   226 	 */
       
   227 	void RemoveNode( const MulDataPath& aPath, int aIndex );
       
   228 	
       
   229 	/**
       
   230 	 * Finds node with specified path
       
   231 	 * 
       
   232 	 * @param aPath path of node to be find
       
   233 	 * @return Node does not return ownership
       
   234 	 */
       
   235     MulTreeNode* FindNode( const MulDataPath& aPath ) const;
       
   236 
       
   237 	/**
       
   238 	 * Finds node with specified index in specified node
       
   239 	 * 
       
   240 	 * @param aParentNode Parent node in which search required to be made
       
   241 	 * @param aIndex Index of node to be find
       
   242 	 * @return Node does not return ownership
       
   243 	 */
       
   244 	MulTreeNode* FindChildNode( MulTreeNode* aParentNode, int aIndex ) const;
       
   245 	
       
   246 	/**
       
   247 	 * Find node with specified absolute index and return path to that node.
       
   248 	 * 
       
   249 	 * @param aIndex Index of node
       
   250 	 * @return Path to node
       
   251 	 */
       
   252 	MulDataPath FindNode( int aIndex ) const;
       
   253 		
       
   254 	/**
       
   255 	 * Create and return path for given node
       
   256 	 * 
       
   257 	 * @param aNode Node of which path is required
       
   258 	 * @return Path of node
       
   259 	 */
       
   260 	MulDataPath Path( MulTreeNode& aNode ) const;
       
   261 	
       
   262 	/**
       
   263 	 * Return number of node this tree contains
       
   264 	 * 
       
   265 	 * @return Number of nodes
       
   266 	 */
       
   267 	int NodeCount() const;
       
   268 	
       
   269 	/**
       
   270 	 * Return number of node this node branch
       
   271 	 * 
       
   272 	 * @return Number of nodes
       
   273 	 */
       
   274 	int NodeCount(MulTreeNode* aCurrentNode ) const;
       
   275 	
       
   276 	/**
       
   277 	 * Return number of items,
       
   278 	 * Checks that node is expanded or not, 
       
   279 	 * if expanded then add it counts child count else ignore child count.
       
   280 	 * 
       
   281 	 * @return Node count
       
   282 	 */
       
   283 	int ExpandedNodeCount() const;
       
   284 	
       
   285 	/**
       
   286 	 * Return number of items,
       
   287 	 * Checks that node is expanded or not, 
       
   288 	 * if expanded then add it couts child count else ignore child count.
       
   289 	 * 
       
   290 	 * @param aCurrentNode Node of which count is needed
       
   291 	 * 
       
   292 	 * @return Node count
       
   293 	 */
       
   294 	int ExpandedNodeCount(MulTreeNode* aCurrentNode ) const;
       
   295 	
       
   296 	/**
       
   297 	 * Returns node's absolute index
       
   298 	 * 
       
   299 	 * @param aPath Path to node
       
   300 	 * @param aIndex index of node in path
       
   301 	 * @return position of node in whole tree
       
   302 	 */
       
   303 	int NodeIndex( const MulDataPath& aPath, int aIndex ) const;
       
   304 		
       
   305 private:	
       
   306 
       
   307 	/**
       
   308 	 * Find node with specified absolute index and return path to that node.
       
   309 	 * 
       
   310 	 * @param aNode Node in which index to be find
       
   311 	 * @param aIndex Index of node
       
   312 	 * @return Path to node
       
   313 	 */
       
   314 	MulDataPath FindNode( MulTreeNode* aNode, int& aAbsoluteIndex, int aIndex ) const;
       
   315 	
       
   316 	/**
       
   317 	 * Returns node's absolute index
       
   318 	 * 
       
   319 	 * @param aCurrentNode Node to compare path with
       
   320 	 * @param aPath Path of node which absolute index is required
       
   321 	 * @param aAbsoluteIndex Counter variable
       
   322 	 * @return position of node in whole tree
       
   323 	 */
       
   324 	int NodeIndex( MulTreeNode* aCurrentNode, const MulDataPath& aPath,int& aAbsoluteIndex ) const;
       
   325 	
       
   326 private: //data
       
   327 
       
   328 	std::auto_ptr<MulTreeNode> mRootNode; //root of tree - owned
       
   329 	};
       
   330 	
       
   331 	} //namespace Alf
       
   332 
       
   333 #endif /*MULTREE_H_*/