mulwidgets/muldatamodel/src/mulvisualitem.cpp
changeset 0 e83bab7cf002
equal deleted inserted replaced
-1:000000000000 0:e83bab7cf002
       
     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:  Visual item class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mul/mulvisualitem.h>
       
    20 
       
    21 #include <mul/imulvarianttype.h>
       
    22 #include <mul/mulvarianttype.h>
       
    23 
       
    24 #include <osn/ustring.h>
       
    25 #include <stdexcept>
       
    26 
       
    27 #include <e32math.h>	// for Pow function
       
    28 #include "mul/mulmodelutility.h"
       
    29 
       
    30 using namespace osncore;
       
    31 using namespace std;
       
    32 
       
    33 namespace Alf
       
    34     {
       
    35 // ---------------------------------------------------------------------------
       
    36 // Defination of class MulAttribute
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 class MulVisualItem::MulAttribute
       
    40     {
       
    41 public: //Constructor and destructor
       
    42 
       
    43 	/**
       
    44 	 * C++ constructor.
       
    45 	 */
       
    46 	MulAttribute():mValue(NULL),mFlag(0)
       
    47 		{
       
    48 		}
       
    49 		
       
    50 	/**
       
    51 	 * C++ constructor.
       
    52 	 *
       
    53 	 * @param aValue value of the attribute
       
    54 	 * @param aAttributeFlags property of this attribute
       
    55 	 */
       
    56 	 MulAttribute( IMulVariantType* aValue, int aAttributeFlags = 0 )
       
    57 	 	{
       
    58 	 	mValue = aValue;
       
    59 	 	mFlag = aAttributeFlags;
       
    60 	 	}
       
    61 
       
    62 	/**
       
    63 	 * Copy constructor.
       
    64 	 *
       
    65 	 * @param aAttribute existing attribute
       
    66 	 */
       
    67 	MulAttribute( const MulAttribute& aAttribute )
       
    68 	 	{
       
    69 	 	mValue = aAttribute.mValue->Clone().release();
       
    70 	 	mFlag = aAttribute.mFlag;
       
    71 	 	}
       
    72 
       
    73 	/**
       
    74 	 * Destructor.
       
    75 	 */
       
    76 	~MulAttribute()
       
    77 		{
       
    78 		delete mValue;
       
    79 		}	
       
    80 	
       
    81 public: //data
       
    82      
       
    83 	IMulVariantType* mValue ; //own    
       
    84     int mFlag ; 
       
    85     
       
    86     };
       
    87     
       
    88 // ---------------------------------------------------------------------------
       
    89 // Constructor
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 OSN_EXPORT MulVisualItem::MulVisualItem( )
       
    93 	{				
       
    94 	// Clearing dirty bit flags to 0;
       
    95 	mBitAttrFlag1 = 0;
       
    96 	mBitAttrFlag2 = 0;
       
    97 	}
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Copy Constructor
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 OSN_EXPORT MulVisualItem::MulVisualItem( const MulVisualItem& aVisualItem )
       
   104 	{	
       
   105 	map<mulvisualitem::TVisualAttribute,MulAttribute*> mapToCopy = aVisualItem.mVisualItem;
       
   106 	map<mulvisualitem::TVisualAttribute,MulAttribute*>::iterator itr;
       
   107 	for( itr = mapToCopy.begin(); itr!= mapToCopy.end(); ++itr )
       
   108 		{
       
   109 		mVisualItem[itr->first] = new (EMM) MulAttribute( *itr->second );
       
   110 		}
       
   111 	mBitAttrFlag1 = aVisualItem.mBitAttrFlag1;
       
   112 	mBitAttrFlag2 = aVisualItem.mBitAttrFlag2;
       
   113 	}
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // Destructor
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 OSN_EXPORT MulVisualItem::~MulVisualItem()
       
   120 	{
       
   121 	map<mulvisualitem::TVisualAttribute,MulAttribute* >::iterator itr;
       
   122 	for( itr = mVisualItem.begin(); itr!= mVisualItem.end(); ++itr )
       
   123 		{
       
   124 		delete itr->second;
       
   125 		itr->second = NULL;
       
   126 		}
       
   127 	mVisualItem.clear();
       
   128 	}
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // Add new attributes to visual item
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 //OSN_EXPORT void MulVisualItem::SetAttribute( mulvisualitem::TVisualAttribute aName, 
       
   135 //											 const UString& aValue, int aAttributeFlags )
       
   136 //	{
       
   137 //	auto_ptr<MulVariantType> variantdata(new (EMM) MulVariantType( aValue ));
       
   138 //	
       
   139 //	SetAttribute( aName, variantdata.release(),aAttributeFlags,EDoesOwn);
       
   140 //	}
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // Add new attributes to visual item
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 OSN_EXPORT void MulVisualItem::SetAttribute( mulvisualitem::TVisualAttribute aName, 
       
   147                                              const TDesC& aValue, int aAttributeFlags )
       
   148     {
       
   149     auto_ptr<MulVariantType> variantdata(new (EMM) MulVariantType( aValue ));
       
   150     
       
   151     SetAttribute( aName, variantdata.release(),aAttributeFlags,EDoesOwn);
       
   152     }
       
   153 
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // Add new attributes to visual item
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 OSN_EXPORT void MulVisualItem::SetAttribute( mulvisualitem::TVisualAttribute aName, 
       
   160 											  int aValue,int aAttributeFlags )
       
   161 	{	
       
   162 	auto_ptr<MulVariantType> variantdata(new ( EMM ) MulVariantType( aValue ));
       
   163 		
       
   164 	SetAttribute(aName,variantdata.release(),aAttributeFlags,EDoesOwn);	
       
   165 	}
       
   166 
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Add new attributes to visual item
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 OSN_EXPORT void MulVisualItem::SetAttribute( mulvisualitem::TVisualAttribute aName, IMulVariantType* aValue, 
       
   173 		  									   int aAttributeFlags ,
       
   174 		  									   TAttributeOwnership /*aAttributeOwership*/ )
       
   175 	{
       
   176 	map<mulvisualitem::TVisualAttribute,MulAttribute* >::iterator findIter = mVisualItem.find( aName );
       
   177 	if( findIter != mVisualItem.end() )
       
   178 		{
       
   179 		delete findIter->second;
       
   180 		findIter->second = NULL;
       
   181 		mVisualItem.erase( aName );
       
   182 		}
       
   183 	mVisualItem[aName] = new (EMM) MulAttribute(aValue,aAttributeFlags );
       
   184 	SetDirty(aName, true);
       
   185 	}
       
   186 
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // Is the input attribute dirty, to be queried before a redraw
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 OSN_EXPORT bool MulVisualItem::IsDirty(mulvisualitem::TVisualAttribute aAttr) const
       
   193 {
       
   194 	int i = (int)aAttr;
       
   195 	TReal base  = 2.0;
       
   196 	TReal r = 0.0;
       
   197 	TReal val = 0.0;
       
   198 	TInt err = KErrNone;
       
   199 
       
   200 	// If mBitAttrflag is binary 101, AND'ing it with 100(attribute 3) will 
       
   201 	// set 100 to return true for attribute 3 being dirty
       
   202 
       
   203 	// if attribute index <18 check against mBitAttrFlag1, else check
       
   204 	// against mBitAttrFlag2
       
   205 	if(i < 18)
       
   206 		{
       
   207 		val = i;
       
   208 		err = Math::Pow(r, base, val);
       
   209 		if(KErrNone != err)
       
   210 			return true;
       
   211 			
       
   212 		return mBitAttrFlag1 & (int)r;
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		val = (int)(mulvisualitem::KMul_n) - i;
       
   217 		err = Math::Pow(r, base, val);
       
   218 		if(KErrNone != err)
       
   219 			return true;
       
   220 		
       
   221 		return mBitAttrFlag2 & (int)r;
       
   222 		}
       
   223     
       
   224 }
       
   225  
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // Set the attribute as dirty in this Visual item based on input flag
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void MulVisualItem::SetDirty(mulvisualitem::TVisualAttribute aAttr, bool aDirty)
       
   232 {
       
   233 	int i = (int)aAttr;
       
   234 	TReal base  = 2.0;
       
   235 	TReal r = 0.0;
       
   236 	TReal val = i;
       
   237 	TInt err = KErrNone;
       
   238 	// If mBitAttrflag is binary 001, OR'ing it with 100(attribute 3) will 
       
   239 	// set 101 to set bit for attribute 3 as 1 in dirty flag
       
   240 	if(aDirty)
       
   241 		{
       
   242 		// if attribute index <18 operate on mBitAttrFlag1
       
   243 		// else operate against mBitAttrFlag1
       
   244 		if(i < 18)
       
   245 			{ 
       
   246 			err = Math::Pow(r,base,val);
       
   247 			mBitAttrFlag1 |= (int)r;
       
   248 			}
       
   249 			else
       
   250 			{
       
   251 			err = Math::Pow(r,base,(TReal)((int)(mulvisualitem::KMul_n) - i));
       
   252 			mBitAttrFlag2 |= (int)r;
       
   253 			}
       
   254 		}
       
   255 	else
       
   256 	{
       
   257 		// if attribute index <18 operate on mBitAttrFlag1
       
   258 		// else operate against mBitAttrFlag1
       
   259 		if(i < 18)
       
   260 		{
       
   261 			err = Math::Pow(r,base,val);
       
   262 			mBitAttrFlag1 &= ~(int)r;
       
   263 		}
       
   264 		else
       
   265 		{
       
   266 			err = Math::Pow(r, base,(TReal)((int)(mulvisualitem::KMul_n) - i));
       
   267 			mBitAttrFlag2 &= ~(int)r;
       
   268 		}
       
   269 	}
       
   270 	if(KErrNone != err)
       
   271 		throw std::invalid_argument("Error setting dirty flag for attribute"); 
       
   272 }
       
   273 
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // ResetDirtyAttribute
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 void MulVisualItem::ResetDirtyAttribute(const MulVisualItem& aVisualItem)
       
   280 {
       
   281 	mBitAttrFlag1 = aVisualItem.mBitAttrFlag1;
       
   282 	mBitAttrFlag2 = aVisualItem.mBitAttrFlag2;
       
   283 }
       
   284 // ---------------------------------------------------------------------------
       
   285 // Attribute
       
   286 // ---------------------------------------------------------------------------
       
   287 //
       
   288 OSN_EXPORT IMulVariantType* MulVisualItem::Attribute( mulvisualitem::TVisualAttribute aName ) const
       
   289 	{
       
   290 	map<mulvisualitem::TVisualAttribute,MulAttribute* >::const_iterator findIter = mVisualItem.find( aName );
       
   291 	if( findIter != mVisualItem.end() )
       
   292 		{		
       
   293 		MulAttribute* attribute = findIter->second;
       
   294 		IMulVariantType* attributeValue = attribute->mValue;
       
   295 		return  attributeValue;
       
   296 		}
       
   297 	else
       
   298 		{
       
   299 		//throw std::invalid_argument("Invalid attribute name.");
       
   300 		return NULL;
       
   301 		}
       
   302 	}
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // AttributeAsInt
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 OSN_EXPORT int MulVisualItem::AttributeAsInt( mulvisualitem::TVisualAttribute aName ) const 
       
   309 	{
       
   310 	IMulVariantType* attributeValue = Attribute(aName);
       
   311 	if( attributeValue ) 
       
   312 	    {
       
   313 	    return attributeValue->integer();
       
   314 	    }
       
   315 	throw std::invalid_argument("Invalid attribute name."); 
       
   316 	}
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // AttributeAsString
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 OSN_EXPORT const TDesC& MulVisualItem::AttributeAsString( mulvisualitem::TVisualAttribute aName ) const
       
   323 	{
       
   324 	IMulVariantType* attributeValue = Attribute(aName);
       
   325     if( attributeValue ) 
       
   326         {
       
   327         return attributeValue->DesC();
       
   328         }
       
   329 	throw std::invalid_argument("Invalid attribute name."); 
       
   330 	}
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // ExistingAttributes
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 OSN_EXPORT const std::vector<mulvisualitem::TVisualAttribute> MulVisualItem::Attributes() const 
       
   337 	{
       
   338 	map<mulvisualitem::TVisualAttribute,MulAttribute* >::const_iterator itr;
       
   339 	std::vector<mulvisualitem::TVisualAttribute> attributesName;
       
   340 	for( itr = mVisualItem.begin(); itr!= mVisualItem.end(); ++itr )
       
   341 		{
       
   342 		attributesName.push_back( itr->first );
       
   343 		}
       
   344 	return attributesName;
       
   345 	}
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // Flag
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 int MulVisualItem::Flag( mulvisualitem::TVisualAttribute aName ) 
       
   352     {
       
   353      MulAttribute* attribute = mVisualItem.find( aName )->second;
       
   354      return attribute->mFlag;
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 // RemoveAttribute
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 void MulVisualItem::RemoveAttribute( mulvisualitem::TVisualAttribute aName )
       
   362     {
       
   363     map<mulvisualitem::TVisualAttribute,MulAttribute* >::iterator findIter = mVisualItem.find( aName );
       
   364     if( findIter != mVisualItem.end() )
       
   365 		{
       
   366 		delete findIter->second;
       
   367 		findIter->second = NULL;
       
   368 		mVisualItem.erase( aName );
       
   369 		}
       
   370     }
       
   371 
       
   372     }// namespace Alf	
       
   373 
       
   374 //End of file