mulwidgets/muldatamodel/src/mulpushmode.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     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:  Base implementation of pull and push mode
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //Includes
       
    21 #include "mulpushmode.h"
       
    22 
       
    23 #include "mulassert.h"
       
    24 #include "mulcachemanager.h"
       
    25 #include "mulpagedatawindow.h"
       
    26 #include "mullog.h"
       
    27 
       
    28 namespace Alf
       
    29     { 
       
    30     	  
       
    31 // ---------------------------------------------------------------------------
       
    32 // MulPushMode()
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 MulPushMode::MulPushMode( MulPageDataWindow& aDataWindow )
       
    36 			: mDataWindow(aDataWindow)
       
    37 	{   	
       
    38 	mCacheManager.reset( new ( EMM ) MulCacheManager());		
       
    39 	}
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // ~MulPushMode()
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 MulPushMode::~MulPushMode()
       
    46 	{
       
    47 	// No implementation is required.
       
    48 	}
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // Insert
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 bool MulPushMode::Insert(int aIndex, int aCount, const MulDataPath& aPath )
       
    55 	{
       
    56 	__MUL_ASSERT( aIndex >= 0 && aIndex <= mCacheManager->ExpandedNodeCount() && aCount > 0, KLInvalidArgument );
       
    57 	
       
    58 	int modelCountBefore  = mCacheManager->ExpandedNodeCount();	
       
    59 	mCacheManager->CreateNodes( aIndex, aCount, aPath );
       
    60 
       
    61 	int modelCount = mCacheManager->ExpandedNodeCount();
       
    62 		
       
    63 	// if there is a change in expanded node count of model then update
       
    64 	// the alf model. THis case would happen if we are inserting childnodes
       
    65 	// for a node which is not expanded. Then no need to update the alf model
       
    66 	if(mDataWindow.IsWindowEnabled() && modelCount != modelCountBefore)
       
    67 		{
       
    68 		ModelCountChanged(modelCountBefore,modelCount);
       
    69 		}
       
    70 		
       
    71 	// The highlight is the same but its relative index is changed 
       
    72 	// because the buffer offsets are changed. Even in this case widget has to update the visualization
       
    73 	return false;
       
    74 	}
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Remove
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 bool MulPushMode::Remove( int aIndex, int aCount,const MulDataPath& aPath)
       
    81 	{
       
    82 	__MUL_ASSERT( aIndex >= 0 && aCount > 0 && aIndex+aCount <= mCacheManager->ExpandedNodeCount(), KLInvalidArgument );
       
    83 
       
    84 	int modelCountBefore  = mCacheManager->ExpandedNodeCount();
       
    85 
       
    86 	mCacheManager->RemoveVisualItem( aIndex, aCount, aPath );
       
    87 	
       
    88 	int modelCount = mCacheManager->ExpandedNodeCount();
       
    89 	
       
    90 	// To inform the calling class whether the highlight has changed 
       
    91 	// If the current highlight is more than the total number of items. Then udjust teh highlight
       
    92 	bool highlightUpdated = ValidateCurrentHighlight();
       
    93 	
       
    94 	// if there is a change in expanded node count of model then update
       
    95 	// the alf model. This case would happen if we are removing childnodes
       
    96 	// for a node which is not expanded. Then no need to update the alf model			
       
    97 	if(mDataWindow.IsWindowEnabled() && modelCount != modelCountBefore)
       
    98 		{
       
    99 		ModelCountChanged(modelCountBefore,modelCount);
       
   100 		}
       
   101 		
       
   102 	// If the highlight is changed because it was greater than current model count or if
       
   103 	// the buffer offsets are changed then return true		
       
   104 	return ( highlightUpdated );
       
   105 	}
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Update
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void MulPushMode::Update(int /*aIndex*/, int /*aCount*/, const MulDataPath& /*aPath*/)
       
   112     {
       
   113     //no implementation required
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Refresh
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void MulPushMode::Refresh( int /*aCount*/, const MulDataPath& /*aPath*/)
       
   121     {
       
   122     // throw exception not implemented in push mode 
       
   123     throw std::logic_error("Not Implemented");
       
   124     }
       
   125 
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // SetTemplate
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 void MulPushMode::SetTemplate( mulwidget::TLogicalTemplate aTemplateId )
       
   132     {
       
   133     mDefaultTemplate =  aTemplateId ;
       
   134     
       
   135     MulDataPath path;
       
   136     for( int i = 0 ; i < mCacheManager->Count() ; i++ )
       
   137         {
       
   138         MulPushMode::SetTemplate( i, mDefaultTemplate, path );           
       
   139         }
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // Template
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 mulwidget::TLogicalTemplate MulPushMode::Template( ) 
       
   147     {
       
   148     return mDefaultTemplate;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // Data
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 const MulVisualItem* MulPushMode::Data( int aIndex, const MulDataPath& /*aPath*/) const
       
   156 	{
       
   157 	__MUL_ASSERT( aIndex >= 0 && aIndex < mCacheManager->ExpandedNodeCount(), KLInvalidArgument );
       
   158 	
       
   159 	return mCacheManager->Data( aIndex );			
       
   160 	}
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // SetData
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void MulPushMode::SetData( int aIndex, std::auto_ptr<MulVisualItem> aVisualItem, 
       
   167                           const MulDataPath& aPath)
       
   168     {
       
   169     __MUL_ASSERT( aIndex >= 0 && aIndex < mCacheManager->ExpandedNodeCount(),_L("Invalid Argument"));
       
   170 
       
   171     MulVisualItem* visualItem = mCacheManager->Data( aIndex );
       
   172     MUL_LOG_INFO2("MUL::MulPushMode::SetData() Index:%d,item:%x",aIndex, visualItem);
       
   173     if( visualItem )
       
   174         {
       
   175         mCacheManager->UpdateVisualItem( *aVisualItem , aPath, aIndex );
       
   176         }
       
   177     else
       
   178         {
       
   179         // Validating input visual item
       
   180         ValidateVisualItem( *aVisualItem );
       
   181         mCacheManager->AddVisualItem( aVisualItem, aPath, aIndex );
       
   182         }
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Count
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 int MulPushMode::Count(const MulDataPath& /*aPath*/) const
       
   190 	{
       
   191 	return mCacheManager->ExpandedNodeCount();
       
   192 	} 
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // SetTemplate
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void MulPushMode::SetTemplate( int aItemIndex, mulwidget::TLogicalTemplate aTemplateId, 
       
   199 	                    	  const MulDataPath& /*aPath*/ )
       
   200 	{	
       
   201 	MUL_LOG_ENTRY_EXIT("MUL::MulPushMode::SetTemplate");
       
   202 	
       
   203     MulVisualItem* visualItem = mCacheManager->Data( aItemIndex );	
       
   204     if( visualItem )
       
   205         {
       
   206         mulwidget::TLogicalTemplate logicalTemplate = (mulwidget::TLogicalTemplate) visualItem->AttributeAsInt( mulvisualitem::KAttributeTemplate );
       
   207     	//if template is not same then only change it		
       
   208         if( logicalTemplate != aTemplateId  )
       
   209         	{
       
   210         	visualItem->SetAttribute( mulvisualitem::KAttributeTemplate, aTemplateId );   		
       
   211         	}
       
   212         }
       
   213 	}
       
   214 	
       
   215 // ---------------------------------------------------------------------------
       
   216 // CurrentItemCount
       
   217 // ---------------------------------------------------------------------------
       
   218 //  
       
   219 int MulPushMode::CurrentItemCount() const
       
   220 	{
       
   221 	return mCacheManager->ExpandedNodeCount();		
       
   222 	}
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CacheManager
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 MulCacheManager& MulPushMode::CacheManager() const
       
   229 	{
       
   230 	return *mCacheManager;
       
   231 	}
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 //ValidateVisualItem
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void MulPushMode::ValidateVisualItem( MulVisualItem& aVisualItem )
       
   238 	{
       
   239 	//add default template to item if template is not already set and default template is not null
       
   240 	IMulVariantType* itemTemplate = aVisualItem.Attribute( mulvisualitem::KAttributeTemplate );
       
   241 	if( !itemTemplate )
       
   242 		{
       
   243 		if( mDefaultTemplate != mulwidget::KTemplateUnknown )
       
   244 			{
       
   245 			//add default template to this visual item	
       
   246 			aVisualItem.SetAttribute( mulvisualitem::KAttributeTemplate, mDefaultTemplate, 0 );
       
   247 			}
       
   248 		else
       
   249 			{
       
   250 			//template must be there
       
   251 			throw std::invalid_argument("Default template not available.");
       
   252 			}
       
   253 		}
       
   254 	}
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // ModelCountChanged
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 void MulPushMode::ModelCountChanged(int aOldCount, int aNewCount)
       
   261 	{
       
   262 	MUL_LOG_ENTRY_EXIT("MUL::MulPushMode::ModelCountChanged()");
       
   263 	
       
   264 	MUL_LOG_INFO2("MUL::MulPushMode::ModelCountChanged() aOldCount:%d,aNewCount:%d", aOldCount, aNewCount );
       
   265 	
       
   266 	mDataWindow.AdjustOffset();	
       
   267 	}
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // ValidateCurrentHighlight
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 bool MulPushMode::ValidateCurrentHighlight()
       
   274 	{
       
   275 	// To inform the calling class whether the highlight has changed
       
   276 	int modelCount = mCacheManager->ExpandedNodeCount();
       
   277 	if( mDataWindow.Highlight() >= modelCount)
       
   278 		{
       
   279 		mDataWindow.UpdateHighlight( modelCount-1 );
       
   280 		return true;
       
   281 		}
       
   282 	return false;
       
   283 	}
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // Item
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 const MulVisualItem& MulPushMode::Item( int aAbsoluteIndex )
       
   290     {
       
   291     MulVisualItem* visualItem = mCacheManager->Data( aAbsoluteIndex );
       
   292     if(!visualItem)
       
   293         {
       
   294         throw std::invalid_argument(KInvalidArgument);  
       
   295         }
       
   296     return *visualItem; 
       
   297     }
       
   298 	
       
   299 	} // namespace ends here
       
   300   
       
   301 //End of file
       
   302