|
1 /* |
|
2 * Copyright (c) 2008 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: Custom listbox model for the contact list |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "cvimpstuilistboxmodel.h" |
|
20 #include "mvimpstprocessarray.h" |
|
21 |
|
22 // ======== MEMBER FUNCTIONS ======== |
|
23 |
|
24 // --------------------------------------------------------------------------- |
|
25 // CVIMPSTUiListBoxModel::CVIMPSTUiListBoxModel |
|
26 // --------------------------------------------------------------------------- |
|
27 // |
|
28 CVIMPSTUiListBoxModel::CVIMPSTUiListBoxModel() |
|
29 { |
|
30 } |
|
31 |
|
32 // --------------------------------------------------------------------------- |
|
33 // CVIMPSTUiListBoxModel::~CVIMPSTUiListBoxModel |
|
34 // --------------------------------------------------------------------------- |
|
35 // |
|
36 CVIMPSTUiListBoxModel::~CVIMPSTUiListBoxModel() |
|
37 { |
|
38 } |
|
39 |
|
40 // --------------------------------------------------------------------------- |
|
41 // CVIMPSTUiListBoxModel::NumberOfItems |
|
42 // --------------------------------------------------------------------------- |
|
43 // |
|
44 TInt CVIMPSTUiListBoxModel::NumberOfItems() const |
|
45 { |
|
46 if( IsFilterActive() ) |
|
47 { |
|
48 // Something is filtered |
|
49 return CountItems(); |
|
50 } |
|
51 if( iItemTextArray ) |
|
52 { |
|
53 // Fast |
|
54 return iItemTextArray->MdcaCount(); |
|
55 } |
|
56 |
|
57 return 0; |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // CVIMPSTUiListBoxModel::ItemText |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 TPtrC CVIMPSTUiListBoxModel::ItemText( TInt aItemIndex ) const |
|
65 { |
|
66 if( IsFilterActive() ) |
|
67 { |
|
68 // Slow |
|
69 return ItemAt( aItemIndex ); |
|
70 } |
|
71 |
|
72 // Fast |
|
73 return iItemTextArray->MdcaPoint( aItemIndex ); |
|
74 } |
|
75 |
|
76 // --------------------------------------------------------------------------- |
|
77 // CVIMPSTUiListBoxModel::FilteredItemIndex |
|
78 // --------------------------------------------------------------------------- |
|
79 // |
|
80 TInt CVIMPSTUiListBoxModel::FilteredItemIndex( TInt aVisibleItemIndex ) const |
|
81 { |
|
82 if( !IsFilterActive() ) |
|
83 { |
|
84 // Nothing is filtered |
|
85 return aVisibleItemIndex; |
|
86 } |
|
87 if( Filter() ) |
|
88 { |
|
89 // Get positions of lists |
|
90 RArray<TInt> pos; |
|
91 // iItemModel->MapContactListPositions( pos ); |
|
92 |
|
93 // Count visible items until we reach the wanted one |
|
94 TInt itemcount = -1; |
|
95 TInt count = iItemTextArray->MdcaCount(); |
|
96 for( TInt i = 0; i < count; ++i ) |
|
97 { |
|
98 if( ( pos.Find( i ) != KErrNotFound && |
|
99 IsContactList( i, pos ) ) || |
|
100 Filter()->VisibleItemIndex( i ) != KErrNotFound ) |
|
101 { |
|
102 // this item is either visible by filter or |
|
103 // it's our dynamically added contact list |
|
104 itemcount++; |
|
105 if( itemcount == aVisibleItemIndex ) |
|
106 { |
|
107 pos.Close(); |
|
108 return i; |
|
109 } |
|
110 } |
|
111 } |
|
112 pos.Close(); |
|
113 } |
|
114 return 0; |
|
115 } |
|
116 |
|
117 // --------------------------------------------------------------------------- |
|
118 // CVIMPSTUiListBoxModel::VisibleItemIndex |
|
119 // --------------------------------------------------------------------------- |
|
120 // |
|
121 TInt CVIMPSTUiListBoxModel::VisibleItemIndex( TInt aOriginalIndex ) const |
|
122 { |
|
123 if( !IsFilterActive() ) |
|
124 { |
|
125 // Nothing is filtered |
|
126 return aOriginalIndex; |
|
127 } |
|
128 |
|
129 // Get positions of contact lists |
|
130 RArray<TInt> pos; |
|
131 //iItemModel->MapContactListPositions( pos ); |
|
132 |
|
133 // Check if it's even visible |
|
134 TInt index = Filter()->VisibleItemIndex( aOriginalIndex ); |
|
135 TBool visibleList = IsContactList( aOriginalIndex, pos ); |
|
136 if( index == KErrNotFound && !visibleList ) |
|
137 { |
|
138 pos.Close(); |
|
139 return KErrNotFound; |
|
140 } |
|
141 |
|
142 // Count up to original index to find out the visible position |
|
143 TInt visibleIndex = -1; |
|
144 for( TInt i = 0; i <= aOriginalIndex; ++i ) |
|
145 { |
|
146 if( ( pos.Find( i ) != KErrNotFound && |
|
147 IsContactList( i, pos ) ) || |
|
148 Filter()->VisibleItemIndex( i ) != KErrNotFound ) |
|
149 { |
|
150 // this item is either visible by filter or |
|
151 // it's our dynamically added contact list |
|
152 visibleIndex++; |
|
153 } |
|
154 } |
|
155 pos.Close(); |
|
156 return visibleIndex; |
|
157 } |
|
158 |
|
159 // --------------------------------------------------------------------------- |
|
160 // CVIMPSTUiListBoxModel::CountItems |
|
161 // --------------------------------------------------------------------------- |
|
162 // |
|
163 TInt CVIMPSTUiListBoxModel::CountItems() const |
|
164 { |
|
165 TInt itemcount = 0; |
|
166 if( Filter() ) |
|
167 { |
|
168 // Count of visible items by filter |
|
169 itemcount = Filter()->FilteredNumberOfItems(); |
|
170 if( itemcount == 0 ) |
|
171 { |
|
172 return itemcount; |
|
173 } |
|
174 |
|
175 // Get positions of contact lists |
|
176 RArray<TInt> pos; |
|
177 //iItemModel->MapContactListPositions( pos ); |
|
178 |
|
179 // Scan through list positions and count the additional visible items. |
|
180 TInt count = pos.Count(); |
|
181 for( TInt i = 0; i < count; ++i ) |
|
182 { |
|
183 if( IsContactList( pos[i], pos ) && |
|
184 Filter()->VisibleItemIndex( pos[i] ) == KErrNotFound ) |
|
185 { |
|
186 // List should be visible because it has found contacts in it |
|
187 // but it's filtered out by searchfield. Add it dynamically. |
|
188 itemcount++; |
|
189 } |
|
190 } |
|
191 pos.Close(); |
|
192 } |
|
193 return itemcount; |
|
194 } |
|
195 |
|
196 // --------------------------------------------------------------------------- |
|
197 // CVIMPSTUiListBoxModel::ItemAt |
|
198 // --------------------------------------------------------------------------- |
|
199 // |
|
200 TPtrC CVIMPSTUiListBoxModel::ItemAt( TInt aIndex ) const |
|
201 { |
|
202 TInt itemcount = -1; |
|
203 if( Filter() ) |
|
204 { |
|
205 // Get positions of contact lists |
|
206 RArray<TInt> pos; |
|
207 |
|
208 // Scan through visible items until we reach the wanted one |
|
209 TInt count = iItemTextArray->MdcaCount(); |
|
210 for( TInt i = 0; i < count; ++i ) |
|
211 { |
|
212 if( ( pos.Find( i ) != KErrNotFound && |
|
213 IsContactList( i, pos ) ) || |
|
214 Filter()->VisibleItemIndex( i ) != KErrNotFound ) |
|
215 { |
|
216 // this item is either visible by filter or |
|
217 // it's our dynamically added contact list |
|
218 itemcount++; |
|
219 if( itemcount == aIndex ) |
|
220 { |
|
221 // Found it |
|
222 pos.Close(); |
|
223 return iItemTextArray->MdcaPoint( i ); |
|
224 } |
|
225 } |
|
226 } |
|
227 pos.Close(); |
|
228 } |
|
229 return KNullDesC(); |
|
230 } |
|
231 |
|
232 // --------------------------------------------------------------------------- |
|
233 // CVIMPSTUiListBoxModel::IsContactList |
|
234 // --------------------------------------------------------------------------- |
|
235 // |
|
236 TBool CVIMPSTUiListBoxModel::IsContactList( TInt aIndex, RArray<TInt>& aPosArray ) const |
|
237 { |
|
238 TInt startInd = aPosArray.Find( aIndex ); |
|
239 if( KErrNotFound == startInd ) |
|
240 { |
|
241 // aIndex not found in array -> not even list |
|
242 return EFalse; |
|
243 } |
|
244 |
|
245 TInt count = aPosArray.Count(); |
|
246 TInt end = 0; |
|
247 if( startInd >= count - 1 ) |
|
248 { |
|
249 // aIndex is the last list: |
|
250 // Search from aIndex to last item |
|
251 end = iItemTextArray->MdcaCount(); |
|
252 } |
|
253 else |
|
254 { |
|
255 // Search from aInded to next item in array |
|
256 end = aPosArray[ startInd + 1 ]; |
|
257 } |
|
258 |
|
259 // Try to find visible contacts for this list |
|
260 for ( TInt i = aIndex + 1; i < end; ++i ) |
|
261 { |
|
262 if( Filter()->VisibleItemIndex( i ) != KErrNotFound ) |
|
263 { |
|
264 // found one |
|
265 return ETrue; |
|
266 } |
|
267 } |
|
268 // not found |
|
269 return EFalse; |
|
270 } |
|
271 |
|
272 // --------------------------------------------------------------------------- |
|
273 // CVIMPSTUiListBoxModel::IsFilterActive |
|
274 // --------------------------------------------------------------------------- |
|
275 // |
|
276 TBool CVIMPSTUiListBoxModel::IsFilterActive() const |
|
277 { |
|
278 if( Filter() ) |
|
279 { |
|
280 return iItemTextArray->MdcaCount() != |
|
281 Filter()->FilteredNumberOfItems(); |
|
282 } |
|
283 return EFalse; |
|
284 } |
|
285 // --------------------------------------------------------------------------- |
|
286 // CVIMPSTUiListBoxModel::MatchableTextArray |
|
287 // --------------------------------------------------------------------------- |
|
288 // |
|
289 const MDesCArray* CVIMPSTUiListBoxModel::MatchableTextArray() const |
|
290 { |
|
291 return this; |
|
292 } |
|
293 |
|
294 // --------------------------------------------------------------------------- |
|
295 // CVIMPSTUiListBoxModel::MdcaCount |
|
296 // --------------------------------------------------------------------------- |
|
297 // |
|
298 TInt CVIMPSTUiListBoxModel::MdcaCount() const |
|
299 { |
|
300 return iItemModel->Count(); |
|
301 } |
|
302 |
|
303 // --------------------------------------------------------------------------- |
|
304 // CVIMPSTUiListBoxModel::MdcaPoint |
|
305 // --------------------------------------------------------------------------- |
|
306 // |
|
307 TPtrC CVIMPSTUiListBoxModel::MdcaPoint( TInt aIndex ) const |
|
308 { |
|
309 return iItemModel->GetItemNameText( aIndex ); |
|
310 } |
|
311 |
|
312 // --------------------------------------------------------------------------- |
|
313 // CVIMPSTUiListBoxModel::SetContactListModel |
|
314 // --------------------------------------------------------------------------- |
|
315 // |
|
316 void CVIMPSTUiListBoxModel::SetContactListModel( MVIMPSTProcessArray& |
|
317 aContactListModel ) |
|
318 { |
|
319 iItemModel = &aContactListModel; |
|
320 } |
|
321 |
|
322 // End of file |