File indexing completed on 2024-04-21 03:48:22

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)
0025     , d(new Private)
0026 {
0027 }
0028 
0029 KEduVocWordType::~KEduVocWordType()
0030 {
0031     foreach (KEduVocTranslation *translation, d->m_translations) {
0032         translation->setWordType(nullptr);
0033     }
0034     delete d;
0035 }
0036 
0037 QList<KEduVocExpression *> KEduVocWordType::entries(EnumEntriesRecursive recursive)
0038 {
0039     if (recursive == Recursive) {
0040         return entriesRecursive();
0041     }
0042 
0043     return d->m_expressions;
0044 }
0045 
0046 int KEduVocWordType::entryCount(EnumEntriesRecursive recursive)
0047 {
0048     if (recursive == Recursive) {
0049         return entriesRecursive().count();
0050     }
0051     return d->m_expressions.count();
0052 }
0053 
0054 void KEduVocWordType::addTranslation(KEduVocTranslation *translation)
0055 {
0056     // add to expression - if not already there because another translation of the same word is there.
0057     bool found = false;
0058     foreach (int i, translation->entry()->translationIndices()) {
0059         if (translation->entry()->translation(i)->wordType() == this) {
0060             found = true;
0061             break;
0062         }
0063     }
0064     if (!found) {
0065         d->m_expressions.append(translation->entry());
0066     }
0067     d->m_translations.append(translation);
0068     invalidateChildLessonEntries();
0069 }
0070 
0071 void KEduVocWordType::removeTranslation(KEduVocTranslation *translation)
0072 {
0073     d->m_translations.removeAt(d->m_translations.indexOf(translation));
0074 
0075     // no lesson found - this entry is being deleted. remove all its siblings.
0076     if (!translation->entry()->lesson()) {
0077         const int index = d->m_expressions.indexOf(translation->entry());
0078         if (index != -1) {
0079             d->m_expressions.removeAt(index);
0080         }
0081     }
0082 
0083     // 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)
0084     bool found = false;
0085     foreach (int i, translation->entry()->translationIndices()) {
0086         if (translation->entry()->translation(i)->wordType() && translation->entry()->translation(i)->wordType() == this
0087             && translation->entry()->translation(i) != translation) {
0088             found = true;
0089             break;
0090         }
0091     }
0092     if (!found) {
0093         const int index = d->m_expressions.indexOf(translation->entry());
0094         if (index >= 0) {
0095             d->m_expressions.removeAt(index);
0096         }
0097     }
0098 
0099     invalidateChildLessonEntries();
0100 }
0101 
0102 KEduVocTranslation *KEduVocWordType::translation(int row)
0103 {
0104     return d->m_translations.value(row);
0105 }
0106 
0107 KEduVocExpression *KEduVocWordType::entry(int row, EnumEntriesRecursive recursive)
0108 {
0109     if (recursive == Recursive) {
0110         return entriesRecursive().value(row);
0111     }
0112     return entries().value(row);
0113 }
0114 
0115 KEduVocWordFlags KEduVocWordType::wordType() const
0116 {
0117     return d->m_flags;
0118 }
0119 
0120 void KEduVocWordType::setWordType(KEduVocWordFlags flags)
0121 {
0122     d->m_flags = flags;
0123 }
0124 
0125 KEduVocWordType *KEduVocWordType::childOfType(KEduVocWordFlags flags)
0126 {
0127     if (d->m_flags == flags) {
0128         return this;
0129     }
0130     foreach (KEduVocContainer *child, childContainers()) {
0131         KEduVocWordType *result = static_cast<KEduVocWordType *>(child)->childOfType(flags);
0132         if (result) {
0133             return result;
0134         }
0135     }
0136     return nullptr;
0137 }