summaryrefslogtreecommitdiffstats
path: root/src/qlistselectionmanager.cpp
blob: c54e309dd6dd83ca34271d2040b50060dd88ebcb (plain)
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
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qlistselectionmanager.h"
#include "qlistselectionmanager_p.h"
#include "qlistmodelinterface.h"

#include <qdebug.h>

QT_BEGIN_NAMESPACE

// selection change data

QtListSelectionChangeData::QtListSelectionChangeData()
    : toggle(false)
{
}

QtListSelectionChangeData::~QtListSelectionChangeData()
{
}

void QtListSelectionChangeData::setRanges(int oldAnchor, int oldCurrent, int newAnchor, int newCurrent,
                                          const QBitArray &selections, QtListSelectionManager::SelectionMode mode)
{
    previousRange.first = qMin(oldAnchor, oldCurrent);
    previousRange.second = qMax(oldAnchor, oldCurrent);
    currentRange.first = qMin(newAnchor, newCurrent);
    currentRange.second = qMax(newAnchor, newCurrent);
    boundingRange.first = qMin(previousRange.first, currentRange.first);
    boundingRange.second = qMax(previousRange.second, currentRange.second);
    toggle = (mode == QtListSelectionManager::Toggle);
    previousSelections = selections;
    currentSelections = selections;
    indexes.clear();
}

void QtListSelectionChangeData::setIndexes(int from, int to, const QList<int> &changed)
{
    previousRange = QPair<int,int>(-1, -1);
    currentRange = QPair<int,int>(-1, -1);
    boundingRange = QPair<int,int>(from, to);
    toggle = false;
    previousSelections.clear();
    currentSelections.clear();
    indexes = changed;
}

void QtListSelectionChangeData::setSelections(const QBitArray &previous, const QBitArray &current)
{
    previousRange = QPair<int,int>(-1, -1);
    currentRange = QPair<int,int>(-1, -1);
    boundingRange = QPair<int,int>(0, qMin(previous.count(), current.count()));
    toggle = false;
    previousSelections = previous;
    currentSelections = current;
    previousSelections.resize(current.size());
    indexes.clear();
}

void QtListSelectionChangeData::clear()
{
    // used to avoid the array being detached
    previousRange = QPair<int,int>(-1, -1);
    currentRange = QPair<int,int>(-1, -1);
    boundingRange = QPair<int,int>(-1, -1);
    toggle = false;
    previousSelections.clear();
    currentSelections.clear();
    indexes.clear();
}

QList<int> QtListSelectionChangeData::changedIndexes() const
{
    if (!indexes.isEmpty() || currentSelections.isEmpty())
        return indexes; // already computed
    for (int i = boundingRange.first; i <= boundingRange.second; ++i) {
        // old selection value
        bool oldSelect = false;
        if (previousRange.first <= i && i <= previousRange.second)
            oldSelect = (toggle ? !previousSelections.testBit(i) : true);
        else
            oldSelect = previousSelections.testBit(i);
        // new selection value
        bool newSelect = false;
        if (currentRange.first <= i && i <= currentRange.second)
            newSelect = (toggle ? !currentSelections.testBit(i) : true);
        else
            newSelect = currentSelections.testBit(i);
        // compare old and new
        if (oldSelect != newSelect)
            indexes.append(i);
    }
    return indexes;
}

int QtListSelectionChangeData::from() const
{
    return boundingRange.first;
}

int QtListSelectionChangeData::to() const
{
    return boundingRange.second;
}

int QtListSelectionChangeData::count() const
{
    return boundingRange.second - boundingRange.first + 1;
}

// QtListSelectionChange

QtListSelectionChange::QtListSelectionChange()
{
    d = new QtListSelectionChangeData();
}

QtListSelectionChange::QtListSelectionChange(const QtListSelectionChange &other)
    : d(other.d)
{
}

int QtListSelectionChange::index() const
{
    return d->from();
}

int QtListSelectionChange::count() const
{
    return d->count();
}

QList<int> QtListSelectionChange::indexes() const
{
    return d->changedIndexes();
}

// selection manager

QtListSelectionManagerPrivate::QtListSelectionManagerPrivate()
    : q_ptr(0),
    model(0),
    current(-1),
    anchor(-1),
    active(false),
    mode(QtListSelectionManager::Select)
{
}

QtListSelectionManagerPrivate::~QtListSelectionManagerPrivate()
{
}

void QtListSelectionManagerPrivate::_q_modelDestroyed()
{
    model = 0;
}

void QtListSelectionManagerPrivate::_q_itemsInserted(int index, int count)
{
    Q_Q(QtListSelectionManager);
    selections.resize(selections.count() + count);
    Q_ASSERT(model->count() == selections.count());
    // move selections
    QList<int> indexes;
    for (int i = selections.count() - 1; i >= index + count; --i) {
        if (selections[i] != selections[i - count]) // ### avoid lookup
            indexes.prepend(i);
        selections[i] = selections[i - count];
    }
    selections.fill(false, index, index + count);
    if (!indexes.isEmpty()) { // indexes are already computed
        change.d->setIndexes(index, selections.count() - 1, indexes);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid selections detach later
    }
    // move current
    const int previousCurrent = current;
    if (current >= index) {
        current += count;
        emit q->currentChanged(current, previousCurrent);
    }
    // move anchor
    const int previousAnchor = anchor;
    if (anchor >= index) {
        anchor += count;
        emit q->anchorChanged(anchor, previousAnchor);
    }
    // update the anchored selection
    if (active && (anchor != previousAnchor || current != previousCurrent)) {
        change.d->setRanges(previousAnchor, previousCurrent, anchor, current, selections, mode);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid detach later
    }
}

void QtListSelectionManagerPrivate::_q_itemsRemoved(int index, int count)
{
    Q_Q(QtListSelectionManager);
    const int end = selections.count() - count;
    Q_ASSERT(count <= selections.count()); // ### maybe make into a warning ?
    // move selections
    QList<int> indexes;
    for (int i = index; i < end; ++i) {
        if (selections[i] != selections[i + count]) // ### avoid lookup
            indexes.prepend(i);
        selections[i] = selections[i + count];
    }
    selections.truncate(end);
    Q_ASSERT(model->count() == selections.count());
    if (!indexes.isEmpty()) {  // indexes are already computed
        change.d->setIndexes(index, selections.count() - 1, indexes);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid selections detach later
    }
    const int last = model->count() ? model->count() - 1 : 0;
    // move current
    const int previousCurrent = current;
    if (current >= index)
        current -= count;
    current = qBound(0, current, last);
    if (previousCurrent != current)
        emit q->currentChanged(current, previousCurrent);

    // move anchor
    const int previousAnchor = anchor;
    if (anchor >= index)
        anchor -= count;
    anchor = qBound(0, anchor, last);
    if (previousAnchor != anchor)
        emit q->anchorChanged(anchor, previousAnchor);

    // update the anchored selection
    if (active && (anchor != previousAnchor || current != previousCurrent)) {
        change.d->setRanges(previousAnchor, previousCurrent, anchor, current, selections, mode);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid detach later
    }
}

void QtListSelectionManagerPrivate::_q_itemsMoved(int from, int to, int count)
{
    Q_Q(QtListSelectionManager);
    if (from == to || count == 0)
        return;
    QBitArray moving(count);
    // save the moved selections
    for (int i = 0; i < count; ++i) {
        moving[i] = selections[from + i];
    }
    QList<int> indexes;
    // move the existing selections
    if (from < to) {
        for (int j = from; j < to; ++j) {
            if (selections[j] != selections[j - count]) // ### avoid lookup
                indexes.append(j);
            selections[j] = selections[j + count];
        }
    } else { // from > to
        for (int j = from + count - 1; j > to; --j) {
            if (selections[j] != selections[j - count]) // ### avoid lookup
                indexes.prepend(j);
            selections[j] = selections[j - count];
         }
    }
    // insert the moved selections
    for (int k = 0; k < count; ++k) {
        if (selections[to + k] != moving[k]) // ### avoid lookup
            indexes.append(to + k);
        selections[to + k] = moving[k];
    }
    if (!indexes.isEmpty()) { // indexes are already computed
        change.d->setIndexes(qMin(from, to), qMax(from, to) + count - 1, indexes);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid selections detach later
    }
    // move the current index if needed
    const int previousCurrent = current;
    if (from < current && current < from + count) {
        current = to + (current - from);
        emit q->currentChanged(current, previousCurrent);
    }
    // move the anchor index if needed
    const int previousAnchor = anchor;
    if (from < anchor && anchor < from + count) {
        anchor = to + (anchor - from);
        emit q->anchorChanged(anchor, previousAnchor);
    }
    // update the anchored selection
    if (active && (anchor != previousAnchor || current != previousCurrent)) {
        change.d->setRanges(previousAnchor, previousCurrent, anchor, current, selections, mode);
        emit q->selectionsChanged(change);
        change.d->clear(); // avoid detach later
    }
}

void QtListSelectionManagerPrivate::_q_reset()
{
    const int count = model ? model->count() : 0;
    selections.resize(count);
    selections.fill(false);
    current = count ? qBound(0, current, count - 1) : -1;
    anchor = count ? qBound(0, anchor, count - 1) : -1;
}

/*!
  \class QtListSelectionManager
  \brief A list selection manager
*/

QtListSelectionManager::QtListSelectionManager(QObject *parent)
    : QObject(parent), d_ptr(new QtListSelectionManagerPrivate)
{
    d_ptr->q_ptr = this;
}

/*!
 */
QtListSelectionManager::QtListSelectionManager(QtListSelectionManagerPrivate &dd, QObject *parent)
    : QObject(parent), d_ptr(&dd)
{
    d_ptr->q_ptr = this;
}

/*!
 */
QtListSelectionManager::~QtListSelectionManager()
{
    delete d_ptr;
}

/*!
 */
void QtListSelectionManager::setModel(QtListModelInterface *model)
{
    Q_D(QtListSelectionManager);
    if (d->model == model)
        return;
    if (d->model) {
        disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed()));
        disconnect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(_q_itemsInserted(int,int)));
        disconnect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(_q_itemsRemoved(int,int)));
        disconnect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(_q_itemsMoved(int,int,int)));
    }
    d->model = model;
    if (d->model) {
        connect(d->model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed()));
        connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(_q_itemsInserted(int,int)));
        connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(_q_itemsRemoved(int,int)));
        connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(_q_itemsMoved(int,int,int)));
    }
    d->_q_reset();
}

/*!
 */
QtListModelInterface *QtListSelectionManager::model() const
{
    Q_D(const QtListSelectionManager);
    return d->model;
}

/*!
  Sets the current item to be the given \a current.

  \sa currentItem()
*/
void QtListSelectionManager::setCurrentItem(int current)
{
    Q_D(QtListSelectionManager);
    if (!d->model || !d->model->count())
        return;
    current = qBound(0, current, d->model->count() - 1);
    if (current != d->current) {
        // update current
        int previous = d->current;
        d->current = current;
        emit currentChanged(current, previous);
        // update the anchored selection
        if (d->active) {
            d->change.d->setRanges(d->anchor, d->anchor, previous, current, d->selections, d->mode);
            emit selectionsChanged(d->change);
            d->change.d->clear(); // avoid detach later
        }
    }
}

/*!
  Returns the index of the current item. If the returned value is -1,
  the current is disabled.

  \sa setCurrentItem()
 */
int QtListSelectionManager::currentItem() const
{
    Q_D(const QtListSelectionManager);
    return d->current;
}

/*!
  Sets the anchor item to be the given \a anchor.

  \sa anchorItem()
*/
void QtListSelectionManager::setAnchorItem(int anchor)
{
    Q_D(QtListSelectionManager);
    if (!d->model || !d->model->count())
        return;
    anchor = qBound(0, anchor, d->model->count() - 1);
    if (anchor != d->anchor) {
        // update anchor
        int previous = d->anchor;
        d->anchor = anchor;
        emit anchorChanged(anchor, previous);
        // update the anchored selection
        if (d->active) {
            d->change.d->setRanges(previous, anchor, d->current, d->current, d->selections, d->mode);
            emit selectionsChanged(d->change);
            d->change.d->clear(); // avoid detach later
        }
    }
}

/*!
  Returns the index of the anchor item. If the returned value is -1,
  the anchor is disabled.

  \sa setAnchorItem()
 */
int QtListSelectionManager::anchorItem() const
{
    Q_D(const QtListSelectionManager);
    return d->anchor;
}

bool QtListSelectionManager::isSelected(int index) const
{
    Q_D(const QtListSelectionManager);
    if (d->active) {
        const int from = qMin(d->anchor, d->current);
        const int to = qMax(d->anchor, d->current);
        if (from <= index && index <= to)
            switch (d->mode) {
            case Select:
                return true;
            case Deselect:
                return false;
            case Toggle:
                return !d->selections.testBit(index);
            default:
                break;
            }
    }
    if (index >= 0 && index < d->selections.count())
        return d->selections.testBit(index);
    return false;
}

/*!
  Sets the selected state of the items in the range indicated by the given
  \a index and \a count to be either true or false, depending on the given
  \a mode.

  \sa isSelected()
 */
void QtListSelectionManager::setSelected(int index, int count, SelectionMode mode)
{
    Q_D(QtListSelectionManager);
    if (index < 0) {
        // cut off the part before the zero index.
        count += index;
        index = 0;
    }
    if (count <= 0 || index >= d->selections.count())
        return;
    const int from = index;
    count = qMin(count, d->selections.count()); // this helps the usecase where count passed is INT_MAX
    const int to = qMin(index + count, d->selections.count());
    // set selection
    const QBitArray old = d->selections;
    switch (mode) {
    case Select:
        d->selections.fill(true, from, to);
        break;
    case Deselect:
        d->selections.fill(false, from, to);
        break;
    case Toggle:
        for (int i = from; i <= to; ++i)
            d->selections.toggleBit(i);
        break;
    }
    d->change.d->setSelections(old, d->selections);
    emit selectionsChanged(d->change);
    d->change.d->clear(); // avoid selections detach later
}

/*!
 */
QList<int> QtListSelectionManager::selectedItems() const
{
    Q_D(const QtListSelectionManager);
    QList<int> items;
    for (int i = 0; i < d->selections.count(); ++i)
        if (isSelected(i))
            items.append(i);
    return items;
}

/*!
 */
void QtListSelectionManager::clearSelections()
{
    Q_D(QtListSelectionManager);
    if (d->active) {
        d->active = false;
        d->mode = Select;
    }
    // make the change find the difference between old selection and no selection
    const QBitArray old = d->selections;
    d->selections.fill(false);
    d->change.d->setSelections(old, d->selections);
    emit selectionsChanged(d->change);
    d->change.d->clear();
}

/*!
 */
bool QtListSelectionManager::isAnchoredSelectionActive() const
{
    Q_D(const QtListSelectionManager);
    return d->active;
}

/*!
 */
QtListSelectionManager::SelectionMode QtListSelectionManager::anchoredSelectionMode() const
{
    Q_D(const QtListSelectionManager);
    return d->mode;
}

/*!
 */
void QtListSelectionManager::beginAnchoredSelection(int anchor, SelectionMode mode)
{
    Q_D(QtListSelectionManager);
    if (d->active)
        return;
    d->active = true;
    d->mode = mode;
    setAnchorItem(anchor);
}

/*!
 */
void QtListSelectionManager::endAnchoredSelection()
{
    Q_D(QtListSelectionManager);
    if (d->active) {
        // commit anchored selection range
        if (d->current >= 0 && d->anchor >= 0) {
            const int from = qMin(d->anchor, d->current);
            const int to = qMax(d->anchor, d->current) + 1;
            switch (d->mode) {
            case Select:
                d->selections.fill(true, from, to);
                break;
            case Deselect:
                d->selections.fill(false, from, to);
                break;
            case Toggle:
                for (int i = from; i <= to; ++i)
                    d->selections.toggleBit(i);
                break;
            }
        }
        d->active = false;
    }
}

QT_END_NAMESPACE

#include "moc_qlistselectionmanager.cpp"