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