skins/AknSkins/alsrc/AknsAlTimingModelRelative.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Implements relative timing model.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32math.h>
       
    22 #include <AknsRlParameter.h>
       
    23 #include "AknsAlTimingModelRelative.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ===============================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CAknsAlTimingModelRelative::CAknsAlTimingModelRelative
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CAknsAlTimingModelRelative::CAknsAlTimingModelRelative():
       
    32     iTimeSpan( EHour ),
       
    33     iSlices( 1 ),
       
    34     iWrap( EFalse ),
       
    35     iRelative( 0 )
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CAknsAlTimingModelRelative::NewL
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CAknsAlTimingModelRelative* CAknsAlTimingModelRelative::NewL()
       
    44     {
       
    45     CAknsAlTimingModelRelative* self = new(ELeave) CAknsAlTimingModelRelative();
       
    46     return self;
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CAknsAlTimingModelRelative::CAknsAlTimingModelRelative
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CAknsAlTimingModelRelative::~CAknsAlTimingModelRelative()
       
    54     {
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CAknsAlTimingModelRelative::SetParametersL
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 void CAknsAlTimingModelRelative::SetParametersL(
       
    62     MAknsRlParameterIterator& aParameters )
       
    63     {
       
    64     // Iterate over available parameters
       
    65     while( aParameters.HasNext() )
       
    66         {
       
    67         const TAknsRlParameterData* param = aParameters.NextL();
       
    68 
       
    69         if( param->iName->Compare( KAknsAlRelativeTimeSpan ) == 0 )
       
    70             {
       
    71             if( param->iType != EAknsRlParameterTypeNumber )
       
    72                 User::Leave( KErrArgument );
       
    73 
       
    74             if( param->iNumber < EHour || param->iNumber > EMonth )
       
    75                 User::Leave( KErrArgument );
       
    76 
       
    77             iTimeSpan = TTimeSpan( param->iNumber );
       
    78             }
       
    79         else if( param->iName->Compare( KAknsAlRelativeSlices ) == 0 )
       
    80             {
       
    81             if( param->iType != EAknsRlParameterTypeNumber )
       
    82                 User::Leave( KErrArgument );
       
    83 
       
    84             if( param->iNumber < 1 )
       
    85                 User::Leave( KErrArgument );
       
    86 
       
    87             iSlices = param->iNumber;
       
    88             }
       
    89         else if( param->iName->Compare( KAknsAlRelativeWrap ) == 0 )
       
    90             {
       
    91             if( param->iType != EAknsRlParameterTypeNumber )
       
    92                 User::Leave( KErrArgument );
       
    93 
       
    94             iWrap = ( param->iNumber != 0 ) ? ETrue: EFalse;
       
    95             }
       
    96         }
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CAknsAlTimingModelRelative::Tick
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CAknsAlTimingModelRelative::Tick( const TInt /*aDeltaTime*/ )
       
   104     {
       
   105     TTime current;
       
   106     current.HomeTime();
       
   107     Tick( current );
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CAknsAlTimingModelRelative::Tick
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CAknsAlTimingModelRelative::Tick( const TTime& aStamp )
       
   115     {
       
   116     // TODO Is 16.16 enough for lengthy time spans?
       
   117     // In a month time span 16.16 leads to about 40 second resolution
       
   118     TDateTime cur = aStamp.DateTime();
       
   119     TTime startStamp;
       
   120     TTime endStamp;
       
   121 
       
   122     switch( iTimeSpan )
       
   123         {
       
   124         case EHour:
       
   125             {
       
   126             startStamp = TDateTime( cur.Year(), cur.Month(), cur.Day(), cur.Hour(), 0, 0, 0 );
       
   127             endStamp = startStamp + TTimeIntervalHours( 1 );
       
   128             }
       
   129             break;
       
   130         case EDay:
       
   131             {
       
   132             startStamp = TDateTime( cur.Year(), cur.Month(), cur.Day(), 0, 0, 0, 0 );
       
   133             endStamp = startStamp + TTimeIntervalDays( 1 );
       
   134             }
       
   135             break;
       
   136         case EWeek:
       
   137             {
       
   138             startStamp = TDateTime( cur.Year(), cur.Month(), cur.Day(), 0, 0, 0, 0 );
       
   139             // Reverse to the start of the week
       
   140             startStamp = startStamp - TTimeIntervalDays( aStamp.DayNoInWeek() );
       
   141             endStamp = startStamp + TTimeIntervalDays( 7 );
       
   142             }
       
   143             break;
       
   144         case EMonth:
       
   145             {
       
   146             startStamp = TDateTime( cur.Year(), cur.Month(), 0, 0, 0, 0, 0 );
       
   147             endStamp = startStamp + TTimeIntervalDays( aStamp.DaysInMonth() );
       
   148             }
       
   149             break;
       
   150         default:
       
   151             iRelative = 0;
       
   152             return;
       
   153         }
       
   154 
       
   155     // Determine second spans ( start -> end, start -> cur )
       
   156     TTimeIntervalSeconds intervalToCur = 0;
       
   157     TTimeIntervalSeconds intervalToEnd = 0;
       
   158 
       
   159     TInt err = startStamp.SecondsFrom( aStamp, intervalToCur );
       
   160     if( KErrNone != err ) // Theoretical, 32bit integer was overflown
       
   161         {
       
   162         iRelative = KAlMaxRelative;
       
   163         return;
       
   164         }
       
   165 
       
   166     err = startStamp.SecondsFrom( endStamp, intervalToEnd );
       
   167     if( KErrNone != err ) // Theoretical, 32bit integer was overflown
       
   168         {
       
   169         iRelative = KAlMaxRelative;
       
   170         return;
       
   171         }
       
   172 
       
   173     // Floating point calculations used intentionally (relative timing model is
       
   174     // called rarely).
       
   175     TReal secsToCur = TReal( intervalToCur.Int() );
       
   176     TReal secsToEnd = TReal( intervalToEnd.Int() );
       
   177     if( secsToEnd == 0 )
       
   178         secsToEnd = 1;
       
   179     TReal posOnSlices = ( secsToCur / secsToEnd ) * TReal( iSlices ); //lint !e414 secsToEnd != 0
       
   180 
       
   181     TInt32 sliceNo;
       
   182     TReal relative;
       
   183 
       
   184     // Current slice is the integer part of posOnSlices
       
   185     err = Math::Int( sliceNo, posOnSlices );
       
   186     if( KErrNone != err )
       
   187         {
       
   188         iRelative = 0;
       
   189         return;
       
   190         }
       
   191 
       
   192     // Relative value is the posOnSlices fractional
       
   193     err = Math::Frac( relative, posOnSlices );
       
   194     if( KErrNone != err )
       
   195         {
       
   196         iRelative = 0;
       
   197         return;
       
   198         }
       
   199 
       
   200     // Reverse every second slice. Wrapping has effect only when there are
       
   201     // more than one slices.
       
   202     if( iWrap && ( sliceNo % 2 ) == 1 )
       
   203         {
       
   204         relative = 1.0 - relative;
       
   205         }
       
   206 
       
   207     // Convert to fixed point, relative is always in range [0.0, 1.0]
       
   208     iRelative = TInt( ( TReal(KAlMaxRelative) * relative ) + 0.5 );
       
   209     }//lint !e414 secsToEnd != 0 (for some reason lint nags about the whole method)
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CAknsAlTimingModelRelative::RelativeValue
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TUint32 CAknsAlTimingModelRelative::RelativeValue() const
       
   216     {
       
   217     return iRelative;
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CAknsAlTimingModelRelative::IsFinished
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 TBool CAknsAlTimingModelRelative::IsFinished() const
       
   225     {
       
   226     // Relative timing model is always infinite
       
   227     return EFalse;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CAknsAlTimingModelRelative::Begin
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CAknsAlTimingModelRelative::Begin()
       
   235     {
       
   236     // Time span type defines the starting point of a time span. System time or
       
   237     // time stamp defines the current poisition in time span -> begin is
       
   238     // irrelevant
       
   239     }
       
   240 
       
   241 // End of file
       
   242