Changeset View
Changeset View
Standalone View
Standalone View
src/models/lessonmodel.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 | 18 | | |||
19 | #include "lessonmodel.h" | 19 | #include "lessonmodel.h" | ||
20 | 20 | | |||
21 | #include <QSignalMapper> | | |||
22 | | ||||
23 | #include <KLocalizedString> | 21 | #include <KLocalizedString> | ||
24 | 22 | | |||
25 | #include <core/course.h> | 23 | #include <core/course.h> | ||
26 | #include <core/lesson.h> | 24 | #include <core/lesson.h> | ||
27 | 25 | | |||
28 | LessonModel::LessonModel(QObject* parent) : | 26 | LessonModel::LessonModel(QObject* parent) : | ||
29 | QAbstractListModel(parent), | 27 | QAbstractListModel(parent), | ||
30 | m_course(0), | 28 | m_course(0) | ||
31 | m_signalMapper(new QSignalMapper(this)) | | |||
32 | { | 29 | { | ||
33 | connect(m_signalMapper, SIGNAL(mapped(int)), SLOT(emitLessonChanged(int))); | | |||
34 | } | 30 | } | ||
35 | 31 | | |||
36 | Course* LessonModel::course() const | 32 | Course* LessonModel::course() const | ||
37 | { | 33 | { | ||
38 | return m_course; | 34 | return m_course; | ||
39 | } | 35 | } | ||
40 | 36 | | |||
41 | void LessonModel::setCourse(Course* course) | 37 | void LessonModel::setCourse(Course* course) | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 89 | { | |||
97 | if (parent.isValid()) | 93 | if (parent.isValid()) | ||
98 | return 0; | 94 | return 0; | ||
99 | 95 | | |||
100 | return m_course->lessonCount(); | 96 | return m_course->lessonCount(); | ||
101 | } | 97 | } | ||
102 | 98 | | |||
103 | void LessonModel::onLessonAboutToBeAdded(Lesson* lesson, int index) | 99 | void LessonModel::onLessonAboutToBeAdded(Lesson* lesson, int index) | ||
104 | { | 100 | { | ||
105 | connect(lesson, SIGNAL(titleChanged()), m_signalMapper, SLOT(map())); | 101 | connect(lesson, &Lesson::titleChanged, this, [=] { emitLessonChanged(index); }); | ||
106 | connect(lesson, SIGNAL(newCharactersChanged()), m_signalMapper, SLOT(map())); | 102 | connect(lesson, &Lesson::newCharactersChanged, this, [=] { emitLessonChanged(index); }); | ||
107 | connect(lesson, SIGNAL(textChanged()), m_signalMapper, SLOT(map())); | 103 | connect(lesson, &Lesson::textChanged, this, [=] { emitLessonChanged(index); }); | ||
108 | beginInsertRows(QModelIndex(), index, index); | 104 | beginInsertRows(QModelIndex(), index, index); | ||
109 | } | 105 | } | ||
110 | 106 | | |||
111 | void LessonModel::onLessonAdded() | 107 | void LessonModel::onLessonAdded() | ||
112 | { | 108 | { | ||
113 | updateMappings(); | 109 | updateMappings(); | ||
114 | endInsertRows(); | 110 | endInsertRows(); | ||
115 | } | 111 | } | ||
Show All 30 Lines | 140 | { | |||
146 | names.insert(LessonModel::DataRole, "dataRole"); | 142 | names.insert(LessonModel::DataRole, "dataRole"); | ||
147 | return names; | 143 | return names; | ||
148 | } | 144 | } | ||
149 | 145 | | |||
150 | void LessonModel::updateMappings() | 146 | void LessonModel::updateMappings() | ||
151 | { | 147 | { | ||
152 | for (int i = 0; i < m_course->lessonCount(); i++) | 148 | for (int i = 0; i < m_course->lessonCount(); i++) | ||
153 | { | 149 | { | ||
154 | m_signalMapper->setMapping(m_course->lesson(i), i); | 150 | disconnect(m_course->lesson(i), &Lesson::titleChanged, this, nullptr); | ||
151 | disconnect(m_course->lesson(i), &Lesson::newCharactersChanged, this, nullptr); | ||||
152 | disconnect(m_course->lesson(i), &Lesson::textChanged, this, nullptr); | ||||
153 | connect(m_course->lesson(i), &Lesson::titleChanged, this, [=] { emitLessonChanged(i); }); | ||||
154 | connect(m_course->lesson(i), &Lesson::newCharactersChanged, this, [=] { emitLessonChanged(i); }); | ||||
155 | connect(m_course->lesson(i), &Lesson::textChanged, this, [=] { emitLessonChanged(i); }); | ||||
gottfried: `updateMappings` is called multiple times. If I am not mistaken, this will result in multiple… | |||||
155 | } | 156 | } | ||
156 | } | 157 | } |
updateMappings is called multiple times. If I am not mistaken, this will result in multiple connections to the lambda with different values of ì of you don't disconnect() first.