|
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: Customized list view item to display autofill items on list. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "nmailuiwidgetsheaders.h" |
|
19 |
|
20 static const QString NmAutoFillListViewDisplayName("AutoFillListViewDisplayName"); |
|
21 static const QString NmAutoFillListViewAddress("AutoFillListViewAddress"); |
|
22 |
|
23 static const QString NmAutoFillListViewItemUnderLineStart("<U>"); |
|
24 static const QString NmAutoFillListViewItemUnderLineEnd("</U>"); |
|
25 |
|
26 |
|
27 /*! |
|
28 \class NmAutoFillListViewItem |
|
29 \brief List view items for autofill items. |
|
30 |
|
31 Customized list view item to display autofill items on list. |
|
32 */ |
|
33 |
|
34 /*! |
|
35 Constructor of NmAutoFillListViewItem. |
|
36 |
|
37 Calls constructUi function and sets stretching to landscape mode. |
|
38 */ |
|
39 NmAutoFillListViewItem::NmAutoFillListViewItem(QGraphicsItem *parent) |
|
40 : HbListViewItem(parent), mDisplayName(NULL), mAddress(NULL) |
|
41 { |
|
42 NM_FUNCTION; |
|
43 } |
|
44 |
|
45 /*! |
|
46 Destructor of NmAutoFillListViewItem. |
|
47 */ |
|
48 NmAutoFillListViewItem::~NmAutoFillListViewItem() |
|
49 { |
|
50 } |
|
51 |
|
52 /*! |
|
53 Item creation. Overwrites HbListViewItem funtion. |
|
54 |
|
55 \return A newly created NmAutoFillListViewItem. |
|
56 */ |
|
57 HbListViewItem* NmAutoFillListViewItem::createItem() |
|
58 { |
|
59 NM_FUNCTION; |
|
60 |
|
61 return new NmAutoFillListViewItem(*this); |
|
62 } |
|
63 |
|
64 /*! |
|
65 Update child items. Overwrites HbListViewItem funtion. |
|
66 |
|
67 The data is fetched from the model by using current model index and displayed. |
|
68 */ |
|
69 void NmAutoFillListViewItem::updateChildItems() |
|
70 { |
|
71 NM_FUNCTION; |
|
72 |
|
73 QString displayText; |
|
74 QString addressText; |
|
75 |
|
76 |
|
77 //Get data from model |
|
78 //check if QVariant is ok for this item etc. |
|
79 NmContactHistoryModelItem item = |
|
80 modelIndex().data(Qt::DisplayRole).value<NmContactHistoryModelItem>(); |
|
81 |
|
82 if (item.subItemCount()) { |
|
83 |
|
84 QList<NmContactHistoryModelSubItem> itemlist = item.subEntries(); |
|
85 |
|
86 //Check if both displayname and address is found. |
|
87 if(itemlist.count() == 2) { |
|
88 displayText.append(setHtmlUnderLine(itemlist[0])); |
|
89 addressText.append(setHtmlUnderLine(itemlist[1])); |
|
90 //If only 1 item is found. It will be set to position of displayname. |
|
91 } else if(itemlist.count() == 1) { |
|
92 displayText.append(setHtmlUnderLine(itemlist[0])); |
|
93 } |
|
94 } else { |
|
95 //Items not found. There is no point to continue. |
|
96 return; |
|
97 } |
|
98 |
|
99 if (!mDisplayName) { |
|
100 mDisplayName = new HbRichTextItem(this); |
|
101 mDisplayName->setObjectName(NmAutoFillListViewDisplayName); |
|
102 HbStyle::setItemName(mDisplayName, NmAutoFillListViewDisplayName); |
|
103 } |
|
104 |
|
105 mDisplayName->setText(displayText); |
|
106 |
|
107 if (!mAddress){ |
|
108 mAddress = new HbRichTextItem(this); |
|
109 mAddress->setObjectName(NmAutoFillListViewAddress); |
|
110 HbStyle::setItemName(mAddress, NmAutoFillListViewAddress); |
|
111 } |
|
112 |
|
113 mAddress->setText(addressText); |
|
114 |
|
115 HbListViewItem::updateChildItems(); |
|
116 } |
|
117 |
|
118 /*! |
|
119 Model index can be set. Overwrites HbListViewItem funtion. |
|
120 |
|
121 Enables set of all model indexes. |
|
122 \param index The model index. |
|
123 \return always true. |
|
124 */ |
|
125 bool NmAutoFillListViewItem::canSetModelIndex(const QModelIndex &index) |
|
126 { |
|
127 NM_FUNCTION; |
|
128 |
|
129 Q_UNUSED(index); |
|
130 return true; |
|
131 } |
|
132 |
|
133 /*! |
|
134 Private helper class to set html underline tags. |
|
135 |
|
136 \param subItem Reference to subitem to do underlining. |
|
137 \return QString underlined by html tags. |
|
138 */ |
|
139 QString NmAutoFillListViewItem::setHtmlUnderLine(const NmContactHistoryModelSubItem &subItem) |
|
140 { |
|
141 NM_FUNCTION; |
|
142 |
|
143 QString ret; |
|
144 bool odd(false); |
|
145 int index_corrector(0); |
|
146 |
|
147 QList<int> matching = subItem.mMatchingRanges; |
|
148 ret.append(subItem.mItemText); |
|
149 |
|
150 //Check that there is even count of matching range indexes. |
|
151 if ((matching.count() % 2) == 0) { |
|
152 |
|
153 foreach(int index, matching) { |
|
154 |
|
155 //Make sanity check for indexes. |
|
156 if (index < subItem.mItemText.length() && index >= 0) { |
|
157 if (!odd) { |
|
158 ret = ret.insert(index+index_corrector,NmAutoFillListViewItemUnderLineStart); |
|
159 index_corrector += NmAutoFillListViewItemUnderLineStart.length(); |
|
160 } else { |
|
161 ret = ret.insert(index+1+index_corrector,NmAutoFillListViewItemUnderLineEnd); |
|
162 index_corrector += NmAutoFillListViewItemUnderLineEnd.length(); |
|
163 } |
|
164 } else { |
|
165 //Sanity check failed. No point to continue. |
|
166 break; |
|
167 } |
|
168 odd = odd ? false : true; |
|
169 } |
|
170 } |
|
171 return ret; |
|
172 } |
|
173 |