Changeset View
Changeset View
Standalone View
Standalone View
libs/bolt/devicemodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "devicemodel.h" | ||||
22 | #include "device.h" | ||||
23 | #include "manager.h" | ||||
24 | | ||||
25 | using namespace Bolt; | ||||
26 | | ||||
27 | Q_DECLARE_METATYPE(QSharedPointer<Bolt::Device>) | ||||
28 | | ||||
29 | void DeviceModel::setManager(Manager *manager) | ||||
30 | { | ||||
31 | if (mManager == manager) { | ||||
32 | return; | ||||
33 | } | ||||
34 | | ||||
35 | if (mManager) { | ||||
36 | mManager->disconnect(this); | ||||
37 | } | ||||
38 | | ||||
39 | beginResetModel(); | ||||
40 | mManager = manager; | ||||
41 | mDevices.clear(); | ||||
42 | if (mManager) { | ||||
43 | connect(mManager, &Manager::deviceAdded, | ||||
44 | this, [this](const QSharedPointer<Device> &device) { | ||||
45 | if (mShowHosts || device->type() == Type::Peripheral) { | ||||
46 | beginInsertRows({}, mDevices.count(), mDevices.count()); | ||||
47 | mDevices.push_back(device); | ||||
48 | endInsertRows(); | ||||
49 | } | ||||
50 | }); | ||||
51 | connect(mManager, &Manager::deviceRemoved, | ||||
52 | this, [this](const QSharedPointer<Device> &device) { | ||||
53 | const int idx = mDevices.indexOf(device); | ||||
54 | if (idx == -1) { | ||||
55 | return; | ||||
56 | } | ||||
57 | beginRemoveRows({}, idx, idx); | ||||
58 | mDevices.removeAt(idx); | ||||
59 | endRemoveRows(); | ||||
60 | }); | ||||
61 | | ||||
62 | populateWithoutReset(); | ||||
63 | } | ||||
64 | endResetModel(); | ||||
65 | | ||||
66 | Q_EMIT managerChanged(mManager); | ||||
67 | } | ||||
68 | | ||||
69 | Manager *DeviceModel::manager() const | ||||
70 | { | ||||
71 | return mManager; | ||||
72 | } | ||||
73 | | ||||
74 | bool DeviceModel::showHosts() const | ||||
75 | { | ||||
76 | return mShowHosts; | ||||
77 | } | ||||
78 | | ||||
79 | void DeviceModel::setShowHosts(bool showHosts) | ||||
80 | { | ||||
81 | if (mShowHosts != showHosts) { | ||||
82 | mShowHosts = showHosts; | ||||
83 | Q_EMIT showHostsChanged(mShowHosts); | ||||
84 | if (mManager) { | ||||
85 | beginResetModel(); | ||||
86 | populateWithoutReset(); | ||||
87 | endResetModel(); | ||||
88 | } | ||||
89 | } | ||||
90 | } | ||||
91 | | ||||
92 | QHash<int, QByteArray> DeviceModel::roleNames() const | ||||
93 | { | ||||
94 | auto roles = QAbstractListModel::roleNames(); | ||||
95 | roles[DeviceRole] = "device"; | ||||
96 | return roles; | ||||
97 | } | ||||
98 | | ||||
99 | int DeviceModel::rowCount(const QModelIndex &parent) const | ||||
100 | { | ||||
101 | if (parent.isValid()) { | ||||
102 | return 0; | ||||
103 | } | ||||
104 | | ||||
105 | return mDevices.count(); | ||||
106 | } | ||||
107 | | ||||
108 | QVariant DeviceModel::data(const QModelIndex &index, int role) const | ||||
109 | { | ||||
110 | if (!index.isValid()) { | ||||
111 | return {}; | ||||
112 | } | ||||
113 | | ||||
114 | if (index.row() >= mDevices.size()) { | ||||
115 | return {}; | ||||
116 | } | ||||
117 | | ||||
118 | if (role == DeviceRole) { | ||||
119 | return QVariant::fromValue(mDevices.at(index.row()).data()); | ||||
120 | } | ||||
121 | | ||||
122 | return {}; | ||||
123 | } | ||||
124 | | ||||
125 | void DeviceModel::populateWithoutReset() | ||||
126 | { | ||||
127 | Q_ASSERT(mManager); | ||||
128 | | ||||
129 | mDevices.clear(); | ||||
130 | const auto all = mManager->devices(); | ||||
131 | std::copy_if(all.cbegin(), all.cend(), std::back_inserter(mDevices), | ||||
132 | [this](const auto &device) { | ||||
133 | return mShowHosts || device->type() == Type::Peripheral; | ||||
134 | }); | ||||
135 | } |