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
|
/*
SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "itemselectionmodel.h"
#include <QAbstractItemModel>
namespace kt
{
ItemSelectionModel::ItemSelectionModel(QAbstractItemModel *model, QObject *parent)
: QItemSelectionModel(model, parent)
{
}
ItemSelectionModel::~ItemSelectionModel()
{
}
void ItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
{
QItemSelection sel(index, index);
select(sel, command);
}
void ItemSelectionModel::select(const QItemSelection &sel, QItemSelectionModel::SelectionFlags command)
{
if (command == NoUpdate)
return;
if (command & QItemSelectionModel::Clear)
selection.clear();
for (const QItemSelectionRange &r : sel)
doRange(r, command);
QItemSelectionModel::select(sel, command);
}
void ItemSelectionModel::doRange(const QItemSelectionRange r, QItemSelectionModel::SelectionFlags command)
{
for (int i = r.topLeft().row(); i <= r.bottomRight().row(); i++) {
void *item = model()->index(i, 0).internalPointer();
if (!item)
continue;
if (command & QItemSelectionModel::Select) {
selection.insert(item);
} else if (command & QItemSelectionModel::Deselect) {
selection.remove(item);
} else if (command & QItemSelectionModel::Toggle) {
if (selection.contains(item))
selection.remove(item);
else
selection.insert(item);
}
}
}
void ItemSelectionModel::reset()
{
selection.clear();
QItemSelectionModel::reset();
}
void ItemSelectionModel::clear()
{
selection.clear();
QItemSelectionModel::clear();
}
void ItemSelectionModel::sorted()
{
QItemSelection ns;
int rows = model()->rowCount(QModelIndex());
int cols = model()->columnCount(QModelIndex());
for (int i = 0; i < rows; i++) {
QModelIndex idx = model()->index(i, 0, QModelIndex());
void *item = idx.internalPointer();
if (item && selection.contains(item)) {
ns.select(idx, model()->index(i, cols - 1, QModelIndex()));
}
}
select(ns, QItemSelectionModel::ClearAndSelect);
}
}
#include "moc_itemselectionmodel.cpp"
|