S60 3rd Edition SDK for Symbian OS
Example Applications Guide

LandmarksLmCategoriesModel.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : LandmarksLmCategoriesModel.cpp
00004 *  Part of     : Landmark Reference Application
00005 *  Interface   : -
00006 *  Description : Implements the CLandmarksLmCategoriesModel class
00007 *  Version     : 1.0
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include <eikenv.h>
00018 #include <gulicon.h>
00019 #include <akniconarray.h>
00020 #include <avkon.mbg>
00021 
00022 #include <EPos_CPosLandmarkCategory.h>
00023 
00024 #include "LandmarksUtils.h"
00025 #include "LandmarksCommonData.h"
00026 #include "LandmarksLmCategoriesModel.h"
00027 #include "LandmarksApplicationEngine.h"
00028 
00029 // CONSTANTS
00030 _LIT(KItemFormat, "\t%S");
00031 
00032 // ============================ MEMBER FUNCTIONS ===============================
00033 
00034 // -----------------------------------------------------------------------------
00035 // CLandmarksLmCategoriesModel::CLandmarksLmCategoriesModel
00036 // C++ constructor can NOT contain any code, that
00037 // might leave.
00038 // -----------------------------------------------------------------------------
00039 //
00040 CLandmarksLmCategoriesModel::CLandmarksLmCategoriesModel(
00041     RArray<TPosLmItemId>& aMarkedCategories)
00042 :   iMarkedCategoryIds(aMarkedCategories)
00043         {
00044         }
00045 
00046 // -----------------------------------------------------------------------------
00047 // CLandmarksLmCategoriesModel::ConstructL
00048 // Symbian 2nd phase constructor can leave.
00049 // -----------------------------------------------------------------------------
00050 //
00051 void CLandmarksLmCategoriesModel::ConstructL(CLandmarksApplicationEngine& aEngine)
00052         {
00053     iCategories = aEngine.CategoriesL();
00054         }
00055 
00056 // -----------------------------------------------------------------------------
00057 // CLandmarksLmCategoriesModel::NewL
00058 // Two-phased constructor.
00059 // -----------------------------------------------------------------------------
00060 //
00061 CLandmarksLmCategoriesModel* CLandmarksLmCategoriesModel::NewL(
00062     CLandmarksApplicationEngine& aEngine,
00063     RArray<TPosLmItemId>& aMarkedCategories)
00064     {
00065     CLandmarksLmCategoriesModel* self = new (ELeave) 
00066         CLandmarksLmCategoriesModel(aMarkedCategories);
00067     CleanupStack::PushL(self);
00068     self->ConstructL(aEngine);
00069     CleanupStack::Pop(self);
00070     return self;
00071     }
00072 
00073 // Destructor
00074 CLandmarksLmCategoriesModel::~CLandmarksLmCategoriesModel()
00075         {
00076     if (iCategories)
00077         {
00078         iCategories->ResetAndDestroy();
00079             delete iCategories;
00080         }
00081         }
00082 
00083 // -----------------------------------------------------------------------------
00084 // CLandmarksLmCategoriesModel::SelectCategoriesL
00085 // 
00086 // (other items were commented in a header).
00087 // -----------------------------------------------------------------------------
00088 //
00089 void CLandmarksLmCategoriesModel::SelectCategoriesL(
00090     CArrayFixFlat<TInt>& aSelectedIndexes) 
00091     {
00092     // clear current list of selected categories
00093     iMarkedCategoryIds.Reset();
00094 
00095     for (TInt i = 0; i < aSelectedIndexes.Count(); i++)
00096         {
00097         TInt categoryIndex = aSelectedIndexes[i];
00098 
00099         // Fetch the id of the selected item.
00100         __ASSERT_ALWAYS((categoryIndex >= 0) && (categoryIndex < iCategories->Count()),
00101                         LandmarksUtils::Panic(KErrGeneral));
00102         TPosLmItemId selectedCategoryId = (*iCategories)[categoryIndex]->CategoryId();
00103 
00104         User::LeaveIfError(iMarkedCategoryIds.Append(selectedCategoryId));
00105         }
00106     }
00107 
00108 // -----------------------------------------------------------------------------
00109 // CLandmarksLmCategoriesModel::SelectedCategoriesL
00110 // 
00111 // (other items were commented in a header).
00112 // -----------------------------------------------------------------------------
00113 //
00114 CArrayFixFlat<TInt>* CLandmarksLmCategoriesModel::SelectedCategoriesL() 
00115     {
00116     CArrayFixFlat<TInt>* selectedItems = new (ELeave) CArrayFixFlat<TInt>(KGranularity);
00117     CleanupStack::PushL(selectedItems);
00118 
00119     for (TInt i = 0; i < iCategories->Count(); i++)
00120         {
00121         TPosLmItemId categoryId = (*iCategories)[i]->CategoryId();
00122 
00123                 TInt index = iMarkedCategoryIds.Find(categoryId);
00124         if (KErrNotFound != index)
00125                 {
00126                 selectedItems->AppendL(i);
00127                 }
00128         }
00129 
00130         CleanupStack::Pop(selectedItems);        
00131         return selectedItems;        
00132     }
00133 
00134 // -----------------------------------------------------------------------------
00135 // CLandmarksLmCategoriesModel::FormattedCategoryNamesL
00136 // 
00137 // (other items were commented in a header).
00138 // -----------------------------------------------------------------------------
00139 //
00140 CDesCArray* CLandmarksLmCategoriesModel::FormattedCategoryNamesL()
00141     {
00142     // Granularity is set to be 8.
00143     CDesCArrayFlat* listItems = new (ELeave) CDesCArrayFlat(KGranularity);
00144     CleanupStack::PushL(listItems);
00145 
00146     for (TInt i = 0; i < iCategories->Count(); i++)
00147         {
00148         // Create buffer that will contain the name and mark status. 3 extra 
00149         // chars for tab and icon index.
00150         const TInt KExtraChars = 3; 
00151         TPtrC categoryName;
00152         User::LeaveIfError((*iCategories)[i]->GetCategoryName(categoryName));
00153         HBufC* formattedCategoryName = HBufC::NewLC(categoryName.Length() + KExtraChars);
00154 
00155         // format string with category
00156         formattedCategoryName->Des().Format(KItemFormat, &categoryName);
00157 
00158         listItems->AppendL(*formattedCategoryName);
00159         CleanupStack::PopAndDestroy(formattedCategoryName);
00160         }
00161 
00162     CleanupStack::Pop(listItems);
00163 
00164     iListItems = listItems;
00165     return iListItems;
00166     }
00167 
00168 // End of File
00169 

© Nokia 2006

Back to top