File indexing completed on 2024-04-28 07:36:30

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)
0022     , d(new Private)
0023 {
0024     // only one top level and children, this is only a list
0025     Q_ASSERT(!parent || !parent->parent());
0026 }
0027 
0028 KEduVocLeitnerBox::~KEduVocLeitnerBox()
0029 {
0030     foreach (KEduVocTranslation *translation, d->m_translations) {
0031         translation->setLeitnerBox(nullptr);
0032     }
0033     delete d;
0034 }
0035 
0036 QList<KEduVocExpression *> KEduVocLeitnerBox::entries(EnumEntriesRecursive recursive)
0037 {
0038     Q_UNUSED(recursive)
0039     return d->m_expressions;
0040 }
0041 
0042 int KEduVocLeitnerBox::entryCount(EnumEntriesRecursive recursive)
0043 {
0044     Q_UNUSED(recursive)
0045     return d->m_expressions.count();
0046 }
0047 
0048 void KEduVocLeitnerBox::addTranslation(KEduVocTranslation *translation)
0049 {
0050     // add to expression - if not already there because another translation of the same word is there.
0051     bool found = false;
0052     foreach (int i, translation->entry()->translationIndices()) {
0053         if (translation->entry()->translation(i)->leitnerBox() == this) {
0054             found = true;
0055             break;
0056         }
0057     }
0058     if (!found) {
0059         d->m_expressions.append(translation->entry());
0060     }
0061     d->m_translations.append(translation);
0062     invalidateChildLessonEntries();
0063 }
0064 
0065 void KEduVocLeitnerBox::removeTranslation(KEduVocTranslation *translation)
0066 {
0067     int index = d->m_translations.indexOf(translation);
0068     d->m_translations.removeAt(index);
0069 
0070     // no lesson found - this entry is being deleted. remove all its siblings.
0071     if (!translation->entry()->lesson()) {
0072         int index = d->m_expressions.indexOf(translation->entry());
0073         if (index != -1) {
0074             d->m_expressions.removeAt(index);
0075         }
0076     }
0077 
0078     // remove from cache
0079     bool found = false;
0080     foreach (int i, translation->entry()->translationIndices()) {
0081         if (translation->entry()->translation(i)->leitnerBox() == this) {
0082             found = true;
0083             break;
0084         }
0085     }
0086     if (!found) {
0087         d->m_expressions.removeAt(d->m_expressions.indexOf(translation->entry()));
0088     }
0089 
0090     invalidateChildLessonEntries();
0091 }
0092 
0093 KEduVocTranslation *KEduVocLeitnerBox::translation(int row)
0094 {
0095     return d->m_translations.value(row);
0096 }
0097 
0098 KEduVocExpression *KEduVocLeitnerBox::entry(int row, EnumEntriesRecursive recursive)
0099 {
0100     Q_UNUSED(recursive)
0101     return entries().value(row);
0102 }