mulwidgets/alfscrollbarwidget/src/alfscrollbarmodel.cpp
branchRCL_3
changeset 19 4ea6f81c838a
parent 17 514d98f21c43
child 20 0e9bb658ef58
equal deleted inserted replaced
17:514d98f21c43 19:4ea6f81c838a
     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:  Model implementation with utilities for scrollbar widget.
       
    15 *
       
    16 */
       
    17 //User Includes
       
    18 #include "alfscrollbarmodel.h"
       
    19 #include "alfscrollbarwidget.h"
       
    20 
       
    21 //Widget includes
       
    22 #include <alf/ialfscrollbardefaultbaseelement.h>
       
    23 #include "ialfscrollbarbaseelementinternal.h"
       
    24 #include "alfscrollbardefaultbaseelement.h"
       
    25 
       
    26 #include <alf/ialfscrollbarwidget.h>
       
    27 #include <alf/alfmodel.h>
       
    28 #include <alf/alfexception.h>
       
    29 
       
    30 namespace Alf
       
    31     {
       
    32     	
       
    33 /*
       
    34  * To store data required in the model class
       
    35  */
       
    36 struct ScrollbarModelDataImpl
       
    37     {
       
    38     int mTotalLength;
       
    39     int mViewLength;
       
    40     int mViewStartPosition;
       
    41     };
       
    42 
       
    43 // ======== MEMBER FUNCTIONS ========
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 //  C++ default constructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 AlfScrollBarModel::AlfScrollBarModel(IAlfVariantType* aDataSource):
       
    50     mModel ( NULL )
       
    51     {
       
    52     construct ( aDataSource );
       
    53     
       
    54      // Create an instance of model specific data
       
    55     mModelData = new (EMM) ScrollbarModelDataImpl();
       
    56     
       
    57     // Initialise model data with default values
       
    58     mModelData->mTotalLength       = 0;
       
    59     mModelData->mViewLength        = 0;
       
    60     mModelData->mViewStartPosition = 0;
       
    61     
       
    62     mScrollWidget = NULL;
       
    63     }//End of AlfScrollBarModel function.
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Destructor.
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 AlfScrollBarModel::~AlfScrollBarModel()
       
    71     {
       
    72     if(mModelData)
       
    73         delete mModelData;
       
    74         
       
    75     delete mModel;
       
    76     }//End of ~AlfScrollBarModel function.
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 //  Second Phase Constructor.
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 void AlfScrollBarModel::construct ( IAlfVariantType* /*aDataSource */)
       
    84     {
       
    85 
       
    86     }//End of construct function.
       
    87 
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // From class IAlfModel.
       
    91 // Add a new observer to be notified of any changes in the model.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void AlfScrollBarModel::addModelChangeObserver(
       
    95     IAlfModelChangeObserver& /*aObserver*/)
       
    96     {
       
    97     
       
    98     }//End of addModelChangeObserver function.
       
    99 
       
   100     
       
   101 // ---------------------------------------------------------------------------
       
   102 // From class IAlfModel.
       
   103 // Add a new observer to be notified of any changes in the model.
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void AlfScrollBarModel::removeModelChangeObserver(
       
   107     IAlfModelChangeObserver& /*aObserver*/ )
       
   108     {
       
   109     
       
   110     }//End of function.
       
   111 
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // From class IAlfModel.
       
   115 // Api to change the model completely at one short.
       
   116 // ---------------------------------------------------------------------------
       
   117 //    
       
   118 void AlfScrollBarModel::setData ( IAlfVariantType* /*aData*/ )
       
   119     {
       
   120         
       
   121     }//End of removeModelChangeObserver function.
       
   122 
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // From class IAlfModel.
       
   126 // Change a given  of data fields in the model.
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void AlfScrollBarModel::updateData(
       
   130     TInt /*aNumContainerIndices*/,
       
   131     TInt* /*aContainerIndices*/,
       
   132     IAlfVariantType* /*aData*/)
       
   133     {
       
   134     ALF_THROW(AlfException,EInvalidArgument,"updateData")        
       
   135     }//End of updateData function.
       
   136 
       
   137     
       
   138 // ---------------------------------------------------------------------------
       
   139 // From class IAlfModel.
       
   140 // Add a number of data fields in the model data.
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 void AlfScrollBarModel::addData(
       
   144     TInt /*aNumContainerIndices*/,
       
   145     TInt* /*aContainerIndices*/,
       
   146     IAlfVariantType* /*aData*/)
       
   147     {
       
   148     ALF_THROW(AlfException,EInvalidArgument,"addData")        
       
   149     }//End of addData function.
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // From class IAlfModel.
       
   154 // Remove a  number of data fields in the model data.
       
   155 // ---------------------------------------------------------------------------
       
   156 //  
       
   157 void AlfScrollBarModel::removeData(
       
   158     TInt /*aNumContainerIndices*/,
       
   159     TInt* /*aContainerIndices*/ )
       
   160     {
       
   161     ALF_THROW(AlfException,EInvalidArgument,"removeData")        
       
   162     }//End of removeData function.
       
   163 
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From class IAlfModel.
       
   167 // Api for executing batch operations.
       
   168 // ---------------------------------------------------------------------------
       
   169 //  
       
   170 void AlfScrollBarModel::executeOperations(
       
   171     AlfPtrVector<AlfModelOperation>& /*aOperationsArray*/)
       
   172     {
       
   173     ALF_THROW(AlfException,EInvalidArgument,"executeOperations")    
       
   174     }//End of executeOperations function.
       
   175 
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // From class IAlfModel.
       
   179 // Api for executing one operation at a Time.
       
   180 // ---------------------------------------------------------------------------
       
   181 //  
       
   182 void AlfScrollBarModel::executeOperation(AlfModelOperation* /*aOperation*/)
       
   183     {
       
   184     ALF_THROW(AlfException,EInvalidArgument,"executeOperation")    
       
   185     }//End of executeOperation function.
       
   186 
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // From class IAlfModel.
       
   190 // Clears the Model.
       
   191 // ---------------------------------------------------------------------------
       
   192 //  
       
   193 void AlfScrollBarModel::clearModel()
       
   194     {
       
   195     
       
   196     }//End of clearModel function.
       
   197 
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // From class IAlfModel.
       
   201 // Get the root data object of this model.
       
   202 // ---------------------------------------------------------------------------
       
   203 //  
       
   204 IAlfVariantType* AlfScrollBarModel::data() const
       
   205     {
       
   206     return NULL;
       
   207     }//End of data function.
       
   208 
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // From class IAlfInterfaceBase.
       
   212 // Getter for interfaces provided by the scrollbar model.
       
   213 // ---------------------------------------------------------------------------
       
   214 IAlfInterfaceBase* AlfScrollBarModel::makeInterface(const IfId& aType)
       
   215     {
       
   216     IAlfInterfaceBase* ret ( 0 );
       
   217     UString param ( aType.mImplementationId );
       
   218 
       
   219     if ( param == IAlfScrollBarModel::type().mImplementationId )
       
   220         {
       
   221         ret = static_cast<IAlfScrollBarModel*>(this);
       
   222         }
       
   223     else if ( param == IAlfModel::type().mImplementationId )
       
   224         {
       
   225         ret = static_cast<IAlfModel*>(this);
       
   226         }
       
   227     else
       
   228         {
       
   229         //do nothing
       
   230         }
       
   231 
       
   232     return ret;
       
   233     }//End of makeInterface function.
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // From class IAlfScrollBarModel.
       
   237 // Create the default data structure for a ScrollBar widget.
       
   238 // ---------------------------------------------------------------------------
       
   239 //
       
   240 void AlfScrollBarModel::initializeData(
       
   241     int aTotalLength,
       
   242     int aViewLengthInSteps,
       
   243     int aViewStartPosition)
       
   244     {
       
   245 
       
   246     validateAllData(aTotalLength, 
       
   247         aViewLengthInSteps,
       
   248         aViewStartPosition);
       
   249     
       
   250     // Initialise model with the data provided
       
   251     mModelData->mTotalLength       = aTotalLength;
       
   252     mModelData->mViewLength        = aViewLengthInSteps;
       
   253     mModelData->mViewStartPosition = aViewStartPosition;
       
   254    
       
   255     changeData(
       
   256         mModelData->mTotalLength,
       
   257         mModelData->mViewLength,
       
   258         mModelData->mViewStartPosition);
       
   259 
       
   260     }//End of function.
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // Set Total Length .
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void AlfScrollBarModel::setTotalLength(int aTotalLength)
       
   267     {
       
   268     int viewLength = getViewLength();
       
   269     int viewStartPos = getViewStartPosition();
       
   270        
       
   271     if (validateAllData(aTotalLength, 
       
   272             viewLength, 
       
   273             viewStartPos))
       
   274         {
       
   275         updateScrollBarModel(aTotalLength, -1, -1 );
       
   276         }
       
   277     else
       
   278         {
       
   279         updateScrollBarModel(aTotalLength, viewLength, viewStartPos);
       
   280         }
       
   281     }//End of setTotalLength function.
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Set ViewLength.
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void AlfScrollBarModel::setViewLength(int aViewLengthInSteps)
       
   288     {
       
   289     validateViewLength(aViewLengthInSteps);
       
   290     updateScrollBarModel ( -1, aViewLengthInSteps, -1);
       
   291     
       
   292     }//End of setViewLength function.
       
   293 
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // Set Thumb Position.
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void AlfScrollBarModel::setViewStartPosition ( int aViewStartPosition )
       
   300     {
       
   301     validateViewStartPos(aViewStartPosition);
       
   302     updateScrollBarModel (-1, -1, aViewStartPosition);
       
   303     }//End of setViewStartPosition function.
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // Changes all the data of model with new values.
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 void AlfScrollBarModel::changeData(
       
   310     int aTotalLength,
       
   311     int aViewLength,
       
   312     int aViewStartPosition )
       
   313     {
       
   314     
       
   315     validateAllData(aTotalLength,
       
   316         aViewLength,
       
   317         aViewStartPosition);
       
   318           
       
   319      if(getViewStartPosition() == aViewStartPosition && 
       
   320         getTotalLength() == aTotalLength && 
       
   321         getViewLength() == aViewLength)
       
   322         { 
       
   323         // nothing is changed
       
   324         return ;
       
   325         }
       
   326     else
       
   327 	    {
       
   328 	    updateScrollBarModel(
       
   329 	            aTotalLength,
       
   330 	            aViewLength,
       
   331 	            aViewStartPosition);
       
   332 	    }
       
   333             
       
   334   
       
   335     }//End of changeData function.
       
   336 
       
   337 // ---------------------------------------------------------------------------
       
   338 // getTotalLength.
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 int AlfScrollBarModel::getTotalLength() const
       
   342     {
       
   343     return mModelData->mTotalLength;
       
   344     }//End of getTotalLength function.
       
   345 
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // Returns ViewLength.
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 int AlfScrollBarModel::getViewLength() const
       
   352     {
       
   353     
       
   354     return mModelData->mViewLength;
       
   355     }//End of getViewLength function.
       
   356 
       
   357     
       
   358 // ---------------------------------------------------------------------------
       
   359 // Returns ViewStartPosition.
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362 int AlfScrollBarModel::getViewStartPosition() const
       
   363     {
       
   364     return mModelData->mViewStartPosition;
       
   365     }//End of getViewStartPosition function.
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // Updates the ScrollBar visualization
       
   369 // according to the model data.
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 void AlfScrollBarModel::updateScrollBarVisualization()
       
   373     {
       
   374      //updateVisualization with new model
       
   375      IAlfElement* element = (mScrollWidget)->control()->findElement("BaseElement");
       
   376      IAlfScrollBarDefaultBaseElement* defaultLCTBaseElement = static_cast<IAlfScrollBarDefaultBaseElement*> (
       
   377     		 element->makeInterface (IAlfScrollBarDefaultBaseElement::type() ) );
       
   378      AlfScrollBarDefaultBaseElement* baseElement = (AlfScrollBarDefaultBaseElement*)defaultLCTBaseElement;
       
   379      
       
   380      baseElement->updateVisualization();
       
   381     }
       
   382 
       
   383 // ---------------------------------------------------------------------------
       
   384 // Updates the ScrollBar model according to the parameters.
       
   385 // ---------------------------------------------------------------------------
       
   386 //
       
   387 void AlfScrollBarModel::updateScrollBarModel(
       
   388     int aTotalLength,
       
   389     int aViewLength,
       
   390     int aViewStartPosition)
       
   391     {
       
   392     //change only if the parameters are non negative . 
       
   393     
       
   394     if(aTotalLength!=-1)
       
   395         {
       
   396         mModelData->mTotalLength = aTotalLength;
       
   397         }
       
   398     else
       
   399         {
       
   400         // Do Nothing
       
   401         }
       
   402     
       
   403     if(aViewLength!=-1)
       
   404         {
       
   405         mModelData->mViewLength =  aViewLength; 
       
   406         }
       
   407     else
       
   408         {
       
   409         // Do Nothing 
       
   410         }
       
   411         
       
   412     
       
   413     if(aViewStartPosition!=-1)
       
   414         {
       
   415         mModelData->mViewStartPosition = aViewStartPosition;  
       
   416         }
       
   417     else
       
   418         {
       
   419         // Do Nothing  
       
   420         }
       
   421  
       
   422     //updateVisualization with new model    
       
   423     updateScrollBarVisualization();
       
   424     }//End of updateScrollBarModel function.
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 // validateViewLength
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 bool AlfScrollBarModel::validateViewLength(int & aViewLength)
       
   431     {
       
   432     bool ret = true;
       
   433     
       
   434     if (aViewLength <= 0)
       
   435         {
       
   436         // Minimum view length
       
   437         aViewLength = 1;
       
   438         ret = false;
       
   439         }
       
   440     else
       
   441         {
       
   442         int totalLength = getTotalLength();
       
   443         if (aViewLength > totalLength)
       
   444             {
       
   445             aViewLength = totalLength;
       
   446             ret = false;
       
   447             }
       
   448         else
       
   449             {
       
   450             // do nothing
       
   451             }
       
   452         }
       
   453     
       
   454     return ret;
       
   455     }
       
   456 
       
   457 // ---------------------------------------------------------------------------
       
   458 // validateViewStartPos
       
   459 // ---------------------------------------------------------------------------
       
   460 //
       
   461 bool AlfScrollBarModel::validateViewStartPos(int & aViewStartPos)
       
   462     {
       
   463     bool ret = true;
       
   464     
       
   465     if (aViewStartPos < 0)
       
   466         {
       
   467         aViewStartPos = 0;
       
   468         ret = false;
       
   469         }
       
   470     else
       
   471         {
       
   472         int totalLength = getTotalLength();
       
   473         int viewLength = getViewLength();     
       
   474         if (aViewStartPos > (totalLength-viewLength))
       
   475             {
       
   476             // Maximum value
       
   477             aViewStartPos = totalLength-viewLength;            
       
   478             ret = false;
       
   479             }
       
   480         else
       
   481             {
       
   482             // do nothing
       
   483             }        
       
   484         }
       
   485     return ret;
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // validateAllData
       
   490 // ---------------------------------------------------------------------------
       
   491 //    
       
   492 bool AlfScrollBarModel::validateAllData(int & aTotalLength, 
       
   493         int & aViewLength, 
       
   494         int & aViewStartPos)
       
   495     {
       
   496      bool ret = true;
       
   497      if (aTotalLength <= 0)
       
   498         {
       
   499         aTotalLength = 1;
       
   500         ret = false;
       
   501         }
       
   502      else
       
   503         {
       
   504         // do nothing
       
   505         }
       
   506      
       
   507      // validate view length   
       
   508      if (aViewLength <= 0)
       
   509         {
       
   510         aViewLength = 1;
       
   511         ret = false;
       
   512         }
       
   513      else if (aViewLength > aTotalLength)
       
   514         {
       
   515         // Setting maximum possible value of View Length
       
   516         aViewLength = aTotalLength;
       
   517         ret = false;
       
   518         }
       
   519      else
       
   520         {
       
   521         // do nothing
       
   522         }
       
   523      
       
   524      // validate view start pos
       
   525      if (aViewStartPos < 0)
       
   526         {
       
   527         aViewStartPos = 0;
       
   528         ret = false;
       
   529         }
       
   530      else if (aViewStartPos > (aTotalLength-aViewLength) )
       
   531         {
       
   532         // Setting maximum possible value of View Length
       
   533         aViewStartPos = aTotalLength-aViewLength;        
       
   534         ret = false;
       
   535         }
       
   536      else
       
   537         {
       
   538         // do nothing
       
   539         }
       
   540         
       
   541      // No need to validate aFocusPos for now
       
   542     return ret;
       
   543     }
       
   544     
       
   545 // ---------------------------------------------------------------------------
       
   546 // StoreWidget
       
   547 // ---------------------------------------------------------------------------
       
   548 //    
       
   549 void AlfScrollBarModel::storeWidget(AlfScrollBarWidget* aWidget)
       
   550     {
       
   551     mScrollWidget = aWidget;
       
   552     }
       
   553 }//Namespace Alf
       
   554