|
1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QBrush> |
|
19 |
|
20 #include "irqisdsdatastructure.h" |
|
21 #include "iruidefines.h" |
|
22 #include "ircategorymodel.h" |
|
23 |
|
24 |
|
25 IRCategoryModel::IRCategoryModel(QObject *aParent) : QAbstractListModel(aParent), iItems(NULL) |
|
26 { |
|
27 } |
|
28 |
|
29 IRCategoryModel::~IRCategoryModel() |
|
30 { |
|
31 destroyAllItems(); |
|
32 delete iItems; |
|
33 iItems = NULL; |
|
34 } |
|
35 |
|
36 int IRCategoryModel::rowCount(const QModelIndex &aParent) const |
|
37 { |
|
38 Q_UNUSED(aParent); |
|
39 |
|
40 if (iItems) |
|
41 { |
|
42 int count = iItems->count(); |
|
43 return count; |
|
44 } |
|
45 |
|
46 return 0; |
|
47 } |
|
48 |
|
49 QVariant IRCategoryModel::data(const QModelIndex &aIndex, int aRole) const |
|
50 { |
|
51 if (!aIndex.isValid()) |
|
52 return QVariant(); |
|
53 |
|
54 if (aIndex.row() >= iItems->count()) |
|
55 return QVariant(); |
|
56 |
|
57 if (aRole == Qt::DisplayRole) |
|
58 { |
|
59 int row = aIndex.row(); |
|
60 |
|
61 QString catNum; |
|
62 catNum.setNum(iItems->at(row)->size); |
|
63 QString secondaryText = catNum + QString(" Stations"); |
|
64 |
|
65 QVariantList list; |
|
66 list.append(iItems->at(row)->catName); |
|
67 list.append(secondaryText); |
|
68 return list; |
|
69 } |
|
70 else if (aRole == Qt::BackgroundRole) |
|
71 { |
|
72 if (aIndex.row() % 2 == 0) |
|
73 { |
|
74 return QBrush(KListEvenRowColor); |
|
75 } |
|
76 else |
|
77 { |
|
78 return QBrush(KListOddRowColor); |
|
79 } |
|
80 } |
|
81 |
|
82 return QVariant(); |
|
83 } |
|
84 |
|
85 const QString& IRCategoryModel::categoryName(const QModelIndex &aIndex) const |
|
86 { |
|
87 int row = aIndex.row(); |
|
88 Q_ASSERT(row >= 0 && row < iItems->count()); |
|
89 |
|
90 return iItems->at(row)->catName; |
|
91 } |
|
92 |
|
93 void IRCategoryModel::categoryReceived(QList<IRQBrowseCategoryItem*> *aItems) |
|
94 { |
|
95 destroyAllItems(); |
|
96 delete iItems; |
|
97 |
|
98 iItems = aItems; |
|
99 |
|
100 emit dataChanged(); |
|
101 } |
|
102 |
|
103 void IRCategoryModel::destroyAllItems() |
|
104 { |
|
105 if (iItems) |
|
106 { |
|
107 while (!iItems->isEmpty()) |
|
108 { |
|
109 IRQBrowseCategoryItem *firstItem = iItems->takeFirst(); |
|
110 delete firstItem; |
|
111 } |
|
112 } |
|
113 } |