mulwidgets/muldatamodel/src/mulcachemanager.cpp
branchRCL_3
changeset 26 0e9bb658ef58
equal deleted inserted replaced
25:4ea6f81c838a 26: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:  Cache Manager class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mulcachemanager.h"
       
    20 
       
    21 #include <mul/imulvarianttype.h>
       
    22 #include <mul/muldatapath.h>
       
    23 
       
    24 #include <alf/alfvarianttype.h>
       
    25 #include <alf/ialfmap.h>
       
    26 #include <alf/ialfcontainer.h>
       
    27 #include <mul/muldatapath.h>
       
    28 
       
    29 #include <stdexcept>
       
    30 #include <osn/ustring.h>
       
    31 
       
    32 #include "mulassert.h"
       
    33 #include "mulmodeldef.h"
       
    34 #include "mulmodelimpl.h"
       
    35 #include "mul/mulmodelutility.h"
       
    36 #include "mullog.h"
       
    37 
       
    38 namespace Alf
       
    39 	{
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Constructor 
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 MulCacheManager::MulCacheManager()
       
    46 			   : mTotalCount(0)
       
    47 	{
       
    48 	}
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Destructor 
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 MulCacheManager::~MulCacheManager()
       
    55 	{	
       
    56 	MulVisualItemMap::iterator itr;
       
    57 	for( itr = mVisualItemMap.begin(); itr!= mVisualItemMap.end(); ++itr )
       
    58 		{
       
    59 		delete itr->second;
       
    60 		itr->second = NULL;
       
    61 		}
       
    62 	mVisualItemMap.clear();
       
    63 	}
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CreateNodes 
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 void MulCacheManager::CreateNodes(int aStartIndex, int aCount, const MulDataPath& /*aPath*/)
       
    70 	{
       
    71 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::CreateNodes()");
       
    72 	
       
    73 	__MUL_ASSERT_DEBUG( aStartIndex >= 0 && aStartIndex <= ExpandedNodeCount() && aCount > 0 , KLInvalidArgument );
       
    74 
       
    75 	if(mTotalCount > 0 && aStartIndex < mTotalCount )
       
    76 		{
       
    77 		UpdataPathInMapAfterInsert(mVisualItemMap, aStartIndex, aCount);
       
    78 		UpdataPathInMapAfterInsert(mSelectionMap, aStartIndex, aCount);
       
    79 		}
       
    80 
       
    81 	mTotalCount += aCount;
       
    82 	}
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // AddVisualItem 
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 void MulCacheManager::AddVisualItem( std::auto_ptr<MulVisualItem> aVisualItem,
       
    89 									const MulDataPath& /*aPath*/ , int aIndex)
       
    90 	{
       
    91 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::AddVisualItem()");
       
    92 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
    93 	
       
    94 	//adding item now
       
    95 	mVisualItemMap[aIndex] = aVisualItem.release();
       
    96 	}
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // AddVisualItem 
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 //void MulCacheManager::AddVisualItem( std::auto_ptr<MulVisualItem> aVisualItem, int aItemIndex )
       
   103 //	{
       
   104 //	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::AddVisualItem()");
       
   105 //	__MUL_ASSERT_DEBUG( aItemIndex >= 0 && aItemIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   106 //	
       
   107 //	MulDataPath path ;//= mTree.FindNode( aItemIndex );
       
   108 //	AddVisualItem( aVisualItem, path, aItemIndex );
       
   109 //	}
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // RemoveVisualItem 
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void MulCacheManager::RemoveVisualItem( int aIndex )
       
   116 	{
       
   117 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::RemoveVisualItem()");
       
   118 	
       
   119 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   120 	
       
   121 	//MulDataPath path = mTree.FindNode( aIndex );
       
   122 	MulDataPath path;
       
   123 	RemoveItemFromMap( path, aIndex );	
       
   124 	}
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // RemoveVisualItem 
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void MulCacheManager::RemoveVisualItem(const MulDataPath& aPath , int aIndex )
       
   131 	{
       
   132 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::RemoveVisualItem()");
       
   133 	
       
   134 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   135 		
       
   136 	RemoveItemFromMap( aPath, aIndex );
       
   137 	mSelectionMap.erase( aIndex ); //remove marking information for this path, index
       
   138 	
       
   139 	UpdataPathInMapAfterRemove( mVisualItemMap, aIndex,1 );	
       
   140 	UpdataPathInMapAfterRemove( mSelectionMap, aIndex,1 );	
       
   141 	mTotalCount--;
       
   142 	}
       
   143 	
       
   144 // ---------------------------------------------------------------------------
       
   145 // RemoveVisualItem 
       
   146 // ---------------------------------------------------------------------------
       
   147 //	
       
   148 void MulCacheManager::RemoveVisualItem(int aIndex, int aCount, const MulDataPath& aPath)
       
   149 	{
       
   150 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::RemoveVisualItem()");
       
   151 	
       
   152 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex+aCount <= ExpandedNodeCount() , KLInvalidArgument );
       
   153 	
       
   154 	for(int i = aIndex ; i < aIndex+aCount; i++ )	
       
   155 		{
       
   156 		RemoveItemFromMap( aPath, i );
       
   157 		mSelectionMap.erase( i ); //remove marking information for this path, index
       
   158 		}
       
   159 	
       
   160 	if(aIndex + aCount < ExpandedNodeCount() )
       
   161 		{
       
   162 		UpdataPathInMapAfterRemove( mVisualItemMap, aIndex, aCount);	
       
   163 		UpdataPathInMapAfterRemove( mSelectionMap, aIndex, aCount );		
       
   164 		}
       
   165 		
       
   166 	mTotalCount-= aCount;
       
   167 	}
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // RemoveItemFromMap 
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void MulCacheManager::RemoveItemFromMap( const MulDataPath& /*aPath*/ , int aIndex )
       
   174 	{
       
   175 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::RemoveItemFromMap()");
       
   176 	
       
   177 	MulVisualItemMap::iterator findIter = mVisualItemMap.find(aIndex);
       
   178 	if( findIter!= mVisualItemMap.end() )
       
   179 		{
       
   180 		delete findIter->second;
       
   181 		findIter->second = NULL;
       
   182 		mVisualItemMap.erase( aIndex );
       
   183 		}		
       
   184 	}
       
   185 	
       
   186 // ---------------------------------------------------------------------------
       
   187 // UpdataPathInMapAftreRemove 
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 template <typename T> void MulCacheManager::UpdataPathInMapAfterRemove( std::map<int,T >& aMap, int aIndex, int aCount )
       
   191 	{	
       
   192 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::UpdataPathInMapAfterRemove()");
       
   193 	
       
   194 	//loop through all items
       
   195 	//for(int i = 0 ; i < ExpandedNodeCount() ; ++i )
       
   196 	for(int i = aIndex ; i < ExpandedNodeCount() ; ++i )
       
   197 		{
       
   198 		// if index is greater than path index then index need to be modified
       
   199 		//if( i >= aIndex )
       
   200 			{	
       
   201 				
       
   202 			if( aMap.find( i ) != aMap.end() )
       
   203 				{			
       
   204 				T item = aMap.find( i )->second;
       
   205 				aMap.erase( i );
       
   206 				aMap[i-aCount] = item; //add item in to new index
       
   207 				}
       
   208 			}
       
   209 		}
       
   210 		
       
   211 	}
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // UpdataPathInMapAftreInsert 
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 template <typename T> void MulCacheManager::UpdataPathInMapAfterInsert( std::map<int,T >& aMap, int aIndex, int aCount )
       
   218 	{
       
   219 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::UpdataPathInMapAfterInsert()");
       
   220 	
       
   221 	//for( int i = ExpandedNodeCount() ; i > 0 ; --i )
       
   222 	for( int i = ExpandedNodeCount() ; i >= aIndex ; --i )
       
   223         {
       
   224         // if index is greater than path index then index need to be modified
       
   225         //if( i >= aIndex )
       
   226             {   
       
   227                 
       
   228             if( aMap.find( i ) != aMap.end() )
       
   229                 {           
       
   230                 T item = aMap.find( i )->second;
       
   231                 aMap.erase( i );
       
   232                 aMap[ i + aCount] = item; //add item in to new index
       
   233                 }
       
   234             }
       
   235         }
       
   236 	}
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // UpdateVisualItem 
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void MulCacheManager::UpdateVisualItem( MulVisualItem& aUpdatedVisualItem, int aItemIndex  )
       
   243 	{
       
   244 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::UpdateVisualItem()");
       
   245 	
       
   246 	__MUL_ASSERT_DEBUG( aItemIndex >= 0 && aItemIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   247 	
       
   248 	MulDataPath path ;
       
   249 	UpdateVisualItem( aUpdatedVisualItem, path, aItemIndex );
       
   250 	}
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // UpdateVisualItem 
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 void MulCacheManager::UpdateVisualItem( MulVisualItem& aVisualItem,const MulDataPath& /*aPath*/, int aIndex )
       
   257 	{
       
   258 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::UpdateVisualItem()");
       
   259 	
       
   260 	MulVisualItem* visualItem = Data( aIndex );
       
   261 	std::vector<mulvisualitem::TVisualAttribute> existingAttributes = aVisualItem.Attributes( );
       
   262 	for( int i = 0 ; i < existingAttributes.size() ; ++i )
       
   263 		{
       
   264 		mulvisualitem::TVisualAttribute attributeName = existingAttributes[i];
       
   265 		IMulVariantType* newValue = aVisualItem.Attribute( attributeName );
       
   266 		if(newValue)
       
   267 		    {
       
   268 		    std::auto_ptr<IMulVariantType> cloneValue = newValue->Clone();
       
   269 		   	int newflags = aVisualItem.Flag( attributeName );
       
   270 		
       
   271 		   	visualItem->SetAttribute( attributeName , cloneValue.get(), newflags );
       
   272 		   	cloneValue.release();
       
   273 		    }
       
   274 		}
       
   275 	visualItem->ResetDirtyAttribute(aVisualItem);
       
   276 	}
       
   277 
       
   278 // ---------------------------------------------------------------------------
       
   279 // Data 
       
   280 // ---------------------------------------------------------------------------
       
   281 //
       
   282 MulVisualItem* MulCacheManager::Data(const MulDataPath& /*aPath*/, int aIndex ) const
       
   283 	{
       
   284 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::Data()");
       
   285 	
       
   286 	//__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   287 	
       
   288 	MulVisualItemMap::const_iterator findIter = mVisualItemMap.find(aIndex);
       
   289 	if( findIter != mVisualItemMap.end() )
       
   290 		{		
       
   291 		return findIter->second;
       
   292 		}
       
   293 	else
       
   294 		{
       
   295 		return NULL;
       
   296 		}
       
   297 	}
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // Data 
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 MulVisualItem* MulCacheManager::Data( int aIndex ) const
       
   304 	{
       
   305 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::Data()");
       
   306 	   
       
   307 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   308 		
       
   309 	MulDataPath path ;
       
   310 	return Data( path, aIndex );
       
   311 	}
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // Count 
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 int MulCacheManager::Count() const
       
   318 	{
       
   319 	return mVisualItemMap.size();
       
   320 	}
       
   321 	
       
   322 // ---------------------------------------------------------------------------
       
   323 // ExpandedNodeCount 
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 int MulCacheManager::ExpandedNodeCount( ) const
       
   327 	{
       
   328 	//return mTree.ExpandedNodeCount();
       
   329 	return mTotalCount;
       
   330 	}
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // SetMarkedIndices
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void MulCacheManager::SetMarkedIndices( MulWidgetDef::TMulWidgetMarkingType aType ,
       
   337 									 const std::vector<int>& aItemsIndex,
       
   338 									 const MulDataPath& /*aPath*/)
       
   339 
       
   340 	{
       
   341 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::SetMarkedIndices()");
       
   342 	
       
   343 	bool selection( false );
       
   344 	// If selection type is not mark or unmark throw invalid argument exception
       
   345 	aType == MulWidgetDef::EMulMark || aType == MulWidgetDef::EMulUnmark ?
       
   346 	selection = ( aType == MulWidgetDef::EMulMark ) :
       
   347 	throw std::invalid_argument( KInvalidArgument ); 
       
   348 	
       
   349 	// Add selected indices to vector and remove unselected
       
   350 	for(int i = 0 ; i < aItemsIndex.size() ; i++ )
       
   351 		{
       
   352 		int  index = aItemsIndex[i];
       
   353 		selection ? mSelectionMap[index] = index : mSelectionMap.erase(index);
       
   354 		}	
       
   355 	}
       
   356 	
       
   357 // ---------------------------------------------------------------------------
       
   358 // SetMarkedIndices
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 void MulCacheManager::SetMarkedIndices(MulWidgetDef::TMulWidgetMarkingType aType)
       
   362 	{	
       
   363 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::SetMarkedIndices()");
       
   364 	
       
   365 	bool selection(false);
       
   366 	// If selection type is not mark all or unmark all throw invalid argument exception
       
   367 	aType == MulWidgetDef::EMulMarkAll || aType == MulWidgetDef::EMulUnmarkAll ?
       
   368 	selection = ( aType == MulWidgetDef::EMulMarkAll ) :
       
   369 	throw std::invalid_argument( KInvalidArgument ); 
       
   370 	
       
   371 	int count = ExpandedNodeCount();
       
   372 	for(int i = 0 ; i < count ; ++i )
       
   373 		{
       
   374 		selection ? mSelectionMap[i] = i : mSelectionMap.erase(i);
       
   375 		}		
       
   376 	}
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // MarkedIndices
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 const std::vector<int>& MulCacheManager::MarkedIndices( const MulDataPath& /*aPath*/  )
       
   383 	{
       
   384 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::MarkedIndices()");
       
   385 	
       
   386 	mSelectionVector.clear();
       
   387 	
       
   388 	int count = ExpandedNodeCount();
       
   389 	for( int i=0; i < count ;++i )
       
   390 		{
       
   391 		MulSelectionMap::const_iterator itr = mSelectionMap.find(i);
       
   392 		if(	itr != mSelectionMap.end() )
       
   393 			{
       
   394 			mSelectionVector.insert(i);
       
   395 			}
       
   396 		}
       
   397 	return mSelectionVector.actualVector();	
       
   398 	}
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // IsItemMarked
       
   402 // ---------------------------------------------------------------------------
       
   403 //  
       
   404 bool MulCacheManager::IsItemMarked( int aIndex ) const
       
   405 	{
       
   406 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::IsItemMarked()");
       
   407 	
       
   408 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   409 		
       
   410 	MulSelectionMap::const_iterator itr = mSelectionMap.find( aIndex );	
       
   411 	return ( itr != mSelectionMap.end() );
       
   412 	}
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // SetMarkedIndex
       
   416 // ---------------------------------------------------------------------------
       
   417 // 
       
   418 void MulCacheManager::SetMarkedIndex( MulWidgetDef::TMulWidgetMarkingType aType, int aIndex )
       
   419 	{
       
   420 	MUL_LOG_ENTRY_EXIT("MUL::MulCacheManager::SetMarkedIndex()");
       
   421 	
       
   422 	__MUL_ASSERT_DEBUG( aIndex >= 0 && aIndex < ExpandedNodeCount() , KLInvalidArgument );
       
   423 	
       
   424 	bool selection( false );
       
   425 	// If selection type is not mark or unmark throw invalid argument exception
       
   426 	aType == MulWidgetDef::EMulMark || aType == MulWidgetDef::EMulUnmark ?
       
   427 	selection = ( aType == MulWidgetDef::EMulMark ) :
       
   428 	throw std::invalid_argument( KInvalidArgument ); 
       
   429 	
       
   430 	selection ? mSelectionMap[aIndex] = aIndex : mSelectionMap.erase(aIndex);
       
   431 	}
       
   432 	
       
   433 } //end of namespace alf
       
   434 
       
   435 //end of file