cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
pagination.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2015-2025 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "pagination.h"
6
7#include <QtCore/QLoggingCategory>
8
9using namespace Cutelyst;
10using namespace Qt::StringLiterals;
11
12Q_LOGGING_CATEGORY(C_PAGINATION, "cutelyst.utils.pagination", QtWarningMsg)
13
14Pagination::Pagination(int numberOfItems, int itemsPerPage, int currentPage, int pageLinks)
15{
16 if (itemsPerPage <= 0) {
17 qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage
18 << "failing back to 1";
19 itemsPerPage = 1;
20 }
21
22 if (currentPage <= 0) {
23 qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
24 currentPage = 1;
25 }
26
27 if (pageLinks <= 0) {
28 qCWarning(C_PAGINATION) << "Invalid number of page links:" << pageLinks
29 << "failing back to 1";
30 pageLinks = 1;
31 }
32
33 insert(u"limit"_s, itemsPerPage);
34 insert(u"offset"_s, (currentPage - 1) * itemsPerPage);
35 insert(u"currentPage"_s, currentPage);
36 insert(u"current"_s, currentPage);
37
38 const int resultLastPage = ((numberOfItems - 1) / itemsPerPage) + 1;
39 currentPage = std::ranges::min(currentPage, resultLastPage);
40
41 const int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
42 const int endPage = std::ranges::min((pageLinks * 2) + startPage, resultLastPage);
43
44 QVector<int> resultPages;
45 for (int i = startPage; i <= endPage; ++i) {
46 resultPages.append(i);
47 }
48 insert(u"enableFirst"_s, currentPage > 1);
49 insert(u"enableLast"_s, currentPage != resultLastPage);
50 insert(u"pages"_s, QVariant::fromValue(resultPages));
51 insert(u"lastPage"_s, resultLastPage);
52 insert(u"numberOfItems"_s, numberOfItems);
53}
54
58
60{
61 return value(u"limit"_s).toInt();
62}
63
65{
66 return value(u"offset"_s).toInt();
67}
68
69int Pagination::offset(int itemsPerPage, int currentPage)
70{
71 if (itemsPerPage <= 0) {
72 qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage
73 << "failing back to 1";
74 itemsPerPage = 1;
75 }
76 if (currentPage <= 0) {
77 qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
78 currentPage = 1;
79 }
80 return (currentPage - 1) * itemsPerPage;
81}
82
84{
85 return value(u"currentPage"_s).toInt();
86}
87
89{
90 return value(u"lastPage"_s).toInt();
91}
92
94{
95 return value(u"numberOfItems"_s).toInt();
96}
97
99{
100 return value(u"enableFirst"_s).toBool();
101}
102
104{
105 return value(u"enableLast"_s).toBool();
106}
107
109{
110 return value(u"pages"_s).value<QVector<int>>();
111}
112
113#include "moc_pagination.cpp"
Helper to calculate values for paginating result lists.
Definition pagination.h:26
QVector< int > pages
Definition pagination.h:59
The Cutelyst namespace holds all public Cutelyst API.
void append(QList::parameter_type value)
QVariant fromValue(T &&value)