18 #include "pagination.h"
20 #include <QtCore/QLoggingCategory>
24 Q_LOGGING_CATEGORY(C_PAGINATION,
"cutelyst.utils.pagination", QtWarningMsg)
28 if (itemsPerPage <= 0) {
29 qCWarning(C_PAGINATION) <<
"Invalid number of items per page:" << itemsPerPage <<
"failing back to 1";
33 if (currentPage <= 0) {
34 qCWarning(C_PAGINATION) <<
"Invalid current page:" << currentPage <<
"failing back to 1";
39 qCWarning(C_PAGINATION) <<
"Invalid number of page links:" << pageLinks <<
"failing back to 1";
43 insert(QStringLiteral(
"limit"), itemsPerPage);
44 insert(QStringLiteral(
"offset"), (currentPage - 1) * itemsPerPage);
45 insert(QStringLiteral(
"currentPage"), currentPage);
46 insert(QStringLiteral(
"current"), currentPage);
48 int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
49 if (currentPage > lastPage) {
50 currentPage = lastPage;
53 int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
54 int endPage = (pageLinks * 2) + startPage;
55 if (lastPage < endPage) {
60 for (
int i = startPage; i <= endPage; ++i) {
63 insert(QStringLiteral(
"enableFirst"), currentPage > 1);
64 insert(QStringLiteral(
"enableLast"), currentPage != lastPage);
65 insert(QStringLiteral(
"pages"), QVariant::fromValue(pages));
66 insert(QStringLiteral(
"lastPage"), lastPage);
67 insert(QStringLiteral(
"numberOfItems"), numberOfItems);
70 Pagination::~Pagination()
75 int Pagination::limit()
const
77 return value(QStringLiteral(
"limit")).toInt();
80 int Pagination::offset()
const
82 return value(QStringLiteral(
"offset")).toInt();
85 int Pagination::currentPage()
const
87 return value(QStringLiteral(
"currentPage")).toInt();
90 int Pagination::lastPage()
const
92 return value(QStringLiteral(
"lastPage")).toInt();
95 int Pagination::numberOfItems()
const
97 return value(QStringLiteral(
"numberOfItems")).toInt();
100 bool Pagination::enableFirst()
const
102 return value(QStringLiteral(
"enableFirst")).toBool();
105 bool Pagination::enableLast()
const
107 return value(QStringLiteral(
"enableLast")).toBool();
110 QVector<int> Pagination::pages()
const
112 return value(QStringLiteral(
"pages")).value<QVector<int> >();
115 #include "moc_pagination.cpp"