|
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 "hbtreeitemselectionmodel_p.h" |
|
27 |
|
28 #include "hbabstractitemview_p.h" |
|
29 #include "hbtreeviewitem.h" |
|
30 #include "hbabstractitemcontainer.h" |
|
31 #include "hbmodeliterator.h" |
|
32 |
|
33 #include <QAbstractItemModel> |
|
34 #include <QModelIndex> |
|
35 #include <QItemSelectionRange> |
|
36 |
|
37 #include <QDebug> |
|
38 |
|
39 HbTreeItemSelectionModel::HbTreeItemSelectionModel( QAbstractItemModel *model, |
|
40 HbAbstractItemViewPrivate *viewPrivate, |
|
41 QObject *parent) : |
|
42 QItemSelectionModel(model, parent), |
|
43 mViewPrivate(viewPrivate) |
|
44 { |
|
45 Q_ASSERT_X(mViewPrivate, "HbTreeItemSelectionModel::HbTreeItemSelectionModel", "HbAbstractItemView is 0"); |
|
46 } |
|
47 |
|
48 HbTreeItemSelectionModel::~HbTreeItemSelectionModel() |
|
49 { |
|
50 } |
|
51 |
|
52 void HbTreeItemSelectionModel::addChildToSelection(QItemSelection &selection, const QModelIndex &index) |
|
53 { |
|
54 int childCount = model()->rowCount(index); |
|
55 for (int current = 0; current < childCount; ++current) { |
|
56 QModelIndex childIndex = model()->index(current, 0, index); |
|
57 addChildToSelection(selection, childIndex); |
|
58 } |
|
59 if (childCount > 0) { |
|
60 selection.select(model()->index(0, 0, index), |
|
61 model()->index(childCount-1, 0, index)); |
|
62 } |
|
63 } |
|
64 |
|
65 /* |
|
66 This function and HbTreeView::currentSelectionChanged() are involded in selection in treeview. |
|
67 This function selects indexed item and every it children. |
|
68 HbTreeView::currentSelectionChanged() handles unselection of deleselected items and partial selection |
|
69 of parents. |
|
70 There is one exception case to partial selection of parents. This exception case is implemented in this function. |
|
71 The exception case is: |
|
72 Case is 1) node is single selected, which has one child 2) then its child is single selected |
|
73 2) causes HbTreeView::currentSelectionChanged() to be called by with |
|
74 no selected items and the node as deselected item. The slot does not have no clue |
|
75 which item is selected (except by asking from model directly) |
|
76 */ |
|
77 void HbTreeItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command ) |
|
78 { |
|
79 if (command == NoUpdate) { |
|
80 return; |
|
81 } |
|
82 |
|
83 QModelIndexList indexes = selection.indexes(); |
|
84 int count = indexes.count(); |
|
85 for (int i = 0; i < count; ++i) { |
|
86 QModelIndex index = indexes.at(0); |
|
87 if (!index.isValid()) { |
|
88 return; |
|
89 } |
|
90 |
|
91 QItemSelection selectedItems; |
|
92 selectedItems.append(QItemSelectionRange(index, index)); |
|
93 addChildToSelection(selectedItems, index); |
|
94 |
|
95 // Toggle is not allowed. All of the items are either selected or deselected. |
|
96 // First item determines, which one is selected |
|
97 bool toggling = command & QItemSelectionModel::Toggle; |
|
98 bool selected = isSelected(index); |
|
99 if (toggling) { |
|
100 command &= ~QItemSelectionModel::Toggle; |
|
101 if (selected) { |
|
102 command |= QItemSelectionModel::Deselect; |
|
103 } else { |
|
104 command |= QItemSelectionModel::Select; |
|
105 } |
|
106 } |
|
107 |
|
108 QItemSelectionModel::select(selectedItems, command); |
|
109 |
|
110 // Partial selection exception (see function comment): |
|
111 if ( mViewPrivate->mSelectionMode == HbAbstractItemView::SingleSelection ) { |
|
112 QModelIndex parentIndex = index.parent(); |
|
113 if (model()->rowCount(parentIndex) == 1) { |
|
114 while (parentIndex != mViewPrivate->mModelIterator->rootIndex()) { |
|
115 if (!isSelected(parentIndex)) { |
|
116 HbAbstractViewItem *item = mViewPrivate->mContainer->itemByIndex(parentIndex); |
|
117 if (item) { |
|
118 item->setCheckState(Qt::PartiallyChecked); |
|
119 } |
|
120 mViewPrivate->mContainer->setItemStateValue(parentIndex, HbAbstractViewItem::CheckStateKey, Qt::PartiallyChecked); |
|
121 } |
|
122 parentIndex = parentIndex.parent(); |
|
123 } |
|
124 } |
|
125 } |
|
126 } |
|
127 } |
|
128 |