Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qpageranges.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qpageranges.h"
5#include "qpageranges_p.h"
6
7#include <QtCore/qstack.h>
8#include <QtCore/qdebug.h>
9#include <QtCore/qdatastream.h>
10
12
14
15void QPageRangesPrivate::mergeIntervals()
16{
17 const int count = intervals.size();
18
19 if (count <= 1)
20 return;
21
22 std::sort(intervals.begin(), intervals.end());
23
24 QStack<QPageRanges::Range> stack;
25 stack.push(intervals[0]);
26
27 for (int i = 1; i < count; ++i) {
28 QPageRanges::Range &top = stack.top();
29
30 if (top.to < intervals[i].from - 1)
31 stack.push(intervals[i]);
32 else if (top.to < intervals[i].to)
33 top.to = intervals[i].to;
34 }
35
36 intervals = stack;
37}
38
40
41
57QPageRanges::QPageRanges() = default;
58
62QPageRanges::QPageRanges(const QPageRanges &other) noexcept = default;
63
74
78QPageRanges &QPageRanges::operator=(const QPageRanges &other) noexcept = default;
79
91void QPageRanges::addPage(int pageNumber)
92{
93 if (pageNumber <= 0) {
94 qWarning("QPageRanges::addPage: 'pageNumber' must be greater than 0");
95 return;
96 }
97
98 detach();
99 d->intervals.append({ pageNumber, pageNumber });
100 d->mergeIntervals();
101}
102
109void QPageRanges::addRange(int from, int to)
110{
111 if (from <= 0 || to <= 0) {
112 qWarning("QPageRanges::addRange: 'from' and 'to' must be greater than 0");
113 return;
114 }
115 if (to < from)
116 std::swap(from, to);
117
118 detach();
119 d->intervals.append({from, to});
120 d->mergeIntervals();
121}
122
126QList<QPageRanges::Range> QPageRanges::toRangeList() const
127{
128 if (d)
129 return d->intervals;
130 return QList<QPageRanges::Range>{};
131}
132
137{
138 d.reset();
139}
140
156{
157 QList<Range> intervals;
158 const QStringList items = ranges.split(u',');
159 for (const QString &item : items) {
160 if (item.isEmpty())
161 return QPageRanges();
162
163 if (item.contains(u'-')) {
164 const QStringList rangeItems = item.split(u'-');
165 if (rangeItems.size() != 2)
166 return QPageRanges();
167
168 bool ok;
169 const int number1 = rangeItems[0].toInt(&ok);
170 if (!ok)
171 return QPageRanges();
172
173 const int number2 = rangeItems[1].toInt(&ok);
174 if (!ok)
175 return QPageRanges();
176
177 if (number1 < 1 || number2 < 1 || number2 < number1)
178 return QPageRanges();
179
180 intervals.append({number1, number2});
181
182 } else {
183 bool ok;
184 const int number = item.toInt(&ok);
185 if (!ok)
186 return QPageRanges();
187
188 if (number < 1)
189 return QPageRanges();
190
191 intervals.append({number, number});
192 }
193 }
194
195 QPageRanges newRanges;
196 newRanges.d.reset(new QPageRangesPrivate);
197 newRanges.d->intervals = intervals;
198 newRanges.d->mergeIntervals();
199 return newRanges;
200}
201
206{
207 if (!d)
208 return QString();
209
211 for (const Range &range : d->intervals) {
212 if (!result.isEmpty())
213 result += u',';
214
215 if (range.from == range.to)
217 else
218 result += QStringLiteral("%1-%2").arg(range.from).arg(range.to);
219 }
220
221 return result;
222}
223
230bool QPageRanges::contains(int pageNumber) const
231{
232 if (!d)
233 return false;
234
235 for (const Range &range : d->intervals) {
236 if (range.from <= pageNumber && range.to >= pageNumber)
237 return true;
238 }
239 return false;
240}
241
246{
247 return !d || d->intervals.isEmpty();
248}
249
255{
256 if (isEmpty())
257 return 0;
258 return d->intervals.constFirst().from;
259}
260
266{
267 if (isEmpty())
268 return 0;
269 return d->intervals.constLast().to;
270}
271
275bool QPageRanges::isEqual(const QPageRanges &other) const noexcept
276{
277 if (d == other.d)
278 return true;
279 if (!d || !other.d)
280 return false;
281 return d->intervals == other.d->intervals;
282}
283
288{
289 if (d)
290 d.detach();
291 else
293}
294
295#if !defined(QT_NO_DATASTREAM)
306{
307 s << pageRanges.toString();
308 return s;
309}
310
321{
322 QString rangesString;
323 s >> rangesString;
324 pageRanges = QPageRanges::fromString(rangesString);
325 return s;
326}
327#endif // QT_NO_DATASTREAM
328
329#ifndef QT_NO_DEBUG_STREAM
330QDebug operator<<(QDebug dbg, const QPageRanges &pageRanges)
331{
332 QDebugStateSaver saver(dbg);
333 dbg.nospace();
334 dbg.noquote();
335 dbg << "QPageRanges(" << pageRanges.toString() << ")";
336
337 return dbg;
338}
339#endif
340
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
void reset(T *ptr=nullptr) noexcept
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
virtual bool contains(const QPointF &point) const
Returns true if this item contains point, which is in local coordinates; otherwise,...
bool isEmpty() const noexcept
Definition qlist.h:401
const T & constLast() const noexcept
Definition qlist.h:650
const T & constFirst() const noexcept
Definition qlist.h:647
void append(parameter_type t)
Definition qlist.h:458
QList< QPageRanges::Range > intervals
The QPageRanges class represents a collection of page ranges.
Definition qpageranges.h:21
QPageRanges()
Constructs an empty QPageRanges object.
bool isEmpty() const
Returns true if the ranges are empty; otherwise returns false.
QPageRanges & operator=(const QPageRanges &other) noexcept
Assigns other to this QPageRanges object.
void clear()
Removes all page ranges.
static QPageRanges fromString(const QString &ranges)
Constructs and returns a QPageRanges object populated with the ranges from the string representation.
QList< Range > toRangeList() const
Returns a list with the values of the ranges.
QString toString() const
Returns the string representation of the page ranges.
int lastPage() const
Returns the index of the last page covered by the page ranges, or 0 if the page ranges are empty.
void addPage(int pageNumber)
Adds the single page pageNumber to the ranges.
int firstPage() const
Returns the index of the first page covered by the page ranges, or 0 if the page ranges are empty.
void addRange(int from, int to)
Adds the range specified with from and to to the ranges.
~QPageRanges()
Destroys the page ranges.
bool contains(int pageNumber) const
Returns true if the ranges include the page pageNumber; otherwise returns false.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QStringList split(const QString &sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
Definition qstring.cpp:8218
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define qWarning
Definition qlogging.h:166
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
GLdouble GLdouble GLdouble GLdouble top
GLenum GLenum GLsizei count
GLsizei range
GLdouble s
[6]
Definition qopenglext.h:235
GLuint64EXT * result
[6]
QDataStream & operator>>(QDataStream &s, QPageRanges &pageRanges)
QDataStream & operator<<(QDataStream &s, const QPageRanges &pageRanges)
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
#define QStringLiteral(str)
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
QList< QTreeWidgetItem * > items
\inmodule QtGui
Definition qpageranges.h:39