|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "radiostationfiltermodel.h" |
|
19 #include "radiouiengine.h" |
|
20 #include "radiostationmodel.h" |
|
21 #include "radiologger.h" |
|
22 |
|
23 #include "radiostation.h" // Remove |
|
24 |
|
25 /*! |
|
26 * |
|
27 */ |
|
28 RadioStationFilterModel::RadioStationFilterModel( RadioUiEngine& uiEngine, QObject* parent ) : |
|
29 QSortFilterProxyModel( parent ), |
|
30 mUiEngine( uiEngine ), |
|
31 mIsCyclic( false ) |
|
32 { |
|
33 setSourceModel( &mUiEngine.stationModel() ); |
|
34 setDynamicSortFilter( true ); |
|
35 } |
|
36 |
|
37 /*! |
|
38 * |
|
39 */ |
|
40 RadioStationFilterModel::~RadioStationFilterModel() |
|
41 { |
|
42 } |
|
43 |
|
44 /*! |
|
45 * \reimp |
|
46 */ |
|
47 int RadioStationFilterModel::rowCount( const QModelIndex& parent ) const |
|
48 { |
|
49 const int count = QSortFilterProxyModel::rowCount( parent ); |
|
50 return mIsCyclic ? count * 2 : count; |
|
51 } |
|
52 |
|
53 /*! |
|
54 * \reimp |
|
55 */ |
|
56 QModelIndex RadioStationFilterModel::index( int row, int column, |
|
57 const QModelIndex &parent ) const |
|
58 { |
|
59 const int count = QSortFilterProxyModel::rowCount(); |
|
60 if ( row < count ) { |
|
61 return QSortFilterProxyModel::index( row, column, parent ); |
|
62 } else { |
|
63 foreach ( const QModelIndex& shadowIndex, mShadowIndexes.keys() ) { |
|
64 if ( shadowIndex.row() == row ) { |
|
65 return shadowIndex; |
|
66 } |
|
67 } |
|
68 |
|
69 return QModelIndex(); |
|
70 } |
|
71 } |
|
72 |
|
73 /*! |
|
74 * \reimp |
|
75 */ |
|
76 QVariant RadioStationFilterModel::data( const QModelIndex& index, int role ) const |
|
77 { |
|
78 QModelIndex dataIndex = index; |
|
79 if ( mShadowIndexes.contains( index ) ) { |
|
80 dataIndex = mShadowIndexes.value( index ); |
|
81 } |
|
82 |
|
83 if ( !index.isValid() || !dataIndex.isValid() ) { |
|
84 return QVariant(); |
|
85 } |
|
86 |
|
87 return QSortFilterProxyModel::data( dataIndex, role ); |
|
88 } |
|
89 |
|
90 /*! |
|
91 * |
|
92 */ |
|
93 void RadioStationFilterModel::setTypeFilter( RadioStation::Type filter ) |
|
94 { |
|
95 mFilter = filter; |
|
96 filterChanged(); |
|
97 } |
|
98 |
|
99 /*! |
|
100 * Returns the model index corresponding to the given frequency |
|
101 */ |
|
102 QModelIndex RadioStationFilterModel::modelIndexFromFrequency( uint frequency ) |
|
103 { |
|
104 QModelIndex index = static_cast<RadioStationModel*>( sourceModel() )->modelIndexFromFrequency( frequency ); |
|
105 return mapFromSource( index ); |
|
106 } |
|
107 |
|
108 /*! |
|
109 * |
|
110 */ |
|
111 void RadioStationFilterModel::setCyclic( bool cyclic ) |
|
112 { |
|
113 mIsCyclic = cyclic; |
|
114 if ( mIsCyclic ) { |
|
115 const int realCount = QSortFilterProxyModel::rowCount(); |
|
116 LOG_FORMAT( "Station count: %d", realCount ); |
|
117 for ( int i = 0; i < realCount; ++i ) { |
|
118 QModelIndex realIndex = QSortFilterProxyModel::index( i, 0 ); |
|
119 QModelIndex shadowIndex = createIndex( i + realCount, 0, realIndex.internalPointer() ); |
|
120 //const uint freq = realIndex.data( RadioStationModel::RadioStationRole ).value<RadioStation>().frequency(); |
|
121 // LOG_FORMAT( "Adding shadow index %d for index %d. Freq: %u", shadowIndex.row(), realIndex.row(), freq ); |
|
122 mShadowIndexes.insert( shadowIndex, realIndex ); |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 /*! |
|
128 * |
|
129 */ |
|
130 bool RadioStationFilterModel::hasLooped( const QModelIndex& index ) const |
|
131 { |
|
132 return mShadowIndexes.contains( index ); |
|
133 } |
|
134 |
|
135 /*! |
|
136 * |
|
137 */ |
|
138 QModelIndex RadioStationFilterModel::realIndex( const QModelIndex& shadowIndex ) const |
|
139 { |
|
140 return mShadowIndexes.value( shadowIndex ); |
|
141 } |
|
142 |
|
143 /*! |
|
144 * |
|
145 */ |
|
146 bool RadioStationFilterModel::isEqual( const QModelIndex& first, const QModelIndex& second ) const |
|
147 { |
|
148 if ( first == second ) { |
|
149 return true; |
|
150 } |
|
151 |
|
152 QModelIndex realFirst = first; |
|
153 if ( mShadowIndexes.contains( first ) ) { |
|
154 realFirst = mShadowIndexes.value( first ); |
|
155 } |
|
156 |
|
157 QModelIndex realSecond = second; |
|
158 if ( mShadowIndexes.contains( second ) ) { |
|
159 realSecond = mShadowIndexes.value( second ); |
|
160 } |
|
161 |
|
162 if ( realFirst == realSecond ) { |
|
163 return true; |
|
164 } |
|
165 |
|
166 return false; |
|
167 } |
|
168 |
|
169 /*! |
|
170 * |
|
171 */ |
|
172 bool RadioStationFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const |
|
173 { |
|
174 QAbstractItemModel* source = sourceModel(); |
|
175 QModelIndex index = source->index( sourceRow, 0, sourceParent ); |
|
176 const RadioStation station = source->data( index, RadioStationModel::RadioStationRole ).value<RadioStation>(); |
|
177 const bool isType = station.isType( mFilter ); |
|
178 return isType; |
|
179 } |