| 1 | // Copyright (C) 2025 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtQmlModels/private/qqmlsorterbase_p.h> |
| 5 | #include <QtQmlModels/private/qqmlsortfilterproxymodel_p.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \qmltype Sorter |
| 11 | \inherits QtObject |
| 12 | \inqmlmodule QtQml.Models |
| 13 | \since 6.10 |
| 14 | \preliminary |
| 15 | \brief Abstract base type providing functionality common to sorters. |
| 16 | |
| 17 | Sorter provides a set of common properties for all the sorters that they |
| 18 | inherit from. |
| 19 | */ |
| 20 | |
| 21 | QQmlSorterBase::QQmlSorterBase(QQmlSorterBasePrivate *privObj, QObject *parent) |
| 22 | : QObject (*privObj, parent) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | /*! |
| 27 | \qmlproperty bool Sorter::enabled |
| 28 | |
| 29 | This property enables the \l SortFilterProxyModel to consider this sorter |
| 30 | while sorting the model data. |
| 31 | |
| 32 | The default value is \c true. |
| 33 | */ |
| 34 | bool QQmlSorterBase::enabled() const |
| 35 | { |
| 36 | Q_D(const QQmlSorterBase); |
| 37 | return d->m_enabled; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | void QQmlSorterBase::setEnabled(const bool enabled) |
| 42 | { |
| 43 | Q_D(QQmlSorterBase); |
| 44 | if (d->m_enabled == enabled) |
| 45 | return; |
| 46 | d->m_enabled = enabled; |
| 47 | invalidate(updateCache: true); |
| 48 | emit enabledChanged(); |
| 49 | } |
| 50 | |
| 51 | /*! |
| 52 | \qmlproperty Qt::SortOrder Sorter::sortOrder |
| 53 | |
| 54 | This property holds the order used by \l SortFilterProxyModel when sorting |
| 55 | the model data. |
| 56 | |
| 57 | The default value is \c Qt::AscendingOrder. |
| 58 | */ |
| 59 | Qt::SortOrder QQmlSorterBase::sortOrder() const |
| 60 | { |
| 61 | Q_D(const QQmlSorterBase); |
| 62 | return d->m_sortOrder; |
| 63 | } |
| 64 | |
| 65 | void QQmlSorterBase::setSortOrder(const Qt::SortOrder sortOrder) |
| 66 | { |
| 67 | Q_D(QQmlSorterBase); |
| 68 | if (d->m_sortOrder == sortOrder) |
| 69 | return; |
| 70 | d->m_sortOrder = sortOrder; |
| 71 | invalidate(); |
| 72 | emit sortOrderChanged(); |
| 73 | } |
| 74 | |
| 75 | /*! |
| 76 | \qmlproperty int Sorter::priority |
| 77 | |
| 78 | This property holds the priority that is given to this sorter compared to |
| 79 | other sorters. The lesser value results in a higher priority and the higher |
| 80 | value results in a lower priority. |
| 81 | |
| 82 | The default value is \c -1. |
| 83 | */ |
| 84 | int QQmlSorterBase::priority() const |
| 85 | { |
| 86 | Q_D(const QQmlSorterBase); |
| 87 | return d->m_sorterPriority; |
| 88 | } |
| 89 | |
| 90 | void QQmlSorterBase::setPriority(const int priority) |
| 91 | { |
| 92 | Q_D(QQmlSorterBase); |
| 93 | if (d->m_sorterPriority == priority) |
| 94 | return; |
| 95 | d->m_sorterPriority = priority; |
| 96 | invalidate(updateCache: true); |
| 97 | emit priorityChanged(); |
| 98 | } |
| 99 | |
| 100 | /*! |
| 101 | \qmlproperty int Sorter::column |
| 102 | |
| 103 | This property holds the column that this sorter is applied to. |
| 104 | |
| 105 | The default value is \c 0. |
| 106 | */ |
| 107 | int QQmlSorterBase::column() const |
| 108 | { |
| 109 | Q_D(const QQmlSorterBase); |
| 110 | return d->m_sortColumn; |
| 111 | } |
| 112 | |
| 113 | void QQmlSorterBase::setColumn(const int column) |
| 114 | { |
| 115 | Q_D(QQmlSorterBase); |
| 116 | if (d->m_sortColumn == column) |
| 117 | return; |
| 118 | d->m_sortColumn = column; |
| 119 | invalidate(); |
| 120 | emit columnChanged(); |
| 121 | } |
| 122 | |
| 123 | /*! |
| 124 | \internal |
| 125 | */ |
| 126 | void QQmlSorterBase::invalidate(bool updateCache) |
| 127 | { |
| 128 | // Update the cached filters and invalidate the model |
| 129 | if (updateCache) |
| 130 | emit invalidateCache(filter: this); |
| 131 | emit invalidateModel(); |
| 132 | } |
| 133 | |
| 134 | QT_END_NAMESPACE |
| 135 | |
| 136 | #include "moc_qqmlsorterbase_p.cpp" |
| 137 |
