1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtMediaservices module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtMediaservices/qmediatimerange.h>
QT_BEGIN_NAMESPACE
/*!
\class QMediaTimeInterval
\brief The QMediaTimeInterval class represents a time interval with integer precision.
\ingroup multimedia
\since 4.7
An interval is specified by an inclusive start() and end() time.
These must be set in the constructor, as this is an immutable class.
The specific units of time represented by the class have not been defined -
it is suitable for any times which can be represented by a signed 64 bit integer.
The isNormal() method determines if a time interval is normal
(a normal time interval has start() <= end()). An abnormal interval can be converted
in to a normal interval by calling the normalized() method.
The contains() method determines if a specified time lies within
the time interval.
The translated() method returns a time interval which has been translated
forwards or backwards through time by a specified offset.
\sa QMediaTimeRange
*/
/*!
\fn QMediaTimeInterval::QMediaTimeInterval()
Constructs an empty interval.
*/
QMediaTimeInterval::QMediaTimeInterval()
: s(0)
, e(0)
{
}
/*!
\fn QMediaTimeInterval::QMediaTimeInterval(qint64 start, qint64 end)
Constructs an interval with the specified \a start and \a end times.
*/
QMediaTimeInterval::QMediaTimeInterval(qint64 start, qint64 end)
: s(start)
, e(end)
{
}
/*!
\fn QMediaTimeInterval::QMediaTimeInterval(const QMediaTimeInterval &other)
Constructs an interval by taking a copy of \a other.
*/
QMediaTimeInterval::QMediaTimeInterval(const QMediaTimeInterval &other)
: s(other.s)
, e(other.e)
{
}
/*!
\fn QMediaTimeInterval::start() const
Returns the start time of the interval.
\sa end()
*/
qint64 QMediaTimeInterval::start() const
{
return s;
}
/*!
\fn QMediaTimeInterval::end() const
Returns the end time of the interval.
\sa start()
*/
qint64 QMediaTimeInterval::end() const
{
return e;
}
/*!
\fn QMediaTimeInterval::contains(qint64 time) const
Returns true if the time interval contains the specified \a time.
That is, start() <= time <= end().
*/
bool QMediaTimeInterval::contains(qint64 time) const
{
return isNormal() ? (s <= time && time <= e)
: (e <= time && time <= s);
}
/*!
\fn QMediaTimeInterval::isNormal() const
Returns true if this time interval is normal.
A normal time interval has start() <= end().
\sa normalized()
*/
bool QMediaTimeInterval::isNormal() const
{
return s <= e;
}
/*!
\fn QMediaTimeInterval::normalized() const
Returns a normalized version of this interval.
If the start() time of the interval is greater than the end() time,
then the returned interval has the start and end times swapped.
*/
QMediaTimeInterval QMediaTimeInterval::normalized() const
{
if(s > e)
return QMediaTimeInterval(e, s);
return *this;
}
/*!
\fn QMediaTimeInterval::translated(qint64 offset) const
Returns a copy of this time interval, translated by a value of \a offset.
An interval can be moved forward through time with a positive offset, or backward
through time with a negative offset.
*/
QMediaTimeInterval QMediaTimeInterval::translated(qint64 offset) const
{
return QMediaTimeInterval(s + offset, e + offset);
}
/*!
\fn operator==(const QMediaTimeInterval &a, const QMediaTimeInterval &b)
\relates QMediaTimeRange
Returns true if \a a is exactly equal to \a b.
*/
bool operator==(const QMediaTimeInterval &a, const QMediaTimeInterval &b)
{
return a.start() == b.start() && a.end() == b.end();
}
/*!
\fn operator!=(const QMediaTimeInterval &a, const QMediaTimeInterval &b)
\relates QMediaTimeRange
Returns true if \a a is not exactly equal to \a b.
*/
bool operator!=(const QMediaTimeInterval &a, const QMediaTimeInterval &b)
{
return a.start() != b.start() || a.end() != b.end();
}
class QMediaTimeRangePrivate : public QSharedData
{
public:
QMediaTimeRangePrivate();
QMediaTimeRangePrivate(const QMediaTimeRangePrivate &other);
QMediaTimeRangePrivate(const QMediaTimeInterval &interval);
QList<QMediaTimeInterval> intervals;
void addInterval(const QMediaTimeInterval &interval);
void removeInterval(const QMediaTimeInterval &interval);
};
QMediaTimeRangePrivate::QMediaTimeRangePrivate()
: QSharedData()
{
}
QMediaTimeRangePrivate::QMediaTimeRangePrivate(const QMediaTimeRangePrivate &other)
: QSharedData()
, intervals(other.intervals)
{
}
QMediaTimeRangePrivate::QMediaTimeRangePrivate(const QMediaTimeInterval &interval)
: QSharedData()
{
if(interval.isNormal())
intervals << interval;
}
void QMediaTimeRangePrivate::addInterval(const QMediaTimeInterval &interval)
{
// Handle normalized intervals only
if(!interval.isNormal())
return;
// Find a place to insert the interval
int i;
for (i = 0; i < intervals.count(); i++) {
// Insert before this element
if(interval.s < intervals[i].s) {
intervals.insert(i, interval);
break;
}
}
// Interval needs to be added to the end of the list
if (i == intervals.count())
intervals.append(interval);
// Do we need to correct the element before us?
if(i > 0 && intervals[i - 1].e >= interval.s - 1)
i--;
// Merge trailing ranges
while (i < intervals.count() - 1
&& intervals[i].e >= intervals[i + 1].s - 1) {
intervals[i].e = qMax(intervals[i].e, intervals[i + 1].e);
intervals.removeAt(i + 1);
}
}
void QMediaTimeRangePrivate::removeInterval(const QMediaTimeInterval &interval)
{
// Handle normalized intervals only
if(!interval.isNormal())
return;
for (int i = 0; i < intervals.count(); i++) {
QMediaTimeInterval r = intervals[i];
if (r.e < interval.s) {
// Before the removal interval
continue;
} else if (interval.e < r.s) {
// After the removal interval - stop here
break;
} else if (r.s < interval.s && interval.e < r.e) {
// Split case - a single range has a chunk removed
intervals[i].e = interval.s -1;
addInterval(QMediaTimeInterval(interval.e + 1, r.e));
break;
} else if (r.s < interval.s) {
// Trimming Tail Case
intervals[i].e = interval.s - 1;
} else if (interval.e < r.e) {
// Trimming Head Case - we can stop after this
intervals[i].s = interval.e + 1;
break;
} else {
// Complete coverage case
intervals.removeAt(i);
--i;
}
}
}
/*!
\class QMediaTimeRange
\brief The QMediaTimeRange class represents a set of zero or more disjoint
time intervals.
\ingroup multimedia
\since 4.7
\reentrant
The earliestTime(), latestTime(), intervals() and isEmpty()
methods are used to get information about the current time range.
The addInterval(), removeInterval() and clear() methods are used to modify
the current time range.
When adding or removing intervals from the time range, existing intervals
within the range may be expanded, trimmed, deleted, merged or split to ensure
that all intervals within the time range remain distinct and disjoint. As a
consequence, all intervals added or removed from a time range must be
\l{QMediaTimeInterval::isNormal()}{normal}.
\sa QMediaTimeInterval
*/
/*!
\fn QMediaTimeRange::QMediaTimeRange()
Constructs an empty time range.
*/
QMediaTimeRange::QMediaTimeRange()
: d(new QMediaTimeRangePrivate)
{
}
/*!
\fn QMediaTimeRange::QMediaTimeRange(qint64 start, qint64 end)
Constructs a time range that contains an initial interval from
\a start to \a end inclusive.
If the interval is not \l{QMediaTimeInterval::isNormal()}{normal},
the resulting time range will be empty.
\sa addInterval()
*/
QMediaTimeRange::QMediaTimeRange(qint64 start, qint64 end)
: d(new QMediaTimeRangePrivate(QMediaTimeInterval(start, end)))
{
}
/*!
\fn QMediaTimeRange::QMediaTimeRange(const QMediaTimeInterval &interval)
Constructs a time range that contains an intitial interval, \a interval.
If \a interval is not \l{QMediaTimeInterval::isNormal()}{normal},
the resulting time range will be empty.
\sa addInterval()
*/
QMediaTimeRange::QMediaTimeRange(const QMediaTimeInterval &interval)
: d(new QMediaTimeRangePrivate(interval))
{
}
/*!
\fn QMediaTimeRange::QMediaTimeRange(const QMediaTimeRange &range)
Constructs a time range by copying another time \a range.
*/
QMediaTimeRange::QMediaTimeRange(const QMediaTimeRange &range)
: d(range.d)
{
}
/*!
\fn QMediaTimeRange::~QMediaTimeRange()
Destructor.
*/
QMediaTimeRange::~QMediaTimeRange()
{
}
/*!
\fn QMediaTimeRange::operator=(const QMediaTimeRange &other)
Takes a copy of the \a other time range and returns itself.
*/
QMediaTimeRange &QMediaTimeRange::operator=(const QMediaTimeRange &other)
{
d = other.d;
return *this;
}
/*!
\fn QMediaTimeRange::operator=(const QMediaTimeInterval &interval)
Sets the time range to a single continuous interval, \a interval.
*/
QMediaTimeRange &QMediaTimeRange::operator=(const QMediaTimeInterval &interval)
{
d = new QMediaTimeRangePrivate(interval);
return *this;
}
/*!
\fn QMediaTimeRange::earliestTime() const
Returns the earliest time within the time range.
For empty time ranges, this value is equal to zero.
\sa latestTime()
*/
qint64 QMediaTimeRange::earliestTime() const
{
if (!d->intervals.isEmpty())
return d->intervals[0].s;
return 0;
}
/*!
\fn QMediaTimeRange::latestTime() const
Returns the latest time within the time range.
For empty time ranges, this value is equal to zero.
\sa earliestTime()
*/
qint64 QMediaTimeRange::latestTime() const
{
if (!d->intervals.isEmpty())
return d->intervals[d->intervals.count() - 1].e;
return 0;
}
/*!
\fn QMediaTimeRange::addInterval(qint64 start, qint64 end)
\overload
Adds the interval specified by \a start and \a end
to the time range.
\sa addInterval()
*/
void QMediaTimeRange::addInterval(qint64 start, qint64 end)
{
d->addInterval(QMediaTimeInterval(start, end));
}
/*!
\fn QMediaTimeRange::addInterval(const QMediaTimeInterval &interval)
Adds the specified \a interval to the time range.
Adding intervals which are not \l{QMediaTimeInterval::isNormal()}{normal}
is invalid, and will be ignored.
If the specified interval is adjacent to, or overlaps existing
intervals within the time range, these intervals will be merged.
This operation takes \l{linear time}
\sa removeInterval()
*/
void QMediaTimeRange::addInterval(const QMediaTimeInterval &interval)
{
d->addInterval(interval);
}
/*!
\fn QMediaTimeRange::addTimeRange(const QMediaTimeRange &range)
Adds each of the intervals in \a range to this time range.
Equivalent to calling addInterval() for each interval in \a range.
*/
void QMediaTimeRange::addTimeRange(const QMediaTimeRange &range)
{
foreach(const QMediaTimeInterval &i, range.intervals()) {
d->addInterval(i);
}
}
/*!
\fn QMediaTimeRange::removeInterval(qint64 start, qint64 end)
\overload
Removes the interval specified by \a start and \a end
from the time range.
\sa removeInterval()
*/
void QMediaTimeRange::removeInterval(qint64 start, qint64 end)
{
d->removeInterval(QMediaTimeInterval(start, end));
}
/*!
\fn QMediaTimeRange::removeInterval(const QMediaTimeInterval &interval)
Removes the specified \a interval from the time range.
Removing intervals which are not \l{QMediaTimeInterval::isNormal()}{normal}
is invalid, and will be ignored.
Intervals within the time range will be trimmed, split or deleted
such that no intervals within the time range include any part of the
target interval.
This operation takes \l{linear time}
\sa addInterval()
*/
void QMediaTimeRange::removeInterval(const QMediaTimeInterval &interval)
{
d->removeInterval(interval);
}
/*!
\fn QMediaTimeRange::removeTimeRange(const QMediaTimeRange &range)
Removes each of the intervals in \a range from this time range.
Equivalent to calling removeInterval() for each interval in \a range.
*/
void QMediaTimeRange::removeTimeRange(const QMediaTimeRange &range)
{
foreach(const QMediaTimeInterval &i, range.intervals()) {
d->removeInterval(i);
}
}
/*!
\fn QMediaTimeRange::operator+=(const QMediaTimeRange &other)
Adds each interval in \a other to the time range and returns the result.
*/
QMediaTimeRange& QMediaTimeRange::operator+=(const QMediaTimeRange &other)
{
addTimeRange(other);
return *this;
}
/*!
\fn QMediaTimeRange::operator+=(const QMediaTimeInterval &interval)
Adds the specified \a interval to the time range and returns the result.
*/
QMediaTimeRange& QMediaTimeRange::operator+=(const QMediaTimeInterval &interval)
{
addInterval(interval);
return *this;
}
/*!
\fn QMediaTimeRange::operator-=(const QMediaTimeRange &other)
Removes each interval in \a other from the time range and returns the result.
*/
QMediaTimeRange& QMediaTimeRange::operator-=(const QMediaTimeRange &other)
{
removeTimeRange(other);
return *this;
}
/*!
\fn QMediaTimeRange::operator-=(const QMediaTimeInterval &interval)
Removes the specified \a interval from the time range and returns the result.
*/
QMediaTimeRange& QMediaTimeRange::operator-=(const QMediaTimeInterval &interval)
{
removeInterval(interval);
return *this;
}
/*!
\fn QMediaTimeRange::clear()
Removes all intervals from the time range.
\sa removeInterval()
*/
void QMediaTimeRange::clear()
{
d->intervals.clear();
}
/*!
\fn QMediaTimeRange::intervals() const
Returns the list of intervals covered by this time range.
*/
QList<QMediaTimeInterval> QMediaTimeRange::intervals() const
{
return d->intervals;
}
/*!
\fn QMediaTimeRange::isEmpty() const
Returns true if there are no intervals within the time range.
\sa intervals()
*/
bool QMediaTimeRange::isEmpty() const
{
return d->intervals.isEmpty();
}
/*!
\fn QMediaTimeRange::isContinuous() const
Returns true if the time range consists of a continuous interval.
That is, there is one or fewer disjoint intervals within the time range.
*/
bool QMediaTimeRange::isContinuous() const
{
return (d->intervals.count() <= 1);
}
/*!
\fn QMediaTimeRange::contains(qint64 time) const
Returns true if the specified \a time lies within the time range.
*/
bool QMediaTimeRange::contains(qint64 time) const
{
for (int i = 0; i < d->intervals.count(); i++) {
if (d->intervals[i].contains(time))
return true;
if (time < d->intervals[i].s)
break;
}
return false;
}
/*!
\fn operator==(const QMediaTimeRange &a, const QMediaTimeRange &b)
\relates QMediaTimeRange
Returns true if all intervals in \a a are present in \a b.
*/
bool operator==(const QMediaTimeRange &a, const QMediaTimeRange &b)
{
if (a.intervals().count() != b.intervals().count())
return false;
for (int i = 0; i < a.intervals().count(); i++)
{
if(a.intervals()[i] != b.intervals()[i])
return false;
}
return true;
}
/*!
\fn operator!=(const QMediaTimeRange &a, const QMediaTimeRange &b)
\relates QMediaTimeRange
Returns true if one or more intervals in \a a are not present in \a b.
*/
bool operator!=(const QMediaTimeRange &a, const QMediaTimeRange &b)
{
return !(a == b);
}
/*!
\fn operator+(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
Returns a time range containing the union between \a r1 and \a r2.
*/
QMediaTimeRange operator+(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
{
return (QMediaTimeRange(r1) += r2);
}
/*!
\fn operator-(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
Returns a time range containing \a r2 subtracted from \a r1.
*/
QMediaTimeRange operator-(const QMediaTimeRange &r1, const QMediaTimeRange &r2)
{
return (QMediaTimeRange(r1) -= r2);
}
QT_END_NAMESPACE
|