5 #include "pagination.h"
7 #include <QtCore/QLoggingCategory>
11 Q_LOGGING_CATEGORY(C_PAGINATION,
"cutelyst.utils.pagination", QtWarningMsg)
15 if (itemsPerPage <= 0) {
16 qCWarning(C_PAGINATION) <<
"Invalid number of items per page:" << itemsPerPage <<
"failing back to 1";
20 if (currentPage <= 0) {
21 qCWarning(C_PAGINATION) <<
"Invalid current page:" << currentPage <<
"failing back to 1";
26 qCWarning(C_PAGINATION) <<
"Invalid number of page links:" << pageLinks <<
"failing back to 1";
30 insert(QStringLiteral(
"limit"), itemsPerPage);
31 insert(QStringLiteral(
"offset"), (currentPage - 1) * itemsPerPage);
32 insert(QStringLiteral(
"currentPage"), currentPage);
33 insert(QStringLiteral(
"current"), currentPage);
35 int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
36 if (currentPage > lastPage) {
37 currentPage = lastPage;
40 int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
41 int endPage = (pageLinks * 2) + startPage;
42 if (lastPage < endPage) {
47 for (
int i = startPage; i <= endPage; ++i) {
50 insert(QStringLiteral(
"enableFirst"), currentPage > 1);
51 insert(QStringLiteral(
"enableLast"), currentPage != lastPage);
52 insert(QStringLiteral(
"pages"), QVariant::fromValue(pages));
53 insert(QStringLiteral(
"lastPage"), lastPage);
54 insert(QStringLiteral(
"numberOfItems"), numberOfItems);
57 Pagination::~Pagination()
62 int Pagination::limit()
const
64 return value(QStringLiteral(
"limit")).toInt();
67 int Pagination::offset()
const
69 return value(QStringLiteral(
"offset")).toInt();
72 int Pagination::offset(
int itemsPerPage,
int currentPage)
74 if (itemsPerPage <= 0) {
75 qCWarning(C_PAGINATION) <<
"Invalid number of items per page:" << itemsPerPage <<
"failing back to 1";
78 if (currentPage <= 0) {
79 qCWarning(C_PAGINATION) <<
"Invalid current page:" << currentPage <<
"failing back to 1";
82 return (currentPage - 1) * itemsPerPage;
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"
The Cutelyst namespace holds all public Cutelyst API.