File indexing completed on 2024-05-19 04:49:18

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "MemoryQueryMakerHelper.h"
0018 
0019 #include "core-impl/collections/support/MemoryCustomValue.h"
0020 #include "core/meta/Meta.h"
0021 
0022 #include <QList>
0023 #include <QSet>
0024 #include <QStack>
0025 #include <QtAlgorithms>
0026 
0027 //#include <QRandomGenerator>
0028 #include <KSortableList>
0029 
0030 using namespace Collections;
0031 
0032 template <class PointerType>
0033 QList<PointerType>
0034 MemoryQueryMakerHelper::orderListByName( const QList<PointerType> &list, bool descendingOrder )
0035 {
0036     QList<PointerType> resultList = list;
0037     KSortableList<PointerType, QString> sortList;
0038     foreach( const PointerType &pointer, list )
0039     {
0040         sortList.insert( pointer->name(), pointer );
0041     }
0042     sortList.sort();
0043     QList<PointerType> tmpList;
0044     typedef KSortableItem<PointerType,QString> SortItem;
0045     foreach( const SortItem &item, sortList )
0046     {
0047        tmpList.append( item.second );
0048     }
0049     if( descendingOrder )
0050     {
0051         //KSortableList uses qSort, which orders a list in ascending order
0052         resultList = reverse<PointerType>( tmpList );
0053     }
0054     else
0055     {
0056         resultList = tmpList;
0057     }
0058     return resultList;
0059 }
0060 
0061 Meta::YearList
0062 MemoryQueryMakerHelper::orderListByYear( const Meta::YearList &list, bool descendingOrder )
0063 {
0064     KSortableList<Meta::YearPtr, double> sortList;
0065     foreach( Meta::YearPtr pointer, list )
0066     {
0067         sortList.insert( pointer->name().toDouble(), pointer );
0068     }
0069     sortList.sort();
0070     QList<Meta::YearPtr> tmpList;
0071     typedef KSortableItem<Meta::YearPtr,double> SortItem;
0072     foreach( const SortItem &item, sortList )
0073     {
0074         tmpList.append( item.second );
0075     }
0076     if( descendingOrder )
0077     {
0078         //KSortableList uses qSort, which orders a list in ascending order
0079         return reverse<Meta::YearPtr>( tmpList );
0080     }
0081     else
0082     {
0083         return tmpList;
0084     }
0085 }
0086 
0087 template<typename T>
0088 QList<T>
0089 MemoryQueryMakerHelper::reverse(const QList<T> &l)
0090 {
0091     QList<T> ret;
0092     for (int i=l.size() - 1; i>=0; --i)
0093         ret.append(l.at(i));
0094     return ret;
0095 }
0096 
0097 Meta::TrackList
0098 MemoryQueryMakerHelper::orderListByString( const Meta::TrackList &tracks, qint64 value, bool orderDescending )
0099 {
0100     Meta::TrackList resultList = tracks;
0101     CustomReturnValue *crv = CustomValueFactory::returnValue( value );
0102     if( crv )
0103     {
0104         KSortableList<Meta::TrackPtr, QString> sortList;
0105         foreach( const Meta::TrackPtr &pointer, tracks )
0106         {
0107             sortList.insert( crv->value( pointer ), pointer );
0108         }
0109         sortList.sort();
0110         Meta::TrackList tmpList;
0111         typedef KSortableItem<Meta::TrackPtr,QString> SortItem;
0112         foreach( const SortItem &item, sortList )
0113         {
0114            tmpList.append( item.second );
0115         }
0116         if( orderDescending )
0117         {
0118             //KSortableList uses qSort, which orders a list in ascending order
0119             resultList = reverse<Meta::TrackPtr>( tmpList );
0120         }
0121         else
0122         {
0123             resultList = tmpList;
0124         }
0125     }
0126     delete crv;
0127     return resultList;
0128 }
0129 
0130 Meta::TrackList
0131 MemoryQueryMakerHelper::orderListByNumber( const Meta::TrackList &tracks, qint64 value, bool orderDescending )
0132 {
0133     Meta::TrackList resultList = tracks;
0134     CustomReturnValue *crv = CustomValueFactory::returnValue( value );
0135     if( crv )
0136     {
0137         KSortableList<Meta::TrackPtr, double> sortList;
0138         foreach( const Meta::TrackPtr &pointer, tracks )
0139         {
0140             sortList.insert( crv->value( pointer ).toDouble(), pointer );
0141         }
0142         sortList.sort();
0143         Meta::TrackList tmpList;
0144         typedef KSortableItem<Meta::TrackPtr,double> SortItem;
0145         foreach( const SortItem &item, sortList )
0146         {
0147            tmpList.append( item.second );
0148         }
0149         if( orderDescending )
0150         {
0151             //KSortableList uses qSort, which orders a list in ascending order
0152             resultList = reverse<Meta::TrackPtr>( tmpList );
0153         }
0154         else
0155         {
0156             resultList = tmpList;
0157         }
0158     }
0159     delete crv;
0160     return resultList;
0161 }
0162 
0163 template QList<Meta::AlbumPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::AlbumPtr > &list, bool );
0164 template QList<Meta::ArtistPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::ArtistPtr > &list, bool );
0165 template QList<Meta::GenrePtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::GenrePtr > &list, bool );
0166 template QList<Meta::ComposerPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::ComposerPtr > &list, bool );
0167 template QList<Meta::LabelPtr> MemoryQueryMakerHelper::orderListByName( const QList<Meta::LabelPtr > &list, bool );
0168