widgetmodel/alfwidgetmodel/src/alfattribute.cpp
changeset 0 e83bab7cf002
equal deleted inserted replaced
-1:000000000000 0:e83bab7cf002
       
     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:   Implementation for creating different types of attributes.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <osn/osnnew.h>
       
    20 #include <osn/ustring.h>
       
    21 #include <osn/alfptrvector.h>
       
    22 #include "alf/alfattribute.h"
       
    23 #include <alf/alfdataexception.h>
       
    24 #include <stdexcept>
       
    25 #include <alf/alftimedvalue.h>
       
    26 #include <alf/alfattributeexception.h>
       
    27 
       
    28 using namespace osncore;
       
    29 
       
    30 namespace Alf
       
    31     {
       
    32 
       
    33 
       
    34 class AlfAttributeImpl
       
    35     {
       
    36 public:
       
    37     AlfAttributeImpl():mCategory(AlfAttribute::EStatic),
       
    38             mInterpolationStyle(EAlfTimedValueStyleUseSystemDefault),
       
    39             mTime(0),
       
    40             mDelay(0),
       
    41             mMappingFunctionId(0),
       
    42             mDirtyFlag(true)
       
    43         {
       
    44         }
       
    45 
       
    46     ~AlfAttributeImpl()
       
    47         {
       
    48         }
       
    49 
       
    50     UString mName;
       
    51     AlfAttribute::attributecategory mCategory;
       
    52     TAlfInterpolationStyle mInterpolationStyle;
       
    53     int mTime;
       
    54     int mDelay;
       
    55     int mMappingFunctionId;
       
    56     UString mDataField;
       
    57     AlfPtrVector<AlfAttributeValueType> mTargetValueContainer;
       
    58     AlfPtrVector<AlfAttributeValueType> mSourceValueContainer;
       
    59     bool mDirtyFlag;
       
    60     };
       
    61 
       
    62 // ======== MEMBER FUNCTIONS ========
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // Constructor.
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 OSN_EXPORT AlfAttribute::AlfAttribute()
       
    69     {
       
    70     reset("", 0, attributecategory(0));
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Constructor.
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 OSN_EXPORT AlfAttribute::AlfAttribute(const char* aName,
       
    78                                       attributecategory aCategory)
       
    79     {
       
    80     reset(aName, 0, aCategory);
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Constructor.
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 OSN_EXPORT AlfAttribute::AlfAttribute(const char* aName, int aValue,
       
    88                                       TAlfUnit aUnit, attributecategory aCategory)
       
    89     {
       
    90     auto_ptr<AlfAttributeValueType> value(new (EMM)
       
    91                                           AlfAttributeValueType(aValue, aUnit));
       
    92     reset(aName, value.get(), aCategory);
       
    93     value.release();
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // Constructor.
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 OSN_EXPORT AlfAttribute::AlfAttribute(const char* aName, float aValue,
       
   101                                       TAlfUnit aUnit, attributecategory aCategory)
       
   102     {
       
   103     auto_ptr<AlfAttributeValueType> value(
       
   104         new( EMM ) AlfAttributeValueType( aValue, aUnit ) );
       
   105     reset(aName, value.get(), aCategory);
       
   106     value.release();
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // Constructor.
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 OSN_EXPORT AlfAttribute::AlfAttribute(const char* aName,
       
   114                                       const UString& aValue, attributecategory aCategory)
       
   115     {
       
   116     if (aCategory == EStaticData)
       
   117         {
       
   118         reset(aName, 0, aCategory);
       
   119         setDataField(aValue.getUtf8());
       
   120         }
       
   121     else
       
   122         {
       
   123         auto_ptr<AlfAttributeValueType> value(
       
   124             new( EMM ) AlfAttributeValueType( aValue ) );
       
   125         reset(aName, value.get(), aCategory);
       
   126         value.release();
       
   127         }
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // Destructor.
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 OSN_EXPORT AlfAttribute::~AlfAttribute()
       
   135     {
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Assigment operation.
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 OSN_EXPORT AlfAttribute& AlfAttribute::operator=(const AlfAttribute& aAttribute)
       
   143     {
       
   144     if ( this == &aAttribute )
       
   145         {
       
   146         return *this;
       
   147         }
       
   148 
       
   149     mData.reset( new( EMM ) AlfAttributeImpl() );
       
   150 
       
   151     mData->mCategory = aAttribute.mData->mCategory;
       
   152     mData->mName = UString(aAttribute.name());
       
   153     mData->mInterpolationStyle = aAttribute.mData->mInterpolationStyle;
       
   154 
       
   155     mData->mTime = aAttribute.mData->mTime;
       
   156     mData->mDelay = aAttribute.mData->mDelay;
       
   157     mData->mMappingFunctionId = aAttribute.mData->mMappingFunctionId;
       
   158     mData->mDataField = UString(aAttribute.getDataField());
       
   159 
       
   160     int count = aAttribute.mData->mTargetValueContainer.count();
       
   161     mData->mTargetValueContainer.resize( count );
       
   162     for ( int i = 0 ; i < count ; i++ )
       
   163         {
       
   164         mData->mTargetValueContainer.insert(
       
   165             i, cloneValueType( aAttribute.mData->mTargetValueContainer[i] ) );
       
   166         }
       
   167 
       
   168     count = aAttribute.mData->mSourceValueContainer.count();
       
   169     mData->mSourceValueContainer.resize( count );
       
   170     for ( int i = 0 ; i < count ; i++ )
       
   171         {
       
   172         mData->mSourceValueContainer.insert(
       
   173             i, cloneValueType( aAttribute.mData->mSourceValueContainer[i] ) );
       
   174         }
       
   175 
       
   176     return *this;
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // Clones the attribute.
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 OSN_EXPORT AlfAttribute* AlfAttribute::clone()
       
   184     {
       
   185     auto_ptr<AlfAttribute> clone( new( EMM ) AlfAttribute() );
       
   186     *clone.get() = *this;
       
   187     return clone.release();
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Gets the name of the Attribute.
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 OSN_EXPORT const char* AlfAttribute::name() const
       
   195     {
       
   196     return mData->mName.getUtf8();
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // Gets the category of the atrribute.
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 OSN_EXPORT AlfAttribute::attributecategory AlfAttribute::category() const
       
   204     {
       
   205     return mData->mCategory;
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // Gets the type of the target atrribute.
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 OSN_EXPORT AlfAttributeValueType::Type AlfAttribute::type(
       
   213     unsigned int aIndex) const
       
   214     {
       
   215     if (aIndex >= mData->mTargetValueContainer.count())
       
   216         {
       
   217         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   218         }
       
   219     return mData->mTargetValueContainer[aIndex]->type();
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Gets the int value.
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 OSN_EXPORT int AlfAttribute::intValue(unsigned int aIndex) const
       
   227     {
       
   228     if (aIndex >= mData->mTargetValueContainer.count())
       
   229         {
       
   230         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   231         }
       
   232     return mData->mTargetValueContainer[aIndex]->intValue();
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Gets the real value.
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 OSN_EXPORT float AlfAttribute::realValue(unsigned int aIndex) const
       
   240     {
       
   241     if (aIndex >= mData->mTargetValueContainer.count())
       
   242         {
       
   243         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   244         }
       
   245     return mData->mTargetValueContainer[aIndex]->realValue();
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // Gets the string value.
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 OSN_EXPORT const UString& AlfAttribute::stringValue(unsigned int aIndex) const
       
   253     {
       
   254     if (aIndex >= mData->mTargetValueContainer.count())
       
   255         {
       
   256         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   257         }
       
   258     return mData->mTargetValueContainer[aIndex]->stringValue();
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // Returns true if attribute is set.
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 OSN_EXPORT bool AlfAttribute::isValueSet() const
       
   266     {
       
   267     return mData->mTargetValueContainer.count() > 0;
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // Gets the unit of the value.
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 OSN_EXPORT TAlfUnit AlfAttribute::unit(unsigned int aIndex) const
       
   275     {
       
   276     if (aIndex >= mData->mTargetValueContainer.count())
       
   277         {
       
   278         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   279         }
       
   280     return mData->mTargetValueContainer[aIndex]->unit();
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Adds a new target value to the attribute.
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 OSN_EXPORT  void AlfAttribute::addTargetValue(AlfAttributeValueType* aValue)
       
   288     {
       
   289     mData->mTargetValueContainer.resize(mData->mTargetValueContainer.count() +1 );
       
   290     mData->mTargetValueContainer.insert(mData->mTargetValueContainer.count(), aValue);
       
   291     }
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // Gets target value count.
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 OSN_EXPORT  unsigned int AlfAttribute::getTargetValueCount() const
       
   298     {
       
   299     return mData->mTargetValueContainer.count();
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // Sets the target value of the attribute.
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 OSN_EXPORT void AlfAttribute::setTargetValue(AlfAttributeValueType* aValue,
       
   307         unsigned int aIndex)
       
   308     {
       
   309     if (aIndex < mData->mTargetValueContainer.count())
       
   310         {
       
   311         mData->mTargetValueContainer.remove(aIndex);
       
   312         }
       
   313     mData->mTargetValueContainer.insert(aIndex, aValue);
       
   314     mData->mDirtyFlag=true;
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // Gets the target value of the attribute.
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 OSN_EXPORT AlfAttributeValueType* AlfAttribute::getTargetValue(
       
   322     unsigned int aIndex) const
       
   323     {
       
   324     if (aIndex >= mData->mTargetValueContainer.count())
       
   325         {
       
   326         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   327         }
       
   328     return mData->mTargetValueContainer[aIndex];
       
   329     }
       
   330 
       
   331 // ---------------------------------------------------------------------------
       
   332 // Removes the target value of the attribute.
       
   333 // ---------------------------------------------------------------------------
       
   334 //
       
   335 OSN_EXPORT void AlfAttribute::removeTargetValue(unsigned int aIndex)
       
   336     {
       
   337     if (aIndex >= mData->mTargetValueContainer.count())
       
   338         {
       
   339         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   340         }
       
   341     mData->mTargetValueContainer.remove(aIndex);
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // Adds a new source value to the attribute.
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 OSN_EXPORT void AlfAttribute::addSourceValue(AlfAttributeValueType* aValue)
       
   349     {
       
   350     mData->mSourceValueContainer.resize(mData->mSourceValueContainer.count() +1 );
       
   351     mData->mSourceValueContainer.insert(mData->mSourceValueContainer.count(), aValue);
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // Gets source value count.
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 OSN_EXPORT unsigned int AlfAttribute::getSourceValueCount() const
       
   359     {
       
   360     return mData->mSourceValueContainer.count();
       
   361     }
       
   362 
       
   363 // ---------------------------------------------------------------------------
       
   364 // Adds a new source value to the attribute.
       
   365 // ---------------------------------------------------------------------------
       
   366 //
       
   367 OSN_EXPORT void AlfAttribute::setSourceValue(AlfAttributeValueType* aValue,
       
   368         unsigned int aIndex)
       
   369     {
       
   370     if (aIndex < mData->mSourceValueContainer.count())
       
   371         {
       
   372         mData->mSourceValueContainer.remove(aIndex);
       
   373         }
       
   374     mData->mSourceValueContainer.insert(aIndex, aValue);
       
   375     mData->mDirtyFlag=true;
       
   376     }
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 //  Gets the source value of the attribute.
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 OSN_EXPORT AlfAttributeValueType* AlfAttribute::getSourceValue(
       
   383     unsigned int aIndex) const
       
   384     {
       
   385     if (aIndex >= mData->mSourceValueContainer.count())
       
   386         {
       
   387         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   388         }
       
   389     return mData->mSourceValueContainer[aIndex];
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // Removes the source value of the attribute.
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 OSN_EXPORT void AlfAttribute::removeSourceValue(unsigned int aIndex)
       
   397     {
       
   398     if (aIndex >= mData->mSourceValueContainer.count())
       
   399         {
       
   400         ALF_THROW(AlfAttributeException,EInvalidAttribute,"AlfAttribute")
       
   401         }
       
   402     mData->mSourceValueContainer.remove(aIndex);
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // Sets the interpolation style of the attribute.
       
   407 // ---------------------------------------------------------------------------
       
   408 //
       
   409 OSN_EXPORT void AlfAttribute::setInterpolationStyle(
       
   410     TAlfInterpolationStyle aInterpolationStyle)
       
   411     {
       
   412     mData->mInterpolationStyle = aInterpolationStyle;
       
   413     mData->mDirtyFlag=true;
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // Gets the interpolation style of the attribute.
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 OSN_EXPORT TAlfInterpolationStyle AlfAttribute::getInterpolationStyle()const
       
   421     {
       
   422     return mData->mInterpolationStyle;
       
   423     }
       
   424 
       
   425 // ---------------------------------------------------------------------------
       
   426 // Sets the transition time for the attribute.
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 OSN_EXPORT void AlfAttribute::setTime(int aTime)
       
   430     {
       
   431     mData->mTime = aTime;
       
   432     mData->mDirtyFlag=true;
       
   433     }
       
   434 
       
   435 // ---------------------------------------------------------------------------
       
   436 // Gets the transition time for the attribute.
       
   437 // ---------------------------------------------------------------------------
       
   438 //
       
   439 OSN_EXPORT int AlfAttribute::getTime()const
       
   440     {
       
   441     return mData->mTime;
       
   442     }
       
   443 
       
   444 // ---------------------------------------------------------------------------
       
   445 // Sets the mapping function id for the attribute.
       
   446 // ---------------------------------------------------------------------------
       
   447 //
       
   448 OSN_EXPORT void AlfAttribute::setMappingFunctionId(int aId)
       
   449     {
       
   450     mData->mMappingFunctionId = aId;
       
   451     mData->mDirtyFlag=true;
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // Gets the mapping function id for the attribute.
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 OSN_EXPORT int AlfAttribute::getMappingFunctionId()const
       
   459     {
       
   460     return mData->mMappingFunctionId;
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // Sets the datafield for the attribute.
       
   465 // ---------------------------------------------------------------------------
       
   466 //
       
   467 OSN_EXPORT void AlfAttribute::setDataField(const char* aDataField)
       
   468     {
       
   469     mData->mDataField = UString(aDataField);
       
   470     mData->mDirtyFlag = true;
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // Gets the datafield for the attribute.
       
   475 // ---------------------------------------------------------------------------
       
   476 //
       
   477 OSN_EXPORT const char* AlfAttribute::getDataField() const
       
   478     {
       
   479     return mData->mDataField.getUtf8();
       
   480     }
       
   481 
       
   482 // ---------------------------------------------------------------------------
       
   483 // Sets the delay before the transition.
       
   484 // ---------------------------------------------------------------------------
       
   485 //
       
   486 OSN_EXPORT void AlfAttribute::setDelay(int aDelay)
       
   487     {
       
   488     mData->mDelay = aDelay;
       
   489     mData->mDirtyFlag = true;
       
   490     }
       
   491 
       
   492 // ---------------------------------------------------------------------------
       
   493 // Gets the delay before the transition.
       
   494 // ---------------------------------------------------------------------------
       
   495 //
       
   496 OSN_EXPORT int AlfAttribute::getDelay() const
       
   497     {
       
   498     return mData->mDelay;
       
   499     }
       
   500 
       
   501 // ---------------------------------------------------------------------------
       
   502 // Resets the data.
       
   503 // ---------------------------------------------------------------------------
       
   504 //
       
   505 void AlfAttribute::reset( const char* aName, AlfAttributeValueType* aValue,
       
   506                           attributecategory aCategory)
       
   507     {
       
   508     mData.reset(new (EMM) AlfAttributeImpl());
       
   509 
       
   510     mData->mCategory = aCategory;
       
   511     mData->mName = UString(aName);
       
   512     mData->mTargetValueContainer.remove(0);
       
   513     mData->mTargetValueContainer.insert(0, aValue);
       
   514     }
       
   515 
       
   516 // ---------------------------------------------------------------------------
       
   517 // Helper function for cloning value type.
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 AlfAttributeValueType* AlfAttribute::cloneValueType(
       
   521     AlfAttributeValueType* aValue) const
       
   522     {
       
   523     AlfAttributeValueType* ret = 0;
       
   524 
       
   525     if (aValue)
       
   526         {
       
   527         switch (aValue->type())
       
   528             {
       
   529             case AlfAttributeValueType::EInt:
       
   530                 {
       
   531                 ret = new (EMM) AlfAttributeValueType(
       
   532                     aValue->intValue(), aValue->unit());
       
   533                 break;
       
   534                 }
       
   535             case AlfAttributeValueType::EFloat:
       
   536                 {
       
   537                 ret = new (EMM) AlfAttributeValueType(
       
   538                     aValue->realValue(), aValue->unit());
       
   539                 break;
       
   540                 }
       
   541             case AlfAttributeValueType::EString:
       
   542                 {
       
   543                 ret = new (EMM) AlfAttributeValueType(
       
   544                     aValue->stringValue());
       
   545                 break;
       
   546                 }
       
   547             default:
       
   548                 break;
       
   549             }
       
   550         }
       
   551 
       
   552     return ret;
       
   553     }
       
   554 OSN_EXPORT bool AlfAttribute::isDirty( ) const
       
   555     {
       
   556     return mData->mDirtyFlag;
       
   557     }
       
   558 
       
   559 OSN_EXPORT void AlfAttribute::setDirty(bool aFlag )
       
   560     {
       
   561     mData->mDirtyFlag = aFlag;
       
   562     }
       
   563 
       
   564     } // Alf