|
1 /* |
|
2 * Copyright (c) 2004-2005 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: Implements the CLandmarksModel class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <eikenv.h> |
|
21 #include <gulicon.h> |
|
22 #include <AknsUtils.h> |
|
23 #include <AknsItemID.h> |
|
24 |
|
25 #include <EPos_CPosLandmark.h> |
|
26 |
|
27 #include <lmrefapp.mbg> |
|
28 #include "LandmarksCommonData.h" |
|
29 #include "LandmarksModel.h" |
|
30 |
|
31 |
|
32 const TInt KDefaultIconIndex = 0; |
|
33 |
|
34 // ============================ MEMBER FUNCTIONS =============================== |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // ----------------------------------------------------------------------------- |
|
38 // |
|
39 CLandmarksModel::CLandmarksModel() |
|
40 { |
|
41 } |
|
42 |
|
43 // ----------------------------------------------------------------------------- |
|
44 // ----------------------------------------------------------------------------- |
|
45 // |
|
46 void CLandmarksModel::ConstructL() |
|
47 { |
|
48 // The list box model uses MDesCArray interface class to take in text items. |
|
49 iListItems = new (ELeave) CDesCArraySeg(KGranularity); |
|
50 } |
|
51 |
|
52 // ----------------------------------------------------------------------------- |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 CLandmarksModel* CLandmarksModel::NewL() |
|
56 { |
|
57 CLandmarksModel* self = new (ELeave) CLandmarksModel(); |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(); |
|
60 CleanupStack::Pop(self); |
|
61 return self; |
|
62 } |
|
63 |
|
64 // ----------------------------------------------------------------------------- |
|
65 // ----------------------------------------------------------------------------- |
|
66 // |
|
67 CLandmarksModel::~CLandmarksModel() |
|
68 { |
|
69 delete iListItems; |
|
70 } |
|
71 |
|
72 // ----------------------------------------------------------------------------- |
|
73 // ----------------------------------------------------------------------------- |
|
74 // |
|
75 void CLandmarksModel::SetItemIds(RArray<TPosLmItemId>* aItemIds) |
|
76 { |
|
77 iItemIds = aItemIds; |
|
78 |
|
79 // Empty descriptor model |
|
80 iListItems->Reset(); |
|
81 } |
|
82 |
|
83 // ----------------------------------------------------------------------------- |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 CDesCArraySeg* CLandmarksModel::ItemList() |
|
87 { |
|
88 return iListItems; |
|
89 } |
|
90 |
|
91 // ----------------------------------------------------------------------------- |
|
92 // ----------------------------------------------------------------------------- |
|
93 // |
|
94 void CLandmarksModel::SetCurrentItem(TInt aIndex) |
|
95 { |
|
96 iCurrentItem = aIndex; |
|
97 } |
|
98 |
|
99 // ----------------------------------------------------------------------------- |
|
100 // ----------------------------------------------------------------------------- |
|
101 // |
|
102 TPosLmItemId CLandmarksModel::CurrentItemId() |
|
103 { |
|
104 if (!iItemIds || iCurrentItem < 0 || |
|
105 iCurrentItem > iItemIds->Count() - 1) |
|
106 { |
|
107 return KPosLmNullItemId; |
|
108 } |
|
109 |
|
110 return (*iItemIds)[iCurrentItem]; |
|
111 } |
|
112 |
|
113 // ----------------------------------------------------------------------------- |
|
114 // ----------------------------------------------------------------------------- |
|
115 // |
|
116 void CLandmarksModel::GetItemIdsL( |
|
117 const CArrayFix<TInt>& aSelectionIndexes, |
|
118 RArray<TPosLmItemId>& aItemIds ) |
|
119 { |
|
120 aItemIds.Reset(); |
|
121 if ( iItemIds ) |
|
122 { |
|
123 for ( TInt i = 0; i < aSelectionIndexes.Count(); i++ ) |
|
124 { |
|
125 if ( i < iItemIds->Count() ) |
|
126 { |
|
127 aItemIds.AppendL( (*iItemIds)[aSelectionIndexes[i]] ); |
|
128 } |
|
129 else |
|
130 { |
|
131 break; |
|
132 } |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 // ----------------------------------------------------------------------------- |
|
138 // ----------------------------------------------------------------------------- |
|
139 // |
|
140 CIconList* CLandmarksModel::CreateIconListL() |
|
141 { |
|
142 CIconList* iconList = |
|
143 (CIconList*) new (ELeave) CArrayPtrFlat<CGulIcon>(KGranularity); |
|
144 CleanupStack::PushL(iconList); |
|
145 |
|
146 // Create the default landmark icon consisting of its image and mask |
|
147 TAknsItemID aknsItemId = {0,0}; |
|
148 CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId, |
|
149 KLandmarksMbmFileName, |
|
150 EMbmLmrefappDefault_lm, |
|
151 EMbmLmrefappDefault_lm_mask); |
|
152 CleanupStack::PushL(icon); |
|
153 icon->SetBitmapsOwnedExternally(EFalse); |
|
154 iconList->AppendL(icon); |
|
155 CleanupStack::Pop(2, iconList); |
|
156 |
|
157 // initialze iIconList. We don't need to delete any possible previous |
|
158 // iIconList since ownership is transferred to calling object. |
|
159 iIconList = iconList; |
|
160 |
|
161 return iIconList; |
|
162 } |
|
163 |
|
164 // ----------------------------------------------------------------------------- |
|
165 // ----------------------------------------------------------------------------- |
|
166 // |
|
167 void CLandmarksModel::RepopulateModelL( |
|
168 CArrayPtr<CPosLandmark>* aLandmarks) |
|
169 { |
|
170 TInt nrOfReadItems = aLandmarks->Count(); |
|
171 |
|
172 // Format and insert the read landmarks |
|
173 for (TInt i = 0; i < nrOfReadItems; i++) |
|
174 { |
|
175 CPosLandmark* landmark = (*aLandmarks)[i]; |
|
176 HBufC* formattedListItem = FormatListItemLC(*landmark); |
|
177 iListItems->AppendL(*formattedListItem); |
|
178 CleanupStack::PopAndDestroy(formattedListItem); |
|
179 } |
|
180 } |
|
181 |
|
182 // ----------------------------------------------------------------------------- |
|
183 // Formats a list item to the following format "X\tlandmarkName\t" |
|
184 // where X = icon index |
|
185 // (other items were commented in a header). |
|
186 // ----------------------------------------------------------------------------- |
|
187 // |
|
188 HBufC* CLandmarksModel::FormatListItemLC(CPosLandmark& aLandmark) |
|
189 { |
|
190 // Extract name |
|
191 TPtrC name; |
|
192 aLandmark.GetLandmarkName(name); |
|
193 // 10 extra chars for icon index and column separator |
|
194 const TInt KExtraChars = 10; |
|
195 HBufC* formattedNameBuf = HBufC::NewLC(name.Length() + KExtraChars); |
|
196 TPtr formattedNamePtr = formattedNameBuf->Des(); |
|
197 |
|
198 // Extract icon info |
|
199 TPtrC mbmIconFile; |
|
200 TInt iconIndex, maskIndex; |
|
201 TInt res = aLandmark.GetIcon(mbmIconFile, iconIndex, maskIndex); |
|
202 |
|
203 // Format list item |
|
204 if (res == KErrNone) |
|
205 { |
|
206 // Create icon and append it to icon array. |
|
207 TRAPD(err, AppendIconL(mbmIconFile, iconIndex, maskIndex)); |
|
208 if (err == KErrNone) |
|
209 { |
|
210 // Append the array index where the icon is appended |
|
211 formattedNamePtr.AppendNum(iIconList->Count() - 1); |
|
212 } |
|
213 else |
|
214 { |
|
215 formattedNamePtr.AppendNum(KDefaultIconIndex); |
|
216 } |
|
217 } |
|
218 else // res == KErrNotFound |
|
219 { |
|
220 formattedNamePtr.AppendNum(KDefaultIconIndex); |
|
221 } |
|
222 formattedNamePtr.Append(KTab); |
|
223 formattedNamePtr.Append(name); |
|
224 |
|
225 return formattedNameBuf; |
|
226 } |
|
227 |
|
228 // ----------------------------------------------------------------------------- |
|
229 // ----------------------------------------------------------------------------- |
|
230 // |
|
231 void CLandmarksModel::AppendIconL( |
|
232 const TDesC& aMbmFile, |
|
233 TInt aIconOffset, |
|
234 TInt aMaskOffset) |
|
235 { |
|
236 TAknsItemID aknsItemId = {0,0}; |
|
237 CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId, |
|
238 aMbmFile, |
|
239 aIconOffset, |
|
240 aMaskOffset); |
|
241 CleanupStack::PushL(icon); |
|
242 icon->SetBitmapsOwnedExternally(EFalse); |
|
243 iIconList->AppendL(icon); |
|
244 CleanupStack::Pop(icon); |
|
245 } |
|
246 |
|
247 |