File indexing completed on 2022-10-04 13:56:35

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "keduvocleitnerbox.h"
0006 
0007 #include "keduvocexpression.h"
0008 
0009 #include <QSet>
0010 
0011 class KEduVocLeitnerBox::Private
0012 {
0013 public:
0014     // cache the entries
0015     QList<KEduVocExpression*> m_expressions;
0016     // list of translations
0017     QList<KEduVocTranslation*> m_translations;
0018 };
0019 
0020 KEduVocLeitnerBox::KEduVocLeitnerBox(const QString& name, KEduVocLeitnerBox *parent)
0021         : KEduVocContainer(name, Leitner, parent), d( new Private )
0022 {
0023     // only one top level and children, this is only a list
0024     Q_ASSERT(!parent || !parent->parent());
0025 }
0026 
0027 KEduVocLeitnerBox::~KEduVocLeitnerBox()
0028 {
0029     foreach(KEduVocTranslation* translation, d->m_translations) {
0030         translation->setLeitnerBox(0);
0031     }
0032     delete d;
0033 }
0034 
0035 QList<KEduVocExpression*> KEduVocLeitnerBox::entries(EnumEntriesRecursive recursive)
0036 {
0037     Q_UNUSED(recursive)
0038     return d->m_expressions;
0039 }
0040 
0041 int KEduVocLeitnerBox::entryCount(EnumEntriesRecursive recursive)
0042 {
0043     Q_UNUSED(recursive)
0044     return d->m_expressions.count();
0045 }
0046 
0047 void KEduVocLeitnerBox::addTranslation(KEduVocTranslation* translation)
0048 {
0049     // add to expression - if not already there because another translation of the same word is there.
0050     bool found = false;
0051     foreach(int i, translation->entry()->translationIndices()) {
0052         if (translation->entry()->translation(i)->leitnerBox() == this) {
0053             found = true;
0054             break;
0055         }
0056     }
0057     if (!found) {
0058         d->m_expressions.append(translation->entry());
0059     }
0060     d->m_translations.append( translation );
0061     invalidateChildLessonEntries();
0062 }
0063 
0064 void KEduVocLeitnerBox::removeTranslation(KEduVocTranslation* translation)
0065 {
0066     int index = d->m_translations.indexOf(translation);
0067     d->m_translations.removeAt(index);
0068 
0069     // no lesson found - this entry is being deleted. remove all its siblings.
0070     if (!translation->entry()->lesson()) {
0071         int index = d->m_expressions.indexOf(translation->entry());
0072         if (index != -1) {
0073             d->m_expressions.removeAt(index);
0074         }
0075     }
0076 
0077     // remove from cache
0078     bool found = false;
0079     foreach(int i, translation->entry()->translationIndices()) {
0080         if (translation->entry()->translation(i)->leitnerBox() == this) {
0081             found = true;
0082             break;
0083         }
0084     }
0085     if (!found) {
0086         d->m_expressions.removeAt(d->m_expressions.indexOf(translation->entry()));
0087     }
0088 
0089     invalidateChildLessonEntries();
0090 }
0091 
0092 KEduVocTranslation * KEduVocLeitnerBox::translation(int row)
0093 {
0094     return d->m_translations.value(row);
0095 }
0096 
0097 KEduVocExpression * KEduVocLeitnerBox::entry(int row, EnumEntriesRecursive recursive)
0098 {
0099     Q_UNUSED(recursive)
0100     return entries().value(row);
0101 }
0102