Cutelyst  2.13.0
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::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"
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Pagination
Definition: pagination.h:26