widgetmodel/alfwidgetmodel/src/alfgridlayoutattributesetter.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 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:   Implements attributesetters for grid layout..
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //widget model includes
       
    20 #include "alf/alfattributecontainer.h"
       
    21 #include "alf/alfattributevaluetype.h"
       
    22 #include "alf/alfattribute.h"
       
    23 #include "alf/alfgridlayoutattributesetter.h"
       
    24 #include <alf/alfvisualexception.h>
       
    25 #include <alf/alfdataexception.h>
       
    26 #include <alf/alfattributeexception.h>
       
    27 
       
    28 //osn includes
       
    29 #include <osn/ustring.h>
       
    30 
       
    31 //alf includes
       
    32 #include <alf/alfenv.h>
       
    33 #include <alf/alfgridlayout.h>
       
    34 
       
    35 //dui includes
       
    36 #include "alf/attrproperty.h"
       
    37 
       
    38 //other includes
       
    39 #include <libc/string.h>
       
    40 
       
    41 using osncore::UString;
       
    42 
       
    43 using namespace duiuimodel::commonvisualattributes;
       
    44 
       
    45 using namespace duiuimodel::layoutattributes;
       
    46 
       
    47 //variable inside ifdef to prevent compiler warning.
       
    48 #ifdef ALF_DEBUG_EXCEPTIONS
       
    49 static const char* const ClassName = "AlfGridLayoutAttributeSetter";
       
    50 #endif
       
    51 
       
    52 namespace Alf
       
    53     {
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // returns int or realvalue as float.
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 float realOrInt(const AlfAttribute& aAttr, int aIndex)
       
    60     {
       
    61     switch(aAttr.type(aIndex))
       
    62         {
       
    63         case AlfAttributeValueType::EInt:
       
    64             return aAttr.intValue(aIndex);
       
    65         case AlfAttributeValueType::EFloat:            
       
    66             return aAttr.realValue(aIndex);
       
    67         default:
       
    68             ALF_THROW ( AlfDataException, ECommonError, ClassName);
       
    69         }
       
    70     }
       
    71 
       
    72 // ======== MEMBER FUNCTIONS ========
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Constructor.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 OSN_EXPORT AlfGridLayoutAttributeSetter::AlfGridLayoutAttributeSetter()
       
    79     {
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // Destructor.
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 OSN_EXPORT AlfGridLayoutAttributeSetter::~AlfGridLayoutAttributeSetter()
       
    87     {
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Sets Attribute Value. Delegates based on attribute Category.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 OSN_EXPORT void AlfGridLayoutAttributeSetter::setAttributeValue(
       
    95     CAlfVisual &aVisual,
       
    96     AlfAttributeContainer* aContainer,
       
    97     IAlfMap* aData )
       
    98     {
       
    99     CAlfGridLayout* gridlayout = dynamic_cast<CAlfGridLayout*>(&aVisual);
       
   100 
       
   101     if (!gridlayout)
       
   102         {
       
   103         ALF_THROW(AlfVisualException, EInvalidVisual, ClassName)
       
   104         }
       
   105 
       
   106     AlfCommonLayoutAttributeSetter::setAttributeValue(aVisual,
       
   107             aContainer, aData);
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Deprecated
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 OSN_EXPORT TAlfCommand* AlfGridLayoutAttributeSetter::createCommand(
       
   115         CAlfVisual& /*aVisual*/,
       
   116         AlfAttributeContainer* /*aContainer*/, 
       
   117         IAlfMap* /*aData*/,
       
   118         int /*aTransitionTime*/, 
       
   119         CAlfVisual* /*aRefVisual*/ )
       
   120     {
       
   121     //deprecated
       
   122     TAlfCommand* cmd = 0;
       
   123     return cmd;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Sends a command to Env
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 OSN_EXPORT void AlfGridLayoutAttributeSetter::createAndSendCommands(
       
   131     CAlfVisual& aVisual,
       
   132     AlfAttributeContainer* aContainer,
       
   133     CAlfVisual* aRefVisual )
       
   134     {
       
   135     //no animatable attributes in grid layout.
       
   136     AlfCommonLayoutAttributeSetter::createAndSendCommands( 
       
   137         aVisual, aContainer, aRefVisual);
       
   138     }
       
   139 
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Sets dynamic attributes to visual
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void AlfGridLayoutAttributeSetter::handleDynamicAttribute(
       
   146     CAlfVisual &aVisual,
       
   147     AlfAttribute& aAttr, 
       
   148     AlfAttributeContainer& aContainer)
       
   149     {
       
   150     //no dynamic attributes in grid layout.
       
   151     AlfCommonLayoutAttributeSetter::handleDynamicAttribute(
       
   152         aVisual, aAttr, aContainer);
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // Sets static attributes to visual
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void AlfGridLayoutAttributeSetter::handleStaticAttribute ( 
       
   160     CAlfVisual &aVisual,
       
   161     AlfAttribute& aAttr, 
       
   162     AlfAttributeContainer& aContainer)
       
   163     {
       
   164     const char* attrName = aAttr.name();
       
   165     CAlfGridLayout* gridlayout = dynamic_cast<CAlfGridLayout*>(&aVisual);
       
   166     if (!gridlayout)
       
   167         {
       
   168         return;
       
   169         }     
       
   170 
       
   171     if (!strcmp(attrName, grid::KRows))
       
   172         {
       
   173         int rows = realOrInt(aAttr, 0);     //this can throw
       
   174         TRAPD(err1, gridlayout->SetRowsL(rows));
       
   175         if (err1 != KErrNone) 
       
   176             {
       
   177             ALF_THROW(AlfException, err1, ClassName)
       
   178             }
       
   179         }
       
   180     else if (!strcmp(attrName, grid::KColumns))
       
   181         {
       
   182         int columns = realOrInt(aAttr, 0);    //this can throw
       
   183         TRAPD(err1, gridlayout->SetColumnsL(columns));
       
   184         if (err1 != KErrNone) 
       
   185             {
       
   186             ALF_THROW(AlfException, err1, ClassName)
       
   187             }
       
   188         }
       
   189     else if (!strcmp( attrName, grid::KColumnWeight))
       
   190         {
       
   191         unsigned int valueCount = aAttr.getTargetValueCount();
       
   192 
       
   193         for (int i = 0; i < valueCount; ++i)
       
   194             {
       
   195             TAlfUnit unitType = aAttr.unit(i);
       
   196 
       
   197             float val = realOrInt(aAttr, i);
       
   198             TAlfMetric metric( val, unitType );
       
   199 
       
   200             TRAPD(err, gridlayout->ReplaceWeightL(EAlfGridColumn, metric, i));
       
   201 
       
   202             int err2 = KErrNone;
       
   203             if (err == KErrArgument)
       
   204                 {
       
   205                 TRAP(err2, gridlayout->AppendWeightL(EAlfGridColumn, metric));
       
   206                 if (err2 != KErrNone)
       
   207                     {
       
   208                     ALF_THROW(AlfException, err2, ClassName)
       
   209                     }
       
   210                 }
       
   211             else if (err != KErrNone)
       
   212                 {
       
   213                 ALF_THROW(AlfException, ECommonError, ClassName)
       
   214                 }
       
   215             }
       
   216         }
       
   217     else if ( !strcmp ( attrName, grid::KRowWeight ) )
       
   218         {
       
   219         unsigned int valueCount = aAttr.getTargetValueCount();
       
   220 
       
   221         for ( int i = 0; i < valueCount; ++i )
       
   222             {
       
   223 
       
   224             TAlfUnit unitType = aAttr.unit(i);
       
   225             float val = realOrInt(aAttr, i);
       
   226             TAlfMetric metric ( val, unitType );
       
   227 
       
   228             TRAPD ( err, gridlayout->ReplaceWeightL ( EAlfGridRow, metric, i ) );
       
   229 
       
   230             int err2 = KErrNone;
       
   231             if ( err == KErrArgument )
       
   232                 {
       
   233                 TRAP ( err2, gridlayout->AppendWeightL ( EAlfGridRow, metric ) );
       
   234                 if (err2 != KErrNone)
       
   235                     {
       
   236                     ALF_THROW(AlfException, err2, ClassName)
       
   237                     }
       
   238                 }
       
   239             else if (err != KErrNone)
       
   240                 {
       
   241                 ALF_THROW(AlfException, ECommonError, ClassName)
       
   242                 }
       
   243             }
       
   244         }
       
   245     else if (!strcmp( attrName, KExpansionFlag ))
       
   246         {
       
   247         gridlayout->SetExpanding ( aAttr.intValue() );
       
   248         }
       
   249     else if (!strcmp( attrName, grid::KSetLayoutModeFlag ))
       
   250         {
       
   251         if (aAttr.getTargetValueCount() == 2 && 
       
   252             aAttr.type(0) == AlfAttributeValueType::EInt && 
       
   253             aAttr.type(1) == AlfAttributeValueType::EInt)
       
   254             {
       
   255             gridlayout->SetLayoutModeFlags(TAlfGridDimension( 
       
   256                 aAttr.intValue(0)), uint(aAttr.intValue(1)));
       
   257             }
       
   258         else
       
   259             {
       
   260             ALF_THROW(AlfAttributeException, EInvalidAttribute, ClassName)
       
   261             }
       
   262         }
       
   263     else if (!strcmp( attrName, grid::KClearLayoutModeFlag ))
       
   264         {
       
   265         if ( aAttr.getTargetValueCount() == 2 && 
       
   266              aAttr.type(0) == AlfAttributeValueType::EInt && 
       
   267              aAttr.type(1) == AlfAttributeValueType::EInt)
       
   268             {
       
   269             gridlayout->ClearLayoutModeFlags(TAlfGridDimension( 
       
   270                 aAttr.intValue(0)), uint(aAttr.intValue(1)));
       
   271             }
       
   272         else
       
   273             {
       
   274             ALF_THROW( AlfAttributeException, EInvalidAttribute, ClassName)
       
   275             }
       
   276         }
       
   277     else
       
   278         {
       
   279         AlfCommonLayoutAttributeSetter::handleStaticAttribute(
       
   280             aVisual, aAttr, aContainer);
       
   281         }
       
   282     }
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // Sets dynamic attributes to visual from data
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 void AlfGridLayoutAttributeSetter::handleDynamicDataAttribute(
       
   289     CAlfVisual &aVisual,
       
   290     AlfAttribute& aAttr, 
       
   291     AlfAttributeContainer& aContainer, 
       
   292     IAlfMap* aData )
       
   293     {
       
   294     //no dynamic attributes in grid layout.
       
   295     AlfCommonLayoutAttributeSetter::handleDynamicDataAttribute(aVisual,
       
   296             aAttr, aContainer, aData);
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // Sets static attributes to visual  from data
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void AlfGridLayoutAttributeSetter::handleStaticDataAttribute(
       
   304     CAlfVisual &aVisual,
       
   305     AlfAttribute& aAttr, 
       
   306     AlfAttributeContainer& aContainer, 
       
   307     IAlfMap* aData)
       
   308     {
       
   309     CAlfGridLayout* gridlayout = dynamic_cast<CAlfGridLayout*> ( &aVisual );
       
   310     if (!gridlayout)
       
   311         {
       
   312         ALF_THROW(AlfDataException, ECommonError, ClassName)
       
   313         }    
       
   314 
       
   315     const char* attrName = aAttr.name();
       
   316     const char* dataField = aAttr.getDataField();
       
   317 
       
   318     if (!dataField)
       
   319         {
       
   320         ALF_THROW(AlfDataException, ECommonError, ClassName)
       
   321         }
       
   322 
       
   323     if (!aData) 
       
   324         {
       
   325         ALF_THROW(AlfDataException, ECommonError, ClassName)
       
   326         }
       
   327 
       
   328     IAlfVariantType* data = aData->item(UString(dataField));
       
   329 
       
   330     if (data)
       
   331         {
       
   332         if (!strcmp(attrName, grid::KRows))
       
   333             {
       
   334             if (data->type() == IAlfVariantType::EInt)
       
   335                 {
       
   336                 int rows = data->integer() ;
       
   337                 TRAPD(err1, gridlayout->SetRowsL ( rows ));
       
   338                 if (err1 != KErrNone) 
       
   339                     {
       
   340                     ALF_THROW(AlfException, err1, ClassName)
       
   341                     }
       
   342                 }
       
   343             }
       
   344         else if (!strcmp( attrName, grid::KColumns))
       
   345             {
       
   346             if (data->type() == IAlfVariantType::EInt)
       
   347                 {
       
   348                 int columns = data->integer();
       
   349                 TRAPD(err1, gridlayout->SetColumnsL (columns));
       
   350                 if (err1 != KErrNone)
       
   351                     {
       
   352                     ALF_THROW ( AlfException, ECommonError, ClassName )
       
   353                     }
       
   354                 }
       
   355             }
       
   356         else if (!strcmp(attrName, KExpansionFlag))
       
   357             {
       
   358             if (data->type() == IAlfVariantType::EInt)
       
   359                 {
       
   360                 gridlayout->SetExpanding(data->integer());
       
   361                 }
       
   362             }
       
   363         else if (!strcmp(attrName, grid::KSetLayoutModeFlag))
       
   364             {
       
   365             if (data->type() == IAlfVariantType::EContainer)
       
   366                 {
       
   367                 IAlfContainer* dataContainer = data->container();
       
   368 
       
   369                 if ( dataContainer->count() != 2)
       
   370                     {
       
   371                     ALF_THROW(AlfDataException, EInvalidContainerOperation, 
       
   372                         ClassName)
       
   373                     }
       
   374 
       
   375                 IAlfVariantType* dimensionData = dataContainer->item(0);
       
   376                 IAlfVariantType* flags = dataContainer->item(1);
       
   377 
       
   378                 if (dimensionData && flags &&
       
   379                     dimensionData->type() == IAlfVariantType::EInt && 
       
   380                     flags->type() == IAlfVariantType::EInt)
       
   381                     {
       
   382                     gridlayout->SetLayoutModeFlags(TAlfGridDimension( 
       
   383                         dimensionData->integer()) , uint(flags->integer()));
       
   384                     }
       
   385                 }
       
   386             }
       
   387         else if (!strcmp(attrName, grid::KClearLayoutModeFlag))
       
   388             {
       
   389             if (data->type() == IAlfVariantType::EContainer)
       
   390                 {
       
   391                 IAlfContainer* dataContainer = data->container();
       
   392 
       
   393                 if (dataContainer->count() != 2)
       
   394                     {
       
   395                     ALF_THROW(AlfDataException, EInvalidContainerOperation, 
       
   396                         ClassName)
       
   397                     }
       
   398 
       
   399                 IAlfVariantType* dimensionData = dataContainer->item(0);
       
   400                 IAlfVariantType* flags = dataContainer->item(1);
       
   401 
       
   402                 if (dimensionData && flags && 
       
   403                     dimensionData->type() == IAlfVariantType::EInt && 
       
   404                     flags->type() == IAlfVariantType::EInt)
       
   405                     {
       
   406                     gridlayout->ClearLayoutModeFlags(TAlfGridDimension(
       
   407                         dimensionData->integer()), uint(flags->integer()));
       
   408                     }
       
   409                 }
       
   410             }
       
   411         else if ( !strcmp ( attrName, grid::KColumnWeight ) )
       
   412             {
       
   413             }
       
   414         else if ( !strcmp ( attrName, grid::KRowWeight ) )
       
   415             {
       
   416             }
       
   417         else
       
   418             {
       
   419             AlfCommonAttributeSetter::handleStaticDataAttribute(aVisual, 
       
   420                 aAttr, aContainer, aData);
       
   421             }
       
   422         }
       
   423     }
       
   424 
       
   425     } // namespace Alf
       
   426 // End of file
       
   427 
       
   428