cutelyst  4.4.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-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #include "pagination.h"
6 
7 #include <QtCore/QLoggingCategory>
8 
9 using namespace Cutelyst;
10 
11 Q_LOGGING_CATEGORY(C_PAGINATION, "cutelyst.utils.pagination", QtWarningMsg)
12 
13 Pagination::Pagination(int numberOfItems, int itemsPerPage, int currentPage, int pageLinks)
14 {
15  if (itemsPerPage <= 0) {
16  qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage
17  << "failing back to 1";
18  itemsPerPage = 1;
19  }
20 
21  if (currentPage <= 0) {
22  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
23  currentPage = 1;
24  }
25 
26  if (pageLinks <= 0) {
27  qCWarning(C_PAGINATION) << "Invalid number of page links:" << pageLinks
28  << "failing back to 1";
29  pageLinks = 1;
30  }
31 
32  insert(QStringLiteral("limit"), itemsPerPage);
33  insert(QStringLiteral("offset"), (currentPage - 1) * itemsPerPage);
34  insert(QStringLiteral("currentPage"), currentPage);
35  insert(QStringLiteral("current"), currentPage);
36 
37  int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
38  if (currentPage > lastPage) {
39  currentPage = lastPage;
40  }
41 
42  int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
43  int endPage = (pageLinks * 2) + startPage;
44  if (lastPage < endPage) {
45  endPage = lastPage;
46  }
47 
48  QVector<int> pages;
49  for (int i = startPage; i <= endPage; ++i) {
50  pages.append(i);
51  }
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);
57 }
58 
60 {
61 }
62 
63 int Pagination::limit() const
64 {
65  return value(QStringLiteral("limit")).toInt();
66 }
67 
68 int Pagination::offset() const
69 {
70  return value(QStringLiteral("offset")).toInt();
71 }
72 
73 int Pagination::offset(int itemsPerPage, int currentPage)
74 {
75  if (itemsPerPage <= 0) {
76  qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage
77  << "failing back to 1";
78  itemsPerPage = 1;
79  }
80  if (currentPage <= 0) {
81  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
82  currentPage = 1;
83  }
84  return (currentPage - 1) * itemsPerPage;
85 }
86 
88 {
89  return value(QStringLiteral("currentPage")).toInt();
90 }
91 
93 {
94  return value(QStringLiteral("lastPage")).toInt();
95 }
96 
98 {
99  return value(QStringLiteral("numberOfItems")).toInt();
100 }
101 
103 {
104  return value(QStringLiteral("enableFirst")).toBool();
105 }
106 
108 {
109  return value(QStringLiteral("enableLast")).toBool();
110 }
111 
113 {
114  return value(QStringLiteral("pages")).value<QVector<int>>();
115 }
116 
117 #include "moc_pagination.cpp"
Helper to calculate values for paginating result lists.
Definition: pagination.h:26
QVector< int > pages
Definition: pagination.h:59
Q_GADGETint limit
Definition: pagination.h:31
The Cutelyst namespace holds all public Cutelyst API.
void append(QList::parameter_type value)
QVariant fromValue(T &&value)