Jump to exact position on page, as identified by TOC entry. Handle
[dorian] / model / sortedlibrary.cpp
1 #include "sortedlibrary.h"
2 #include "book.h"
3 #include "trace.h"
4
5 SortedLibrary::SortedLibrary(QObject *parent):
6         QSortFilterProxyModel(parent), mSortBy(SortByTitle)
7 {
8     setSourceModel(Library::instance());
9     sort(0);
10 }
11
12 void SortedLibrary::sortBy(SortBy key)
13 {
14     mSortBy = key;
15 }
16
17 bool SortedLibrary::lessThan(const QModelIndex &left,
18                              const QModelIndex &right) const
19 {
20     Book *leftBook = Library::instance()->book(left);
21     Book *rightBook = Library::instance()->book(right);
22
23     QString leftString;
24     QString rightString;
25
26     switch (mSortBy) {
27     case SortByTitle:
28         leftString = leftBook->shortName();
29         rightString = rightBook->shortName();
30         break;
31     default:
32         leftString = leftBook->creators[0];
33         rightString = rightBook->creators[0];
34         break;
35     }
36
37     return QString::localeAwareCompare(leftString, rightString) < 0;
38 }