| 1 | /* Copyright (C) 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "MazDBSortProxy.h" |
| 19 | #include <QDebug> |
| 20 | #include "MazDBListModel.h" |
| 21 | #include "QJsEngineShim.h" |
| 22 | |
| 23 | MazDBSortProxy::MazDBSortProxy(QObject *parent) : QSortFilterProxyModel(parent), |
| 24 | m_sortOrder(Qt::AscendingOrder) |
| 25 | { |
| 26 | setDynamicSortFilter(true); |
| 27 | sort(0, m_sortOrder); |
| 28 | } |
| 29 | |
| 30 | QHash<int, QByteArray> MazDBSortProxy::roleNames() const |
| 31 | { |
| 32 | return sourceModel() ? sourceModel()->roleNames() : QHash<int, QByteArray>(); |
| 33 | } |
| 34 | |
| 35 | QString MazDBSortProxy::sortBy() const |
| 36 | { |
| 37 | return m_sortBy; |
| 38 | } |
| 39 | |
| 40 | QJSValue MazDBSortProxy::sortCallBack() const |
| 41 | { |
| 42 | return m_sortCallBack; |
| 43 | } |
| 44 | |
| 45 | void MazDBSortProxy::setModel(QAbstractItemModel *model) |
| 46 | { |
| 47 | if (sourceModel() == model) |
| 48 | return; |
| 49 | |
| 50 | setSourceModel(model); |
| 51 | reload(); |
| 52 | emit modelChanged(); |
| 53 | } |
| 54 | |
| 55 | void MazDBSortProxy::setSortBy(QString sortBy) |
| 56 | { |
| 57 | if (m_sortBy == sortBy) |
| 58 | return; |
| 59 | |
| 60 | m_sortBy = sortBy; |
| 61 | setSortRole(roleFromName(m_sortBy)); |
| 62 | emit sortByChanged(sortBy); |
| 63 | reload(); |
| 64 | } |
| 65 | |
| 66 | void MazDBSortProxy::setSortOrder(Qt::SortOrder order) |
| 67 | { |
| 68 | if (m_sortOrder != order) { |
| 69 | m_sortOrder = order; |
| 70 | sort(0, m_sortOrder); |
| 71 | emit sortOrderChanged(); |
| 72 | } |
| 73 | reload(); |
| 74 | } |
| 75 | |
| 76 | void MazDBSortProxy::setSortCallBack(QJSValue sortCallBack) |
| 77 | { |
| 78 | m_sortCallBack = sortCallBack; |
| 79 | reload(); |
| 80 | emit sortCallBackChanged(sortCallBack); |
| 81 | } |
| 82 | |
| 83 | bool MazDBSortProxy::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const |
| 84 | { |
| 85 | if (m_sortCallBack.isCallable()) { |
| 86 | QVariant leftData = sourceModel()->data(source_left, roleFromName(m_sortBy)); |
| 87 | QVariant rightData = sourceModel()->data(source_right, roleFromName(m_sortBy)); |
| 88 | QJSEngine *engine = qjsEngine(this); |
| 89 | |
| 90 | return const_cast<QJSValue *>(&m_sortCallBack)->call( |
| 91 | QJSValueList() |
| 92 | << engine->toScriptValue<QVariant>(leftData) |
| 93 | << engine->toScriptValue<QVariant>(rightData) |
| 94 | ).toBool(); |
| 95 | } else { |
| 96 | return QSortFilterProxyModel::lessThan(source_left, source_right); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void MazDBSortProxy::reload() |
| 101 | { |
| 102 | invalidate(); |
| 103 | setSortRole(roleFromName(m_sortBy)); |
| 104 | sort(0, m_sortOrder); |
| 105 | } |
| 106 | |
| 107 | int MazDBSortProxy::roleFromName(const QString &role) const |
| 108 | { |
| 109 | if (!sourceModel()) { |
| 110 | return 0; |
| 111 | } |
| 112 | const QByteArray roleName = role.toUtf8(); |
| 113 | const QHash<int, QByteArray> roles = sourceModel()->roleNames(); |
| 114 | QHashIterator<int, QByteArray> i(roles); |
| 115 | while (i.hasNext()) { |
| 116 | i.next(); |
| 117 | if (i.value() == roleName) |
| 118 | return i.key(); |
| 119 | } |
| 120 | return 0; |
| 121 | } |