| 230 | } |
| 231 | |
| 232 | void SortingProxyModel::sortMapping( SortingProxyModel::Mapping& mapping, int column, |
| 233 | SortOrder sortOrder ) { |
| 234 | if ( column == -1 ) { |
| 235 | int rowCount = source().rowCount( mapping.sourceParent ); |
| 236 | for ( int i = 0; i < rowCount; ++i ) { |
| 237 | mapping.sourceRows[i] = i; |
| 238 | mapping.proxyRows[i] = i; |
| 239 | } |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | auto oldSourceRows = mapping.sourceRows; |
| 244 | |
| 245 | int rowCount = source().rowCount( mapping.sourceParent ); |
| 246 | for ( int i = 0; i < rowCount; ++i ) |
| 247 | mapping.sourceRows[i] = i; |
| 248 | |
| 249 | std::stable_sort( |
| 250 | mapping.sourceRows.begin(), mapping.sourceRows.end(), [&]( auto row1, auto row2 ) -> bool { |
| 251 | bool isLessThan = |
| 252 | this->lessThan( mSource->index( row1, column, mapping.sourceParent ), |
| 253 | mSource->index( row2, column, mapping.sourceParent ) ); |
| 254 | return sortOrder == SortOrder::Ascending ? isLessThan : !isLessThan; |
| 255 | } ); |
| 256 | |
| 257 | for ( int i = 0; i < rowCount; ++i ) |
| 258 | mapping.proxyRows[mapping.sourceRows[i]] = i; |
| 259 | |
| 260 | // FIXME: I really feel like this should be done at the view layer somehow. |
| 261 | forEachView( [&]( UIAbstractView* view ) { |
| 262 | // Update the view's selection. |
| 263 | view->getSelection().changeFromModel( [&]( ModelSelection& selection ) { |
| 264 | std::vector<ModelIndex> selectedIndexesInSource; |
| 265 | std::vector<ModelIndex> staleIndexesInSelection; |
| 266 | selection.forEachIndex( [&]( const ModelIndex& index ) { |
| 267 | if ( index.parent() == mapping.sourceParent ) { |
| 268 | staleIndexesInSelection.push_back( index ); |
| 269 | selectedIndexesInSource.push_back( source().index( |
| 270 | oldSourceRows[index.row()], index.column(), mapping.sourceParent ) ); |
| 271 | } |
| 272 | } ); |
| 273 | |
| 274 | for ( auto& index : staleIndexesInSelection ) |
| 275 | selection.remove( index ); |
| 276 | |
| 277 | for ( auto& index : selectedIndexesInSource ) { |
| 278 | for ( size_t i = 0; i < mapping.sourceRows.size(); ++i ) { |
| 279 | if ( mapping.sourceRows[i] == index.row() ) { |
| 280 | auto newSourceIndex = |
| 281 | this->index( i, index.column(), mapping.sourceParent ); |
| 282 | selection.add( newSourceIndex ); |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } ); |
| 288 | } ); |
| 289 | } |
nothing calls this directly
no test coverage detected