S60 3rd Edition SDK for Symbian OS
Example Applications Guide

LandmarksCategoriesModel.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : LandmarksCategoriesModel.cpp
00004 *  Part of     : Landmark Reference Application
00005 *  Interface   : -
00006 *  Description : Implements the CLandmarksCategoriesModel class and 
00007                                         some help functions
00008 *  Version     : 1.0
00009 *
00010 *  Copyright (c) 2004-2006 Nokia Corporation.
00011 *  This material, including documentation and any related 
00012 *  computer programs, is protected by copyright controlled by 
00013 *  Nokia Corporation.
00014 * ==============================================================================
00015 */
00016 
00017 // INCLUDE FILES
00018 #include <eikenv.h>
00019 #include <gulicon.h>
00020 #include <AknsUtils.h>
00021 #include <AknsItemID.h>
00022 
00023 #include <EPos_CPosLandmarkCategory.h>
00024 
00025 #include <LmRefApp.mbg>
00026 #include "LandmarksCommonData.h"
00027 #include "LandmarksCategoriesModel.h"
00028 
00029 // ============================= LOCAL FUNCTIONS ===============================
00030 
00031 // -----------------------------------------------------------------------------
00032 // DefaultIconIndex returns the icon index of the appropriate default icon.
00033 // User defined categories have different default icons than global ones.
00034 //
00035 // Returns: an icon index
00036 // Param:   aCategory the category to investigate
00037 // -----------------------------------------------------------------------------
00038 //
00039 TInt DefaultIconIndex(CPosLandmarkCategory& aCategory)
00040     {
00041     // Global categories have different default icon than user-defined.
00042     TBool isGlobal = aCategory.GlobalCategory() != KPosLmNullGlobalCategory;
00043 
00044     if (isGlobal)
00045         {
00046         return KDefaultGlobalIconIndex;
00047         }
00048     else
00049         {
00050         return KDefaultUserIconIndex;
00051         }
00052     }
00053 
00054 // -----------------------------------------------------------------------------
00055 // ResetAndDestroyIcons
00056 // Resets and destroys an array of icons. This function is used as a 
00057 // TCleanupOperation.
00058 // Params: aAny: The icons array to reset ands destroy
00059 // -----------------------------------------------------------------------------
00060 //
00061 void ResetAndDestroyIcons(TAny* aAny)
00062     {
00063     CIconList* icons = reinterpret_cast <CIconList*> (aAny);
00064     for (TInt i = 0; i < icons->Count(); i++)
00065         {
00066         delete &icons[i];
00067         }
00068     icons->Reset();
00069     }
00070 
00071 // ============================ MEMBER FUNCTIONS ===============================
00072 
00073 // -----------------------------------------------------------------------------
00074 // CLandmarksCategoriesModel::CLandmarksCategoriesModel
00075 // C++ constructor can NOT contain any code, that
00076 // might leave.
00077 // -----------------------------------------------------------------------------
00078 //
00079 CLandmarksCategoriesModel::CLandmarksCategoriesModel()
00080         {
00081         }
00082 
00083 // -----------------------------------------------------------------------------
00084 // CLandmarksCategoriesModel::ConstructL
00085 // Symbian 2nd phase constructor can leave.
00086 // -----------------------------------------------------------------------------
00087 //
00088 void CLandmarksCategoriesModel::ConstructL()
00089         {
00090         // The list box model uses MDesCArray interface class to take in text items.
00091         iListItems = new(ELeave) CDesCArraySeg(KGranularity);
00092         }
00093 
00094 // -----------------------------------------------------------------------------
00095 // CLandmarksCategoriesModel::NewL
00096 // Two-phased constructor.
00097 // -----------------------------------------------------------------------------
00098 //
00099 CLandmarksCategoriesModel* CLandmarksCategoriesModel::NewL()
00100     {
00101     CLandmarksCategoriesModel* self = 
00102         new (ELeave) CLandmarksCategoriesModel();
00103     CleanupStack::PushL(self);
00104     self->ConstructL();
00105     CleanupStack::Pop(self);
00106     return self;
00107     }
00108 
00109 // Destructor
00110 CLandmarksCategoriesModel::~CLandmarksCategoriesModel()
00111         {
00112         delete iListItems;
00113         }
00114 
00115 // -----------------------------------------------------------------------------
00116 // CLandmarksCategoriesModel::SetItemIds
00117 // 
00118 // (other items were commented in a header).
00119 // -----------------------------------------------------------------------------
00120 //
00121 void CLandmarksCategoriesModel::SetItemIds(RArray<TPosLmItemId>* aItemIds)
00122     {
00123     iItemIds = aItemIds;
00124 
00125     // Empty descriptor model
00126     iListItems->Reset();
00127     }
00128 
00129 // -----------------------------------------------------------------------------
00130 // CLandmarksCategoriesModel::ItemList
00131 // 
00132 // (other items were commented in a header).
00133 // -----------------------------------------------------------------------------
00134 //
00135 CDesCArraySeg* CLandmarksCategoriesModel::ItemList()
00136     {
00137     return iListItems;
00138     }
00139 
00140 // -----------------------------------------------------------------------------
00141 // CLandmarksCategoriesModel::SetCurrentItem
00142 // 
00143 // (other items were commented in a header).
00144 // -----------------------------------------------------------------------------
00145 //
00146 void CLandmarksCategoriesModel::SetCurrentItem(TInt aIndex) 
00147     {
00148     iCurrentItem = aIndex;
00149     }
00150 
00151 // -----------------------------------------------------------------------------
00152 // CLandmarksCategoriesModel::CurrentItemId
00153 // 
00154 // (other items were commented in a header).
00155 // -----------------------------------------------------------------------------
00156 //
00157 TPosLmItemId CLandmarksCategoriesModel::CurrentItemId()
00158     {
00159     if (iCurrentItem < 0 || iCurrentItem > iItemIds->Count() - 1)
00160         {
00161         return KPosLmNullItemId;
00162         }
00163 
00164     return (*iItemIds)[iCurrentItem];
00165     }
00166 
00167 // -----------------------------------------------------------------------------
00168 // CLandmarksCategoriesModel::CreateIconListL
00169 // 
00170 // (other items were commented in a header).
00171 // -----------------------------------------------------------------------------
00172 //
00173 CIconList* CLandmarksCategoriesModel::CreateIconListL()
00174     {
00175     CIconList* iconList = new (ELeave) CArrayPtrFlat<CGulIcon>(KGranularity);
00176     CleanupStack::PushL(TCleanupItem(ResetAndDestroyIcons, iconList));
00177     
00178     // Create the global category icon consisting of its image and mask.
00179     TAknsItemID aknsItemId ={0,0};
00180     CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
00181         KLandmarksMbmFileName, 
00182         EMbmLmrefappCategory_global, 
00183         EMbmLmrefappCategory_global_mask);
00184     CleanupStack::PushL(icon);
00185     icon->SetBitmapsOwnedExternally(EFalse); 
00186     iconList->AppendL(icon);
00187     CleanupStack::Pop(icon);
00188 
00189     // Create the user defined category icon consisting of its image and mask.
00190     icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
00191         KLandmarksMbmFileName, 
00192         EMbmLmrefappCategory_user, 
00193         EMbmLmrefappCategory_user_mask);
00194     CleanupStack::PushL(icon);
00195     icon->SetBitmapsOwnedExternally(EFalse); 
00196     iconList->AppendL(icon);
00197     CleanupStack::Pop(icon);
00198     
00199     CleanupStack::Pop(); // iconList
00200 
00201     // initialze iIconList. We don't need to delete any possible previous 
00202     // iIconList since ownership is transferred to calling object.
00203     iIconList = iconList;
00204 
00205     return iconList;
00206     }
00207 
00208 // -----------------------------------------------------------------------------
00209 // CLandmarksCategoriesModel::RepopulateModelL
00210 // Refreshes a part of the model. Those categories that have been read from the 
00211 // database overwrites old categories. The model is refreshed incrementally 
00212 // from top to bottom.
00213 // (other items were commented in a header).
00214 // -----------------------------------------------------------------------------
00215 //
00216 void CLandmarksCategoriesModel::RepopulateModelL(
00217     CArrayPtr<CPosLandmarkCategory>* aCategories)
00218     {
00219     TInt nrOfReadItems = aCategories->Count();
00220 
00221     // Format and insert the read categories
00222     for (TInt i = 0; i < nrOfReadItems; i++)
00223         {
00224         // Format name and icon
00225         CPosLandmarkCategory* category = (*aCategories)[i];
00226         HBufC* formattedName = FormatListItemLC(*category);
00227 
00228         // Insert formatted descriptor into model
00229         iListItems->AppendL(*formattedName);
00230 
00231         CleanupStack::PopAndDestroy(formattedName);
00232         }
00233     }
00234 
00235 // -----------------------------------------------------------------------------
00236 // CLandmarksCategoriesModel::FormatListItemLC
00237 // Formats a list item to the following format "X\tcategoryName"
00238 // where X = icon index
00239 // (other items were commented in a header).
00240 // -----------------------------------------------------------------------------
00241 //
00242 HBufC* CLandmarksCategoriesModel::FormatListItemLC(CPosLandmarkCategory& aCategory)
00243     {
00244     // Extract name
00245     TPtrC name;
00246     aCategory.GetCategoryName(name);
00247     // 10 extra chars for icon index and column separator
00248     const TInt KExtraChars = 10;
00249     HBufC* formattedNameBuf = HBufC::NewLC(name.Length() + KExtraChars);
00250     TPtr formattedNamePtr = formattedNameBuf->Des();
00251 
00252     // Extract icon info
00253     TPtrC mbmIconFile;
00254     TInt iconIndex, maskIndex;
00255     TInt res = aCategory.GetIcon(mbmIconFile, iconIndex, maskIndex);
00256 
00257     // Format list item
00258     if (res == KErrNone)
00259         {
00260         // Create icon and append it to icon array. 
00261         TRAPD(err, AppendIconL(mbmIconFile, iconIndex, maskIndex));
00262         if (err == KErrNone)
00263             {
00264             // Append the array index where the icon is appended
00265             formattedNamePtr.AppendNum(iIconList->Count() - 1);
00266             }
00267         else // Unable to create icon, use appropriate default icon.
00268             {
00269             formattedNamePtr.AppendNum(DefaultIconIndex(aCategory));
00270             }
00271         }
00272     else // res == KErrNotFound
00273         {
00274         formattedNamePtr.AppendNum(DefaultIconIndex(aCategory));
00275         }
00276     formattedNamePtr.Append(KTab);
00277     formattedNamePtr.Append(name);
00278 
00279     return formattedNameBuf;
00280     }
00281 
00282 // -----------------------------------------------------------------------------
00283 // CLandmarksCategoriesModel::AppendIconL
00284 // 
00285 // (other items were commented in a header).
00286 // -----------------------------------------------------------------------------
00287 //
00288 void CLandmarksCategoriesModel::AppendIconL(
00289     const TDesC& aMbmFile, 
00290     TInt aIconOffset, 
00291     TInt aMaskOffset)
00292     {
00293     TAknsItemID aknsItemId = {0,0};
00294     CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
00295         aMbmFile, 
00296         aIconOffset, 
00297         aMaskOffset);
00298     CleanupStack::PushL(icon);
00299     icon->SetBitmapsOwnedExternally(EFalse);
00300     iIconList->AppendL(icon);
00301     CleanupStack::Pop(icon);
00302     }
00303 
00304 // End of File

© Nokia 2006

Back to top