LibKEduVocDocument

keduvoccontainer.cpp
1/*
2 * SPDX-FileCopyrightText: 2007 Jeremy Whiting <jpwhiting@kde.org>
3 * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "keduvoccontainer.h"
8
9#include "keduvocdocument.h"
10#include "keduvocexpression.h"
11
12#include <QDebug>
13
14/** private class to store information about a lesson */
15class KEduVocContainer::Private
16{
17public:
18 ~Private();
19
20 // properties for this lesson
21 QString m_name;
22 bool m_inPractice;
23
24 // The containing document. This is only set for the top lesson, so to
25 // get to the document, you need to follow the parent pointer to the top
26 // container.
27 KEduVocDocument *m_document;
28
29 // other lessons in the tree
30 KEduVocContainer *m_parentContainer;
31 QList<KEduVocContainer *> m_childContainers;
32
33 EnumContainerType m_type;
34
35 QList<KEduVocExpression *> m_childLessonEntries;
36 bool m_childLessonEntriesValid;
37
38 /// Image url
39 QUrl m_imageUrl;
40};
41
42KEduVocContainer::Private::~Private()
43{
44 qDeleteAll(m_childContainers);
45}
46
47// This is a private constructor only used by KEduVocDocument when creating
48// the top level lesson.
50 : d(new Private)
51{
52 d->m_parentContainer = nullptr;
53 d->m_name = name;
54 d->m_inPractice = true;
55 d->m_type = type;
56 d->m_childLessonEntriesValid = false;
57
58 d->m_document = document;
59}
60
61KEduVocContainer::KEduVocContainer(const QString &name, EnumContainerType type, KEduVocContainer *parent)
62 : d(new Private)
63{
64 d->m_parentContainer = parent;
65 d->m_name = name;
66 d->m_inPractice = true;
67 d->m_type = type;
68 d->m_childLessonEntriesValid = false;
69
70 d->m_document = nullptr;
71}
72
74 : d(new Private)
75{
76 d->m_name = other.d->m_name;
77 d->m_inPractice = other.d->m_inPractice;
78 d->m_type = other.d->m_type;
79 d->m_parentContainer = other.d->m_parentContainer;
80 d->m_childLessonEntriesValid = false;
81}
82
87
89{
90 KEduVocContainer *cont = (KEduVocContainer *)this;
91 while (cont->d->m_parentContainer) {
92 cont = cont->d->m_parentContainer;
93 }
94
95 Q_ASSERT(cont->d->m_document);
96 return cont->d->m_document;
97}
98
99void KEduVocContainer::appendChildContainer(KEduVocContainer *child)
100{
101 d->m_childContainers.append(child);
102 child->d->m_parentContainer = this;
103
105}
106
107KEduVocContainer *KEduVocContainer::childContainer(int row)
108{
109 return d->m_childContainers.value(row);
110}
111
112KEduVocContainer *KEduVocContainer::childContainer(const QString &name)
113{
114 if (d->m_name == name) {
115 return this;
116 }
117
118 foreach (KEduVocContainer *container, d->m_childContainers) {
119 KEduVocContainer *found = container->childContainer(name);
120 if (found) {
121 return found;
122 }
123 }
124 return nullptr;
125}
126
127void KEduVocContainer::deleteChildContainer(int row)
128{
129 qDebug() << "Delete of container - check entry deletion!";
130 delete d->m_childContainers.takeAt(row);
131
133}
134
135void KEduVocContainer::removeChildContainer(int row)
136{
137 d->m_childContainers.removeAt(row);
139}
140
142{
143 return d->m_childContainers.count();
144}
145
146int KEduVocContainer::row() const
147{
148 if (d->m_parentContainer) {
149 return d->m_parentContainer->d->m_childContainers.indexOf(const_cast<KEduVocContainer *>(this));
150 }
151 return 0;
152}
153
155{
156 d->m_name = other.d->m_name;
157 d->m_inPractice = other.d->m_inPractice;
158 return *this;
159}
160
162{
163 return d->m_name == other.d->m_name && d->m_inPractice == other.d->m_inPractice
164 /// @todo make this return something useful
165 ;
166}
167
169{
170 d->m_name = name;
171}
172
174{
175 return d->m_name;
176}
177
178bool KEduVocContainer::inPractice()
179{
180 return d->m_inPractice;
181}
182
183void KEduVocContainer::setInPractice(bool inPractice)
184{
185 d->m_inPractice = inPractice;
186}
187
189{
190 foreach (KEduVocContainer *childContainer, d->m_childContainers) {
191 childContainer->removeTranslation(translation);
192 }
193
194 foreach (KEduVocExpression *entry, entries()) {
195 entry->removeTranslation(translation);
196 }
197}
198
199QList<KEduVocExpression *> KEduVocContainer::entriesRecursive()
200{
201 if (!d->m_childLessonEntriesValid) {
203 }
204 return d->m_childLessonEntries;
205}
206
207QList<KEduVocContainer *> KEduVocContainer::childContainers()
208{
209 return d->m_childContainers;
210}
211
212KEduVocContainer *KEduVocContainer::parent()
213{
214 return d->m_parentContainer;
215}
216
217void KEduVocContainer::setContainerType(KEduVocContainer::EnumContainerType type)
218{
219 d->m_type = type;
220}
221
222KEduVocContainer::EnumContainerType KEduVocContainer::containerType()
223{
224 return d->m_type;
225}
226
228{
229 return d->m_imageUrl;
230}
231
233{
234 d->m_imageUrl = url;
235}
236
237void KEduVocContainer::insertChildContainer(int row, KEduVocContainer *child)
238{
239 d->m_childContainers.insert(row, child);
240 child->d->m_parentContainer = this;
241
243}
244
246{
247 QList<KEduVocExpression *> entriesRecursive = entries();
248
249 foreach (KEduVocContainer *childContainer, d->m_childContainers)
250 foreach (KEduVocExpression *expr, childContainer->entries(Recursive))
251 entriesRecursive.append(expr);
252
253 d->m_childLessonEntries = entriesRecursive;
254 d->m_childLessonEntriesValid = true;
255}
256
258{
259 d->m_childLessonEntriesValid = false;
260 // propagate to parent
261 if (d->m_parentContainer) {
262 d->m_parentContainer->invalidateChildLessonEntries();
263 }
264}
265
266double KEduVocContainer::averageGrade(int translation, EnumEntriesRecursive recursive)
267{
268 int sum = 0, presum = 0, count = 0;
269 foreach (KEduVocExpression *entry, entries(recursive)) {
270 KEduVocTranslation &trans(*entry->translation(translation));
271 if (!trans.isEmpty()) {
272 ++count;
273 sum += trans.grade();
274 presum += trans.preGrade();
275 }
276 }
277 // make that a percentage
278 // There are KV_MAX_GRADE grades from 0 -> 100 %
279 // There are KV_MAX_GRADE preGrades within the first grade.
280 if (count == 0) {
281 return 100.0;
282 }
283 return ((sum * 100.0 / KV_MAX_GRADE) + (presum * 100.0 / (KV_MAX_GRADE * KV_MAX_GRADE))) / count;
284}
285
286int KEduVocContainer::expressionsOfGrade(int translation, grade_t grade, EnumEntriesRecursive recursive)
287{
288 int sum = 0;
289 foreach (KEduVocExpression *entry, entries(recursive)) {
290 if (entry->translation(translation)->grade() == grade) {
291 sum++;
292 }
293 }
294 return sum;
295}
296
297void KEduVocContainer::resetGrades(int translation, EnumEntriesRecursive recursive)
298{
299 foreach (KEduVocExpression *entry, entries(recursive)) {
300 entry->resetGrades(translation);
301 }
302
303 document()->setModified(true);
304}
class to store information about a container - that can be a lesson or word types
virtual ~KEduVocContainer()
destructor
KEduVocDocument * document() const
void setName(const QString &name)
set the container name
virtual QList< KEduVocExpression * > entries(EnumEntriesRecursive recursive=NotRecursive)=0
get a list of all entries in the container
void setContainerType(KEduVocContainer::EnumContainerType type)
Set the type of container.
bool operator==(const KEduVocContainer &other) const
equality operator
QUrl imageUrl()
get the image url for this container if it exists
void removeTranslation(int translation)
Removes a translation.
KEduVocContainer & operator=(const KEduVocContainer &)
assignment operator
int childContainerCount() const
Find a child container.
KEduVocContainer::EnumContainerType containerType()
The type of this container.
void resetGrades(int translation, EnumEntriesRecursive recursive)
Remove grades from all entries of this lessons.
void setImageUrl(const QUrl &url)
set the image url for this container
void updateChildLessonEntries()
Recreate the cache of entries in this lesson's child lessons.
QString name()
get the container name
void invalidateChildLessonEntries()
Set the child entry cache to invalid.
KEduVocContainer(const QString &name, EnumContainerType type, KEduVocContainer *parent=nullptr)
default constructor
The primary entry point to the hierarchy of objects describing vocabularies.
void setModified(bool dirty=true)
Indicates if the document is modified.
This class contains one vocabulary expression as an original with one or more translations.
void resetGrades(int index)
reset all grades of the entry
void removeTranslation(int index)
removes a translation
KEduVocTranslation * translation(int index)
Get a pointer to the translation.
grade_t grade() const
returns grade as int
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
void append(QList< T > &&value)
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
iterator insert(const_iterator before, parameter_type value)
void removeAt(qsizetype i)
T takeAt(qsizetype i)
T value(qsizetype i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.