Cutelyst  2.14.2
pagination.cpp
1 /*
2  * Copyright (C) 2015-2018 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "pagination.h"
19 
20 #include <QtCore/QLoggingCategory>
21 
22 using namespace Cutelyst;
23 
24 Q_LOGGING_CATEGORY(C_PAGINATION, "cutelyst.utils.pagination", QtWarningMsg)
25 
26 Pagination::Pagination(int numberOfItems, int itemsPerPage, int currentPage, int pageLinks)
27 {
28  if (itemsPerPage <= 0) {
29  qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage << "failing back to 1";
30  itemsPerPage = 1;
31  }
32 
33  if (currentPage <= 0) {
34  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
35  currentPage = 1;
36  }
37 
38  if (pageLinks <= 0) {
39  qCWarning(C_PAGINATION) << "Invalid number of page links:" << pageLinks << "failing back to 1";
40  pageLinks = 1;
41  }
42 
43  insert(QStringLiteral("limit"), itemsPerPage);
44  insert(QStringLiteral("offset"), (currentPage - 1) * itemsPerPage);
45  insert(QStringLiteral("currentPage"), currentPage);
46  insert(QStringLiteral("current"), currentPage);
47 
48  int lastPage = (numberOfItems - 1) / itemsPerPage + 1;
49  if (currentPage > lastPage) {
50  currentPage = lastPage;
51  }
52 
53  int startPage = (currentPage < pageLinks + 1) ? 1 : currentPage - pageLinks;
54  int endPage = (pageLinks * 2) + startPage;
55  if (lastPage < endPage) {
56  endPage = lastPage;
57  }
58 
59  QVector<int> pages;
60  for (int i = startPage; i <= endPage; ++i) {
61  pages.append(i);
62  }
63  insert(QStringLiteral("enableFirst"), currentPage > 1);
64  insert(QStringLiteral("enableLast"), currentPage != lastPage);
65  insert(QStringLiteral("pages"), QVariant::fromValue(pages));
66  insert(QStringLiteral("lastPage"), lastPage);
67  insert(QStringLiteral("numberOfItems"), numberOfItems);
68 }
69 
70 Pagination::~Pagination()
71 {
72 
73 }
74 
75 int Pagination::limit() const
76 {
77  return value(QStringLiteral("limit")).toInt();
78 }
79 
80 int Pagination::offset() const
81 {
82  return value(QStringLiteral("offset")).toInt();
83 }
84 
85 int Pagination::offset(int itemsPerPage, int currentPage)
86 {
87  if (itemsPerPage <= 0) {
88  qCWarning(C_PAGINATION) << "Invalid number of items per page:" << itemsPerPage << "failing back to 1";
89  itemsPerPage = 1;
90  }
91  if (currentPage <= 0) {
92  qCWarning(C_PAGINATION) << "Invalid current page:" << currentPage << "failing back to 1";
93  currentPage = 1;
94  }
95  return (currentPage - 1) * itemsPerPage;
96 }
97 
98 int Pagination::currentPage() const
99 {
100  return value(QStringLiteral("currentPage")).toInt();
101 }
102 
103 int Pagination::lastPage() const
104 {
105  return value(QStringLiteral("lastPage")).toInt();
106 }
107 
108 int Pagination::numberOfItems() const
109 {
110  return value(QStringLiteral("numberOfItems")).toInt();
111 }
112 
113 bool Pagination::enableFirst() const
114 {
115  return value(QStringLiteral("enableFirst")).toBool();
116 }
117 
118 bool Pagination::enableLast() const
119 {
120  return value(QStringLiteral("enableLast")).toBool();
121 }
122 
124 {
125  return value(QStringLiteral("pages")).value<QVector<int> >();
126 }
127 
128 #include "moc_pagination.cpp"
QVector< int > pages() const
bool enableLast() const
void append(const T &value)
int lastPage() const
bool enableFirst() const
QVariant fromValue(const T &value)
int limit() const
int offset() const
int currentPage() const
int numberOfItems() const