|
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 <btuimodelsortfilter.h> |
|
19 #include <btdevicemodel.h> |
|
20 |
|
21 /*! |
|
22 Constructor. |
|
23 */ |
|
24 BtuiModelSortFilter::BtuiModelSortFilter( QObject *parent ) |
|
25 : QSortFilterProxyModel( parent ) |
|
26 { |
|
27 setDynamicSortFilter( true ); |
|
28 } |
|
29 |
|
30 /*! |
|
31 Destructor. |
|
32 */ |
|
33 BtuiModelSortFilter::~BtuiModelSortFilter() |
|
34 { |
|
35 } |
|
36 |
|
37 /*! |
|
38 Replace current filter values for filtering on major device class with |
|
39 the specified. This Model will not reset itself for performance reason, |
|
40 the caller shall call reset after all filters have been set. |
|
41 */ |
|
42 void BtuiModelSortFilter::setDeviceMajorFilter( int filter, FilterMode mode ) |
|
43 { |
|
44 mFilters.clear(); |
|
45 addDeviceMajorFilter( filter, mode ); |
|
46 } |
|
47 |
|
48 /*! |
|
49 Add the specified filter value for filtering on major device class |
|
50 if the specified filter doesn't exist when this function is called. |
|
51 This Model will not reset itself for performance reason, |
|
52 the caller shall call reset after all filters have been set. |
|
53 */ |
|
54 void BtuiModelSortFilter::addDeviceMajorFilter( int filter, FilterMode mode ) |
|
55 { |
|
56 FilterItem f(filter, mode); |
|
57 if ( mFilters.indexOf(f) == -1 ) { |
|
58 mFilters.append( f ); |
|
59 } |
|
60 } |
|
61 |
|
62 /*! |
|
63 Clear the specified filter value for filtering on major device class from this model. |
|
64 This Model will not reset itself for performance reason, |
|
65 the caller shall call reset after all filters have been set. |
|
66 */ |
|
67 void BtuiModelSortFilter::clearDeviceMajorFilter( int filter, FilterMode mode ) |
|
68 { |
|
69 FilterItem f(filter, mode); |
|
70 int i = mFilters.indexOf(f); |
|
71 if ( i > -1 ) { |
|
72 mFilters.removeAt( i ); |
|
73 } |
|
74 } |
|
75 |
|
76 /*! |
|
77 clear all filters for filtering on major device class. |
|
78 This Model will not reset itself for performance reason, |
|
79 the caller shall call reset after all filters have been set. |
|
80 */ |
|
81 void BtuiModelSortFilter::clearDeviceMajorFilters() |
|
82 { |
|
83 // model reset is needed if there are filters : |
|
84 bool shReset = ( mFilters.size() > 0 ); |
|
85 |
|
86 if ( shReset ) { |
|
87 reset(); |
|
88 } |
|
89 mFilters.clear(); |
|
90 } |
|
91 |
|
92 /*! |
|
93 return true if the specified filter exists; return false otherwise. |
|
94 */ |
|
95 bool BtuiModelSortFilter::hasFilter( int filter, FilterMode mode ) |
|
96 { |
|
97 FilterItem f(filter, mode); |
|
98 return mFilters.indexOf(f) > -1 ; |
|
99 } |
|
100 |
|
101 /*! |
|
102 \reimp |
|
103 */ |
|
104 bool BtuiModelSortFilter::filterAcceptsRow( |
|
105 int sourceRow, const QModelIndex &sourceParent) const |
|
106 { |
|
107 bool accepted (false ); |
|
108 // Get the device name from the model |
|
109 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); |
|
110 |
|
111 // the row shall pass all filters: |
|
112 for (int i = 0; i < mFilters.size(); i++ ) { |
|
113 if ( mFilters.at(i).mFilter == BtDeviceModel::NullProperty ) { |
|
114 accepted = true; // There is no filter, we accept all |
|
115 } |
|
116 else { |
|
117 int majorProperty = |
|
118 sourceModel()->data(index, BtDeviceModel::MajorPropertyRole).toInt(); |
|
119 switch (mFilters.at(i).mMode) { |
|
120 case ExactMatch: |
|
121 // Accept if the match is spot-on |
|
122 accepted = majorProperty == mFilters.at(i).mFilter ; |
|
123 break; |
|
124 case AtLeastMatch: |
|
125 // accept if it matches all specified filters: |
|
126 accepted = ( mFilters.at(i).mFilter == |
|
127 ( majorProperty & mFilters.at(i).mFilter ) ); |
|
128 break; |
|
129 case RoughMatch: |
|
130 // Accept if it matches one of specified filters: |
|
131 accepted = (majorProperty & mFilters.at(i).mFilter) != 0; |
|
132 break; |
|
133 case Exclusive: |
|
134 // Accept if this is not one that we want to filter out |
|
135 accepted = (majorProperty & mFilters.at(i).mFilter) == 0; |
|
136 break; |
|
137 default: |
|
138 accepted = false; |
|
139 } |
|
140 } |
|
141 } |
|
142 if (accepted) { |
|
143 // emit signal to inform a row has been accepted by fitler, |
|
144 // currently this is only needed by search view |
|
145 emit const_cast<BtuiModelSortFilter*>(this)->deviceAcceptedByFilter( sourceRow ); |
|
146 } |
|
147 return accepted; |
|
148 } |
|
149 |
|
150 /*! |
|
151 \reimp |
|
152 */ |
|
153 bool BtuiModelSortFilter::lessThan( |
|
154 const QModelIndex &left, const QModelIndex &right) const |
|
155 { |
|
156 Q_UNUSED( left ); |
|
157 Q_UNUSED( right ); |
|
158 if (sortRole() == BtDeviceModel::NameAliasRole || |
|
159 sortRole() == BtDeviceModel::LastUsedTimeRole || |
|
160 sortRole() == BtDeviceModel::RssiRole) { |
|
161 // base class provides sorting for these types already: |
|
162 return QSortFilterProxyModel::lessThan(left, right); |
|
163 } |
|
164 // no custom sort supported yet. |
|
165 return true; |
|
166 } |