Debian Codegraph @

liblearnerprofile/src/learner.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2013-2016 Andreas Cord-Landwehr <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7 #include "learner.h"
8 #include "learner_p.h"
9 #include "liblearner_debug.h"
10 #include <QDir>
11 #include <QFileInfo>
12 #include <QPixmap>
13
14 using namespace LearnerProfile;
15
16 Learner::Learner(QObject *parent)
17 : QObject(parent)
18 , d(new LearnerPrivate)
19 {
20 connect(this, &Learner::goalAdded, this, &Learner::goalCountChanged);
21 connect(this, &Learner::goalRemoved, this, &Learner::goalCountChanged);
22 }
23
24 Learner::~Learner()
25 {
26 }
27
28 QString Learner::name() const
29 {
30 return d->m_name;
31 }
32
33 void Learner::setName(const QString &name)
34 {
35 if (name == d->m_name) {
36 return;
37 }
38 d->m_name = name;
39 Q_EMIT nameChanged();
40 }
41
42 int Learner::identifier() const
43 {
44 return d->m_identifier;
45 }
46
47 void Learner::setIdentifier(int identifier)
48 {
49 if (identifier == d->m_identifier) {
50 return;
51 }
52 d->m_identifier = identifier;
53 Q_EMIT identifierChanged();
54 }
55
56 QString Learner::imageUrl() const
57 {
58 QString path = d->imagePath();
59 if (!QFileInfo::exists(path)) {
60 return QString();
61 }
62 return QStringLiteral("file://") + path;
63 }
64
65 void Learner::clearImage()
66 {
67 const QString path{d->imagePath()};
68 if (!QFileInfo::exists(path)) {
69 return;
70 }
71 QFile file;
72 if (!file.remove(path)) {
73 qCCritical(LIBLEARNER_LOG) << "could not remove image:" << path;
74 }
75 Q_EMIT imageChanged();
76 }
77
78 void Learner::importImage(const QString &path)
79 {
80 if (!QFileInfo::exists(path)) {
81 qCWarning(LIBLEARNER_LOG) << "image path points to a non-existing file, aborting: " << path;
82 return;
83 }
84
85 // create image directory if it does not exist
86 QDir dir;
87 if (!dir.exists(d->imageDirectory())) {
88 dir.mkdir(d->imageDirectory());
89 }
90
91 QPixmap image = QPixmap(path);
92 image = image.scaled(120, 120);
93 if (!image.save(d->imagePath(), "PNG")) {
94 qCCritical(LIBLEARNER_LOG()) << "could not save scaled image to" << d->imagePath();
95 }
96 Q_EMIT imageChanged();
97 qCDebug(LIBLEARNER_LOG) << "saved scaled image from " << path << " at " << d->imagePath();
98 }
99
100 QList<LearningGoal *> Learner::goals() const
101 {
102 return d->m_goals;
103 }
104
105 void Learner::addGoal(LearnerProfile::LearningGoal *goal)
106 {
107 if (d->m_goals.contains(goal)) {
108 return;
109 }
110 Q_EMIT goalAboutToBeAdded(goal, d->m_goals.count());
111 d->m_goals.append(goal);
112 Q_EMIT goalAdded();
113 }
114
115 void Learner::removeGoal(LearnerProfile::LearningGoal *goal)
116 {
117 int index = d->m_goals.indexOf(goal);
118 if (index < 0) {
119 qCritical() << "Cannot remove goal, not found: aborting";
120 return;
121 }
122 Q_EMIT goalAboutToBeRemoved(index);
123 d->m_goals.removeAt(index);
124 Q_EMIT goalRemoved(this, goal);
125 }
126
127 bool Learner::hasGoal(LearningGoal *goal) const
128 {
129 for (LearningGoal *cmpGoal : std::as_const(d->m_goals)) {
130 if (goal->identifier() == cmpGoal->identifier()) {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 void Learner::setActiveGoal(LearningGoal *goal)
138 {
139 if (d->m_activeGoal.contains(goal->category()) && d->m_activeGoal[goal->category()] == goal) {
140 return;
141 }
142
143 d->m_activeGoal.insert(goal->category(), goal);
144 Q_EMIT activeGoalChanged();
145 }
146
147 void Learner::setActiveGoal(Learner::Category categoryLearner, const QString &identifier)
148 {
149 // TODO:Qt5 change method parameter to LearningGoal::Category
150 // workaround for Q_INVOKABLE access of enum
151 LearningGoal::Category category = static_cast<LearningGoal::Category>(categoryLearner);
152
153 if (d->m_activeGoal.contains(category) && d->m_activeGoal[category]->identifier() == identifier) {
154 return;
155 }
156
157 for (LearningGoal *goal : std::as_const(d->m_goals)) {
158 if (goal->category() == category && goal->identifier() == identifier) {
159 setActiveGoal(goal);
160 return;
161 }
162 }
163 qCritical() << "Could not select learning goal with ID " << identifier << ": not registered for this learner";
164 }
165
166 LearningGoal *Learner::activeGoal(Learner::Category categoryLearner) const
167 {
168 // TODO:Qt5 change method parameter to LearningGoal::Category
169 // workaround for Q_INVOKABLE access of enum
170 LearningGoal::Category category = static_cast<LearningGoal::Category>(categoryLearner);
171 if (!d->m_activeGoal.contains(category)) {
172 qCWarning(LIBLEARNER_LOG) << "(Learner " << identifier() << ") No current learning goal set for category " << category
173 << " : fall back to first in list";
174 for (LearningGoal *goal : std::as_const(d->m_goals)) {
175 if (goal->category() == category) {
176 return goal;
177 }
178 }
179 qCWarning(LIBLEARNER_LOG) << "No learning goals of category " << category << " registered";
180 return nullptr;
181 }
182 return d->m_activeGoal[category];
183 }