~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/globalsearchsortmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "globalsearchwidget.h"
 
19
#include "globalsearchsortmodel.h"
 
20
#include "searchprovider.h"
 
21
#include "core/logging.h"
 
22
 
 
23
GlobalSearchSortModel::GlobalSearchSortModel(QObject* parent)
 
24
  : QSortFilterProxyModel(parent)
 
25
{
 
26
}
 
27
 
 
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>();
 
33
 
 
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();
 
38
 
 
39
  if (order_left < order_right) return true;
 
40
  if (order_left > order_right) return false;
 
41
 
 
42
#define CompareInt(field) \
 
43
  if (r1.field < r2.field) return true; \
 
44
  if (r1.field > r2.field) return false
 
45
 
 
46
  int ret = 0;
 
47
 
 
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
 
52
 
 
53
  // If they arrived at the same time then sort by quality and type.
 
54
  CompareInt(match_quality_);
 
55
  CompareInt(type_);
 
56
 
 
57
  // Failing that, compare title, artist and album
 
58
  switch (r1.type_) {
 
59
  case globalsearch::Type_Track:
 
60
  case globalsearch::Type_Stream:
 
61
    CompareString(title);
 
62
    // fallthrough
 
63
  case globalsearch::Type_Album:
 
64
    CompareString(artist);
 
65
    CompareString(album);
 
66
    break;
 
67
  }
 
68
 
 
69
  return false;
 
70
 
 
71
#undef CompareInt
 
72
#undef CompareString
 
73
}