2011-08-28 23:20:22 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-06-10 22:11:55 +02:00
|
|
|
#include "globalsearchmodel.h"
|
2011-08-28 23:20:22 +02:00
|
|
|
#include "globalsearchsortmodel.h"
|
|
|
|
#include "searchprovider.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
|
|
|
GlobalSearchSortModel::GlobalSearchSortModel(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QSortFilterProxyModel(parent) {}
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool GlobalSearchSortModel::lessThan(const QModelIndex& left,
|
|
|
|
const QModelIndex& right) const {
|
2012-06-04 19:18:37 +02:00
|
|
|
// Compare the provider sort index first.
|
2014-02-07 16:34:20 +01:00
|
|
|
const int index_left =
|
|
|
|
left.data(GlobalSearchModel::Role_ProviderIndex).toInt();
|
|
|
|
const int index_right =
|
|
|
|
right.data(GlobalSearchModel::Role_ProviderIndex).toInt();
|
2012-06-04 19:18:37 +02:00
|
|
|
if (index_left < index_right) return true;
|
|
|
|
if (index_left > index_right) return false;
|
|
|
|
|
|
|
|
// Dividers always go first
|
2014-02-07 16:34:20 +01:00
|
|
|
if (left.data(LibraryModel::Role_IsDivider).toBool()) return true;
|
2012-06-04 19:18:37 +02:00
|
|
|
if (right.data(LibraryModel::Role_IsDivider).toBool()) return false;
|
|
|
|
|
|
|
|
// Containers go before songs if they're at the same level
|
2014-02-07 16:34:20 +01:00
|
|
|
const bool left_is_container =
|
|
|
|
left.data(LibraryModel::Role_ContainerType).isValid();
|
|
|
|
const bool right_is_container =
|
|
|
|
right.data(LibraryModel::Role_ContainerType).isValid();
|
2012-06-04 19:18:37 +02:00
|
|
|
if (left_is_container && !right_is_container) return true;
|
|
|
|
if (right_is_container && !left_is_container) return false;
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// Containers get sorted on their sort text.
|
|
|
|
if (left_is_container) {
|
|
|
|
return QString::localeAwareCompare(
|
2014-02-07 16:34:20 +01:00
|
|
|
left.data(LibraryModel::Role_SortText).toString(),
|
|
|
|
right.data(LibraryModel::Role_SortText).toString()) < 0;
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
2011-09-29 23:29:20 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// Otherwise we're comparing songs. Sort by disc, track, then title.
|
2014-02-07 16:34:20 +01:00
|
|
|
const SearchProvider::Result r1 =
|
|
|
|
left.data(GlobalSearchModel::Role_Result).value<SearchProvider::Result>();
|
2012-06-10 22:11:55 +02:00
|
|
|
const SearchProvider::Result r2 = right.data(GlobalSearchModel::Role_Result)
|
2014-02-07 16:34:20 +01:00
|
|
|
.value<SearchProvider::Result>();
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#define CompareInt(field) \
|
2012-06-04 19:18:37 +02:00
|
|
|
if (r1.metadata_.field() < r2.metadata_.field()) return true; \
|
|
|
|
if (r1.metadata_.field() > r2.metadata_.field()) return false
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2011-09-29 23:29:20 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#define CompareString(field) \
|
|
|
|
ret = \
|
|
|
|
QString::localeAwareCompare(r1.metadata_.field(), r2.metadata_.field()); \
|
|
|
|
if (ret < 0) return true; \
|
2011-08-29 00:24:54 +02:00
|
|
|
if (ret > 0) return false
|
2011-08-28 23:20:22 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
CompareInt(disc);
|
|
|
|
CompareInt(track);
|
|
|
|
CompareString(title);
|
2011-08-28 23:20:22 +02:00
|
|
|
|
|
|
|
return false;
|
2011-08-29 00:24:54 +02:00
|
|
|
|
|
|
|
#undef CompareInt
|
|
|
|
#undef CompareString
|
2011-08-28 23:20:22 +02:00
|
|
|
}
|