|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : LandmarksModel.cpp 00004 * Part of : Landmark Reference Application 00005 * Interface : - 00006 * Description : Implements the CLandmarksModel 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 <AknsUtils.h> 00020 #include <AknsItemID.h> 00021 00022 #include <EPos_CPosLandmark.h> 00023 00024 #include <LmRefApp.mbg> 00025 #include "LandmarksCommonData.h" 00026 #include "LandmarksModel.h" 00027 00028 // CONSTANTS 00029 const TInt KDefaultIconIndex = 0; 00030 00031 // ============================ MEMBER FUNCTIONS =============================== 00032 00033 // ----------------------------------------------------------------------------- 00034 // CLandmarksModel::CLandmarksModel 00035 // C++ constructor can NOT contain any code, that 00036 // might leave. 00037 // ----------------------------------------------------------------------------- 00038 // 00039 CLandmarksModel::CLandmarksModel() 00040 { 00041 } 00042 00043 // ----------------------------------------------------------------------------- 00044 // CLandmarksModel::ConstructL 00045 // Symbian 2nd phase constructor can leave. 00046 // ----------------------------------------------------------------------------- 00047 // 00048 void CLandmarksModel::ConstructL() 00049 { 00050 // The list box model uses MDesCArray interface class to take in text items. 00051 iListItems = new (ELeave) CDesCArraySeg(KGranularity); 00052 } 00053 00054 // ----------------------------------------------------------------------------- 00055 // CLandmarksModel::NewL 00056 // Two-phased constructor. 00057 // ----------------------------------------------------------------------------- 00058 // 00059 CLandmarksModel* CLandmarksModel::NewL() 00060 { 00061 CLandmarksModel* self = new (ELeave) CLandmarksModel(); 00062 CleanupStack::PushL(self); 00063 self->ConstructL(); 00064 CleanupStack::Pop(self); 00065 return self; 00066 } 00067 00068 // Destructor 00069 CLandmarksModel::~CLandmarksModel() 00070 { 00071 delete iListItems; 00072 } 00073 00074 // ----------------------------------------------------------------------------- 00075 // CLandmarksModel::SetItemIds 00076 // 00077 // (other items were commented in a header). 00078 // ----------------------------------------------------------------------------- 00079 // 00080 void CLandmarksModel::SetItemIds(RArray<TPosLmItemId>* aItemIds) 00081 { 00082 iItemIds = aItemIds; 00083 00084 // Empty descriptor model 00085 iListItems->Reset(); 00086 } 00087 00088 // ----------------------------------------------------------------------------- 00089 // CLandmarksModel::ItemList 00090 // 00091 // (other items were commented in a header). 00092 // ----------------------------------------------------------------------------- 00093 // 00094 CDesCArraySeg* CLandmarksModel::ItemList() 00095 { 00096 return iListItems; 00097 } 00098 00099 // ----------------------------------------------------------------------------- 00100 // CLandmarksModel::SetCurrentItem 00101 // 00102 // (other items were commented in a header). 00103 // ----------------------------------------------------------------------------- 00104 // 00105 void CLandmarksModel::SetCurrentItem(TInt aIndex) 00106 { 00107 iCurrentItem = aIndex; 00108 } 00109 00110 // ----------------------------------------------------------------------------- 00111 // CLandmarksModel::CurrentItemId 00112 // 00113 // (other items were commented in a header). 00114 // ----------------------------------------------------------------------------- 00115 // 00116 TPosLmItemId CLandmarksModel::CurrentItemId() 00117 { 00118 if (!iItemIds || iCurrentItem < 0 || 00119 iCurrentItem > iItemIds->Count() - 1) 00120 { 00121 return KPosLmNullItemId; 00122 } 00123 00124 return (*iItemIds)[iCurrentItem]; 00125 } 00126 00127 // ----------------------------------------------------------------------------- 00128 // CLandmarksModel::CreateIconListL 00129 // 00130 // (other items were commented in a header). 00131 // ----------------------------------------------------------------------------- 00132 // 00133 CIconList* CLandmarksModel::CreateIconListL() 00134 { 00135 CIconList* iconList = 00136 (CIconList*) new (ELeave) CArrayPtrFlat<CGulIcon>(KGranularity); 00137 CleanupStack::PushL(iconList); 00138 00139 // Create the default landmark icon consisting of its image and mask 00140 TAknsItemID aknsItemId = {0,0}; 00141 CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId, 00142 KLandmarksMbmFileName, 00143 EMbmLmrefappDefault_lm, 00144 EMbmLmrefappDefault_lm_mask); 00145 CleanupStack::PushL(icon); 00146 icon->SetBitmapsOwnedExternally(EFalse); 00147 iconList->AppendL(icon); 00148 CleanupStack::Pop(2, iconList); 00149 00150 // initialze iIconList. We don't need to delete any possible previous 00151 // iIconList since ownership is transferred to calling object. 00152 iIconList = iconList; 00153 00154 return iIconList; 00155 } 00156 00157 // ----------------------------------------------------------------------------- 00158 // CLandmarksModel::RepopulateModelL 00159 // 00160 // (other items were commented in a header). 00161 // ----------------------------------------------------------------------------- 00162 // 00163 void CLandmarksModel::RepopulateModelL( 00164 CArrayPtr<CPosLandmark>* aLandmarks) 00165 { 00166 TInt nrOfReadItems = aLandmarks->Count(); 00167 00168 // Format and insert the read landmarks 00169 for (TInt i = 0; i < nrOfReadItems; i++) 00170 { 00171 CPosLandmark* landmark = (*aLandmarks)[i]; 00172 HBufC* formattedListItem = FormatListItemLC(*landmark); 00173 iListItems->AppendL(*formattedListItem); 00174 CleanupStack::PopAndDestroy(formattedListItem); 00175 } 00176 } 00177 00178 // ----------------------------------------------------------------------------- 00179 // CLandmarksModel::FormatListItemLC 00180 // Formats a list item to the following format "X\tlandmarkName\t" 00181 // where X = icon index 00182 // (other items were commented in a header). 00183 // ----------------------------------------------------------------------------- 00184 // 00185 HBufC* CLandmarksModel::FormatListItemLC(CPosLandmark& aLandmark) 00186 { 00187 // Extract name 00188 TPtrC name; 00189 aLandmark.GetLandmarkName(name); 00190 // 10 extra chars for icon index and column separator 00191 const TInt KExtraChars = 10; 00192 HBufC* formattedNameBuf = HBufC::NewLC(name.Length() + KExtraChars); 00193 TPtr formattedNamePtr = formattedNameBuf->Des(); 00194 00195 // Extract icon info 00196 TPtrC mbmIconFile; 00197 TInt iconIndex, maskIndex; 00198 TInt res = aLandmark.GetIcon(mbmIconFile, iconIndex, maskIndex); 00199 00200 // Format list item 00201 if (res == KErrNone) 00202 { 00203 // Create icon and append it to icon array. 00204 TRAPD(err, AppendIconL(mbmIconFile, iconIndex, maskIndex)); 00205 if (err == KErrNone) 00206 { 00207 // Append the array index where the icon is appended 00208 formattedNamePtr.AppendNum(iIconList->Count() - 1); 00209 } 00210 else 00211 { 00212 formattedNamePtr.AppendNum(KDefaultIconIndex); 00213 } 00214 } 00215 else // res == KErrNotFound 00216 { 00217 formattedNamePtr.AppendNum(KDefaultIconIndex); 00218 } 00219 formattedNamePtr.Append(KTab); 00220 formattedNamePtr.Append(name); 00221 00222 return formattedNameBuf; 00223 } 00224 00225 // ----------------------------------------------------------------------------- 00226 // CLandmarksModel::AppendIconL 00227 // 00228 // (other items were commented in a header). 00229 // ----------------------------------------------------------------------------- 00230 // 00231 void CLandmarksModel::AppendIconL( 00232 const TDesC& aMbmFile, 00233 TInt aIconOffset, 00234 TInt aMaskOffset) 00235 { 00236 TAknsItemID aknsItemId = {0,0}; 00237 CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId, 00238 aMbmFile, 00239 aIconOffset, 00240 aMaskOffset); 00241 CleanupStack::PushL(icon); 00242 icon->SetBitmapsOwnedExternally(EFalse); 00243 iIconList->AppendL(icon); 00244 CleanupStack::Pop(icon); 00245 } 00246 00247 // End of File 00248
|
© Nokia 2006 |