7#include <QtCore/QLoggingCategory>
11Q_LOGGING_CATEGORY(C_PAGINATION,
"cutelyst.utils.pagination", QtWarningMsg)
15 if (itemsPerPage <= 0) {
16 qCWarning(C_PAGINATION) <<
"Invalid number of items per page:" << itemsPerPage
17 <<
"failing back to 1";
21 if (currentPage <= 0) {
22 qCWarning(C_PAGINATION) <<
"Invalid current page:" << currentPage <<
"failing back to 1";
27 qCWarning(C_PAGINATION) <<
"Invalid number of page links:" << pageLinks
28 <<
"failing back to 1";
32 insert(QStringLiteral(
"limit"), itemsPerPage);
33 insert(QStringLiteral(
"offset"), (currentPage - 1) * itemsPerPage);
34 insert(QStringLiteral(
"currentPage"), currentPage);
35 insert(QStringLiteral(
"current"), currentPage);
37 int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
38 if (currentPage > lastPage) {
39 currentPage = lastPage;
42 int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
43 int endPage = (pageLinks * 2) + startPage;
44 if (lastPage < endPage) {
49 for (
int i = startPage; i <= endPage; ++i) {
52 insert(QStringLiteral(
"enableFirst"), currentPage > 1);
53 insert(QStringLiteral(
"enableLast"), currentPage != lastPage);
54 insert(QStringLiteral(
"pages"), QVariant::fromValue(pages));
55 insert(QStringLiteral(
"lastPage"), lastPage);
56 insert(QStringLiteral(
"numberOfItems"), numberOfItems);
59Pagination::~Pagination()
63int Pagination::limit()
const
65 return value(QStringLiteral(
"limit")).toInt();
68int Pagination::offset()
const
70 return value(QStringLiteral(
"offset")).toInt();
73int Pagination::offset(
int itemsPerPage,
int currentPage)
75 if (itemsPerPage <= 0) {
76 qCWarning(C_PAGINATION) <<
"Invalid number of items per page:" << itemsPerPage
77 <<
"failing back to 1";
80 if (currentPage <= 0) {
81 qCWarning(C_PAGINATION) <<
"Invalid current page:" << currentPage <<
"failing back to 1";
84 return (currentPage - 1) * itemsPerPage;
87int Pagination::currentPage()
const
89 return value(QStringLiteral(
"currentPage")).toInt();
92int Pagination::lastPage()
const
94 return value(QStringLiteral(
"lastPage")).toInt();
97int Pagination::numberOfItems()
const
99 return value(QStringLiteral(
"numberOfItems")).toInt();
102bool Pagination::enableFirst()
const
104 return value(QStringLiteral(
"enableFirst")).toBool();
107bool Pagination::enableLast()
const
109 return value(QStringLiteral(
"enableLast")).toBool();
112QVector<int> Pagination::pages()
const
114 return value(QStringLiteral(
"pages")).value<QVector<int>>();
117#include "moc_pagination.cpp"
The Cutelyst namespace holds all public Cutelyst API.