landmarksui/uicontrols/src/CLmkDistanceEditor.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2002 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:    LandmarksUi Content File -
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include <eikmfne.h>
       
    27 
       
    28 #include <eikenv.h>
       
    29 #include <stringloader.h>
       
    30 #include <e32math.h>
       
    31 #include <AknUtils.h>
       
    32 
       
    33 #include <LmkUi.rsg>
       
    34 #include "landmarks.hrh"
       
    35 #include "LmkConsts.h"
       
    36 
       
    37 #include "CLmkMfneFloat.h"
       
    38 #include "CLmkDistanceEditor.h"
       
    39 #include <AknBidiTextUtils.h>
       
    40 
       
    41 // CONSTANTS
       
    42 /// Unnamed namespace for local definitions
       
    43 namespace {
       
    44 
       
    45 const TInt KKeyToMatchEmptyUnits = 0;
       
    46 
       
    47 const TInt KKeyToMatchMeters = 109;
       
    48 const TInt KKeyToMatchKilometers = 75;
       
    49 const TInt KKeyToMatchFeet = 87;
       
    50 const TInt KKeyToMatchMiles = 77;
       
    51 
       
    52 const TInt KNumMetricUnitSymbols = 2;
       
    53 const TInt KNumImperialUnitSymbols = 2;
       
    54 
       
    55 const TInt KMaxNumSignificantDigits = 7;  // DDDDD.DD
       
    56 const TInt KMaxNumDecimalDigits = 2;
       
    57 const TInt KNumFields = 2; // float, Units
       
    58 }  // namespace
       
    59 
       
    60 // ============================ MEMBER FUNCTIONS ==============================
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // CLmkDistanceEditor::CLmkDistanceEditor
       
    64 // C++ default constructor can NOT contain any code, that
       
    65 // might leave.
       
    66 // ----------------------------------------------------------------------------
       
    67 //
       
    68 CLmkDistanceEditor::CLmkDistanceEditor()
       
    69     {
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // CLmkDistanceEditor::ConstructL
       
    74 // Symbian 2nd phase constructor can leave.
       
    75 // ----------------------------------------------------------------------------
       
    76 //
       
    77 void CLmkDistanceEditor::ConstructL(
       
    78     TReal32 aMinimumValue,
       
    79     TReal32 aMaximumValue,
       
    80     TReal32 aInitialValue)
       
    81     {
       
    82     iMinimumValue = aMinimumValue;
       
    83     iMaximumValue = aMaximumValue;
       
    84 
       
    85     // create float field
       
    86     BaseConstructL(KNumFields);
       
    87 
       
    88     if (!AknLayoutUtils::LayoutMirrored())
       
    89         {
       
    90         ConstructValueFieldL(KMaxNumSignificantDigits, KMaxNumDecimalDigits);
       
    91         ConstructUnitFieldL();
       
    92         }
       
    93     else
       
    94         {
       
    95         ConstructUnitFieldL();
       
    96         ConstructValueFieldL(KMaxNumSignificantDigits, KMaxNumDecimalDigits);
       
    97         }
       
    98 
       
    99     // Init value
       
   100     SetUnit(DefaultUnit());
       
   101     UpdateValueLimits();
       
   102     SetNumber(aInitialValue);
       
   103     ShowUnits(!Math::IsNaN(aInitialValue));
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // CLmkDistanceEditor::ConstructUnitFieldL
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 void CLmkDistanceEditor::ConstructUnitFieldL()
       
   111     {
       
   112     TLocale locale;
       
   113 
       
   114     //create distance types
       
   115     TInt numUnitsymbols = KNumMetricUnitSymbols + 1; // +empty Units
       
   116     if (locale.UnitsGeneral() == EUnitsImperial)
       
   117         {
       
   118         numUnitsymbols += KNumImperialUnitSymbols;
       
   119         }
       
   120 
       
   121     iUnitsField = CEikMfneSymbol::NewL(numUnitsymbols);
       
   122 
       
   123     // create Units field
       
   124     HBufC* type = NULL;
       
   125 
       
   126     // add empty Units
       
   127     type = HBufC::NewLC(1);
       
   128     iUnitsField->AddSymbolicItem(
       
   129         CEikMfneSymbol::CItem::NewL(EUnitNone, KKeyToMatchEmptyUnits, type ),
       
   130         ETrue);
       
   131     CleanupStack::Pop(type);
       
   132 
       
   133     // add metric Units
       
   134     type = StringLoader::LoadLC( R_LMK_DISTANCE_EDITOR_TYPE_M, iCoeEnv );
       
   135     iUnitsField->AddSymbolicItem(
       
   136         CEikMfneSymbol::CItem::NewL( EUnitMeters, KKeyToMatchMeters, type ),
       
   137         EFalse);
       
   138     CleanupStack::Pop(type);
       
   139 
       
   140     type = StringLoader::LoadLC( R_LMK_DISTANCE_EDITOR_TYPE_KM, iCoeEnv );
       
   141     iUnitsField->AddSymbolicItem(
       
   142         CEikMfneSymbol::CItem::NewL( EUnitKilometers, KKeyToMatchKilometers, type ),
       
   143         EFalse);
       
   144     CleanupStack::Pop(type);
       
   145 
       
   146     // Add imperial Units for those who use them
       
   147     if ( locale.UnitsGeneral() == EUnitsImperial )
       
   148         {
       
   149         type = StringLoader::LoadLC( R_LMK_DISTANCE_EDITOR_TYPE_FT, iCoeEnv );
       
   150         iUnitsField->AddSymbolicItem(
       
   151             CEikMfneSymbol::CItem::NewL( EUnitFeet, KKeyToMatchFeet, type ),
       
   152             EFalse);
       
   153         CleanupStack::Pop(type);
       
   154 
       
   155         type = StringLoader::LoadLC( R_LMK_DISTANCE_EDITOR_TYPE_MI, iCoeEnv );
       
   156         iUnitsField->AddSymbolicItem(
       
   157             CEikMfneSymbol::CItem::NewL( EUnitMiles, KKeyToMatchMiles, type ),
       
   158             EFalse);
       
   159         CleanupStack::Pop(type);
       
   160         }
       
   161 
       
   162     AddField(iUnitsField);
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // CLmkDistanceEditor::~CLmkDistanceEditor
       
   167 // ----------------------------------------------------------------------------
       
   168 //
       
   169 CLmkDistanceEditor::~CLmkDistanceEditor()
       
   170     {
       
   171     }
       
   172 
       
   173 // ----------------------------------------------------
       
   174 // CLmkDistanceEditor::Number
       
   175 // ----------------------------------------------------
       
   176 //
       
   177 TReal CLmkDistanceEditor::Number() const
       
   178     {
       
   179     return UnitsToMeters(CLmkFloatEditor::Number(), Unit());
       
   180     }
       
   181 
       
   182 // ----------------------------------------------------
       
   183 // CLmkDistanceEditor::SetNumber
       
   184 // ----------------------------------------------------
       
   185 //
       
   186 void CLmkDistanceEditor::SetNumber(TReal aNumber)
       
   187     {
       
   188     ShowUnits(!Math::IsNaN(aNumber));
       
   189     CLmkFloatEditor::SetNumber(MetersToUnits(aNumber, Unit()));
       
   190     }
       
   191 
       
   192 // ----------------------------------------------------
       
   193 // CLmkDistanceEditor::SetUnit
       
   194 // ----------------------------------------------------
       
   195 //
       
   196 void CLmkDistanceEditor::SetUnit(TDistanceUnit aUnitId)
       
   197     {
       
   198     iUnitsField->SetCurrentSymbolicItemToId(aUnitId);
       
   199     UpdateValueLimits();
       
   200     }
       
   201 
       
   202 // ----------------------------------------------------
       
   203 // CLmkDistanceEditor::Unit
       
   204 // ----------------------------------------------------
       
   205 //
       
   206 CLmkDistanceEditor::TDistanceUnit CLmkDistanceEditor::Unit() const
       
   207     {
       
   208     return (TDistanceUnit) iUnitsField->IdOfCurrentSymbolicItem();
       
   209     }
       
   210 
       
   211 // ----------------------------------------------------
       
   212 // CLmkDistanceEditor::DefaultUnit
       
   213 // ----------------------------------------------------
       
   214 //
       
   215 CLmkDistanceEditor::TDistanceUnit CLmkDistanceEditor::DefaultUnit() const
       
   216     {
       
   217     return EUnitMeters;
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------
       
   221 // CLmkDistanceEditor::UpdateValueLimits
       
   222 // ----------------------------------------------------
       
   223 //
       
   224 void CLmkDistanceEditor::UpdateValueLimits()
       
   225     {
       
   226     if (Unit() != EUnitNone)
       
   227         {
       
   228         SetLimits(
       
   229             MetersToUnits(iMinimumValue, Unit()),
       
   230             MetersToUnits(iMaximumValue, Unit()));
       
   231         }
       
   232     }
       
   233 
       
   234 // ----------------------------------------------------
       
   235 // CLmkDistanceEditor::MetersToUnits
       
   236 // ----------------------------------------------------
       
   237 //
       
   238 TReal32 CLmkDistanceEditor::MetersToUnits(
       
   239     TReal32 aMeters,
       
   240     TDistanceUnit aToUnits) const
       
   241     {
       
   242     TRealX nan;
       
   243     nan.SetNaN();
       
   244 
       
   245     if (Math::IsNaN(aMeters))
       
   246         {
       
   247         return nan;
       
   248         }
       
   249 
       
   250     switch (aToUnits)
       
   251         {
       
   252         case EUnitMeters:
       
   253             return aMeters;
       
   254         case EUnitKilometers:
       
   255             return aMeters / KKilometerToMeter;
       
   256         case EUnitMiles:
       
   257             return aMeters / KMileToMeter;
       
   258         case EUnitFeet:
       
   259             return aMeters * KMetreToFoot;
       
   260         default:
       
   261             return nan;
       
   262         }
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------
       
   266 // CLmkDistanceEditor::UnitsToMeters
       
   267 // ----------------------------------------------------
       
   268 //
       
   269 TReal32 CLmkDistanceEditor::UnitsToMeters(
       
   270     TReal32 aValue,
       
   271     TDistanceUnit aFromUnits) const
       
   272     {
       
   273     TRealX nan;
       
   274     nan.SetNaN();
       
   275 
       
   276     if (Math::IsNaN(aValue))
       
   277         {
       
   278         return nan;
       
   279         }
       
   280 
       
   281     switch (aFromUnits)
       
   282         {
       
   283         case EUnitMeters:
       
   284             return aValue;
       
   285         case EUnitKilometers:
       
   286             return aValue * KKilometerToMeter;
       
   287         case EUnitMiles:
       
   288             return aValue * KMileToMeter;
       
   289         case EUnitFeet:
       
   290             return aValue / KMetreToFoot;
       
   291         default:
       
   292             return nan;
       
   293         }
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------
       
   297 // CLmkDistanceEditor::ShowUnits
       
   298 // ----------------------------------------------------
       
   299 //
       
   300 void CLmkDistanceEditor::ShowUnits(TBool aVisible)
       
   301     {
       
   302     if (aVisible)
       
   303         {
       
   304         if (Unit() == EUnitNone)
       
   305             {
       
   306             SetUnit(DefaultUnit());
       
   307             }
       
   308         }
       
   309     else
       
   310         {
       
   311         SetUnit(EUnitNone);
       
   312         }
       
   313     }
       
   314 
       
   315 // ----------------------------------------------------
       
   316 // CLmkDistanceEditor::HandleEnter
       
   317 // ----------------------------------------------------
       
   318 //
       
   319 void CLmkDistanceEditor::HandleEnter()
       
   320     {
       
   321     ShowUnits(ETrue);
       
   322     CLmkFloatEditor::HandleEnter();
       
   323     }
       
   324 
       
   325 // ----------------------------------------------------
       
   326 // CLmkDistanceEditor::HandleExit
       
   327 // ----------------------------------------------------
       
   328 //
       
   329 void CLmkDistanceEditor::HandleExit()
       
   330     {
       
   331     ShowUnits(!IsNan());
       
   332     CLmkFloatEditor::HandleExit();
       
   333     }
       
   334 
       
   335 // ----------------------------------------------------
       
   336 // CLmkDistanceEditor::HandleCustomFieldChanged
       
   337 // ----------------------------------------------------
       
   338 //
       
   339 void CLmkDistanceEditor::HandleCustomFieldChanged(TInt aFieldIndex)
       
   340     {
       
   341     if (Field(aFieldIndex) == iUnitsField)
       
   342         {
       
   343         if (Unit() == EUnitNone)
       
   344             {
       
   345             // cycle from empty unit
       
   346             SetUnit(DefaultUnit());
       
   347             }
       
   348         else
       
   349             {
       
   350             UpdateValueLimits();
       
   351             }
       
   352         }
       
   353     }
       
   354 
       
   355 //  End of File