mulwidgets/muldatamodel/src/mulpullmode.cpp
branchRCL_3
changeset 26 0e9bb658ef58
parent 0 e83bab7cf002
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:  Implementation of Pull Mode
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //Includes
       
    20 #include "mulpullmode.h"
       
    21 
       
    22 #include <mul/imulmodelprovider.h>
       
    23 
       
    24 
       
    25 #include "mulassert.h"
       
    26 #include "mulcachemanager.h"
       
    27 #include "muldatawindow.h"
       
    28 #include "mulpagedatawindow.h"
       
    29 #include "mul/mulmodelutility.h"
       
    30 #include "mullog.h"
       
    31 #include "mulasyncdataprovider.h"
       
    32 
       
    33 namespace Alf
       
    34     { 
       
    35     
       
    36 // ---------------------------------------------------------------------------
       
    37 // MulPullMode()
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 MulPullMode::MulPullMode(MulPageDataWindow& aDataWindow,
       
    41 						 const IMulModelProvider& aModelProvider)
       
    42 			:MulPushMode( aDataWindow )
       
    43 	{	
       
    44 	//mModelProvider = const_cast<IMulModelProvider*>( &aModelProvider );
       
    45 	MUL_LOG_ENTRY_EXIT("MUL::MulPullModeImpl::MulPullModeImpl()");
       
    46 	mModelProvider.reset( new (ELeave) MulAsyncDataProvider( const_cast<IMulModelProvider&>(aModelProvider), aDataWindow ));		
       
    47 	}
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // ~MulPullMode()
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 MulPullMode::~MulPullMode()
       
    54 	{
       
    55 	// No implementation is required.
       
    56 	MUL_LOG_ENTRY_EXIT("MUL::MulPullModeImpl::~MulPullModeImpl()");
       
    57 	}
       
    58 
       
    59 // ------------------------ From IMulModel -----------------------------------
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Update
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void MulPullMode::Update(int aIndex, int aCount, const MulDataPath& /*aPath*/)
       
    66 	{
       
    67 	// throw exception if index is out of bound
       
    68 	__MUL_ASSERT( aIndex >= 0 && aCount > 0 && aIndex+aCount <= mCacheManager->ExpandedNodeCount(), KLInvalidArgument );
       
    69 	
       
    70 	int startIndex = aIndex;
       
    71 	int range = 0;
       
    72 	bool firstTime = true;
       
    73 	for(int i = aIndex; i < aCount+aIndex ; ++i)
       
    74 		{
       
    75 		if( mDataWindow.IsItemInDataWindow(i) )
       
    76 			{
       
    77 			range++;
       
    78 			firstTime = true;
       
    79 			}
       
    80 		else
       
    81 			{
       
    82 			if(firstTime)
       
    83 				{
       
    84 				firstTime = false;
       
    85 				if(range>0)
       
    86 					{
       
    87 					ProvideData(startIndex,range);
       
    88 					range = 0;						
       
    89 					}
       
    90 				}
       
    91 			startIndex = i + 1;			
       
    92 			}
       
    93 		}
       
    94 	ProvideData(startIndex,range);	
       
    95 	}
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // Refresh
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void MulPullMode::Refresh( int aCount, const MulDataPath& aPath )
       
   102 	{
       
   103 	int modelCountBefore  = mCacheManager->ExpandedNodeCount();
       
   104 	
       
   105 	// remove all old nodes
       
   106     for(int currentIndex = 0; currentIndex < modelCountBefore; currentIndex++)	
       
   107 		{
       
   108 		mCacheManager->RemoveVisualItem(aPath,0);			
       
   109 		}
       
   110     
       
   111     //mCacheManager->RemoveVisalItem(0, aCount, aPath );
       
   112 	
       
   113     // insert new nodes	
       
   114 	mCacheManager->CreateNodes( 0, aCount, aPath );	
       
   115 
       
   116 	int modelCount = mCacheManager->ExpandedNodeCount();		
       
   117 	// update alf model accordingly
       
   118 	if(mDataWindow.IsWindowEnabled())
       
   119 		{
       
   120 		if(modelCountBefore == modelCount)
       
   121 			{
       
   122 			Update(0,aCount,aPath);
       
   123 			}
       
   124 		else
       
   125 			{
       
   126 			ModelCountChanged(modelCountBefore,modelCount);	
       
   127 			}		
       
   128 		}				
       
   129 	}
       
   130 		
       
   131 // ---------------------------------------------------------------------------
       
   132 // SetData
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void MulPullMode::SetData(int aIndex, auto_ptr<MulVisualItem> aVisualItem,const MulDataPath& aPath)
       
   136 	{
       
   137 	MUL_LOG_ENTRY_EXIT("MUL::MulPullModeImpl::SetData()");
       
   138 	
       
   139 	__MUL_ASSERT( aIndex >= 0 && aIndex < Count(aPath),_L("Invalid Argument."));
       
   140 		
       
   141 	//Set data to model
       
   142 	if( mDataWindow.IsItemInDataWindow( aIndex ) )
       
   143 		{		
       
   144 		MulPushMode::SetData( aIndex, aVisualItem, aPath );
       
   145 		}
       
   146 	}
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // SetTemplate
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 void MulPullMode::SetTemplate( mulwidget::TLogicalTemplate aTemplateId )
       
   153 	{
       
   154 	mDefaultTemplate = aTemplateId;
       
   155 
       
   156 	MulDataPath path ;
       
   157 	if( mDataWindow.IsWindowEnabled() )
       
   158 		{
       
   159 		int topOffset = mDataWindow.TopOffset();
       
   160 		int bottomOffset = mDataWindow.BottomOffset();
       
   161 		for( int i = topOffset ; i <= bottomOffset ; i++ )
       
   162 			{
       
   163 			MulPushMode::SetTemplate( i, mDefaultTemplate, path);			
       
   164 			}
       
   165 		
       
   166 		int rearTopOffset = mDataWindow.RearTopOffset();
       
   167 		int rearBottomOffset = mDataWindow.RearBottomOffset();
       
   168         for( int i = rearTopOffset; i <= rearBottomOffset && i >= 0 ; i++ )
       
   169 			{	
       
   170 			MulPushMode::SetTemplate( i, mDefaultTemplate, path);
       
   171 			}
       
   172 		}
       
   173 	}
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // WindowSiftedDown
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void MulPullMode::WindowSiftedDown()
       
   180 	{
       
   181 	MUL_LOG_ENTRY_EXIT("MUL::MulPullModeImpl::WindowSiftedDown"); 	
       
   182 		
       
   183 	// If the complete datawindow is invalid.   	
       
   184 //	if(mDataWindow.TopOffset() - mDataWindow.OldTopOffset() > mDataWindow.ActualBufferSize() 
       
   185 //		&& mDataWindow.OldTopOffset() != KNotInitialized)
       
   186 	//if(mDataWindow.TopOffset() - mDataWindow.OldTopOffset() > mDataWindow.ActualBufferSize() )
       
   187 	int oldTopOffset = mDataWindow.OldTopOffset();
       
   188 	int oldBottomOffset = mDataWindow.OldBottomOffset();
       
   189 	int topOffset = mDataWindow.TopOffset();
       
   190 	int bottomOffset = mDataWindow.BottomOffset();
       
   191 	int oldRearTopOffset = mDataWindow.OldRearTopOffset();
       
   192 	int oldRearBottomOffset = mDataWindow.OldRearBottomOffset();
       
   193 	
       
   194 	 if(  ( oldTopOffset == KNotInitialized && oldBottomOffset == KNotInitialized ) ||
       
   195 	      ( topOffset - oldTopOffset >= mDataWindow.ActualBufferSize()) ||
       
   196 	      ( oldRearTopOffset != KNotInitialized && 
       
   197 	      ( topOffset - oldTopOffset >= ( mDataWindow.WindowSize() + mDataWindow.BufferSize() ) ) 
       
   198 	    ) ) 
       
   199 		{
       
   200 		//remove old data from  cache
       
   201         for( int i = oldTopOffset ; i <= oldBottomOffset && i >= 0 ; i++ )
       
   202             {
       
   203             mCacheManager->RemoveVisualItem( i );          
       
   204             }
       
   205         
       
   206         for( int i = oldRearTopOffset; i <= oldRearBottomOffset && i >= 0 ; i++ )
       
   207             {   
       
   208             mCacheManager->RemoveVisualItem( i );
       
   209             }
       
   210         
       
   211 		int startIndex = topOffset;
       
   212 		int range = bottomOffset - startIndex + 1;
       
   213 		
       
   214 		MUL_LOG_INFO2("MUL::MulPullModeImpl::WindowSiftedUp ProvideData startIndex:%d,range:%d",startIndex,range);
       
   215     	ProvideData(startIndex,range);
       
   216 		} 
       
   217 	else
       
   218 		{
       
   219 		//remove extra items from top and add new items at bottom
       
   220 		for( int i = oldTopOffset ; i >= 0 && i < topOffset ; i++ )
       
   221 			{       
       
   222 			//remove item from top
       
   223 			mCacheManager->RemoveVisualItem( i );
       
   224 			}
       
   225 
       
   226 		int startIndex = oldBottomOffset +1 ;
       
   227 		int range = ( bottomOffset - oldBottomOffset );				
       
   228 		MUL_LOG_INFO2("MUL::MulPullModeImpl::DataWindowUpdated ProvideData startIndex:%d,range:%d",startIndex,range); 		       
       
   229     	ProvideData(startIndex,range);				
       
   230 		}
       
   231 	}
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // WindowSiftedUp
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void MulPullMode::WindowSiftedUp()
       
   238 	{
       
   239 	MUL_LOG_ENTRY_EXIT("MUL::MulPullModeImpl::WindowSiftedUp "); 
       
   240 	// If the complete datawindow is invalid.  	
       
   241 //	if(mDataWindow.OldBottomOffset() - mDataWindow.BottomOffset() > mDataWindow.ActualBufferSize()
       
   242 //		&& mDataWindow.OldBottomOffset() != KNotInitialized)
       
   243 
       
   244 	  if( mDataWindow.OldBottomOffset() - mDataWindow.BottomOffset() >= mDataWindow.ActualBufferSize() ||
       
   245 	    ( mDataWindow.OldRearBottomOffset() != KNotInitialized && 
       
   246 	      ( mDataWindow.OldBottomOffset() - mDataWindow.BottomOffset() >= ( mDataWindow.WindowSize() + mDataWindow.BufferSize() ) ) 
       
   247 	    ) )	
       
   248 		{
       
   249 		//remove old data from  cache
       
   250         for( int i = mDataWindow.OldTopOffset() ; i <= mDataWindow.OldBottomOffset() && i >= 0 ; i++ )
       
   251             {
       
   252             mCacheManager->RemoveVisualItem( i );          
       
   253             }
       
   254         
       
   255         for( int i = mDataWindow.OldRearTopOffset( ); i <= mDataWindow.OldRearBottomOffset() && i >= 0 ; i++ )
       
   256             {   
       
   257             mCacheManager->RemoveVisualItem( i );
       
   258             }
       
   259 		        
       
   260 		int startIndex = mDataWindow.TopOffset();
       
   261 		int range = mDataWindow.BottomOffset() - startIndex + 1;
       
   262 		
       
   263 		MUL_LOG_INFO2("MUL::MulPullModeImpl::WindowSiftedUp ProvideData startIndex:%d,range:%d",startIndex,range);
       
   264 		ProvideData(startIndex,range);
       
   265 		}  
       
   266 	else
       
   267 		{
       
   268 		//remove extra items from bottom and add at top
       
   269 		for( int i = mDataWindow.BottomOffset() ; i < mDataWindow.OldBottomOffset() ; i++ )
       
   270 			{		
       
   271 			mCacheManager->RemoveVisualItem(i+1);
       
   272 			}
       
   273 
       
   274 		int startIndex = mDataWindow.TopOffset();
       
   275 		int range = mDataWindow.OldTopOffset() - mDataWindow.TopOffset();
       
   276 
       
   277 		MUL_LOG_INFO2("MUL::MulPullModeImpl::WindowSiftedUp ProvideData startIndex:%d,range:%d",startIndex,range); 		       
       
   278 		ProvideData(startIndex,range);			
       
   279 		}  			
       
   280 	}
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // ProvideData
       
   284 // ---------------------------------------------------------------------------
       
   285 //
       
   286 void MulPullMode::ProvideData(int aStartIndex, int aRange)
       
   287 	{
       
   288 	 //call provide data for visible window first then for buffer
       
   289 	if( aRange > 0 )
       
   290 	    {
       
   291 	    // check if the current higlighted item is there in the data asked for
       
   292 	    if( mDataWindow.Highlight() >= aStartIndex &&
       
   293 	            mDataWindow.Highlight() < aStartIndex+aRange )
       
   294 	        {
       
   295 	        mModelProvider->ProvideData( mDataWindow.Highlight(), 1 ,MulDataPath(),true );
       
   296 	        }
       
   297 	    //__MUL_ASSERT( ( aRange % 18 ) == 0, _L("Invalid Renage") );
       
   298 	    //check that visible data is there in provide data call
       
   299 	    if( aStartIndex < mDataWindow.TopWindowOffset() && 
       
   300 	        aStartIndex+aRange > mDataWindow.BottomWindowOffset() )
       
   301 	        {
       
   302 	            {
       
   303 	            //request visible item
       
   304                 int startIndex = mDataWindow.TopWindowOffset();
       
   305                 int range = ( mDataWindow.BottomWindowOffset() - mDataWindow.TopWindowOffset() ) + 1 ; 
       
   306                 
       
   307                 MUL_LOG_INFO2("MUL::MulPullModeImpl::ProvideData visible buffer startIndex:%d,range:%d",startIndex,range);
       
   308                 mModelProvider->ProvideData(startIndex, range ,MulDataPath() );
       
   309                 }
       
   310 	        
       
   311     	        {
       
   312     	        //request item from top
       
   313     	        int startIndex = aStartIndex;
       
   314     	        int range = mDataWindow.TopWindowOffset() - startIndex; 
       
   315     	        
       
   316     	        MUL_LOG_INFO2("MUL::MulPullModeImpl::ProvideData top buffer startIndex:%d,range:%d",startIndex,range);
       
   317     	        mModelProvider->ProvideData(startIndex, range ,MulDataPath() );
       
   318     	        }
       
   319     	        
       
   320     	        {
       
   321     	        //request item from bottom
       
   322                 int startIndex = mDataWindow.BottomWindowOffset() + 1 ;
       
   323                 int range = ( aStartIndex + aRange - 1) - mDataWindow.BottomWindowOffset(); 
       
   324 
       
   325                 MUL_LOG_INFO2("MUL::MulPullModeImpl::ProvideData bottom buffer startIndex:%d,range:%d",startIndex,range);
       
   326                 mModelProvider->ProvideData(startIndex, range ,MulDataPath() );
       
   327                 }
       
   328 	        }
       
   329 	    else
       
   330 	        {
       
   331 	        //there is not provide data pending for visible window
       
   332 	        mModelProvider->ProvideData(aStartIndex, aRange ,MulDataPath() );
       
   333 	        }
       
   334 	    }
       
   335 	}
       
   336 
       
   337 // ---------------------------------------------------------------------------
       
   338 // ModelCountChanged
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void MulPullMode::ModelCountChanged(int aOldCount, int aNewCount)
       
   342     {
       
   343     MUL_LOG_ENTRY_EXIT("MUL::MulPullMode::AdjustOffsetAndData()");
       
   344     
       
   345     MUL_LOG_INFO1("MUL::MulPullMode::AdjustAlfModelData() modelCount:%d", aNewCount );
       
   346     
       
   347     MulPushMode::ModelCountChanged( aOldCount, aNewCount );
       
   348     
       
   349     // fetch data for the new buffers
       
   350     if( mDataWindow.RearTopOffset() != KNotInitialized &&
       
   351         mDataWindow.RearBottomOffset() != KNotInitialized )
       
   352         {   
       
   353         if(mDataWindow.RearTopOffset() == 0)
       
   354             {
       
   355             //updating all buffer
       
   356             ProvideData( mDataWindow.RearTopOffset() , mDataWindow.RearBottomOffset() - 
       
   357                 mDataWindow.RearTopOffset() + 1);
       
   358             
       
   359             ProvideData( mDataWindow.TopOffset() , mDataWindow.BottomOffset() -
       
   360                 mDataWindow.TopOffset() + 1);
       
   361             }
       
   362         else
       
   363             {
       
   364             ProvideData( mDataWindow.TopOffset() , mDataWindow.BottomOffset() -
       
   365                 mDataWindow.TopOffset() + 1);
       
   366                 
       
   367             ProvideData( mDataWindow.RearTopOffset() , mDataWindow.RearBottomOffset() - 
       
   368                 mDataWindow.RearTopOffset() + 1);                               
       
   369             }       
       
   370         }
       
   371     else
       
   372         {
       
   373         ProvideData( mDataWindow.TopOffset() , mDataWindow.BottomOffset() -
       
   374              mDataWindow.TopOffset() + 1);  
       
   375         }
       
   376     }
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // DataWindowUpdated
       
   380 // ---------------------------------------------------------------------------
       
   381 //    
       
   382 void MulPullMode::DataWindowUpdated()
       
   383     {
       
   384     //here sequence of Function call are important dont change function call sequences
       
   385     //UpdateLoopBuffer must be called first 
       
   386     if( mDataWindow.RearTopOffset() != KNotInitialized &&
       
   387         mDataWindow.OldRearTopOffset() != KNotInitialized ) //there is looping on
       
   388         {
       
   389         UpdateLoopBuffer();
       
   390         //returning from here only dont need to do any thing else
       
   391         return ;
       
   392         }
       
   393     //window shifted down
       
   394     if( mDataWindow.OldBottomOffset() < mDataWindow.BottomOffset() )
       
   395         {
       
   396         WindowSiftedDown();     
       
   397         }       
       
   398     //window shifted up
       
   399     else if( mDataWindow.OldTopOffset() > mDataWindow.TopOffset())
       
   400         {
       
   401         WindowSiftedUp();       
       
   402         }
       
   403 
       
   404      //add buffer in looping case
       
   405     if(mDataWindow.RearTopOffset() != KNotInitialized && mDataWindow.RearBottomOffset() != KNotInitialized) //rear offset changed
       
   406         {
       
   407         // here ask data from top to bottom
       
   408         int startIndex = mDataWindow.RearTopOffset()  ;
       
   409         int range = mDataWindow.RearBottomOffset() - mDataWindow.RearTopOffset() + 1;
       
   410                 
       
   411         MUL_LOG_INFO2("MUL::MulPullMode::DataWindowUpdated startIndex:%d,range:%d",startIndex,range);              
       
   412         ProvideData(startIndex,range); 
       
   413         }       
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // UpdateLoopBuffer
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void MulPullMode::UpdateLoopBuffer()
       
   421     {
       
   422     MUL_LOG_ENTRY_EXIT("MUL::MulPushMode::UpdateLoopBuffer");   
       
   423     if( mDataWindow.RearTopOffset() < mDataWindow.OldRearTopOffset() )
       
   424         {
       
   425         int startIndex = mDataWindow.TopOffset();
       
   426         int range = mDataWindow.OldRearTopOffset() - startIndex;
       
   427     
       
   428         MUL_LOG_INFO2("MUL::MulPushMode::UpdateLoopBuffer startIndex:%d,range:%d",startIndex,range);               
       
   429         ProvideData(startIndex,range);
       
   430         }
       
   431     else
       
   432         {
       
   433         int startIndex = mDataWindow.OldRearBottomOffset()+1 ;
       
   434         int range = mDataWindow.BottomOffset() - startIndex + 1;
       
   435          
       
   436         MUL_LOG_INFO2("MUL::MulPushMode::UpdateLoopBuffer startIndex:%d,range:%d",startIndex,range);               
       
   437         ProvideData(startIndex,range);
       
   438         }
       
   439     }
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // Item
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 const MulVisualItem& MulPullMode::Item( int aAbsoluteIndex )
       
   446     {
       
   447     MulVisualItem* visualItem = mCacheManager->Data( aAbsoluteIndex );
       
   448     if( !visualItem )
       
   449         {
       
   450         if( mDataWindow.IsItemInDataWindow(aAbsoluteIndex) )
       
   451             {
       
   452             std::auto_ptr<MulVisualItem> dummyItem = MulModelUtility::CreateVisualItem( Template() );
       
   453             visualItem = dummyItem.get();
       
   454             mCacheManager->AddVisualItem(dummyItem, MulDataPath() ,aAbsoluteIndex );  
       
   455             }
       
   456         else
       
   457             {
       
   458             throw std::invalid_argument(KInvalidArgument);  
       
   459             }
       
   460         }
       
   461     return *visualItem; 
       
   462     }
       
   463 
       
   464 				
       
   465     } // namespace Alf
       
   466 
       
   467 //End of file