BluezQt

declarativedevicesmodel.cpp
1 /*
2  * BluezQt - Asynchronous Bluez wrapper library
3  *
4  * SPDX-FileCopyrightText: 2014 David Rosca <[email protected]>
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 "declarativemediaplayer.h"
14 
15 DeclarativeDevicesModel::DeclarativeDevicesModel(QObject *parent)
16  : QSortFilterProxyModel(parent)
17  , m_manager(nullptr)
18  , m_model(nullptr)
19 {
20 }
21 
22 DeclarativeManager *DeclarativeDevicesModel::manager() const
23 {
24  return m_manager;
25 }
26 
27 void DeclarativeDevicesModel::setManager(DeclarativeManager *manager)
28 {
29  m_manager = manager;
30  m_model = new BluezQt::DevicesModel(m_manager, this);
31  setSourceModel(m_model);
32 }
33 
34 QHash<int, QByteArray> DeclarativeDevicesModel::roleNames() const
35 {
37 
38  roles[DeviceRole] = QByteArrayLiteral("Device");
39  roles[AdapterRole] = QByteArrayLiteral("Adapter");
40  roles[MediaPlayerRole] = QByteArrayLiteral("MediaPlayer");
41  roles[BatteryRole] = QByteArrayLiteral("Battery");
42 
43  return roles;
44 }
45 
46 QVariant DeclarativeDevicesModel::data(const QModelIndex &index, int role) const
47 {
48  if (!m_model) {
49  return QSortFilterProxyModel::data(index, role);
50  }
51 
52  BluezQt::DevicePtr dev = m_model->device(mapToSource(index));
53  if (!dev) {
54  return QSortFilterProxyModel::data(index, role);
55  }
56 
57  switch (role) {
58  case DeviceRole:
59  return QVariant::fromValue(m_manager->declarativeDeviceFromPtr(dev));
60  case AdapterRole:
61  return QVariant::fromValue(m_manager->declarativeAdapterFromPtr(dev->adapter()));
62  case MediaPlayerRole:
63  if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(dev)) {
64  return QVariant::fromValue(device->mediaPlayer());
65  }
66  break;
67  case BatteryRole:
68  if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(dev)) {
69  return QVariant::fromValue(device->battery());
70  }
71  break;
72  }
73 
74  return QSortFilterProxyModel::data(index, role);
75 }
QVariant fromValue(const T &value)
virtual QHash< int, QByteArray > roleNames() const const
virtual QVariant data(const QModelIndex &index, int role) const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:19:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.