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;
14 Qt::SortOrder sortOrder = Qt::AscendingOrder;
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) {
56 return QHash<int, QByteArray>{};
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
79 beginResetModel();
80 d->sourceModel = newSourceModel;
81 endResetModel();
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]() {
88 endInsertRows();
89 });
90 connect(newSourceModel,
91 &QAbstractItemModel::columnsAboutToBeMoved,
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]() {
97 endMoveRows();
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]() {
103 endRemoveRows();
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]() {
111 beginResetModel();
112 });
113 connect(newSourceModel, &QAbstractItemModel::modelReset, this, [this]() {
114 endResetModel();
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"
166

source code of kitemmodels/src/core/kcolumnheadersmodel.cpp