File indexing completed on 2024-04-28 04:57:05

0001 /**
0002  * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "devicessortproxymodel.h"
0008 
0009 #include "dbusinterfaces.h"
0010 #include "devicesmodel.h"
0011 
0012 DevicesSortProxyModel::DevicesSortProxyModel(DevicesModel *devicesModel)
0013     : QSortFilterProxyModel(devicesModel)
0014 {
0015     setSourceModel(devicesModel);
0016     setSortRole(DevicesModel::StatusModelRole);
0017     sort(0);
0018 }
0019 
0020 bool DevicesSortProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0021 {
0022     QAbstractItemModel *model = sourceModel();
0023     Q_ASSERT(qobject_cast<DevicesModel *>(model));
0024 
0025     // Show connected devices first
0026     int statusLeft = model->data(left, DevicesModel::StatusModelRole).toInt();
0027     int statusRight = model->data(right, DevicesModel::StatusModelRole).toInt();
0028 
0029     if (statusLeft != statusRight) {
0030         return statusLeft > statusRight;
0031     }
0032 
0033     // Fallback to alphabetical order
0034     QString nameLeft = model->data(left, DevicesModel::NameModelRole).toString();
0035     QString nameRight = model->data(right, DevicesModel::NameModelRole).toString();
0036 
0037     return nameLeft > nameRight;
0038 }
0039 
0040 bool DevicesSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0041 {
0042     Q_UNUSED(source_row);
0043     Q_UNUSED(source_parent);
0044     // Possible to-do: Implement filter
0045     return true;
0046 }
0047 
0048 #include "moc_devicessortproxymodel.cpp"