File: learninggoalmodel.cpp

package info (click to toggle)
artikulate 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,936 kB
  • sloc: cpp: 11,286; xml: 3,163; makefile: 5; sh: 5
file content (225 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
    SPDX-FileCopyrightText: 2013-2016 Andreas Cord-Landwehr <cordlandwehr@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "learninggoalmodel.h"
#include "learner.h"
#include "profilemanager.h"

#include <QSignalMapper>

#include "liblearner_debug.h"
#include <KLocalizedString>

using namespace LearnerProfile;

// private class LearningGoalModelPrivate
class LearningGoalModelPrivate
{
public:
    LearningGoalModelPrivate()
        : m_profileManager(nullptr)
        , m_learner(nullptr)
        , m_signalMapper(new QSignalMapper())
    {
    }

    ~LearningGoalModelPrivate()
    {
        delete m_signalMapper;
    }

    void updateGoals();
    void updateMappings();

    ProfileManager *m_profileManager;
    Learner *m_learner;
    QList<LearningGoal *> m_goals;
    QSignalMapper *m_signalMapper;
};

void LearningGoalModelPrivate::updateGoals()
{
    m_goals.clear();
    // set all registered goals from profile manager
    if (m_profileManager) {
        foreach (LearningGoal *goal, m_profileManager->goals()) {
            m_goals.append(goal);
        }
    }
    // TODO add learner status information
}

void LearningGoalModelPrivate::updateMappings()
{
    if (!m_profileManager) {
        return;
    }
    int goals = m_goals.count();
    for (int i = 0; i < goals; ++i) {
        m_signalMapper->setMapping(m_goals.at(i), i);
    }
}

// class LearningGoalModel
LearningGoalModel::LearningGoalModel(QObject *parent)
    : QAbstractListModel(parent)
    , d(new LearningGoalModelPrivate)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
    connect(d->m_signalMapper, static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped), this, &LearningGoalModel::emitLearningGoalChanged);
#else
    connect(d->m_signalMapper, &QSignalMapper::mappedInt, this, &LearningGoalModel::emitLearningGoalChanged);
#endif
}

LearningGoalModel::~LearningGoalModel()
{
}

QHash<int, QByteArray> LearningGoalModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TitleRole] = "title";
    roles[IdRole] = "id";
    roles[DataRole] = "dataRole";

    return roles;
}

void LearningGoalModel::setProfileManager(ProfileManager *profileManager)
{
    if (d->m_profileManager == profileManager) {
        return;
    }

    beginResetModel();
    if (d->m_profileManager) {
        d->m_profileManager->disconnect(this);
    }

    d->m_profileManager = profileManager;
    d->updateGoals();
    d->updateMappings();
    endResetModel();

    emit profileManagerChanged();
}

ProfileManager *LearningGoalModel::profileManager() const
{
    return d->m_profileManager;
}

Learner *LearningGoalModel::learner() const
{
    return d->m_learner;
}

void LearningGoalModel::setLearner(Learner *learner)
{
    if (!learner) {
        return;
    }
    beginResetModel();
    if (d->m_learner) {
        learner->disconnect(this);
    }
    d->m_learner = learner;
    d->updateGoals();
    d->updateMappings();
    connect(learner, &Learner::goalAboutToBeAdded, this, &LearningGoalModel::onLearningGoalAboutToBeAdded);
    connect(learner, &Learner::goalAdded, this, &LearningGoalModel::onLearningGoalAdded);
    connect(learner, &Learner::goalAboutToBeRemoved, this, &LearningGoalModel::onLearningGoalAboutToBeRemoved);
    emit learnerChanged();
    endResetModel();
}

QVariant LearningGoalModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (index.row() >= d->m_goals.count()) {
        return QVariant();
    }

    LearningGoal *const goal = d->m_goals.at(index.row());

    switch (role) {
        case Qt::DisplayRole:
            return !goal->name().isEmpty() ? QVariant(goal->name()) : QVariant(i18nc("@item:inlistbox unknown learning goal", "unknown"));
        case Qt::ToolTipRole:
            return QVariant(goal->name());
        case TitleRole:
            return goal->name();
        case IdRole:
            return goal->identifier();
        case DataRole:
            return QVariant::fromValue<QObject *>(goal);
        default:
            return QVariant();
    }
}

int LearningGoalModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }
    return d->m_goals.count();
}

void LearningGoalModel::onLearningGoalAboutToBeAdded(LearningGoal *goal, int index)
{
    Q_UNUSED(index)
    beginInsertRows(QModelIndex(), d->m_goals.count(), d->m_goals.count());
    d->m_goals.append(goal);
    d->updateMappings();
}

void LearningGoalModel::onLearningGoalAdded()
{
    endInsertRows();
}

void LearningGoalModel::onLearningGoalAboutToBeRemoved(int index)
{
    if (!d->m_learner) {
        return;
    }

    if (index < 0 || d->m_goals.count() <= index) {
        qCWarning(LIBLEARNER_LOG) << "Cannot remove learning goal from model, not registered";
        return;
    }
    beginRemoveRows(QModelIndex(), index, index);
    d->m_goals.removeAt(index);
    d->updateMappings();
    endRemoveRows();
}

void LearningGoalModel::emitLearningGoalChanged(int row)
{
    emit learningGoalChanged(row);
    emit dataChanged(index(row, 0), index(row, 0));
}

QVariant LearningGoalModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole) {
        return QVariant();
    }
    if (orientation == Qt::Vertical) {
        return QVariant(section + 1);
    }
    return QVariant(i18nc("@title:column", "Learning Goal"));
}

QVariant LearningGoalModel::learningGoal(int row) const
{
    return data(index(row, 0), LearningGoalModel::DataRole);
}