BluezQt

declarativedevicesmodel.cpp
1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "declarativedevicesmodel.h"
10#include "declarativeadapter.h"
11#include "declarativebattery.h"
12#include "declarativedevice.h"
13#include "declarativemanager.h"
14#include "declarativemediaplayer.h"
15
16DeclarativeDevicesModel::DeclarativeDevicesModel(QObject *parent)
17 : QSortFilterProxyModel(parent)
18 , m_manager(nullptr)
19 , m_model(nullptr)
20{
21}
22
23DeclarativeManager *DeclarativeDevicesModel::manager() const
24{
25 return m_manager;
26}
27
28void DeclarativeDevicesModel::setManager(DeclarativeManager *manager)
29{
30 m_manager = manager;
31 m_model = new BluezQt::DevicesModel(m_manager, this);
32 setSourceModel(m_model);
33}
34
35QHash<int, QByteArray> DeclarativeDevicesModel::roleNames() const
36{
38
39 roles[DeviceRole] = QByteArrayLiteral("Device");
40 roles[AdapterRole] = QByteArrayLiteral("Adapter");
41 roles[MediaPlayerRole] = QByteArrayLiteral("MediaPlayer");
42 roles[BatteryRole] = QByteArrayLiteral("Battery");
43
44 return roles;
45}
46
47QVariant DeclarativeDevicesModel::data(const QModelIndex &index, int role) const
48{
49 if (!m_model) {
51 }
52
54 if (!dev) {
56 }
57
58 switch (role) {
59 case DeviceRole:
60 return QVariant::fromValue(m_manager->declarativeDeviceFromPtr(dev));
61 case AdapterRole:
62 return QVariant::fromValue(m_manager->declarativeAdapterFromPtr(dev->adapter()));
63 case MediaPlayerRole:
64 if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(dev)) {
65 return QVariant::fromValue(device->mediaPlayer());
66 }
67 break;
68 case BatteryRole:
69 if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(dev)) {
70 return QVariant::fromValue(device->battery());
71 }
72 break;
73 }
74
76}
77
78#include "moc_declarativedevicesmodel.cpp"
Model of all devices.
DevicePtr device(const QModelIndex &index) const
Returns a device for specified index.
virtual QHash< int, QByteArray > roleNames() const const
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.