Changeset View
Changeset View
Standalone View
Standalone View
src/pulseaudio.cpp
Show All 35 Lines | |||||
36 | namespace QPulseAudio | 36 | namespace QPulseAudio | ||
37 | { | 37 | { | ||
38 | 38 | | |||
39 | AbstractModel::AbstractModel(const MapBaseQObject *map, QObject *parent) | 39 | AbstractModel::AbstractModel(const MapBaseQObject *map, QObject *parent) | ||
40 | : QAbstractListModel(parent) | 40 | : QAbstractListModel(parent) | ||
41 | , m_map(map) | 41 | , m_map(map) | ||
42 | { | 42 | { | ||
43 | Context::instance()->ref(); | 43 | Context::instance()->ref(); | ||
44 | //deref context after we've deleted this object | | |||
45 | //see https://bugs.kde.org/show_bug.cgi?id=371215 | | |||
46 | connect(this, &QObject::destroyed, []() { | | |||
47 | Context::instance()->unref(); | | |||
48 | }); | | |||
49 | 44 | | |||
50 | connect(m_map, &MapBaseQObject::added, this, &AbstractModel::onDataAdded); | 45 | connect(m_map, &MapBaseQObject::aboutToBeAdded, this, [this](int index) { | ||
46 | beginInsertRows(QModelIndex(), index, index); | ||||
47 | }); | ||||
48 | connect(m_map, &MapBaseQObject::added, this, [this](int index) { | ||||
49 | onDataAdded(index); | ||||
50 | endInsertRows(); | ||||
51 | }); | ||||
51 | connect(m_map, &MapBaseQObject::aboutToBeRemoved, this, [this](int index) { | 52 | connect(m_map, &MapBaseQObject::aboutToBeRemoved, this, [this](int index) { | ||
52 | beginRemoveRows(QModelIndex(), index, index); | 53 | beginRemoveRows(QModelIndex(), index, index); | ||
53 | }); | 54 | }); | ||
54 | connect(m_map, &MapBaseQObject::removed, this, [this](int index) { | 55 | connect(m_map, &MapBaseQObject::removed, this, [this](int index) { | ||
55 | Q_UNUSED(index); | 56 | Q_UNUSED(index); | ||
56 | endRemoveRows(); | 57 | endRemoveRows(); | ||
57 | }); | 58 | }); | ||
59 | } | ||||
58 | 60 | | |||
61 | AbstractModel::~AbstractModel() | ||||
62 | { | ||||
63 | //deref context after we've deleted this object | ||||
64 | //see https://bugs.kde.org/show_bug.cgi?id=371215 | ||||
65 | Context::instance()->unref(); | ||||
59 | } | 66 | } | ||
60 | 67 | | |||
61 | QHash<int, QByteArray> AbstractModel::roleNames() const | 68 | QHash<int, QByteArray> AbstractModel::roleNames() const | ||
62 | { | 69 | { | ||
63 | if (!m_roles.empty()) { | 70 | if (!m_roles.empty()) { | ||
64 | qCDebug(PLASMAPA) << "returning roles" << m_roles; | 71 | qCDebug(PLASMAPA) << "returning roles" << m_roles; | ||
65 | return m_roles; | 72 | return m_roles; | ||
66 | } | 73 | } | ||
67 | Q_UNREACHABLE(); | 74 | Q_UNREACHABLE(); | ||
68 | return QHash<int, QByteArray>(); | 75 | return QHash<int, QByteArray>(); | ||
69 | } | 76 | } | ||
70 | 77 | | |||
71 | int AbstractModel::rowCount(const QModelIndex &parent) const | 78 | int AbstractModel::rowCount(const QModelIndex &parent) const | ||
72 | { | 79 | { | ||
73 | Q_UNUSED(parent); | 80 | if (parent.isValid()) { | ||
81 | return 0; | ||||
82 | } | ||||
74 | return m_map->count(); | 83 | return m_map->count(); | ||
75 | } | 84 | } | ||
76 | 85 | | |||
77 | QVariant AbstractModel::data(const QModelIndex &index, int role) const | 86 | QVariant AbstractModel::data(const QModelIndex &index, int role) const | ||
78 | { | 87 | { | ||
88 | if (!hasIndex(index.row(), index.column())) { | ||||
89 | return QVariant(); | ||||
90 | } | ||||
79 | QObject *data = m_map->objectAt(index.row()); | 91 | QObject *data = m_map->objectAt(index.row()); | ||
80 | Q_ASSERT(data); | 92 | Q_ASSERT(data); | ||
81 | if (role == PulseObjectRole) { | 93 | if (role == PulseObjectRole) { | ||
82 | return QVariant::fromValue(data); | 94 | return QVariant::fromValue(data); | ||
95 | } else if (role == Qt::DisplayRole) { | ||||
96 | return static_cast<PulseObject*>(data)->properties().value(QStringLiteral("name")).toString(); | ||||
83 | } | 97 | } | ||
84 | int property = m_objectProperties.value(role, -1); | 98 | int property = m_objectProperties.value(role, -1); | ||
85 | if (property == -1) { | 99 | if (property == -1) { | ||
86 | return QVariant(); | 100 | return QVariant(); | ||
87 | } | 101 | } | ||
88 | return data->metaObject()->property(property).read(data); | 102 | return data->metaObject()->property(property).read(data); | ||
89 | } | 103 | } | ||
90 | 104 | | |||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 177 | { | |||
174 | } | 188 | } | ||
175 | int index = m_map->indexOfObject(sender()); | 189 | int index = m_map->indexOfObject(sender()); | ||
176 | qCDebug(PLASMAPA) << "PROPERTY CHANGED (" << index << ") :: " << role << roleNames().value(role); | 190 | qCDebug(PLASMAPA) << "PROPERTY CHANGED (" << index << ") :: " << role << roleNames().value(role); | ||
177 | emit dataChanged(createIndex(index, 0), createIndex(index, 0), {role}); | 191 | emit dataChanged(createIndex(index, 0), createIndex(index, 0), {role}); | ||
178 | } | 192 | } | ||
179 | 193 | | |||
180 | void AbstractModel::onDataAdded(int index) | 194 | void AbstractModel::onDataAdded(int index) | ||
181 | { | 195 | { | ||
182 | beginInsertRows(QModelIndex(), index, index); | | |||
183 | QObject *data = m_map->objectAt(index); | 196 | QObject *data = m_map->objectAt(index); | ||
184 | const QMetaObject *mo = data->metaObject(); | 197 | const QMetaObject *mo = data->metaObject(); | ||
185 | // We have all the data changed notify signals already stored | 198 | // We have all the data changed notify signals already stored | ||
186 | auto keys = m_signalIndexToProperties.keys(); | 199 | auto keys = m_signalIndexToProperties.keys(); | ||
187 | foreach (int index, keys) { | 200 | foreach (int index, keys) { | ||
188 | QMetaMethod meth = mo->method(index); | 201 | QMetaMethod meth = mo->method(index); | ||
189 | connect(data, meth, this, propertyChangedMetaMethod()); | 202 | connect(data, meth, this, propertyChangedMetaMethod()); | ||
190 | } | 203 | } | ||
191 | endInsertRows(); | | |||
192 | } | 204 | } | ||
193 | 205 | | |||
194 | QMetaMethod AbstractModel::propertyChangedMetaMethod() const | 206 | QMetaMethod AbstractModel::propertyChangedMetaMethod() const | ||
195 | { | 207 | { | ||
196 | auto mo = metaObject(); | 208 | auto mo = metaObject(); | ||
197 | int methodIndex = mo->indexOfMethod("propertyChanged()"); | 209 | int methodIndex = mo->indexOfMethod("propertyChanged()"); | ||
198 | if (methodIndex == -1) { | 210 | if (methodIndex == -1) { | ||
199 | return QMetaMethod(); | 211 | return QMetaMethod(); | ||
▲ Show 20 Lines • Show All 171 Lines • Show Last 20 Lines |