ff87f687336cca7969feb405d2cdc0bb056635d6
[dorian] / sortedlibrary.cpp
1 #include "sortedlibrary.h"
2 #include "book.h"
3
4 SortedLibrary::SortedLibrary(QObject *parent): QSortFilterProxyModel(parent)
5 {
6     setSourceModel(Library::instance());
7 }
8
9 void SortedLibrary::sortBy(SortBy key)
10 {
11     mSortBy = key;
12 }
13
14 bool SortedLibrary::lessThan(const QModelIndex &left,
15                              const QModelIndex &right) const
16 {
17     Book leftBook = sourceModel()->data(left, Library::BookRole).value<Book>();
18     Book rightBook = sourceModel()->data(right, Library::BookRole).value<Book>();
19
20     QString leftString;
21     QString rightString;
22
23     switch (mSortBy) {
24     case SortByTitle:
25         leftString = leftBook.title;
26         rightString = rightBook.title;
27         break;
28     default:
29         leftString = leftBook.creators[0];
30         rightString = rightBook.creators[0];
31         break;
32     }
33
34     return QString::localeAwareCompare(leftString, rightString);
35 }