| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2013-2016 Andreas Cord-Landwehr <[email protected]> |
| 3 |
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "learninggoalmodel.h" |
| 7 |
#include "learner.h" |
| 8 |
#include "liblearner_debug.h" |
| 9 |
#include "profilemanager.h" |
| 10 |
#include <KLocalizedString> |
| 11 |
#include <QSignalMapper> |
| 12 |
|
| 13 |
using namespace LearnerProfile; |
| 14 |
|
| 15 |
// private class LearningGoalModelPrivate |
| 16 |
class LearningGoalModelPrivate |
| 17 |
{ |
| 18 |
public: |
| 19 |
LearningGoalModelPrivate() |
| 20 |
: m_profileManager(nullptr) |
| 21 |
, m_learner(nullptr) |
| 22 |
, m_signalMapper(new QSignalMapper()) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
~LearningGoalModelPrivate() |
| 27 |
{ |
| 28 |
delete m_signalMapper; |
| 29 |
} |
| 30 |
|
| 31 |
void updateGoals(); |
| 32 |
void updateMappings(); |
| 33 |
|
| 34 |
ProfileManager *m_profileManager; |
| 35 |
Learner *m_learner; |
| 36 |
QList<LearningGoal *> m_goals; |
| 37 |
QSignalMapper *m_signalMapper; |
| 38 |
}; |
| 39 |
|
| 40 |
void LearningGoalModelPrivate::updateGoals() |
| 41 |
{ |
| 42 |
m_goals.clear(); |
| 43 |
// set all registered goals from profile manager |
| 44 |
if (m_profileManager) { |
| 45 |
for (LearningGoal *goal : m_profileManager->goals()) { |
| 46 |
m_goals.append(goal); |
| 47 |
} |
| 48 |
} |
| 49 |
// TODO add learner status information |
| 50 |
} |
| 51 |
|
| 52 |
void LearningGoalModelPrivate::updateMappings() |
| 53 |
{ |
| 54 |
if (!m_profileManager) { |
| 55 |
return; |
| 56 |
} |
| 57 |
int goals = m_goals.count(); |
| 58 |
for (int i = 0; i < goals; ++i) { |
| 59 |
m_signalMapper->setMapping(m_goals.at(i), i); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// class LearningGoalModel |
| 64 |
LearningGoalModel::LearningGoalModel(QObject *parent) |
| 65 |
: QAbstractListModel(parent) |
| 66 |
, d(new LearningGoalModelPrivate) |
| 67 |
{ |
| 68 |
connect(d->m_signalMapper, &QSignalMapper::mappedInt, this, &LearningGoalModel::emitLearningGoalChanged); |
| 69 |
} |
| 70 |
|
| 71 |
LearningGoalModel::~LearningGoalModel() |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
QHash<int, QByteArray> LearningGoalModel::roleNames() const |
| 76 |
{ |
| 77 |
QHash<int, QByteArray> roles; |
| 78 |
roles[TitleRole] = "title"; |
| 79 |
roles[IdRole] = "id"; |
| 80 |
roles[DataRole] = "dataRole"; |
| 81 |
|
| 82 |
return roles; |
| 83 |
} |
| 84 |
|
| 85 |
void LearningGoalModel::setProfileManager(ProfileManager *profileManager) |
| 86 |
{ |
| 87 |
if (d->m_profileManager == profileManager) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
beginResetModel(); |
| 92 |
if (d->m_profileManager) { |
| 93 |
d->m_profileManager->disconnect(this); |
| 94 |
} |
| 95 |
|
| 96 |
d->m_profileManager = profileManager; |
| 97 |
d->updateGoals(); |
| 98 |
d->updateMappings(); |
| 99 |
endResetModel(); |
| 100 |
|
| 101 |
Q_EMIT profileManagerChanged(); |
| 102 |
} |
| 103 |
|
| 104 |
ProfileManager *LearningGoalModel::profileManager() const |
| 105 |
{ |
| 106 |
return d->m_profileManager; |
| 107 |
} |
| 108 |
|
| 109 |
Learner *LearningGoalModel::learner() const |
| 110 |
{ |
| 111 |
return d->m_learner; |
| 112 |
} |
| 113 |
|
| 114 |
void LearningGoalModel::setLearner(Learner *learner) |
| 115 |
{ |
| 116 |
if (!learner) { |
| 117 |
return; |
| 118 |
} |
| 119 |
beginResetModel(); |
| 120 |
if (d->m_learner) { |
| 121 |
learner->disconnect(this); |
| 122 |
} |
| 123 |
d->m_learner = learner; |
| 124 |
d->updateGoals(); |
| 125 |
d->updateMappings(); |
| 126 |
connect(learner, &Learner::goalAboutToBeAdded, this, &LearningGoalModel::onLearningGoalAboutToBeAdded); |
| 127 |
connect(learner, &Learner::goalAdded, this, &LearningGoalModel::onLearningGoalAdded); |
| 128 |
connect(learner, &Learner::goalAboutToBeRemoved, this, &LearningGoalModel::onLearningGoalAboutToBeRemoved); |
| 129 |
Q_EMIT learnerChanged(); |
| 130 |
endResetModel(); |
| 131 |
} |
| 132 |
|
| 133 |
QVariant LearningGoalModel::data(const QModelIndex &index, int role) const |
| 134 |
{ |
| 135 |
if (!index.isValid()) { |
| 136 |
return QVariant(); |
| 137 |
} |
| 138 |
|
| 139 |
if (index.row() >= d->m_goals.count()) { |
| 140 |
return QVariant(); |
| 141 |
} |
| 142 |
|
| 143 |
LearningGoal *const goal = d->m_goals.at(index.row()); |
| 144 |
|
| 145 |
switch (role) { |
| 146 |
case Qt::DisplayRole: |
| 147 |
return !goal->name().isEmpty() ? QVariant(goal->name()) : QVariant(i18nc("@item:inlistbox unknown learning goal", "unknown")); |
| 148 |
case Qt::ToolTipRole: |
| 149 |
return QVariant(goal->name()); |
| 150 |
case TitleRole: |
| 151 |
return goal->name(); |
| 152 |
case IdRole: |
| 153 |
return goal->identifier(); |
| 154 |
case DataRole: |
| 155 |
return QVariant::fromValue<QObject *>(goal); |
| 156 |
default: |
| 157 |
return QVariant(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
int LearningGoalModel::rowCount(const QModelIndex &parent) const |
| 162 |
{ |
| 163 |
if (parent.isValid()) { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
return d->m_goals.count(); |
| 167 |
} |
| 168 |
|
| 169 |
void LearningGoalModel::onLearningGoalAboutToBeAdded(LearningGoal *goal, int index) |
| 170 |
{ |
| 171 |
Q_UNUSED(index) |
| 172 |
beginInsertRows(QModelIndex(), d->m_goals.count(), d->m_goals.count()); |
| 173 |
d->m_goals.append(goal); |
| 174 |
d->updateMappings(); |
| 175 |
} |
| 176 |
|
| 177 |
void LearningGoalModel::onLearningGoalAdded() |
| 178 |
{ |
| 179 |
endInsertRows(); |
| 180 |
} |
| 181 |
|
| 182 |
void LearningGoalModel::onLearningGoalAboutToBeRemoved(int index) |
| 183 |
{ |
| 184 |
if (!d->m_learner) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
if (index < 0 || d->m_goals.count() <= index) { |
| 189 |
qCWarning(LIBLEARNER_LOG) << "Cannot remove learning goal from model, not registered"; |
| 190 |
return; |
| 191 |
} |
| 192 |
beginRemoveRows(QModelIndex(), index, index); |
| 193 |
d->m_goals.removeAt(index); |
| 194 |
d->updateMappings(); |
| 195 |
endRemoveRows(); |
| 196 |
} |
| 197 |
|
| 198 |
void LearningGoalModel::emitLearningGoalChanged(int row) |
| 199 |
{ |
| 200 |
Q_EMIT learningGoalChanged(row); |
| 201 |
Q_EMIT dataChanged(index(row, 0), index(row, 0)); |
| 202 |
} |
| 203 |
|
| 204 |
QVariant LearningGoalModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 205 |
{ |
| 206 |
if (role != Qt::DisplayRole) { |
| 207 |
return QVariant(); |
| 208 |
} |
| 209 |
if (orientation == Qt::Vertical) { |
| 210 |
return QVariant(section + 1); |
| 211 |
} |
| 212 |
return QVariant(i18nc("@title:column", "Learning Goal")); |
| 213 |
} |
| 214 |
|
| 215 |
QVariant LearningGoalModel::learningGoal(int row) const |
| 216 |
{ |
| 217 |
return data(index(row, 0), LearningGoalModel::DataRole); |
| 218 |
} |