[OpenDocString] kdeconnect-kde (cpp)
devicessortproxymodel.cpp
DevicesSortProxyModel::DevicesSortProxyModel(DevicesModel *devicesModel)
    : QSortFilterProxyModel(devicesModel)
{
    setSourceModel(devicesModel);
    setSortRole(DevicesModel::StatusModelRole);
    sort(0);
}
Sets the source model and sort role of the devices model and sorts the devices by status model.
bool DevicesSortProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
    QAbstractItemModel *model = sourceModel();
    Q_ASSERT(qobject_cast(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;
}
This implements the less than operator to determine if the left device is less than the right device. The function first checks that the left device has a status value and if it has the status value, it checks that the right device has a status value. If the status value is not the same, the function falls back to alphabetical order.
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;
}
This implements a filter by ensuring that the source row and source parent are unused.