Changeset View
Changeset View
Standalone View
Standalone View
src/browsers/CollectionSortFilterProxyModel.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | |||||
53 | bool | 53 | bool | ||
54 | CollectionSortFilterProxyModel::hasChildren(const QModelIndex & parent) const | 54 | CollectionSortFilterProxyModel::hasChildren(const QModelIndex & parent) const | ||
55 | { | 55 | { | ||
56 | QModelIndex sourceParent = mapToSource(parent); | 56 | QModelIndex sourceParent = mapToSource(parent); | ||
57 | return sourceModel()->hasChildren(sourceParent); | 57 | return sourceModel()->hasChildren(sourceParent); | ||
58 | } | 58 | } | ||
59 | 59 | | |||
60 | bool | 60 | bool | ||
61 | CollectionSortFilterProxyModel::filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const | ||||
62 | { | ||||
63 | bool stringAccepted = QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent ); | ||||
64 | | ||||
65 | if( AmarokConfig::showYears()) | ||||
66 | { | ||||
67 | QModelIndex index = sourceModel()->index( source_row, 0, source_parent ); | ||||
68 | if( treeItem( index )->isAlbumItem() ) | ||||
69 | { | ||||
70 | bool yearLoaded = index.data( PrettyTreeRoles::YearRole ) >= 0; | ||||
71 | return yearLoaded && stringAccepted; | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | return stringAccepted; | ||||
76 | } | ||||
77 | | ||||
78 | bool | ||||
61 | CollectionSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const | 79 | CollectionSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const | ||
62 | { | 80 | { | ||
63 | CollectionTreeItem *leftItem = treeItem( left ); | 81 | CollectionTreeItem *leftItem = treeItem( left ); | ||
64 | CollectionTreeItem *rightItem = treeItem( right ); | 82 | CollectionTreeItem *rightItem = treeItem( right ); | ||
65 | 83 | | |||
66 | // various artists and no label items are always at the top | 84 | // various artists and no label items are always at the top | ||
67 | if( !leftItem || leftItem->isVariousArtistItem() || leftItem->isNoLabelItem() ) | 85 | if( !leftItem || leftItem->isVariousArtistItem() || leftItem->isNoLabelItem() ) | ||
68 | return true; | 86 | return true; | ||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Line(s) | 149 | { | |||
133 | error() << "Should never have compared these two indexes" | 151 | error() << "Should never have compared these two indexes" | ||
134 | << left.data(Qt::DisplayRole) << "and" << right.data(Qt::DisplayRole); | 152 | << left.data(Qt::DisplayRole) << "and" << right.data(Qt::DisplayRole); | ||
135 | return QSortFilterProxyModel::lessThan( left, right ); | 153 | return QSortFilterProxyModel::lessThan( left, right ); | ||
136 | } | 154 | } | ||
137 | 155 | | |||
138 | // compare by year | 156 | // compare by year | ||
139 | if( AmarokConfig::showYears() ) | 157 | if( AmarokConfig::showYears() ) | ||
140 | { | 158 | { | ||
141 | int leftYear = albumYear( leftAlbum ); | 159 | int leftYear = left.data( PrettyTreeRoles::YearRole ).toInt(); | ||
142 | int rightYear = albumYear( rightAlbum ); | 160 | int rightYear = right.data( PrettyTreeRoles::YearRole ).toInt(); | ||
143 | 161 | | |||
144 | if( leftYear < rightYear ) | 162 | if( leftYear < rightYear ) | ||
145 | return false; // left album is newer | 163 | return false; // left album is newer | ||
146 | if( leftYear > rightYear ) | 164 | if( leftYear > rightYear ) | ||
147 | return true; | 165 | return true; | ||
148 | } | 166 | } | ||
149 | 167 | | |||
150 | // compare by name | 168 | // compare by name | ||
151 | { | 169 | { | ||
152 | int comp = m_col->compare( leftAlbum->sortableName(), rightAlbum->sortableName() ); | 170 | int comp = m_col->compare( leftAlbum->sortableName(), rightAlbum->sortableName() ); | ||
153 | if( comp < 0 ) | 171 | if( comp < 0 ) | ||
154 | return true; | 172 | return true; | ||
155 | if( comp > 0 ) | 173 | if( comp > 0 ) | ||
156 | return false; | 174 | return false; | ||
157 | } | 175 | } | ||
158 | 176 | | |||
159 | return leftAlbum.data() < rightAlbum.data(); // prevent expanded albums from switching places | 177 | return leftAlbum.data() < rightAlbum.data(); // prevent expanded albums from switching places | ||
160 | } | 178 | } | ||
161 | 179 | | |||
162 | bool | 180 | bool | ||
163 | CollectionSortFilterProxyModel::lessThanItem( const QModelIndex &left, const QModelIndex &right ) const | 181 | CollectionSortFilterProxyModel::lessThanItem( const QModelIndex &left, const QModelIndex &right ) const | ||
164 | { | 182 | { | ||
165 | Meta::DataPtr leftData = Meta::DataPtr::dynamicCast( treeItem(left)->data() ); | 183 | Meta::DataPtr leftData = treeItem(left)->data(); | ||
166 | Meta::DataPtr rightData = Meta::DataPtr::dynamicCast( treeItem(right)->data() ); | 184 | Meta::DataPtr rightData = treeItem(right)->data(); | ||
167 | 185 | | |||
168 | if( !leftData || !rightData ) | 186 | if( !leftData || !rightData ) | ||
169 | { | 187 | { | ||
170 | DEBUG_BLOCK | 188 | DEBUG_BLOCK | ||
171 | error() << "Should never have compared these two indexes" | 189 | error() << "Should never have compared these two indexes" | ||
172 | << left.data(Qt::DisplayRole) << "and" << right.data(Qt::DisplayRole); | 190 | << left.data(Qt::DisplayRole) << "and" << right.data(Qt::DisplayRole); | ||
173 | return QSortFilterProxyModel::lessThan( left, right ); | 191 | return QSortFilterProxyModel::lessThan( left, right ); | ||
174 | } | 192 | } | ||
Show All 10 Lines | |||||
185 | return leftData.data() < rightData.data(); // prevent expanded data from switching places | 203 | return leftData.data() < rightData.data(); // prevent expanded data from switching places | ||
186 | } | 204 | } | ||
187 | 205 | | |||
188 | inline CollectionTreeItem* | 206 | inline CollectionTreeItem* | ||
189 | CollectionSortFilterProxyModel::treeItem( const QModelIndex &index ) const | 207 | CollectionSortFilterProxyModel::treeItem( const QModelIndex &index ) const | ||
190 | { | 208 | { | ||
191 | return static_cast<CollectionTreeItem*>( index.internalPointer() ); | 209 | return static_cast<CollectionTreeItem*>( index.internalPointer() ); | ||
192 | } | 210 | } | ||
193 | | ||||
194 | int | | |||
195 | CollectionSortFilterProxyModel::albumYear( Meta::AlbumPtr album ) const | | |||
196 | { | | |||
197 | if( album->name().isEmpty() ) // an unnamed album has no year | | |||
198 | return 0; | | |||
199 | | ||||
200 | Meta::TrackList tracks = album->tracks(); | | |||
201 | if( !tracks.isEmpty() ) | | |||
202 | { | | |||
203 | Meta::YearPtr year = tracks.first()->year(); | | |||
204 | if( year && (year->year() != 0) ) | | |||
205 | return year->year(); | | |||
206 | } | | |||
207 | return 0; | | |||
208 | } | | |||
209 | |