File indexing completed on 2024-04-14 03:46:39
0001 /* 0002 * Vocabulary Expression for KDE Edu 0003 * SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de> 0004 * SPDX-FileCopyrightText: 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net> 0005 * SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net> 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "keduvocexpression.h" 0010 0011 #include <QMap> 0012 0013 class KEduVocExpression::KEduVocExpressionPrivate 0014 { 0015 public: 0016 KEduVocExpressionPrivate() 0017 { 0018 m_active = true; 0019 m_lesson = nullptr; 0020 } 0021 ~KEduVocExpressionPrivate(); 0022 0023 KEduVocExpressionPrivate(const KEduVocExpressionPrivate &other); 0024 KEduVocExpressionPrivate &operator=(const KEduVocExpressionPrivate &other); 0025 0026 bool operator==(const KEduVocExpressionPrivate &p) const; 0027 0028 KEduVocLesson *m_lesson; 0029 bool m_active; 0030 0031 QMap<int, KEduVocTranslation *> m_translations; 0032 }; 0033 0034 KEduVocExpression::KEduVocExpressionPrivate::~KEduVocExpressionPrivate() 0035 { 0036 QMap<int, KEduVocTranslation *> translations = m_translations; 0037 // empty the translations map, otherwise removal from word type will try to access them again when they don't exist any more 0038 m_translations.clear(); 0039 qDeleteAll(translations); 0040 } 0041 0042 KEduVocExpression::KEduVocExpressionPrivate::KEduVocExpressionPrivate(const KEduVocExpressionPrivate &other) 0043 { 0044 m_active = other.m_active; 0045 m_lesson = nullptr; 0046 } 0047 0048 KEduVocExpression::KEduVocExpressionPrivate &KEduVocExpression::KEduVocExpressionPrivate::operator=(const KEduVocExpressionPrivate &other) 0049 { 0050 m_active = other.m_active; 0051 m_lesson = nullptr; 0052 0053 return *this; 0054 } 0055 0056 bool KEduVocExpression::KEduVocExpressionPrivate::operator==(const KEduVocExpression::KEduVocExpressionPrivate &p) const 0057 { 0058 return m_translations == p.m_translations && m_lesson == p.m_lesson && m_active == p.m_active; 0059 } 0060 0061 KEduVocExpression::KEduVocExpression() 0062 : d(new KEduVocExpressionPrivate) 0063 { 0064 } 0065 0066 KEduVocExpression::KEduVocExpression(const QString &expression) 0067 : d(new KEduVocExpressionPrivate) 0068 { 0069 setTranslation(0, expression.simplified()); 0070 } 0071 0072 KEduVocExpression::KEduVocExpression(const QStringList &translations) 0073 : d(new KEduVocExpressionPrivate) 0074 { 0075 foreach (const QString &translation, translations) { 0076 setTranslation(d->m_translations.count(), translation); 0077 } 0078 } 0079 0080 KEduVocExpression::KEduVocExpression(const KEduVocExpression &other) 0081 : d(new KEduVocExpressionPrivate(*other.d)) 0082 { 0083 foreach (int key, other.d->m_translations.keys()) { 0084 d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key)); 0085 d->m_translations[key]->setEntry(this); 0086 } 0087 } 0088 0089 KEduVocExpression &KEduVocExpression::operator=(const KEduVocExpression &other) 0090 { 0091 *d = *other.d; 0092 foreach (int key, other.d->m_translations.keys()) { 0093 d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key)); 0094 d->m_translations[key]->setEntry(this); 0095 } 0096 return *this; 0097 } 0098 0099 KEduVocExpression::~KEduVocExpression() 0100 { 0101 setLesson(nullptr); 0102 delete d; 0103 } 0104 0105 void KEduVocExpression::removeTranslation(int index) 0106 { 0107 int count = d->m_translations.count(); 0108 0109 // remove the index we delete 0110 delete d->m_translations.take(index); 0111 0112 // shift all other indexes, +1 for the deleted 0113 for (int j = index; j < count - 1; j++) { 0114 d->m_translations[j] = d->m_translations.take(j + 1); 0115 } 0116 } 0117 0118 void KEduVocExpression::setTranslation(int index, const QString &expr) 0119 { 0120 if (index < 0) { 0121 return; 0122 } 0123 0124 if (!d->m_translations.contains(index)) { 0125 d->m_translations[index] = new KEduVocTranslation(this); 0126 } 0127 d->m_translations[index]->setText(expr.simplified()); 0128 } 0129 0130 KEduVocLesson *KEduVocExpression::lesson() const 0131 { 0132 return d->m_lesson; 0133 } 0134 0135 bool KEduVocExpression::isActive() const 0136 { 0137 return d->m_active; 0138 } 0139 0140 void KEduVocExpression::setActive(bool flag) 0141 { 0142 d->m_active = flag; 0143 } 0144 0145 void KEduVocExpression::resetGrades(int index) 0146 { 0147 if (index == -1) { // clear grades for all languages 0148 foreach (KEduVocTranslation *trans, d->m_translations) { 0149 trans->resetGrades(); 0150 } 0151 return; 0152 } 0153 0154 // only language index 0155 if (d->m_translations.contains(index)) { 0156 d->m_translations[index]->resetGrades(); 0157 } 0158 } 0159 0160 bool KEduVocExpression::operator==(const KEduVocExpression &expression) const 0161 { 0162 return (*d == *expression.d); 0163 } 0164 0165 KEduVocTranslation *KEduVocExpression::translation(int index) 0166 { 0167 if (translationIndices().contains(index)) { 0168 return d->m_translations[index]; 0169 } 0170 d->m_translations[index] = new KEduVocTranslation(this); 0171 return d->m_translations[index]; 0172 } 0173 0174 KEduVocTranslation *KEduVocExpression::translation(int index) const 0175 { 0176 if (d->m_translations.contains(index)) { 0177 return nullptr; 0178 } 0179 return d->m_translations[index]; 0180 } 0181 0182 QList<int> KEduVocExpression::translationIndices() const 0183 { 0184 return d->m_translations.keys(); 0185 } 0186 0187 void KEduVocExpression::setLesson(KEduVocLesson *l) 0188 { 0189 if (d->m_lesson) { 0190 d->m_lesson->removeEntry(this); 0191 } 0192 d->m_lesson = l; 0193 }