Skip to content

Commit eb059b5

Browse files
Added sorting for 'PercentageRead'
1 parent 010afcd commit eb059b5

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/adapters/data_models/library_model/library_proxy_model.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ bool LibraryProxyModel::lessThan(const QModelIndex& left,
5353
{
5454
return addedToLibraryAfter(left, right);
5555
}
56+
case SortRole::Percentage:
57+
{
58+
return higherProgressPercentage(left, right);
59+
}
60+
case SortRole::SortRole_END:
61+
{
62+
qCritical() << QString("Sorting called with role: 'SortRole_END'");
63+
return false;
64+
}
5665
default:
5766
return false;
5867
}
@@ -128,9 +137,8 @@ bool LibraryProxyModel::filterAcceptsRow(int source_row,
128137
void LibraryProxyModel::setSortRole(int newRole)
129138
{
130139
int firstRole = SortRole::RecentlyAdded;
131-
int lastRole = SortRole::LastOpened;
132140

133-
bool inRange = firstRole <= newRole && newRole <= lastRole;
141+
bool inRange = firstRole <= newRole && newRole <= SortRole::SortRole_END;
134142
if(!inRange)
135143
return;
136144

@@ -227,6 +235,17 @@ bool LibraryProxyModel::addedToLibraryAfter(const QModelIndex& left,
227235
return lhsAddedDate > rhsAddedDate;
228236
}
229237

238+
bool LibraryProxyModel::higherProgressPercentage(const QModelIndex& left,
239+
const QModelIndex& right) const
240+
{
241+
auto lhs =
242+
sourceModel()->data(left, LibraryModel::BookProgressPercentageRole);
243+
auto rhs =
244+
sourceModel()->data(right, LibraryModel::BookProgressPercentageRole);
245+
246+
return lhs.toInt() >= rhs.toInt();
247+
}
248+
230249
bool LibraryProxyModel::filterAcceptsTags(const QModelIndex& bookIndex) const
231250
{
232251
auto tags = getTags(bookIndex);

src/adapters/data_models/library_model/library_proxy_model.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class LibraryProxyModel : public QSortFilterProxyModel
2626
RecentlyAdded = Qt::UserRole + 1,
2727
Title,
2828
Authors,
29-
LastOpened
29+
LastOpened,
30+
Percentage,
31+
SortRole_END
3032
};
3133

3234
Q_ENUM(SortRole);
@@ -64,6 +66,8 @@ class LibraryProxyModel : public QSortFilterProxyModel
6466
bool openedAfter(const QModelIndex& left, const QModelIndex& right) const;
6567
bool addedToLibraryAfter(const QModelIndex& left,
6668
const QModelIndex& right) const;
69+
bool higherProgressPercentage(const QModelIndex& left,
70+
const QModelIndex& right) const;
6771
bool filterAcceptsTags(const QModelIndex& bookIndex) const;
6872
std::vector<adapters::dtos::TagDto> getTags(const QModelIndex& index) const;
6973
bool bookContainsAllTags(std::vector<adapters::dtos::TagDto> tags) const;

src/presentation/homePage/toolbar/sortByButton/MSortByPopup.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Popup
7373
{
7474
ListElement { text: "Recently added"; role: LibraryProxyModel.RecentlyAdded }
7575
ListElement { text: "Recently read"; role: LibraryProxyModel.LastOpened }
76-
ListElement { text: "Percentage"; role: LibraryProxyModel.Title }
76+
ListElement { text: "Percentage"; role: LibraryProxyModel.Percentage }
7777
ListElement { text: "Book (A-Z)"; role: LibraryProxyModel.Title }
7878
ListElement { text: "Authors (A-Z)"; role: LibraryProxyModel.Authors }
7979
}

0 commit comments

Comments
 (0)