1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "globalsearchwidget.h"
19
#include "globalsearchsortmodel.h"
20
#include "searchprovider.h"
21
#include "core/logging.h"
23
GlobalSearchSortModel::GlobalSearchSortModel(QObject* parent)
24
: QSortFilterProxyModel(parent)
28
bool GlobalSearchSortModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
29
const SearchProvider::Result r1 = left.data(GlobalSearchWidget::Role_PrimaryResult)
30
.value<SearchProvider::Result>();
31
const SearchProvider::Result r2 = right.data(GlobalSearchWidget::Role_PrimaryResult)
32
.value<SearchProvider::Result>();
34
// Order results that arrived first first, so that the results don't jump
35
// around while the user is trying to navigate through them.
36
const int order_left = left.data(GlobalSearchWidget::Role_OrderArrived).toInt();
37
const int order_right = right.data(GlobalSearchWidget::Role_OrderArrived).toInt();
39
if (order_left < order_right) return true;
40
if (order_left > order_right) return false;
42
#define CompareInt(field) \
43
if (r1.field < r2.field) return true; \
44
if (r1.field > r2.field) return false
48
#define CompareString(field) \
49
ret = QString::localeAwareCompare(r1.metadata_.field(), r2.metadata_.field()); \
50
if (ret < 0) return true; \
51
if (ret > 0) return false
53
// If they arrived at the same time then sort by quality and type.
54
CompareInt(match_quality_);
57
// Failing that, compare title, artist and album
59
case globalsearch::Type_Track:
60
case globalsearch::Type_Stream:
63
case globalsearch::Type_Album:
64
CompareString(artist);