File: devicessortproxymodel.cpp

package info (click to toggle)
kdeconnect 25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: cpp: 22,922; xml: 520; python: 92; sh: 25; makefile: 5
file content (48 lines) | stat: -rw-r--r-- 1,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "devicessortproxymodel.h"

#include "dbusinterfaces.h"
#include "devicesmodel.h"

DevicesSortProxyModel::DevicesSortProxyModel(DevicesModel *devicesModel)
    : QSortFilterProxyModel(devicesModel)
{
    setSourceModel(devicesModel);
    setSortRole(DevicesModel::StatusModelRole);
    sort(0);
}

bool DevicesSortProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
    QAbstractItemModel *model = sourceModel();
    Q_ASSERT(qobject_cast<DevicesModel *>(model));

    // Show connected devices first
    int statusLeft = model->data(left, DevicesModel::StatusModelRole).toInt();
    int statusRight = model->data(right, DevicesModel::StatusModelRole).toInt();

    if (statusLeft != statusRight) {
        return statusLeft > statusRight;
    }

    // Fallback to alphabetical order
    QString nameLeft = model->data(left, DevicesModel::NameModelRole).toString();
    QString nameRight = model->data(right, DevicesModel::NameModelRole).toString();

    return nameLeft < nameRight;
}

bool DevicesSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    Q_UNUSED(source_row);
    Q_UNUSED(source_parent);
    // Possible to-do: Implement filter
    return true;
}

#include "moc_devicessortproxymodel.cpp"