Changeset View
Changeset View
Standalone View
Standalone View
src/models/resourcemodel.cpp
Show All 12 Lines | |||||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU General Public License | 14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | 16 | */ | ||
17 | 17 | | |||
18 | #include "resourcemodel.h" | 18 | #include "resourcemodel.h" | ||
19 | 19 | | |||
20 | #include <QIcon> | 20 | #include <QIcon> | ||
21 | #include <QSignalMapper> | | |||
22 | 21 | | |||
23 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
24 | #include <KCategorizedSortFilterProxyModel> | 23 | #include <KCategorizedSortFilterProxyModel> | ||
25 | 24 | | |||
26 | ResourceModel::ResourceModel(QObject* parent) : | 25 | ResourceModel::ResourceModel(QObject* parent) : | ||
27 | QAbstractListModel(parent), | 26 | QAbstractListModel(parent), | ||
28 | m_dataIndex(0), | 27 | m_dataIndex(0) | ||
29 | m_signalMapper(new QSignalMapper(this)) | | |||
30 | { | 28 | { | ||
31 | connect(m_signalMapper, SIGNAL(mapped(int)), SLOT(emitDataChanged(int))); | | |||
32 | } | 29 | } | ||
33 | 30 | | |||
34 | DataIndex* ResourceModel::dataIndex() const | 31 | DataIndex* ResourceModel::dataIndex() const | ||
35 | { | 32 | { | ||
36 | return m_dataIndex; | 33 | return m_dataIndex; | ||
37 | } | 34 | } | ||
38 | 35 | | |||
39 | void ResourceModel::setDataIndex(DataIndex* dataIndex) | 36 | void ResourceModel::setDataIndex(DataIndex* dataIndex) | ||
Show All 18 Lines | 50 | { | |||
58 | connect(m_dataIndex, &DataIndex::keyboardLayoutAboutToBeAdded, this, &ResourceModel::onKeyboardLayoutAboutToBeAdded); | 55 | connect(m_dataIndex, &DataIndex::keyboardLayoutAboutToBeAdded, this, &ResourceModel::onKeyboardLayoutAboutToBeAdded); | ||
59 | connect(m_dataIndex, &DataIndex::keyboardLayoutAdded, this, &ResourceModel::onResourceAdded); | 56 | connect(m_dataIndex, &DataIndex::keyboardLayoutAdded, this, &ResourceModel::onResourceAdded); | ||
60 | connect(m_dataIndex, &DataIndex::keyboardLayoutsAboutToBeRemoved, this, &ResourceModel::onKeyboardLayoutsAboutToBeRemoved); | 57 | connect(m_dataIndex, &DataIndex::keyboardLayoutsAboutToBeRemoved, this, &ResourceModel::onKeyboardLayoutsAboutToBeRemoved); | ||
61 | connect(m_dataIndex, &DataIndex::keyboardLayoutsRemoved, this, &ResourceModel::onResourceRemoved); | 58 | connect(m_dataIndex, &DataIndex::keyboardLayoutsRemoved, this, &ResourceModel::onResourceRemoved); | ||
62 | } | 59 | } | ||
63 | 60 | | |||
64 | for (int i = 0; i < dataIndex->courseCount(); i++) | 61 | for (int i = 0; i < dataIndex->courseCount(); i++) | ||
65 | { | 62 | { | ||
66 | connectToCourse(dataIndex->course(i)); | 63 | connectToCourse(dataIndex->course(i), i); | ||
67 | } | 64 | } | ||
68 | 65 | | |||
69 | for (int i = 0; i < dataIndex->keyboardLayoutCount(); i++) | 66 | for (int i = 0; i < dataIndex->keyboardLayoutCount(); i++) | ||
70 | { | 67 | { | ||
71 | connectToKeyboardLayout(dataIndex->keyboardLayout(i)); | 68 | connectToKeyboardLayout(dataIndex->keyboardLayout(i), i); | ||
72 | } | 69 | } | ||
73 | 70 | | |||
74 | updateMappings(); | 71 | updateMappings(); | ||
75 | 72 | | |||
76 | endResetModel(); | 73 | endResetModel(); | ||
77 | 74 | | |||
78 | emit dataIndexChanged(); | 75 | emit dataIndexChanged(); | ||
79 | } | 76 | } | ||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Line(s) | 122 | { | |||
127 | names.insert(ResourceModel::KeyboardLayoutNameRole, "keyboardLayoutName"); | 124 | names.insert(ResourceModel::KeyboardLayoutNameRole, "keyboardLayoutName"); | ||
128 | names.insert(ResourceModel::PathRole, "path"); | 125 | names.insert(ResourceModel::PathRole, "path"); | ||
129 | names.insert(ResourceModel::DataRole, "dataRole"); | 126 | names.insert(ResourceModel::DataRole, "dataRole"); | ||
130 | return names; | 127 | return names; | ||
131 | } | 128 | } | ||
132 | 129 | | |||
133 | void ResourceModel::onCourseAboutToBeAdded(DataIndexCourse* course, int index) | 130 | void ResourceModel::onCourseAboutToBeAdded(DataIndexCourse* course, int index) | ||
134 | { | 131 | { | ||
135 | connectToCourse(course); | 132 | connectToCourse(course, index); | ||
136 | beginInsertRows(QModelIndex(), index, index); | 133 | beginInsertRows(QModelIndex(), index, index); | ||
137 | } | 134 | } | ||
138 | 135 | | |||
139 | void ResourceModel::onCoursesAboutToBeRemoved(int first, int last) | 136 | void ResourceModel::onCoursesAboutToBeRemoved(int first, int last) | ||
140 | { | 137 | { | ||
141 | beginRemoveRows(QModelIndex(), first, last); | 138 | beginRemoveRows(QModelIndex(), first, last); | ||
142 | } | 139 | } | ||
143 | 140 | | |||
144 | void ResourceModel::onKeyboardLayoutAboutToBeAdded(DataIndexKeyboardLayout* keyboardLayout, int index) | 141 | void ResourceModel::onKeyboardLayoutAboutToBeAdded(DataIndexKeyboardLayout* keyboardLayout, int index) | ||
145 | { | 142 | { | ||
146 | if (!m_dataIndex) | 143 | if (!m_dataIndex) | ||
147 | return; | 144 | return; | ||
148 | 145 | | |||
149 | const int offset = m_dataIndex->courseCount(); | 146 | const int offset = m_dataIndex->courseCount(); | ||
150 | connectToKeyboardLayout(keyboardLayout); | 147 | connectToKeyboardLayout(keyboardLayout, index + offset); | ||
151 | beginInsertRows(QModelIndex(), index + offset, index + offset); | 148 | beginInsertRows(QModelIndex(), index + offset, index + offset); | ||
152 | } | 149 | } | ||
153 | 150 | | |||
154 | void ResourceModel::onKeyboardLayoutsAboutToBeRemoved(int first, int last) | 151 | void ResourceModel::onKeyboardLayoutsAboutToBeRemoved(int first, int last) | ||
155 | { | 152 | { | ||
156 | if (!m_dataIndex) | 153 | if (!m_dataIndex) | ||
157 | return; | 154 | return; | ||
158 | 155 | | |||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Line(s) | 237 | case ResourceModel::IndexRole: | |||
241 | return QVariant(row); | 238 | return QVariant(row); | ||
242 | case ResourceModel::SourceRole: | 239 | case ResourceModel::SourceRole: | ||
243 | return QVariant(m_dataIndex->keyboardLayout(row)->source()); | 240 | return QVariant(m_dataIndex->keyboardLayout(row)->source()); | ||
244 | default: | 241 | default: | ||
245 | return QVariant(); | 242 | return QVariant(); | ||
246 | } | 243 | } | ||
247 | } | 244 | } | ||
248 | 245 | | |||
249 | void ResourceModel::connectToCourse(DataIndexCourse* course) | 246 | void ResourceModel::connectToCourse(DataIndexCourse* course, int index) | ||
250 | { | 247 | { | ||
251 | connect(course, SIGNAL(titleChanged()), m_signalMapper, SLOT(map())); | 248 | disconnect(course, &DataIndexCourse::titleChanged, this, nullptr); | ||
252 | connect(course, SIGNAL(descriptionChanged()), m_signalMapper, SLOT(map())); | 249 | disconnect(course, &DataIndexCourse::descriptionChanged, this, nullptr); | ||
253 | connect(course, SIGNAL(keyboardLayoutNameChanged()), m_signalMapper, SLOT(map())); | 250 | disconnect(course, &DataIndexCourse::keyboardLayoutNameChanged, this, nullptr); | ||
254 | connect(course, SIGNAL(pathChanged()), m_signalMapper, SLOT(map())); | 251 | disconnect(course, &DataIndexCourse::pathChanged, this, nullptr); | ||
255 | connect(course, SIGNAL(sourceChanged()), m_signalMapper, SLOT(map())); | 252 | disconnect(course, &DataIndexCourse::sourceChanged, this, nullptr); | ||
256 | } | 253 | connect(course, &DataIndexCourse::titleChanged, this, [=] { emitDataChanged(index); }); | ||
257 | 254 | connect(course, &DataIndexCourse::descriptionChanged, this, [=] { emitDataChanged(index); }); | |||
258 | void ResourceModel::connectToKeyboardLayout(DataIndexKeyboardLayout *keyboardLayout) | 255 | connect(course, &DataIndexCourse::keyboardLayoutNameChanged, this, [=] { emitDataChanged(index); }); | ||
259 | { | 256 | connect(course, &DataIndexCourse::pathChanged, this, [=] { emitDataChanged(index); }); | ||
260 | connect(keyboardLayout, SIGNAL(titleChanged()), m_signalMapper, SLOT(map())); | 257 | connect(course, &DataIndexCourse::sourceChanged, this, [=] { emitDataChanged(index); }); | ||
gottfried: We need to disconnect here first, too. | |||||
261 | connect(keyboardLayout, SIGNAL(nameChanged()), m_signalMapper, SLOT(map())); | 258 | } | ||
262 | connect(keyboardLayout, SIGNAL(pathChanged()), m_signalMapper, SLOT(map())); | 259 | | ||
263 | connect(keyboardLayout, SIGNAL(sourceChanged()), m_signalMapper, SLOT(map())); | 260 | void ResourceModel::connectToKeyboardLayout(DataIndexKeyboardLayout *keyboardLayout, int index) | ||
261 | { | ||||
262 | disconnect(keyboardLayout, &DataIndexKeyboardLayout::titleChanged, this, nullptr); | ||||
263 | disconnect(keyboardLayout, &DataIndexKeyboardLayout::nameChanged, this, nullptr); | ||||
264 | disconnect(keyboardLayout, &DataIndexKeyboardLayout::pathChanged, this, nullptr); | ||||
265 | disconnect(keyboardLayout, &DataIndexKeyboardLayout::sourceChanged, this, nullptr); | ||||
266 | connect(keyboardLayout, &DataIndexKeyboardLayout::titleChanged, this, [=] { emitDataChanged(index); }); | ||||
267 | connect(keyboardLayout, &DataIndexKeyboardLayout::nameChanged, this, [=] { emitDataChanged(index); }); | ||||
268 | connect(keyboardLayout, &DataIndexKeyboardLayout::pathChanged, this, [=] { emitDataChanged(index); }); | ||||
269 | connect(keyboardLayout, &DataIndexKeyboardLayout::sourceChanged, this, [=] { emitDataChanged(index); }); | ||||
gottfried: We need to disconnect here first, too. | |||||
264 | } | 270 | } | ||
265 | 271 | | |||
266 | void ResourceModel::updateMappings() | 272 | void ResourceModel::updateMappings() | ||
267 | { | 273 | { | ||
268 | if (!m_dataIndex) | 274 | if (!m_dataIndex) | ||
269 | return; | 275 | return; | ||
270 | 276 | | |||
271 | for (int i = 0; i < m_dataIndex->courseCount(); i++) | 277 | for (int i = 0; i < m_dataIndex->courseCount(); i++) | ||
272 | { | 278 | { | ||
273 | m_signalMapper->setMapping(m_dataIndex->course(i), i); | 279 | connectToCourse(m_dataIndex->course(i), i); | ||
274 | } | 280 | } | ||
275 | 281 | | |||
276 | const int offset = m_dataIndex->courseCount(); | 282 | const int offset = m_dataIndex->courseCount(); | ||
277 | 283 | | |||
278 | for (int i = 0; i < m_dataIndex->keyboardLayoutCount(); i++) | 284 | for (int i = 0; i < m_dataIndex->keyboardLayoutCount(); i++) | ||
279 | { | 285 | { | ||
280 | m_signalMapper->setMapping(m_dataIndex->keyboardLayout(i), i + offset); | 286 | connectToKeyboardLayout(m_dataIndex->keyboardLayout(i), i + offset); | ||
281 | } | 287 | } | ||
282 | } | 288 | } | ||
283 | 289 | | |||
284 | QIcon ResourceModel::resourceIcon(DataIndex::Source source) const | 290 | QIcon ResourceModel::resourceIcon(DataIndex::Source source) const | ||
285 | { | 291 | { | ||
286 | static QIcon systemIcon = QIcon::fromTheme("computer"); | 292 | static QIcon systemIcon = QIcon::fromTheme("computer"); | ||
287 | static QIcon userIcon = QIcon::fromTheme("user-identity"); | 293 | static QIcon userIcon = QIcon::fromTheme("user-identity"); | ||
288 | return source == DataIndex::BuiltInResource? systemIcon: userIcon; | 294 | return source == DataIndex::BuiltInResource? systemIcon: userIcon; | ||
289 | } | 295 | } |
We need to disconnect here first, too.