Use common classes for Tidal and Deezer
This commit is contained in:
79
src/internet/internetsearchsortmodel.cpp
Normal file
79
src/internet/internetsearchsortmodel.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This code was part of Clementine (GlobalSearch)
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QString>
|
||||
|
||||
#include "core/logging.h"
|
||||
#include "internetsearchmodel.h"
|
||||
#include "internetsearchsortmodel.h"
|
||||
|
||||
InternetSearchSortModel::InternetSearchSortModel(QObject *parent)
|
||||
: QSortFilterProxyModel(parent) {}
|
||||
|
||||
bool InternetSearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
|
||||
// Compare the provider sort index first.
|
||||
const int index_left = left.data(InternetSearchModel::Role_ProviderIndex).toInt();
|
||||
const int index_right = right.data(InternetSearchModel::Role_ProviderIndex).toInt();
|
||||
if (index_left < index_right) return true;
|
||||
if (index_left > index_right) return false;
|
||||
|
||||
// Dividers always go first
|
||||
if (left.data(CollectionModel::Role_IsDivider).toBool()) return true;
|
||||
if (right.data(CollectionModel::Role_IsDivider).toBool()) return false;
|
||||
|
||||
// Containers go before songs if they're at the same level
|
||||
const bool left_is_container = left.data(CollectionModel::Role_ContainerType).isValid();
|
||||
const bool right_is_container = right.data(CollectionModel::Role_ContainerType).isValid();
|
||||
if (left_is_container && !right_is_container) return true;
|
||||
if (right_is_container && !left_is_container) return false;
|
||||
|
||||
// Containers get sorted on their sort text.
|
||||
if (left_is_container) {
|
||||
return QString::localeAwareCompare(left.data(CollectionModel::Role_SortText).toString(), right.data(CollectionModel::Role_SortText).toString()) < 0;
|
||||
}
|
||||
|
||||
// Otherwise we're comparing songs. Sort by disc, track, then title.
|
||||
const InternetSearch::Result r1 = left.data(InternetSearchModel::Role_Result).value<InternetSearch::Result>();
|
||||
const InternetSearch::Result r2 = right.data(InternetSearchModel::Role_Result).value<InternetSearch::Result>();
|
||||
|
||||
#define CompareInt(field) \
|
||||
if (r1.metadata_.field() < r2.metadata_.field()) return true; \
|
||||
if (r1.metadata_.field() > r2.metadata_.field()) return false
|
||||
|
||||
int ret = 0;
|
||||
|
||||
#define CompareString(field) \
|
||||
ret = QString::localeAwareCompare(r1.metadata_.field(), r2.metadata_.field()); \
|
||||
if (ret < 0) return true; \
|
||||
if (ret > 0) return false
|
||||
|
||||
CompareInt(disc);
|
||||
CompareInt(track);
|
||||
CompareString(title);
|
||||
|
||||
return false;
|
||||
|
||||
#undef CompareInt
|
||||
#undef CompareString
|
||||
}
|
||||
Reference in New Issue
Block a user