|
1 /* |
|
2 * Copyright (c) 2010 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 <QGraphicsLinearLayout> |
|
19 #include <QScopedPointer> |
|
20 #include <HbPushButton> |
|
21 #include <HbSelectionDialog> |
|
22 #include <HbLabel> |
|
23 #include <HbExtendedLocale> |
|
24 |
|
25 #include "nmipssettingsmultiselectionitem.h" |
|
26 |
|
27 /*! |
|
28 \class NmIpsSettingsMultiSelectionItem |
|
29 \brief The class implements a custom HbDataFormViewItem for showing multi selection dialog. |
|
30 |
|
31 */ |
|
32 |
|
33 // ======== MEMBER FUNCTIONS ======== |
|
34 |
|
35 /*! |
|
36 Constructor of NmIpsSettingsMultiSelectionItem. |
|
37 */ |
|
38 |
|
39 |
|
40 NmIpsSettingsMultiSelectionItem::NmIpsSettingsMultiSelectionItem( |
|
41 QGraphicsItem *parent, Qt::WindowFlags wFlags) |
|
42 : HbWidget(parent, wFlags), |
|
43 mButton(0), |
|
44 mSelectionDialog(0) |
|
45 { |
|
46 // Create widget layout. |
|
47 QScopedPointer<QGraphicsLinearLayout> layout(new QGraphicsLinearLayout(Qt::Vertical, this)); |
|
48 this->setLayout(layout.data()); |
|
49 |
|
50 // Create button. |
|
51 mButton = new HbPushButton(); |
|
52 layout->addItem(mButton); |
|
53 |
|
54 connect(mButton, SIGNAL(clicked()), this, SLOT(launchSelectionDialog())); |
|
55 (void)layout.take(); |
|
56 } |
|
57 |
|
58 /*! |
|
59 Destructor of NmIpsSettingsMultiSelectionItem. |
|
60 */ |
|
61 NmIpsSettingsMultiSelectionItem::~NmIpsSettingsMultiSelectionItem() |
|
62 { |
|
63 delete mSelectionDialog; |
|
64 } |
|
65 |
|
66 /*! |
|
67 Returns shown text. |
|
68 |
|
69 \return Shown text. |
|
70 */ |
|
71 QString NmIpsSettingsMultiSelectionItem::text() const |
|
72 { |
|
73 return mButton->text(); |
|
74 } |
|
75 |
|
76 /*! |
|
77 Sets shown text. |
|
78 |
|
79 \param text Shown text. |
|
80 */ |
|
81 void NmIpsSettingsMultiSelectionItem::setText(QString text) |
|
82 { |
|
83 mButton->setText(text); |
|
84 } |
|
85 |
|
86 /*! |
|
87 Returns items which are shown in dialog. |
|
88 |
|
89 \return items which are shown in dialog. |
|
90 */ |
|
91 QStringList NmIpsSettingsMultiSelectionItem::stringItems() const |
|
92 { |
|
93 return mItems; |
|
94 } |
|
95 |
|
96 /*! |
|
97 Sets items which are shown in dialog. |
|
98 |
|
99 \param items Items which are shown in dialog. |
|
100 */ |
|
101 void NmIpsSettingsMultiSelectionItem::setStringItems(QStringList items) |
|
102 { |
|
103 mItems = items; |
|
104 generateButtonText(); |
|
105 } |
|
106 |
|
107 /*! |
|
108 Returns selected items. |
|
109 |
|
110 \return selected items. |
|
111 */ |
|
112 QList<QVariant> NmIpsSettingsMultiSelectionItem::selectedItems()const |
|
113 { |
|
114 return mSelectedItems; |
|
115 } |
|
116 |
|
117 /*! |
|
118 Sets selected items. |
|
119 |
|
120 \param selectedItems Indexes of items which will be seleceted when dialog is shown. |
|
121 */ |
|
122 void NmIpsSettingsMultiSelectionItem::setSelectedItems(QList<QVariant> selectedItems) |
|
123 { |
|
124 // Store selected items and generate button text. |
|
125 mSelectedItems = selectedItems; |
|
126 generateButtonText(); |
|
127 } |
|
128 |
|
129 /*! |
|
130 Returns heading text. |
|
131 |
|
132 \return heading text. |
|
133 */ |
|
134 QString NmIpsSettingsMultiSelectionItem::heading() const |
|
135 { |
|
136 return mHeading; |
|
137 } |
|
138 |
|
139 /*! |
|
140 Sets heading text. |
|
141 |
|
142 \param heading Shown heading text in dialog. |
|
143 */ |
|
144 void NmIpsSettingsMultiSelectionItem::setHeading(QString heading) |
|
145 { |
|
146 mHeading = heading; |
|
147 } |
|
148 |
|
149 /*! |
|
150 Launches the Selection dialog. |
|
151 */ |
|
152 void NmIpsSettingsMultiSelectionItem::launchSelectionDialog() |
|
153 { |
|
154 if (mSelectionDialog) { |
|
155 delete mSelectionDialog; |
|
156 mSelectionDialog = 0; |
|
157 } |
|
158 |
|
159 // Create the dialog. |
|
160 mSelectionDialog = new HbSelectionDialog(); |
|
161 mSelectionDialog->setSelectionMode(HbAbstractItemView::MultiSelection); |
|
162 mSelectionDialog->setStringItems(mItems, mItems.count() + 1); |
|
163 mSelectionDialog->setSelectedItems(mSelectedItems); |
|
164 |
|
165 // Set the heading for the dialog. |
|
166 HbLabel *headingLabel = new HbLabel(mHeading, mSelectionDialog); |
|
167 mSelectionDialog->setHeadingWidget(headingLabel); |
|
168 mSelectionDialog->open(this, SLOT(selectionDialogClosed(HbAction *))); |
|
169 } |
|
170 |
|
171 /*! |
|
172 Handles the action from selection dialog. |
|
173 |
|
174 \param action Dialog closing action. |
|
175 */ |
|
176 void NmIpsSettingsMultiSelectionItem::selectionDialogClosed(HbAction *action) |
|
177 { |
|
178 if (action == mSelectionDialog->primaryAction()) { |
|
179 // Get selected items. |
|
180 mSelectedItems = mSelectionDialog->selectedItems(); |
|
181 |
|
182 // Generate button text from selected items. |
|
183 generateButtonText(); |
|
184 |
|
185 QMap<QString, QVariant> properties; |
|
186 properties.insert("selectedItems", mSelectedItems); |
|
187 |
|
188 emit propertyChanged(properties); |
|
189 |
|
190 // Emit the signal. |
|
191 emit editingFinished(); |
|
192 } |
|
193 } |
|
194 |
|
195 /*! |
|
196 Generates shown text from selected items. |
|
197 */ |
|
198 void NmIpsSettingsMultiSelectionItem::generateButtonText() |
|
199 { |
|
200 // Generate button text only if both properties are valid. |
|
201 const int itemCount(mItems.count()); |
|
202 if (mItems.count() >= mSelectedItems.count()) { |
|
203 // Construct separator for button text. |
|
204 QChar groupSeparator = HbExtendedLocale::system().groupSeparator(); |
|
205 QString separator(" "); |
|
206 separator.insert(0, groupSeparator); |
|
207 |
|
208 QString buttonText; |
|
209 QListIterator<QVariant> itemIterator(mSelectedItems); |
|
210 while (itemIterator.hasNext()) { |
|
211 QVariant item(itemIterator.next()); |
|
212 bool conversionOk(false); |
|
213 const int selectedIndex(item.toInt(&conversionOk)); |
|
214 if (conversionOk && (selectedIndex >= 0) && (selectedIndex < itemCount)) { |
|
215 // Generate button text. |
|
216 buttonText.append(mItems.at(selectedIndex)); |
|
217 // Add separator if not last item. |
|
218 if (itemIterator.hasNext()) { |
|
219 buttonText.append(separator); |
|
220 } |
|
221 } |
|
222 } |
|
223 mButton->setText(buttonText); |
|
224 } |
|
225 } |