KItemModels

kcolumnheadersmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kcolumnheadersmodel.h"
8
9class KColumnHeadersModelPrivate
10{
11public:
12 QAbstractItemModel *sourceModel = nullptr;
13 int sortColumn = -1;
15};
16
17KColumnHeadersModel::KColumnHeadersModel(QObject *parent)
18 : QAbstractListModel(parent)
19 , d(new KColumnHeadersModelPrivate)
20{
21}
22
23KColumnHeadersModel::~KColumnHeadersModel()
24{
25}
26
27int KColumnHeadersModel::rowCount(const QModelIndex &parent) const
28{
29 if (!d->sourceModel || parent.isValid()) {
30 return 0;
31 }
32
33 return d->sourceModel->columnCount();
34}
35
36QVariant KColumnHeadersModel::data(const QModelIndex &index, int role) const
37{
38 if (!d->sourceModel || !index.isValid()) {
39 return QVariant{};
40 }
41
42 if (role == SortRole) {
43 if (index.row() == d->sortColumn) {
44 return d->sortOrder;
45 } else {
46 return QVariant{};
47 }
48 }
49
50 return sourceModel()->headerData(index.row(), Qt::Horizontal, role);
51}
52
53QHash<int, QByteArray> KColumnHeadersModel::roleNames() const
54{
55 if (!d->sourceModel) {
57 }
58
59 auto names = d->sourceModel->roleNames();
60 names.insert(SortRole, "sort");
61 return names;
62}
63
64QAbstractItemModel *KColumnHeadersModel::sourceModel() const
65{
66 return d->sourceModel;
67}
68
69void KColumnHeadersModel::setSourceModel(QAbstractItemModel *newSourceModel)
70{
71 if (newSourceModel == d->sourceModel) {
72 return;
73 }
74
75 if (d->sourceModel) {
76 d->sourceModel->disconnect(this);
77 }
78
80 d->sourceModel = newSourceModel;
82
83 if (newSourceModel) {
84 connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeInserted, this, [this](const QModelIndex &, int first, int last) {
85 beginInsertRows(QModelIndex{}, first, last);
86 });
87 connect(newSourceModel, &QAbstractItemModel::columnsInserted, this, [this]() {
89 });
90 connect(newSourceModel,
92 this,
93 [this](const QModelIndex &, int start, int end, const QModelIndex &, int destination) {
94 beginMoveRows(QModelIndex{}, start, end, QModelIndex{}, destination);
95 });
96 connect(newSourceModel, &QAbstractItemModel::columnsMoved, this, [this]() {
98 });
99 connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeRemoved, this, [this](const QModelIndex &, int first, int last) {
100 beginRemoveRows(QModelIndex{}, first, last);
101 });
102 connect(newSourceModel, &QAbstractItemModel::columnsRemoved, this, [this]() {
104 });
105 connect(newSourceModel, &QAbstractItemModel::headerDataChanged, this, [this](Qt::Orientation orientation, int first, int last) {
106 if (orientation == Qt::Horizontal) {
107 Q_EMIT dataChanged(index(first, 0), index(last, 0));
108 }
109 });
110 connect(newSourceModel, &QAbstractItemModel::modelAboutToBeReset, this, [this]() {
112 });
113 connect(newSourceModel, &QAbstractItemModel::modelReset, this, [this]() {
115 });
116 }
117}
118
119int KColumnHeadersModel::sortColumn() const
120{
121 return d->sortColumn;
122}
123
124void KColumnHeadersModel::setSortColumn(int newSortColumn)
125{
126 if (newSortColumn == d->sortColumn) {
127 return;
128 }
129
130 auto previousSortColumn = d->sortColumn;
131
132 d->sortColumn = newSortColumn;
133
134 if (previousSortColumn >= 0) {
135 Q_EMIT dataChanged(index(previousSortColumn), index(previousSortColumn), {SortRole});
136 }
137
138 if (newSortColumn >= 0) {
139 Q_EMIT dataChanged(index(newSortColumn), index(newSortColumn), {SortRole});
140 }
141
142 Q_EMIT sortColumnChanged();
143}
144
145Qt::SortOrder KColumnHeadersModel::sortOrder() const
146{
147 return d->sortOrder;
148}
149
150void KColumnHeadersModel::setSortOrder(Qt::SortOrder newSortOrder)
151{
152 if (newSortOrder == d->sortOrder) {
153 return;
154 }
155
156 d->sortOrder = newSortOrder;
157
158 if (d->sortColumn >= 0) {
159 Q_EMIT dataChanged(index(d->sortColumn), index(d->sortColumn), {SortRole});
160 }
161
162 Q_EMIT sortOrderChanged();
163}
164
165#include "moc_kcolumnheadersmodel.cpp"
Q_SCRIPTABLE Q_NOREPLY void start()
const QList< QKeySequence > & end()
void beginInsertRows(const QModelIndex &parent, int first, int last)
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
void columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
void columnsInserted(const QModelIndex &parent, int first, int last)
void columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
void columnsRemoved(const QModelIndex &parent, int first, int last)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
void headerDataChanged(Qt::Orientation orientation, int first, int last)
void modelAboutToBeReset()
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
Horizontal
SortOrder
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.