Changeset View
Changeset View
Standalone View
Standalone View
src/maps.h
Show All 40 Lines | |||||
41 | /** | 41 | /** | ||
42 | * @see MapBase | 42 | * @see MapBase | ||
43 | * This class is nothing more than the QObject base since moc cannot handle | 43 | * This class is nothing more than the QObject base since moc cannot handle | ||
44 | * templates. | 44 | * templates. | ||
45 | */ | 45 | */ | ||
46 | class Q_DECL_EXPORT MapBaseQObject : public QObject | 46 | class Q_DECL_EXPORT MapBaseQObject : public QObject | ||
47 | { | 47 | { | ||
48 | Q_OBJECT | 48 | Q_OBJECT | ||
49 | | ||||
50 | public: | ||||
51 | virtual int count() const = 0; | ||||
52 | virtual QObject *objectAt(int index) const = 0; | ||||
53 | virtual int indexOfObject(QObject *object) const = 0; | ||||
54 | | ||||
49 | signals: | 55 | signals: | ||
50 | void added(quint32 index); | 56 | void added(int index); | ||
51 | void updated(quint32 index); | 57 | void removed(int index); | ||
52 | void removed(quint32 index); | | |||
53 | }; | 58 | }; | ||
54 | 59 | | |||
55 | /** | 60 | /** | ||
56 | * Maps a specific index to a specific object pointer. | 61 | * Maps a specific index to a specific object pointer. | ||
57 | * This is used to give the unique arbitrary PulseAudio index of a PulseObject a | 62 | * This is used to give the unique arbitrary PulseAudio index of a PulseObject a | ||
58 | * serialized list index. Namely it enables us to translate a discrete list | 63 | * serialized list index. Namely it enables us to translate a discrete list | ||
59 | * index to a pulse index to an object, and any permutation thereof. | 64 | * index to a pulse index to an object, and any permutation thereof. | ||
60 | */ | 65 | */ | ||
61 | template<typename Type, typename PAInfo> | 66 | template<typename Type, typename PAInfo> | ||
62 | class Q_DECL_EXPORT MapBase : public MapBaseQObject | 67 | class Q_DECL_EXPORT MapBase : public MapBaseQObject | ||
63 | { | 68 | { | ||
64 | public: | 69 | public: | ||
65 | virtual ~MapBase() {} | 70 | virtual ~MapBase() {} | ||
66 | 71 | | |||
67 | const QMap<quint32, Type *> &data() const { return m_data; } | 72 | const QMap<quint32, Type *> &data() const { return m_data; } | ||
68 | 73 | | |||
69 | int modelIndexForQObject(QObject *qobject) const | 74 | int count() const Q_DECL_OVERRIDE | ||
70 | { | 75 | { | ||
71 | Type *t = qobject_cast<Type *>(qobject); | 76 | return m_data.count(); | ||
72 | if (!t) | | |||
73 | return -1; | | |||
74 | int key = m_data.key(t, -1); | | |||
75 | if (key == -1) | | |||
76 | return -1; | | |||
77 | return m_data.keys().indexOf(key); | | |||
78 | } | 77 | } | ||
79 | 78 | | |||
80 | /** | 79 | int indexOfObject(QObject *object) const Q_DECL_OVERRIDE | ||
81 | * @param dataIndex index in the data model | | |||
82 | * @return -1 on invalid index, otherwise PA index | | |||
83 | */ | | |||
84 | qint64 dataIndexToPaIndex(int dataIndex) const | | |||
85 | { | 80 | { | ||
86 | auto list = m_data.values(); | 81 | int index = 0; | ||
87 | qCDebug(PLASMAPA) << Q_FUNC_INFO << list.length() << dataIndex; | 82 | QMapIterator<quint32, Type *> it(m_data); | ||
88 | if (list.length() <= dataIndex) { | 83 | while (it.hasNext()) { | ||
89 | return -1; | 84 | it.next(); | ||
85 | if (it.value() == object) { | ||||
86 | return index; | ||||
90 | } | 87 | } | ||
91 | qCDebug(PLASMAPA) << " " << list.at(dataIndex)->index(); | 88 | index++; | ||
92 | qCDebug(PLASMAPA) << " " << list.at(dataIndex)->name(); | 89 | } | ||
93 | return list.at(dataIndex)->index(); | 90 | return -1; | ||
94 | } | 91 | } | ||
95 | 92 | | |||
96 | int paIndexToDataIndex(quint32 index) const | 93 | QObject *objectAt(int index) const Q_DECL_OVERRIDE | ||
97 | { | 94 | { | ||
98 | qCDebug(PLASMAPA) << m_data.keys() << index; | 95 | return (m_data.constBegin() + index).value(); | ||
broulik: Not too fond of allocating a list just to get an item from it, can we perhaps do something like… | |||||
99 | return m_data.keys().indexOf(index); | | |||
100 | } | 96 | } | ||
101 | 97 | | |||
102 | void reset() | 98 | void reset() | ||
103 | { | 99 | { | ||
104 | while (!m_data.isEmpty()) { | 100 | while (!m_data.isEmpty()) { | ||
105 | removeEntry(m_data.lastKey()); | 101 | removeEntry(m_data.lastKey()); | ||
106 | } | 102 | } | ||
107 | m_pendingRemovals.clear(); | 103 | m_pendingRemovals.clear(); | ||
Show All 20 Lines | 110 | { | |||
128 | obj->update(info); | 124 | obj->update(info); | ||
129 | m_data.insert(info->index, obj); | 125 | m_data.insert(info->index, obj); | ||
130 | 126 | | |||
131 | const int modelIndex = m_data.keys().indexOf(info->index); | 127 | const int modelIndex = m_data.keys().indexOf(info->index); | ||
132 | Q_ASSERT(modelIndex >= 0); | 128 | Q_ASSERT(modelIndex >= 0); | ||
133 | 129 | | |||
134 | if (isNew) { | 130 | if (isNew) { | ||
135 | emit added(modelIndex); | 131 | emit added(modelIndex); | ||
136 | } else { | | |||
137 | emit updated(modelIndex); | | |||
138 | } | 132 | } | ||
139 | } | 133 | } | ||
140 | 134 | | |||
141 | void removeEntry(quint32 index) | 135 | void removeEntry(quint32 index) | ||
142 | { | 136 | { | ||
143 | if (!m_data.contains(index)) { | 137 | if (!m_data.contains(index)) { | ||
144 | m_pendingRemovals.insert(index); | 138 | m_pendingRemovals.insert(index); | ||
145 | } else { | 139 | } else { | ||
146 | const int modelIndex = m_data.keys().indexOf(index); | 140 | const int modelIndex = m_data.keys().indexOf(index); | ||
147 | m_data.take(index)->deleteLater(); | 141 | delete m_data.take(index); | ||
148 | emit removed(modelIndex); | 142 | emit removed(modelIndex); | ||
149 | } | 143 | } | ||
150 | } | 144 | } | ||
151 | 145 | | |||
152 | protected: | 146 | protected: | ||
153 | QMap<quint32, Type *> m_data; | 147 | QMap<quint32, Type *> m_data; | ||
154 | QSet<quint32> m_pendingRemovals; | 148 | QSet<quint32> m_pendingRemovals; | ||
155 | }; | 149 | }; | ||
Show All 11 Lines |
Not too fond of allocating a list just to get an item from it, can we perhaps do something like (m_data.constBegin() + index).value();