1 /*
2  * SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
3  * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 #include "keduvocleitnerbox.h"
6 
7 #include "keduvocexpression.h"
8 
9 #include <QSet>
10 
11 class KEduVocLeitnerBox::Private
12 {
13 public:
14     // cache the entries
15     QList<KEduVocExpression*> m_expressions;
16     // list of translations
17     QList<KEduVocTranslation*> m_translations;
18 };
19 
KEduVocLeitnerBox(const QString & name,KEduVocLeitnerBox * parent)20 KEduVocLeitnerBox::KEduVocLeitnerBox(const QString& name, KEduVocLeitnerBox *parent)
21         : KEduVocContainer(name, Leitner, parent), d( new Private )
22 {
23     // only one top level and children, this is only a list
24     Q_ASSERT(!parent || !parent->parent());
25 }
26 
~KEduVocLeitnerBox()27 KEduVocLeitnerBox::~KEduVocLeitnerBox()
28 {
29     foreach(KEduVocTranslation* translation, d->m_translations) {
30         translation->setLeitnerBox(0);
31     }
32     delete d;
33 }
34 
entries(EnumEntriesRecursive recursive)35 QList<KEduVocExpression*> KEduVocLeitnerBox::entries(EnumEntriesRecursive recursive)
36 {
37     Q_UNUSED(recursive)
38     return d->m_expressions;
39 }
40 
entryCount(EnumEntriesRecursive recursive)41 int KEduVocLeitnerBox::entryCount(EnumEntriesRecursive recursive)
42 {
43     Q_UNUSED(recursive)
44     return d->m_expressions.count();
45 }
46 
addTranslation(KEduVocTranslation * translation)47 void KEduVocLeitnerBox::addTranslation(KEduVocTranslation* translation)
48 {
49     // add to expression - if not already there because another translation of the same word is there.
50     bool found = false;
51     foreach(int i, translation->entry()->translationIndices()) {
52         if (translation->entry()->translation(i)->leitnerBox() == this) {
53             found = true;
54             break;
55         }
56     }
57     if (!found) {
58         d->m_expressions.append(translation->entry());
59     }
60     d->m_translations.append( translation );
61     invalidateChildLessonEntries();
62 }
63 
removeTranslation(KEduVocTranslation * translation)64 void KEduVocLeitnerBox::removeTranslation(KEduVocTranslation* translation)
65 {
66     int index = d->m_translations.indexOf(translation);
67     d->m_translations.removeAt(index);
68 
69     // no lesson found - this entry is being deleted. remove all its siblings.
70     if (!translation->entry()->lesson()) {
71         int index = d->m_expressions.indexOf(translation->entry());
72         if (index != -1) {
73             d->m_expressions.removeAt(index);
74         }
75     }
76 
77     // remove from cache
78     bool found = false;
79     foreach(int i, translation->entry()->translationIndices()) {
80         if (translation->entry()->translation(i)->leitnerBox() == this) {
81             found = true;
82             break;
83         }
84     }
85     if (!found) {
86         d->m_expressions.removeAt(d->m_expressions.indexOf(translation->entry()));
87     }
88 
89     invalidateChildLessonEntries();
90 }
91 
translation(int row)92 KEduVocTranslation * KEduVocLeitnerBox::translation(int row)
93 {
94     return d->m_translations.value(row);
95 }
96 
entry(int row,EnumEntriesRecursive recursive)97 KEduVocExpression * KEduVocLeitnerBox::entry(int row, EnumEntriesRecursive recursive)
98 {
99     Q_UNUSED(recursive)
100     return entries().value(row);
101 }
102 
103