mulwidgets/muldatamodel/src/muldatawindow.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:  Implementation of Data window
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //Includes
       
    20 #include "muldatawindow.h"
       
    21 
       
    22 #include <stdexcept>
       
    23 #include <e32debug.h>
       
    24 
       
    25 //Internal includes
       
    26 #include "mulassert.h"
       
    27 #include "mulmodelimpl.h"
       
    28 #include "mullog.h"
       
    29 
       
    30 using namespace std;
       
    31 
       
    32 namespace Alf
       
    33     {
       
    34     
       
    35 // ---------------------------------------------------------------------------
       
    36 // MulDataWindow
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 MulDataWindow::MulDataWindow( MulModelImpl& aMulModel )
       
    40 			  :mMulModel( aMulModel ),
       
    41 			   mHighlight(KNotInitialized),
       
    42 			   mItemIndex(KNotInitialized),
       
    43 			   mOldItemIndex(KNotInitialized),
       
    44 			   mBufferSize(KNotInitialized),
       
    45 			   mWindowSize(KNotInitialized),
       
    46 			   mWindowOffset(KNotInitialized),
       
    47 			   mWindowTop(0),
       
    48 			   mWindowBottom(0),
       
    49 			   mBufferTop(0),
       
    50 			   mBufferBottom(0),
       
    51 			   mOldBufferTop(KNotInitialized),
       
    52 			   mOldBufferBottom(KNotInitialized),
       
    53 			   mRearBufferTop(0),
       
    54 			   mRearBufferBottom(0),
       
    55 			   mOldRearBufferTop(KNotInitialized),
       
    56 			   mOldRearBufferBottom(KNotInitialized)
       
    57 	{	
       
    58 	}
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // SetWindowSize
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void MulDataWindow::SetWindowSize( int aWindowSize )
       
    65 	{
       
    66 	MUL_LOG_ENTRY_EXIT("MUL::MulDataWindow::SetWindowSize()"); 
       
    67 	
       
    68 	__MUL_ASSERT( aWindowSize > 0, KLInvalidArgument );
       
    69 	
       
    70 	if( mWindowSize != aWindowSize )
       
    71 		{
       
    72 		if( KNotInitialized == mBufferSize )
       
    73 			{
       
    74 			mBufferSize = aWindowSize;			
       
    75 			}
       
    76 			
       
    77 		mWindowSize = aWindowSize;
       
    78 		//devide window half 
       
    79 		//Try to keep this much item above highlight and below highlight
       
    80 		mWindowOffset = mWindowSize / 2; 
       
    81 		
       
    82 		//adjusting window offset
       
    83 		//if window size changes then the top and bottom offset should be 
       
    84 		// adjusted such that highlight remains at the centre
       
    85 		//window top and bottom are inclusive
       
    86 		
       
    87 		mWindowTop = mItemIndex - mWindowOffset;	
       
    88 		mWindowTop = mWindowTop < 0 ? 0 : mWindowTop ;		
       
    89 		mWindowBottom = mWindowTop + mWindowSize - 1;
       
    90 		mWindowBottom = mWindowBottom >= mMulModel.CurrentItemCount() ? (mMulModel.CurrentItemCount() -1) : mWindowBottom;	
       
    91 		
       
    92 		MUL_LOG_INFO2("MUL::MulDataWindow::SetWindowSize() mWindowTop:%d,mWindowBottom:%d",mWindowTop,mWindowBottom); 
       
    93 		
       
    94 		AdjustBuffer();
       
    95 		mMulModel.DataWindowUpdated();
       
    96 		}
       
    97 	//else nothing needed
       
    98 	}
       
    99 	
       
   100 // ---------------------------------------------------------------------------
       
   101 // SetBufferSize
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void MulDataWindow::SetBufferSize( int aBufferSize )
       
   105 	{
       
   106 	MUL_LOG_ENTRY_EXIT("MUL::MulDataWindow::SetBufferSize()"); 
       
   107 	
       
   108 	__MUL_ASSERT( aBufferSize > 0, KLInvalidArgument );
       
   109 	
       
   110 	if( mBufferSize != aBufferSize )
       
   111 		{			
       
   112 		mBufferSize = aBufferSize;
       
   113 		
       
   114 		if(mMulModel.CurrentItemCount() > 0)
       
   115 			{
       
   116 			AdjustBuffer();
       
   117 			mMulModel.DataWindowUpdated();
       
   118 			}
       
   119 		}
       
   120 	//else nothing needed
       
   121 	}
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // ~MulDataWindow
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 MulDataWindow::~MulDataWindow()
       
   128 	{
       
   129 	// do nothing
       
   130 	}
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // SetHighlight
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void MulDataWindow::SetHighlight( int aHighlightIndex )
       
   137 	{
       
   138 	MUL_LOG_INFO2("MUL::MulDataWindow::SetHighlight() mHighlight:%d,modelCount:%d",aHighlightIndex,mMulModel.CurrentItemCount()); 
       
   139 	
       
   140 	__MUL_ASSERT( aHighlightIndex >= 0 && aHighlightIndex <= mMulModel.CurrentItemCount()-1, KLInvalidArgument );
       
   141 	
       
   142 	if( mHighlight != aHighlightIndex )
       
   143 		{
       
   144 		mHighlight = aHighlightIndex; 
       
   145 	
       
   146 		ScrollWindow( mHighlight );
       
   147 		}
       
   148 	//else same highlight
       
   149 	}
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // UpdateHighlight
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 void MulDataWindow::UpdateHighlight( int aHighlightIndex )
       
   157 	{
       
   158 	// No need to assert. We should send highlight -1 when all the items are removed.
       
   159 	// Widget should handle. to show emty text visualization
       
   160 	mHighlight = aHighlightIndex; 
       
   161 	}
       
   162 		
       
   163 // ---------------------------------------------------------------------------
       
   164 // ScrollWindow
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 void MulDataWindow::ScrollWindow( int aItemIndex )
       
   168 	{
       
   169 	MUL_LOG_INFO2("MUL::MulDataWindow::ScrollWindow() aItemIndex:%d,modelCount:%d",aItemIndex,mMulModel.CurrentItemCount()); 
       
   170 	
       
   171 	__MUL_ASSERT( aItemIndex >= 0 && aItemIndex <= mMulModel.CurrentItemCount()-1, KLInvalidArgument );
       
   172 	
       
   173 	if( mItemIndex != aItemIndex )
       
   174 		{
       
   175 		mOldItemIndex = mItemIndex;
       
   176 		mItemIndex = aItemIndex; 
       
   177 	
       
   178         if(IsWindowEnabled())
       
   179 	        {
       
   180 		    UpdateDataWindow();
       
   181 	        }
       
   182 		}
       
   183 	//else same highlight
       
   184 	}
       
   185 
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // UpdateDataWindow
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 void MulDataWindow::UpdateDataWindow()
       
   192 	{
       
   193 #ifdef _DEBUG
       
   194 	int diff = mItemIndex - mOldItemIndex;
       
   195 #endif //_DEBUG 
       
   196 	
       
   197 	//If diffrence is negative than highlight is moved up else down
       
   198 	(( mItemIndex - mOldItemIndex ) < 0 ) ? ShiftWindowUp() : ShiftWindowDown() ;
       
   199 	}
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // ShiftWindowDown
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void MulDataWindow::ShiftWindowDown()
       
   206 	{
       
   207 	MUL_LOG_ENTRY_EXIT("MUL::MulDataWindow::ShiftWindowDown()");  
       
   208 	MUL_LOG_INFO2("MUL::MulDataWindow::ShiftWindowDown() mItemIndex:%d,mWindowBottom:%d",mItemIndex,mWindowBottom); 
       
   209 	
       
   210 	if( mItemIndex > mWindowBottom )
       
   211 		{
       
   212 		mWindowTop = mItemIndex - mWindowOffset;
       
   213 		mWindowBottom = mWindowTop + mWindowSize -1;
       
   214 		//bottom is exceeding model count
       
   215 		mWindowBottom = mWindowBottom >= mMulModel.CurrentItemCount() ? 
       
   216 						mWindowBottom = mMulModel.CurrentItemCount() - 1 : mWindowBottom;
       
   217 		
       
   218 		mWindowTop = mWindowBottom - mWindowSize + 1;
       
   219 		//top can't be negative
       
   220 		mWindowTop = mWindowTop < 0 ? 0 : mWindowTop ;
       
   221 		
       
   222 		MUL_LOG_INFO2("MUL::MulDataWindow::ShiftWindowDown() mWindowTop:%d,mWindowBottom:%d",mWindowTop,mWindowBottom); 
       
   223 		SaveOldValues();
       
   224 		AdjustBuffer();
       
   225 		mMulModel.DataWindowUpdated();
       
   226 		}
       
   227 	//else nothing needed
       
   228 	}
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // ShiftWindowUp
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void MulDataWindow::ShiftWindowUp()
       
   235 	{
       
   236 	MUL_LOG_ENTRY_EXIT("MUL::MulDataWindow::ShiftWindowUp() Start");  
       
   237 	MUL_LOG_INFO2("MUL::MulDataWindow::ShiftWindowUp() mItemIndex:%d,mWindowBottom:%d",mItemIndex,mWindowBottom); 
       
   238 	
       
   239 	if( mItemIndex < mWindowTop )
       
   240 		{
       
   241 		mWindowTop = mItemIndex - mWindowOffset;
       
   242 		//top can't be negative
       
   243 		mWindowTop = mWindowTop < 0 ? 0 : mWindowTop ;
       
   244 		
       
   245 		mWindowBottom = mWindowTop + mWindowSize -1;
       
   246 		
       
   247 		//bottom cant exceed model count
       
   248 		mWindowBottom = mWindowBottom >= mMulModel.CurrentItemCount() ? 
       
   249 						mWindowBottom = mMulModel.CurrentItemCount() - 1 : mWindowBottom;
       
   250 		
       
   251 		MUL_LOG_INFO2("MUL::MulDataWindow::ShiftWindowUp() mWindowTop:%d,mWindowBottom:%d",mWindowTop,mWindowBottom); 
       
   252 		
       
   253 		SaveOldValues();
       
   254 		AdjustBuffer();
       
   255 		mMulModel.DataWindowUpdated();
       
   256 		}
       
   257 	//else nothing needed
       
   258 	}
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // AdjustBuffer
       
   262 // ---------------------------------------------------------------------------
       
   263 //
       
   264 void MulDataWindow::AdjustBuffer()
       
   265 	{
       
   266 	MUL_LOG_ENTRY_EXIT("MUL::MulDataWindow::AdjustBuffer()");  
       
   267 	
       
   268 	int modelCount = mMulModel.CurrentItemCount() -1;
       
   269 		
       
   270 	//remove data is new window is smaller/add is new window is bigger
       
   271 	if( mWindowTop == 0 && ActualBufferSize() <= modelCount )
       
   272 		{
       
   273 		MUL_LOG_INFO("MUL::MulDataWindow::AdjustBuffer() mWindowTop == 0 ");  		
       
   274 		
       
   275 		mBufferTop = mWindowTop;
       
   276 		mBufferBottom = mBufferTop + mWindowSize + mBufferSize - 1;
       
   277 		
       
   278 		//Create rear top and rear bottom only in looping case
       
   279 		//window top is zero so data at other end should be buffered
       
   280 		mRearBufferBottom = modelCount;
       
   281 		mRearBufferTop = mRearBufferBottom - mBufferSize +1;		
       
   282 		}
       
   283 	else if( mWindowBottom == modelCount && ActualBufferSize() <= modelCount )
       
   284 		{
       
   285 		MUL_LOG_INFO("MUL::MulDataWindow::AdjustBuffer() mWindowBottom == modelCount ");  
       
   286 		
       
   287 		mBufferBottom = mWindowBottom ;
       
   288 		mBufferTop = mBufferBottom - (mWindowSize + mBufferSize) + 1;
       
   289 		
       
   290 		//Create rear top and rear bottom only in looping case
       
   291 		//window bottom is equal to model count so data at other end should be buffered
       
   292 		mRearBufferTop = 0;
       
   293 		mRearBufferBottom = mRearBufferTop + mBufferSize -1;
       
   294 		}
       
   295 	else
       
   296 		{
       
   297 		MUL_LOG_INFO("MUL::MulDataWindow::AdjustBuffer() else ");  
       
   298 		
       
   299 		mBufferTop = mWindowTop - mBufferSize;
       
   300 		mBufferBottom = mWindowBottom + mBufferSize;
       
   301 		
       
   302 		//check if top or bottom is out of bound and update offset accordingly
       
   303 		if( mBufferTop < 0 )
       
   304 			{
       
   305 			MUL_LOG_INFO("MUL::MulDataWindow::AdjustBuffer() mBufferTop < 0 "); 
       
   306 			
       
   307 			mBufferTop = 0;
       
   308 			mBufferBottom = mBufferTop + ActualBufferSize()  - 1;
       
   309 			//buffer bottom cant be larger then model count
       
   310 			mBufferBottom = mBufferBottom > modelCount ? modelCount : mBufferBottom;
       
   311 			}
       
   312 		else if( mBufferBottom > modelCount )
       
   313 			{
       
   314 			MUL_LOG_INFO("MUL::MulDataWindow::AdjustBuffer() mBufferBottom > modelCount"); 
       
   315 			
       
   316 			mBufferBottom = modelCount ;
       
   317 			mBufferTop = mBufferBottom - ActualBufferSize() + 1;
       
   318 			//buffer top cant be less then 0
       
   319 			mBufferTop = mBufferTop < 0 ? 0 : mBufferTop;
       
   320 			}
       
   321 		
       
   322 		//in other case rear top and rear bottom is not use set to -1
       
   323 		mRearBufferTop = KNotInitialized;
       
   324 		mRearBufferBottom = KNotInitialized;
       
   325 		}
       
   326 	
       
   327 	MUL_LOG_INFO2("MUL::MulDataWindow::AdjustBuffer() mOldBufferTop:%d,mOldBufferBottom:%d",mOldBufferTop,mOldBufferBottom); 
       
   328 	MUL_LOG_INFO2("MUL::MulDataWindow::AdjustBuffer() mOldRearBufferBottom:%d,mOldRearBufferTop:%d",mOldRearBufferBottom,mOldRearBufferTop); 
       
   329 	
       
   330 	MUL_LOG_INFO2("MUL::MulDataWindow::AdjustBuffer() mBufferTop:%d,mBufferBottom:%d",mBufferTop,mBufferBottom); 
       
   331 	MUL_LOG_INFO2("MUL::MulDataWindow::AdjustBuffer() mRearBufferBottom:%d,mRearBufferTop:%d",mRearBufferBottom,mRearBufferTop); 
       
   332 
       
   333 	__MUL_ASSERT_DEBUG( mWindowTop >= 0 && mWindowBottom < mMulModel.CurrentItemCount(), _L("Invlid Window"));
       
   334 	__MUL_ASSERT_DEBUG( mBufferTop >= 0 && mBufferBottom < mMulModel.CurrentItemCount(), _L("Invlid Buffer"));
       
   335 	}
       
   336 		
       
   337 // ---------------------------------------------------------------------------
       
   338 // IsItemInDataWindow
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 bool MulDataWindow::IsItemInDataWindow(int aItemIndex ) const
       
   342 	{
       
   343 	//__MUL_ASSERT_DEBUG( aItemIndex >= 0 && aItemIndex <= mMulModel.CurrentItemCount()-1, KLInvalidArgument );
       
   344 	
       
   345 	bool result( false );
       
   346 	
       
   347 	if(mWindowSize == KNotInitialized )
       
   348 		{
       
   349 		result = false;
       
   350 		}
       
   351 	//check that rear buffer is on or not in looping case
       
   352 	else if( mRearBufferTop != KNotInitialized && mRearBufferBottom != KNotInitialized )
       
   353 		{
       
   354 		if( ( aItemIndex >= mBufferTop &&  aItemIndex <= mBufferBottom ) || 
       
   355 			( aItemIndex >= mRearBufferTop &&  aItemIndex <= mRearBufferBottom )	)
       
   356 			{
       
   357 			result = true;
       
   358 			}
       
   359 		}
       
   360 	else if( aItemIndex >= mBufferTop &&  aItemIndex <= mBufferBottom )
       
   361 		{
       
   362 		result = true;
       
   363 		}
       
   364 	return result;
       
   365 	}
       
   366 	
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // RelativeIndex
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 int MulDataWindow::RelativeIndex( int aAbsoluteIndex ) const
       
   373 	{
       
   374 	__MUL_ASSERT_DEBUG( aAbsoluteIndex >= 0 && aAbsoluteIndex <= mMulModel.CurrentItemCount()-1, KLInvalidArgument );
       
   375 	
       
   376 	if( !IsItemInDataWindow(aAbsoluteIndex) )
       
   377 	    {
       
   378 	    return -1;
       
   379 	    }
       
   380 	
       
   381 	if( mRearBufferTop != KNotInitialized && mRearBufferBottom != KNotInitialized )
       
   382 		{
       
   383 		if( mRearBufferBottom == mMulModel.CurrentItemCount() - 1 )
       
   384 			{
       
   385 			if( aAbsoluteIndex >= mRearBufferTop && aAbsoluteIndex <= mRearBufferBottom )	
       
   386 				{
       
   387 				int bufferSize = BottomOffset() - TopOffset() + 1;
       
   388 				int rearDiff = aAbsoluteIndex - mRearBufferTop;
       
   389 				int relativeIndex = bufferSize + rearDiff;
       
   390 				return relativeIndex;
       
   391 				}
       
   392 			else
       
   393 				{
       
   394 				int relativeIndex = aAbsoluteIndex < TopOffset() ? 
       
   395 									aAbsoluteIndex : aAbsoluteIndex - TopOffset() ;	
       
   396 				return relativeIndex;	
       
   397 				}		
       
   398 			}
       
   399 		else
       
   400 			{
       
   401 			if( aAbsoluteIndex >= mRearBufferTop && aAbsoluteIndex <= mRearBufferBottom )
       
   402 				{
       
   403 				int relativeIndex = aAbsoluteIndex < TopOffset() ? 
       
   404 									aAbsoluteIndex : aAbsoluteIndex - TopOffset() ;	
       
   405 				return relativeIndex;	
       
   406 				}
       
   407 			else
       
   408 				{	
       
   409 				int bufferSize = mRearBufferBottom - mRearBufferTop + 1;
       
   410 				int diff = aAbsoluteIndex - TopOffset();
       
   411 				int relativeIndex = bufferSize + diff;
       
   412 				return relativeIndex;		
       
   413 				}
       
   414 			}
       
   415 		}
       
   416 	else
       
   417 		{		
       
   418 		int relativeIndex = aAbsoluteIndex < TopOffset() ? 
       
   419 							aAbsoluteIndex : aAbsoluteIndex - TopOffset() ;	
       
   420 		return relativeIndex;
       
   421 		}
       
   422 	}
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // AbsoluteIndex
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 int MulDataWindow::AbsoluteIndex( int aRelativeIndex ) const
       
   429 	{
       
   430 	__MUL_ASSERT_DEBUG( aRelativeIndex >= 0 && aRelativeIndex <= ActualBufferSize(), _L("Invalid Relative Index"));
       
   431 	
       
   432 	if( mRearBufferTop != KNotInitialized && mRearBufferBottom != KNotInitialized )
       
   433 		{
       
   434 		if( mRearBufferBottom == mMulModel.CurrentItemCount() - 1 )
       
   435 			{
       
   436 			if( aRelativeIndex > BottomOffset() )
       
   437 				{
       
   438 				//relative index is in loop buffer
       
   439 				int diff = aRelativeIndex - BottomOffset() - 1;
       
   440 				int absoluteIndex = RearTopOffset() + diff;
       
   441 				return absoluteIndex;
       
   442 				}
       
   443 			else
       
   444 				{
       
   445 				int absoluteIndex = TopOffset() + aRelativeIndex;
       
   446 				return absoluteIndex;
       
   447 				}
       
   448 			}
       
   449 		else
       
   450 			{
       
   451 			if( aRelativeIndex <= RearBottomOffset() )
       
   452 				{
       
   453 				//relative index is in loop buffer
       
   454 				int absoluteIndex = RearTopOffset() + aRelativeIndex;
       
   455 				return absoluteIndex;
       
   456 				}
       
   457 			else
       
   458 				{
       
   459 				int diff = aRelativeIndex - RearBottomOffset() - 1;
       
   460 				int absoluteIndex = TopOffset() + diff;
       
   461 				return absoluteIndex;
       
   462 				}
       
   463 			}
       
   464 		}
       
   465 	else
       
   466 		{		
       
   467 		int absoluteIndex = TopOffset() + aRelativeIndex;
       
   468 		return absoluteIndex;
       
   469 		}
       
   470 	}
       
   471 
       
   472 // ---------------------------------------------------------------------------
       
   473 // SetVisibleWindow
       
   474 // ---------------------------------------------------------------------------
       
   475 //
       
   476 void MulDataWindow::SetVisibleWindow(int aWindowTop, int aWindowBottom)
       
   477 	{	
       
   478 	mWindowTop = aWindowTop;
       
   479 	mWindowBottom = aWindowBottom;
       
   480 	}
       
   481 
       
   482 // ---------------------------------------------------------------------------
       
   483 // IsBufferOffsetChanged
       
   484 // ---------------------------------------------------------------------------
       
   485 //
       
   486 bool MulDataWindow::IsBufferOffsetChanged()
       
   487 	{
       
   488 	if(mRearBufferBottom != mOldRearBufferBottom || mRearBufferTop != mOldRearBufferTop
       
   489 		|| mBufferTop != mOldBufferTop || mBufferBottom != mOldBufferBottom)
       
   490 		{
       
   491 		return true;
       
   492 		}
       
   493 	return false;
       
   494 	}
       
   495 			
       
   496     } // namespace Alf
       
   497 
       
   498 //End of file