|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbdataform_p.h" |
|
27 #include <hbdataformviewitem.h> |
|
28 #include "hbdataitemcontainer_p.h" |
|
29 #include <hbcombobox.h> |
|
30 #include <hbapplication.h> |
|
31 #include <hbdataformmodel.h> |
|
32 #include "hbdataformheadingwidget_p.h" |
|
33 #include "hbtreemodeliterator_p.h" |
|
34 #include <hbdatagroup_p.h> |
|
35 |
|
36 #include <QStringListModel> |
|
37 #include <QHash> |
|
38 |
|
39 HbDataFormPrivate::HbDataFormPrivate() : |
|
40 HbAbstractItemViewPrivate(), |
|
41 mHeadingWidget(0) |
|
42 { |
|
43 } |
|
44 |
|
45 HbDataFormPrivate::~HbDataFormPrivate() |
|
46 { |
|
47 } |
|
48 |
|
49 int HbDataFormPrivate::childCount(const QModelIndex &index) const |
|
50 { |
|
51 Q_Q(const HbDataForm); |
|
52 |
|
53 int itemCount = 0; |
|
54 int rowCount = 0; |
|
55 if(!index.isValid()) { |
|
56 return 0; |
|
57 } |
|
58 if (q->isExpanded(index)) { |
|
59 rowCount = index.model()->rowCount(index); |
|
60 } |
|
61 itemCount += rowCount; |
|
62 for (int row = 0; row < rowCount; ++row) { |
|
63 itemCount += childCount(index.child(row, 0)); |
|
64 } |
|
65 return itemCount; |
|
66 } |
|
67 |
|
68 void HbDataFormPrivate::init() |
|
69 { |
|
70 Q_Q(HbDataForm); |
|
71 |
|
72 QList<HbAbstractViewItem*> protos; |
|
73 protos.append(new HbDataFormViewItem()); |
|
74 protos.append(new HbDataGroup()); |
|
75 q->setItemPrototypes(protos); |
|
76 q->setClampingStyle(HbScrollArea::BounceBackClamping); |
|
77 q->setItemRecycling(true); |
|
78 treeModelIterator()->setItemContainer(mContainer, HbDataFormViewItem::ExpansionKey); |
|
79 } |
|
80 |
|
81 void HbDataFormPrivate::_q_page_changed(int index) |
|
82 { |
|
83 Q_Q(const HbDataForm); |
|
84 QStringListModel *model = static_cast<QStringListModel*>( |
|
85 mHeadingWidget->mPageCombo->model()); |
|
86 QModelIndex changedIndex = model->index(index, 0); |
|
87 if(changedIndex.isValid()) { |
|
88 if(changedIndex.row() != mHeadingWidget->mActivePage) { |
|
89 QModelIndex prevPageIndex = q->model()->index(mHeadingWidget->mActivePage,0); |
|
90 QModelIndex newPageIndex = q->model()->index(changedIndex.row(),0); |
|
91 if(prevPageIndex.isValid()) { |
|
92 HbDataGroup *prevPage = static_cast<HbDataGroup *>( |
|
93 q->itemByIndex(prevPageIndex)); |
|
94 if(prevPage) { |
|
95 prevPage->setExpanded(false); |
|
96 } |
|
97 } |
|
98 if(newPageIndex.isValid()) { |
|
99 HbDataGroup *newPage = static_cast<HbDataGroup *>( |
|
100 q->itemByIndex(newPageIndex)); |
|
101 if(newPage) { |
|
102 newPage->setExpanded(true); |
|
103 } |
|
104 } |
|
105 mHeadingWidget->mActivePage = changedIndex.row(); |
|
106 } |
|
107 } |
|
108 } |
|
109 |
|
110 |
|
111 /*! |
|
112 Creates a DataForm Page \a page in DataForm . |
|
113 DataForm Page is an invisible DataItem which can be changed/selected using combo box. |
|
114 \sa addFormPage |
|
115 */ |
|
116 void HbDataFormPrivate::addFormPage(const QString& page) |
|
117 { |
|
118 Q_Q(HbDataForm); |
|
119 |
|
120 // Create combobox if not created |
|
121 if(!mHeadingWidget) { |
|
122 mHeadingWidget = new HbDataFormHeadingWidget(); |
|
123 static_cast<HbDataItemContainer*>(q->container())->setFormHeading(mHeadingWidget); |
|
124 QEvent polishEvent(QEvent::Polish); |
|
125 QCoreApplication::sendEvent(mHeadingWidget, &polishEvent); |
|
126 } |
|
127 |
|
128 if(!mHeadingWidget->mPageCombo) { |
|
129 mHeadingWidget->createPrimitives(); |
|
130 mHeadingWidget->mPageCombo = new HbComboBox(mHeadingWidget); |
|
131 HbStyle::setItemName(mHeadingWidget->mPageCombo,"dataForm_Combo"); |
|
132 QEvent polishEvent(QEvent::Polish); |
|
133 QCoreApplication::sendEvent(mHeadingWidget->mPageCombo, &polishEvent); |
|
134 // setFormHeading to the layout |
|
135 if(mHeadingWidget->mPageCombo || !mHeadingWidget->mDescription.isEmpty() || |
|
136 !mHeadingWidget->mHeading.isEmpty()) { |
|
137 static_cast<HbDataItemContainer*>(q->container())->setFormHeading(mHeadingWidget); |
|
138 } |
|
139 QObject::connect(mHeadingWidget->mPageCombo,SIGNAL(currentIndexChanged(int)), |
|
140 q,SLOT(_q_page_changed(int))); |
|
141 } |
|
142 |
|
143 // Get the model and add the page string to the model of combobox |
|
144 QStringListModel *model = static_cast<QStringListModel*>(mHeadingWidget->mPageCombo->model()); |
|
145 if(!model) { |
|
146 QStringList list; |
|
147 model = new QStringListModel(list); |
|
148 } |
|
149 |
|
150 QStringList list = model->stringList(); |
|
151 if(!list.contains(page)){ |
|
152 list.append(page); |
|
153 model->setStringList(list); |
|
154 mHeadingWidget->mPageCombo->setModel(model); |
|
155 //mHeadingWidget->mPageCombo->setCurrentIndex(model->index(0,0)); |
|
156 mHeadingWidget->mPageCombo->setCurrentIndex(0); |
|
157 _q_page_changed((mHeadingWidget->mPageCombo)->currentIndex()); |
|
158 } |
|
159 mHeadingWidget->updatePrimitives(); |
|
160 } |
|
161 |
|
162 void HbDataFormPrivate::removeFormPage(const QString& page) |
|
163 { |
|
164 Q_Q(HbDataForm); |
|
165 |
|
166 if(mHeadingWidget && mHeadingWidget->mPageCombo) { |
|
167 if(mHeadingWidget->mPageCombo->currentText() == page){ |
|
168 if(mHeadingWidget->mActivePage != 0) { |
|
169 mHeadingWidget->mPageCombo->setCurrentIndex(0); |
|
170 } |
|
171 else { |
|
172 mHeadingWidget->mPageCombo->setCurrentIndex(mHeadingWidget->mActivePage+1); |
|
173 } |
|
174 } |
|
175 } |
|
176 QObject::disconnect(mHeadingWidget->mPageCombo,SIGNAL(currentIndexChanged(int)), |
|
177 q,SLOT(_q_page_changed(int))); |
|
178 |
|
179 mHeadingWidget->mPageCombo->removeItem(mHeadingWidget->mPageCombo->findText(page)); |
|
180 |
|
181 QObject::connect(mHeadingWidget->mPageCombo,SIGNAL(currentIndexChanged(int)), |
|
182 q,SLOT(_q_page_changed(int))); |
|
183 |
|
184 mHeadingWidget->callPolish(); |
|
185 } |
|
186 |
|
187 void HbDataFormPrivate::_q_item_displayed(const QModelIndex &index) |
|
188 { |
|
189 Q_Q( HbDataForm); |
|
190 emit q->itemShown(index); |
|
191 emit q->activated(index); |
|
192 qWarning("activated signal will not be emitted when items are created ," |
|
193 "instead itemShown SIGNAL should be used"); |
|
194 } |
|
195 |
|
196 void HbDataFormPrivate::makeConnection(QModelIndex index) |
|
197 { |
|
198 Q_Q( HbDataForm); |
|
199 if(!index.isValid()){ |
|
200 return; |
|
201 } |
|
202 if(q->model()) { |
|
203 HbDataFormModelItem *modelItem = static_cast<HbDataFormModel *>(q->model())->itemFromIndex(index); |
|
204 if(modelItem){ |
|
205 QList<ItemSignal> signalList = mConnectionList.values(modelItem); |
|
206 if(signalList.count() > 0){ |
|
207 HbDataFormViewItem *viewItem = q->dataFormViewItem(index); |
|
208 if(viewItem){ |
|
209 HbWidget *contentWidget = viewItem->dataItemContentWidget(); |
|
210 if(contentWidget){ |
|
211 foreach(ItemSignal signal, signalList) { |
|
212 QObject *objct = signal.reciever; |
|
213 QString signalName = signal.signal; |
|
214 QString slot = signal.slot; |
|
215 // Make connection |
|
216 QObject::connect(contentWidget, signalName.toAscii().data(), |
|
217 objct,slot.toAscii().data()); |
|
218 |
|
219 } |
|
220 } |
|
221 } |
|
222 } |
|
223 } |
|
224 } |
|
225 } |
|
226 |
|
227 void HbDataFormPrivate::removeConnection(HbDataFormModelItem * modelItem, |
|
228 QString signal, |
|
229 QObject *reciever, |
|
230 QString slot) |
|
231 { |
|
232 Q_Q( HbDataForm); |
|
233 if(q->model()) { |
|
234 if(modelItem){ |
|
235 QList<ItemSignal> signalList = mConnectionList.values(modelItem); |
|
236 mConnectionList.remove(modelItem); |
|
237 if(signalList.count() > 0){ |
|
238 QModelIndex index = |
|
239 static_cast<HbDataFormModel*>(modelItem->model())->indexFromItem(modelItem); |
|
240 HbDataFormViewItem *viewItem = q->dataFormViewItem(index); |
|
241 if(viewItem){ |
|
242 HbWidget *contentWidget = viewItem->dataItemContentWidget(); |
|
243 if(contentWidget){ |
|
244 //foreach(ItemSignal signalItem, signalList) { |
|
245 for(int i = 0; i < signalList.count() ;i++){ |
|
246 ItemSignal signalItem = signalList.at(i); |
|
247 if(reciever == signalItem.reciever && |
|
248 signal == signalItem.signal && |
|
249 slot == signalItem.slot){ |
|
250 // disconnect |
|
251 QObject::disconnect(contentWidget, signal.toAscii().data(), |
|
252 reciever,slot.toAscii().data()); |
|
253 signalList.removeAt(i); |
|
254 for(int j = 0; j < signalList.count(); j++){ |
|
255 mConnectionList.insertMulti(modelItem, signalList.at(j)); |
|
256 } |
|
257 break; |
|
258 } |
|
259 } |
|
260 } |
|
261 } |
|
262 } |
|
263 } |
|
264 } |
|
265 } |
|
266 |
|
267 void HbDataFormPrivate::connectNow(HbDataFormModelItem * modelItem, |
|
268 QString signal, |
|
269 QObject *reciever, |
|
270 QString slot) |
|
271 { |
|
272 Q_Q( HbDataForm); |
|
273 QModelIndex index = static_cast<HbDataFormModel*>(modelItem->model())->indexFromItem(modelItem); |
|
274 Q_UNUSED(index); |
|
275 if(q->model()) { |
|
276 if(modelItem){ |
|
277 HbDataFormViewItem *viewItem = q->dataFormViewItem(index); |
|
278 if(viewItem){ |
|
279 HbWidget *contentWidget = viewItem->dataItemContentWidget(); |
|
280 // Make connection |
|
281 if(contentWidget){ |
|
282 QObject::connect(contentWidget, signal.toAscii().data(), |
|
283 reciever,slot.toAscii().data()); |
|
284 } |
|
285 } |
|
286 } |
|
287 } |
|
288 } |
|
289 |
|
290 void HbDataFormPrivate::removeAllConnection() |
|
291 { |
|
292 Q_Q( HbDataForm); |
|
293 if(q->model()) { |
|
294 QList<HbDataFormModelItem*> keys = mConnectionList.uniqueKeys(); |
|
295 |
|
296 foreach(HbDataFormModelItem* item ,keys) { |
|
297 QList<ItemSignal> signalList = mConnectionList.values(item); |
|
298 mConnectionList.remove(item); |
|
299 if(signalList.count() > 0){ |
|
300 QModelIndex index = static_cast<HbDataFormModel*>(item->model())->indexFromItem(item); |
|
301 HbDataFormViewItem *viewItem = q->dataFormViewItem(index); |
|
302 if(viewItem){ |
|
303 HbWidget *contentWidget = viewItem->dataItemContentWidget(); |
|
304 // disconnect signal and remove signal from list |
|
305 for(int i = 0;i<signalList.count();) { |
|
306 ItemSignal signalItem = signalList.takeAt(i); |
|
307 // Make connection |
|
308 QObject::disconnect(contentWidget, signalItem.signal.toAscii().data(), |
|
309 signalItem.reciever,signalItem.slot.toAscii().data()); |
|
310 |
|
311 |
|
312 } |
|
313 } |
|
314 } |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 void HbDataFormPrivate::removeAllConnection(HbDataFormModelItem *modelItem) |
|
320 { |
|
321 Q_Q( HbDataForm); |
|
322 if(q->model()) { |
|
323 if(modelItem){ |
|
324 QList<ItemSignal> signalList = mConnectionList.values(modelItem); |
|
325 mConnectionList.remove(modelItem); |
|
326 if(signalList.count() > 0){ |
|
327 QModelIndex index = static_cast<HbDataFormModel*>(modelItem->model())->indexFromItem(modelItem); |
|
328 HbDataFormViewItem *viewItem = q->dataFormViewItem(index); |
|
329 if(viewItem){ |
|
330 HbWidget *contentWidget = viewItem->dataItemContentWidget(); |
|
331 // disconnect signal and remove signal from list |
|
332 for(int i = 0;i<signalList.count(); ) { |
|
333 ItemSignal signalItem = signalList.takeAt(i); |
|
334 // Make connection |
|
335 QObject::disconnect(contentWidget, signalItem.signal.toAscii().data(), |
|
336 signalItem.reciever,signalItem.slot.toAscii().data()); |
|
337 } |
|
338 } |
|
339 } |
|
340 } |
|
341 } |
|
342 } |
|
343 |