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