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

0001 /*
0002  * SPDX-FileCopyrightText: 2007 Jeremy Whiting <jpwhiting@kde.org>
0003  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "keduvocwordtype.h"
0008 
0009 #include "keduvocexpression.h"
0010 
0011 #include <QSet>
0012 
0013 class KEduVocWordType::Private
0014 {
0015 public:
0016     // bitvector of word type flags
0017     KEduVocWordFlags m_flags;
0018     QList<KEduVocExpression*> m_expressions;
0019     // list of translations
0020     QList<KEduVocTranslation*> m_translations;
0021 };
0022 
0023 KEduVocWordType::KEduVocWordType(const QString& name, KEduVocWordType *parent)
0024         : KEduVocContainer(name, WordType, parent), d( new Private )
0025 {}
0026 
0027 KEduVocWordType::~KEduVocWordType()
0028 {
0029     foreach(KEduVocTranslation* translation, d->m_translations) {
0030         translation->setWordType(0);
0031     }
0032     delete d;
0033 }
0034 
0035 QList<KEduVocExpression*> KEduVocWordType::entries(EnumEntriesRecursive recursive)
0036 {
0037     if (recursive == Recursive) {
0038         return entriesRecursive();
0039     }
0040 
0041     return d->m_expressions;
0042 }
0043 
0044 int KEduVocWordType::entryCount(EnumEntriesRecursive recursive)
0045 {
0046     if (recursive == Recursive) {
0047         return entriesRecursive().count();
0048     }
0049     return d->m_expressions.count();
0050 }
0051 
0052 void KEduVocWordType::addTranslation(KEduVocTranslation* translation)
0053 {
0054     // add to expression - if not already there because another translation of the same word is there.
0055     bool found = false;
0056     foreach(int i, translation->entry()->translationIndices()) {
0057         if (translation->entry()->translation(i)->wordType() == this) {
0058             found = true;
0059             break;
0060         }
0061     }
0062     if (!found) {
0063         d->m_expressions.append(translation->entry());
0064     }
0065     d->m_translations.append( translation );
0066     invalidateChildLessonEntries();
0067 }
0068 
0069 void KEduVocWordType::removeTranslation(KEduVocTranslation* translation)
0070 {
0071     d->m_translations.removeAt( d->m_translations.indexOf(translation) );
0072 
0073     // no lesson found - this entry is being deleted. remove all its siblings.
0074     if (!translation->entry()->lesson()) {
0075         int index = d->m_expressions.indexOf(translation->entry());
0076         if (index != -1) {
0077             d->m_expressions.removeAt(index);
0078         }
0079     }
0080 
0081     // remove from cache if none of the translations use this word type (other than the one we are removing that should not be taken into account)
0082     bool found = false;
0083     foreach(int i, translation->entry()->translationIndices()) {
0084         if (translation->entry()->translation(i)->wordType() && translation->entry()->translation(i)->wordType() == this && translation->entry()->translation(i) != translation) {
0085             found = true;
0086             break;
0087         }
0088     }
0089     if (!found) {
0090         d->m_expressions.removeAt(d->m_expressions.indexOf(translation->entry()));
0091     }
0092 
0093     invalidateChildLessonEntries();
0094 }
0095 
0096 KEduVocTranslation * KEduVocWordType::translation(int row)
0097 {
0098 
0099     return d->m_translations.value(row);
0100 }
0101 
0102 KEduVocExpression * KEduVocWordType::entry(int row, EnumEntriesRecursive recursive)
0103 {
0104     if (recursive == Recursive) {
0105         return entriesRecursive().value(row);
0106     }
0107     return entries().value(row);
0108 }
0109 
0110 KEduVocWordFlags KEduVocWordType::wordType() const
0111 {
0112     return d->m_flags;
0113 }
0114 
0115 void KEduVocWordType::setWordType(KEduVocWordFlags flags)
0116 {
0117     d->m_flags = flags;
0118 }
0119 
0120 KEduVocWordType* KEduVocWordType::childOfType(KEduVocWordFlags flags)
0121 {
0122     if(d->m_flags == flags) {
0123         return this;
0124     }
0125     foreach(KEduVocContainer* child, childContainers()) {
0126         KEduVocWordType* result = static_cast<KEduVocWordType*>(child)->childOfType(flags);
0127         if(result) {
0128             return result;
0129         }
0130     }
0131     return 0;
0132 }
0133