cutelyst  3.7.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 << "failing back to 1";
17  itemsPerPage = 1;
18  }
19 
20  if (currentPage <= 0) {
21  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
22  currentPage = 1;
23  }
24 
25  if (pageLinks <= 0) {
26  qCWarning(C_PAGINATION) << "Invalid number of page links:" << pageLinks << "failing back to 1";
27  pageLinks = 1;
28  }
29 
30  insert(QStringLiteral("limit"), itemsPerPage);
31  insert(QStringLiteral("offset"), (currentPage - 1) * itemsPerPage);
32  insert(QStringLiteral("currentPage"), currentPage);
33  insert(QStringLiteral("current"), currentPage);
34 
35  int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
36  if (currentPage > lastPage) {
37  currentPage = lastPage;
38  }
39 
40  int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
41  int endPage = (pageLinks * 2) + startPage;
42  if (lastPage < endPage) {
43  endPage = lastPage;
44  }
45 
46  QVector<int> pages;
47  for (int i = startPage; i <= endPage; ++i) {
48  pages.append(i);
49  }
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);
55 }
56 
57 Pagination::~Pagination()
58 {
59 
60 }
61 
62 int Pagination::limit() const
63 {
64  return value(QStringLiteral("limit")).toInt();
65 }
66 
67 int Pagination::offset() const
68 {
69  return value(QStringLiteral("offset")).toInt();
70 }
71 
72 int Pagination::offset(int itemsPerPage, int currentPage)
73 {
74  if (itemsPerPage <= 0) {
75  qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage << "failing back to 1";
76  itemsPerPage = 1;
77  }
78  if (currentPage <= 0) {
79  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
80  currentPage = 1;
81  }
82  return (currentPage - 1) * itemsPerPage;
83 }
84 
85 int Pagination::currentPage() const
86 {
87  return value(QStringLiteral("currentPage")).toInt();
88 }
89 
90 int Pagination::lastPage() const
91 {
92  return value(QStringLiteral("lastPage")).toInt();
93 }
94 
95 int Pagination::numberOfItems() const
96 {
97  return value(QStringLiteral("numberOfItems")).toInt();
98 }
99 
100 bool Pagination::enableFirst() const
101 {
102  return value(QStringLiteral("enableFirst")).toBool();
103 }
104 
105 bool Pagination::enableLast() const
106 {
107  return value(QStringLiteral("enableLast")).toBool();
108 }
109 
110 QVector<int> Pagination::pages() const
111 {
112  return value(QStringLiteral("pages")).value<QVector<int> >();
113 }
114 
115 #include "moc_pagination.cpp"
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8