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

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 = 0;
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 = 0;
0046 }
0047 
0048 KEduVocExpression::KEduVocExpressionPrivate & KEduVocExpression::KEduVocExpressionPrivate::operator =(const KEduVocExpressionPrivate & other)
0049 {
0050     m_active = other.m_active;
0051     m_lesson = 0;
0052 
0053     return *this;
0054 }
0055 
0056 bool KEduVocExpression::KEduVocExpressionPrivate::operator== ( const KEduVocExpression::KEduVocExpressionPrivate &p ) const
0057 {
0058     return
0059         m_translations == p.m_translations &&
0060         m_lesson == p.m_lesson &&
0061         m_active == p.m_active;
0062 }
0063 
0064 KEduVocExpression::KEduVocExpression()
0065         : d( new KEduVocExpressionPrivate )
0066 {}
0067 
0068 KEduVocExpression::KEduVocExpression( const QString & expression )
0069         : d( new KEduVocExpressionPrivate )
0070 {
0071     setTranslation( 0, expression.simplified() );
0072 }
0073 
0074 KEduVocExpression::KEduVocExpression( const QStringList & translations)
0075         : d( new KEduVocExpressionPrivate )
0076 {
0077     foreach ( const QString &translation, translations ) {
0078         setTranslation(d->m_translations.count(), translation);
0079     }
0080 }
0081 
0082 
0083 KEduVocExpression::KEduVocExpression(const KEduVocExpression & other)
0084     : d(new KEduVocExpressionPrivate(*other.d))
0085 {
0086     foreach (int key, other.d->m_translations.keys()) {
0087         d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key));
0088         d->m_translations[key]->setEntry(this);
0089     }
0090 }
0091 
0092 KEduVocExpression& KEduVocExpression::operator= ( const KEduVocExpression &other )
0093 {
0094     *d = *other.d;
0095     foreach (int key, other.d->m_translations.keys()) {
0096         d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key));
0097         d->m_translations[key]->setEntry(this);
0098     }
0099     return *this;
0100 }
0101 
0102 KEduVocExpression::~KEduVocExpression()
0103 {
0104     setLesson(0);
0105     delete d;
0106 }
0107 
0108 void KEduVocExpression::removeTranslation( int index )
0109 {
0110     int count = d->m_translations.count();
0111 
0112     // remove the index we delete
0113     delete d->m_translations.take(index);
0114 
0115     // shift all other indexes, +1 for the deleted
0116     for (int j = index; j <  count-1; j++) {
0117         d->m_translations[j] = d->m_translations.take(j+1);
0118     }
0119 }
0120 
0121 
0122 void KEduVocExpression::setTranslation( int index, const QString & expr )
0123 {
0124     if ( index < 0 ) {
0125         return;
0126     }
0127 
0128     if (!d->m_translations.contains(index)) {
0129         d->m_translations[index] = new KEduVocTranslation(this);
0130     }
0131     d->m_translations[index]->setText(expr.simplified());
0132 }
0133 
0134 
0135 KEduVocLesson* KEduVocExpression::lesson() const
0136 {
0137     return d->m_lesson;
0138 }
0139 
0140 
0141 bool KEduVocExpression::isActive() const
0142 {
0143     return d->m_active;
0144 }
0145 
0146 
0147 void KEduVocExpression::setActive( bool flag )
0148 {
0149     d->m_active = flag;
0150 }
0151 
0152 
0153 void KEduVocExpression::resetGrades( int index )
0154 {
0155     if ( index == -1 ) { // clear grades for all languages
0156         foreach( KEduVocTranslation* trans, d->m_translations ) {
0157             trans->resetGrades();
0158         }
0159         return;
0160     }
0161 
0162     // only language index
0163     if ( d->m_translations.contains( index ) ) {
0164         d->m_translations[index]->resetGrades();
0165     }
0166 }
0167 
0168 bool KEduVocExpression::operator== ( const KEduVocExpression &expression ) const
0169 {
0170     return ( *d == *expression.d );
0171 }
0172 
0173 KEduVocTranslation* KEduVocExpression::translation( int index )
0174 {
0175     if(translationIndices().contains(index)) {
0176         return d->m_translations[index];
0177     }
0178     d->m_translations[index] = new KEduVocTranslation(this);
0179     return d->m_translations[index];
0180 }
0181 
0182 KEduVocTranslation * KEduVocExpression::translation(int index) const
0183 {
0184     if(d->m_translations.contains(index)) {
0185         return 0;
0186     }
0187     return d->m_translations[index];
0188 }
0189 
0190 QList< int > KEduVocExpression::translationIndices() const
0191 {
0192     return d->m_translations.keys();
0193 }
0194 
0195 void KEduVocExpression::setLesson(KEduVocLesson * l)
0196 {
0197     if (d->m_lesson) {
0198         d->m_lesson->removeEntry(this);
0199     }
0200     d->m_lesson = l;
0201 }
0202 
0203