Warning, file /education/libkeduvocdocument/keduvocdocument/keduvoctranslation.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Vocabulary Expression Translation for KDE Edu
0003  * SPDX-FileCopyrightText: 2007-2010 Frederik Gladhorn <gladhorn@kde.org>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "keduvoctranslation.h"
0008 
0009 #include "keduvocdeclension.h"
0010 #include "keduvocwordtype.h"
0011 #include "keduvocleitnerbox.h"
0012 #include "kvtml2defs.h"
0013 #include "keduvockvtml2writer.h"
0014 
0015 
0016 class KEduVocTranslation::KEduVocTranslationPrivate
0017 {
0018 public:
0019     KEduVocTranslationPrivate(KEduVocExpression* parent);
0020 
0021     ~KEduVocTranslationPrivate();
0022 
0023     KEduVocExpression* m_entry;
0024 
0025     /// Type of a word noun, verb, adjective etc
0026     KEduVocWordType* m_wordType;
0027 
0028     /// Leitner box of the translation.
0029     KEduVocLeitnerBox* m_leitnerBox;
0030 
0031     /// A comment giving additional information.
0032     QString m_comment;
0033     /// A hint, to make guessing the word easier.
0034     QString m_hint;
0035     /// Paraphrase
0036     QString m_paraphrase;
0037     /// An example
0038     QString m_example;
0039     /// Pronunciation
0040     QString m_pronunciation;
0041     /// Image url
0042     QUrl m_imageUrl;
0043     /// Sound url
0044     QUrl m_soundUrl;
0045 
0046     /// When creating multiple choice tests, these are possible answers. (otherwise other words are added randomly)
0047     QStringList m_multipleChoice;
0048 
0049     /// Conjugations of a word (I go, you go, he goes... boring in english)
0050     QMap <QString, KEduVocConjugation> m_conjugations;
0051 
0052     /// The comparison forms of adjectives and adverbs: (fast), faster, fastest
0053     KEduVocText* m_comparative;
0054     KEduVocText* m_superlative;
0055 
0056     /// The grade of an article. The text part should not be used.
0057     KEduVocText* m_articleGrade;
0058 
0059     KEduVocDeclension* m_declension;
0060 
0061     // connections to other translations
0062     /// Synonyms for a word: sick and ill, student and pupil
0063     QList< KEduVocTranslation* > m_synonyms;
0064     /// An antonym - the opposite: hot - cold
0065     QList< KEduVocTranslation* > m_antonyms;
0066     /// List of false friends
0067     QList< KEduVocTranslation* > m_falseFriends;
0068 };
0069 
0070 KEduVocTranslation::KEduVocTranslationPrivate::KEduVocTranslationPrivate(KEduVocExpression* parent)
0071 {
0072     m_entry = parent;
0073     m_wordType = nullptr;
0074     m_leitnerBox = nullptr;
0075     m_declension = nullptr;
0076 
0077     m_comparative = nullptr;
0078     m_superlative = nullptr;
0079     m_articleGrade = nullptr;
0080 }
0081 
0082 
0083 KEduVocTranslation::KEduVocTranslationPrivate::~KEduVocTranslationPrivate()
0084 {
0085     delete m_declension;
0086 }
0087 
0088 KEduVocTranslation::KEduVocTranslation(KEduVocExpression* entry) : d( new KEduVocTranslationPrivate(entry) )
0089 {
0090 }
0091 
0092 
0093 KEduVocTranslation::KEduVocTranslation(KEduVocExpression* entry, const QString &translation ) : d( new KEduVocTranslationPrivate(entry) )
0094 {
0095     setText(translation.simplified());
0096 }
0097 
0098 KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other )
0099     : KEduVocText(other),
0100     // set the entry to 0, the translation will be put into a copied entry by the expression copy constructor
0101     d( new KEduVocTranslationPrivate(nullptr) )
0102 {
0103     // better no word type copy as this is pointer copying
0104     // will not work as this is not added to the word type container!
0105 //  d->m_wordType = other.d->m_wordType;
0106 //  d->m_leitnerBox = translation.d->m_leitnerBox;
0107     d->m_comment = other.d->m_comment;
0108     d->m_paraphrase = other.d->m_paraphrase;
0109     d->m_example = other.d->m_example;
0110     d->m_pronunciation = other.d->m_pronunciation;
0111     d->m_conjugations = other.d->m_conjugations;
0112     d->m_comparative = other.d->m_comparative;
0113     d->m_superlative = other.d->m_superlative;
0114     d->m_multipleChoice = other.d->m_multipleChoice;
0115     d->m_imageUrl = other.d->m_imageUrl;
0116     d->m_soundUrl = other.d->m_soundUrl;
0117 //  no copies of the following for now. we don't know enough to also add this as synonym/etc
0118 //  d->m_synonyms = other.d->m_synonyms;
0119 //  d->m_antonyms = other.d->m_antonyms;
0120 //  d->m_falseFriends = other.d->m_falseFriends;
0121     if (other.d->m_declension) {
0122         d->m_declension = new KEduVocDeclension(*other.d->m_declension);
0123     }
0124 }
0125 
0126 KEduVocTranslation::~KEduVocTranslation()
0127 {
0128     setWordType(nullptr);
0129     setLeitnerBox(nullptr);
0130     foreach (KEduVocTranslation *synonym, d->m_synonyms) {
0131         synonym->removeSynonym(this);
0132     }
0133     foreach (KEduVocTranslation *antonym, d->m_antonyms) {
0134         antonym->removeAntonym(this);
0135     }
0136     foreach (KEduVocTranslation *falseFriend, d->m_falseFriends) {
0137         falseFriend->removeFalseFriend(this);
0138     }
0139     delete d;
0140 }
0141 
0142 bool KEduVocTranslation::operator == ( const KEduVocTranslation & translation ) const
0143 {
0144     return KEduVocText::operator==(translation) &&
0145         d->m_wordType == translation.d->m_wordType &&
0146         d->m_leitnerBox == translation.d->m_leitnerBox &&
0147         d->m_comment == translation.d->m_comment &&
0148         d->m_paraphrase == translation.d->m_paraphrase &&
0149         d->m_example == translation.d->m_example &&
0150         d->m_pronunciation == translation.d->m_pronunciation &&
0151         d->m_imageUrl == translation.d->m_imageUrl &&
0152         d->m_soundUrl == translation.d->m_soundUrl &&
0153         d->m_comparative == translation.d->m_comparative &&
0154         d->m_superlative == translation.d->m_superlative &&
0155         d->m_multipleChoice == translation.d->m_multipleChoice &&
0156         d->m_synonyms == translation.d->m_synonyms &&
0157         d->m_antonyms == translation.d->m_antonyms &&
0158         d->m_falseFriends == translation.d->m_falseFriends &&
0159         d->m_conjugations == translation.d->m_conjugations;
0160            /// @todo check and include declensions d->m_declension == translation.d->m_declension;
0161 }
0162 
0163 KEduVocTranslation & KEduVocTranslation::operator = ( const KEduVocTranslation & translation )
0164 {
0165     KEduVocText::operator=(translation);
0166     d->m_entry = translation.d->m_entry;
0167 //     d->m_wordType = translation.d->m_wordType;
0168 //     d->m_leitnerBox = translation.d->m_leitnerBox;
0169     d->m_comment = translation.d->m_comment;
0170     d->m_paraphrase = translation.d->m_paraphrase;
0171     d->m_example = translation.d->m_example;
0172     d->m_pronunciation = translation.d->m_pronunciation;
0173     d->m_imageUrl = translation.d->m_imageUrl;
0174     d->m_soundUrl = translation.d->m_soundUrl;
0175     d->m_comparative = translation.d->m_comparative;
0176     d->m_superlative = translation.d->m_superlative;
0177     d->m_multipleChoice = translation.d->m_multipleChoice;
0178     d->m_falseFriends = translation.d->m_falseFriends;
0179     d->m_synonyms = translation.d->m_synonyms;
0180     d->m_antonyms = translation.d->m_antonyms;
0181     d->m_conjugations = translation.d->m_conjugations;
0182     if (translation.d->m_declension) {
0183         d->m_declension = new KEduVocDeclension(*translation.d->m_declension);
0184     }
0185 
0186     return *this;
0187 }
0188 
0189 
0190 QString KEduVocTranslation::comment() const
0191 {
0192     return d->m_comment;
0193 }
0194 
0195 
0196 void KEduVocTranslation::setComment( const QString & expr )
0197 {
0198     d->m_comment = expr.simplified();
0199 }
0200 
0201 
0202 void KEduVocTranslation::addFalseFriend( KEduVocTranslation* falseFriend )
0203 {
0204     d->m_falseFriends.append(falseFriend);
0205 }
0206 
0207 void KEduVocTranslation::removeFalseFriend(KEduVocTranslation * falseFriend)
0208 {
0209     d->m_falseFriends.removeAt(d->m_falseFriends.indexOf(falseFriend));
0210 }
0211 
0212 QList< KEduVocTranslation* > KEduVocTranslation::falseFriends() const
0213 {
0214     return d->m_falseFriends;
0215 }
0216 
0217 
0218 void KEduVocTranslation::addSynonym( KEduVocTranslation* synonym )
0219 {
0220     d->m_synonyms.append(synonym);
0221 }
0222 
0223 void KEduVocTranslation::removeSynonym(KEduVocTranslation * synonym)
0224 {
0225     d->m_synonyms.removeAt(d->m_synonyms.indexOf(synonym));
0226 }
0227 
0228 QList<KEduVocTranslation*> KEduVocTranslation::synonyms() const
0229 {
0230     return d->m_synonyms;
0231 }
0232 
0233 void KEduVocTranslation::addAntonym( KEduVocTranslation* antonym )
0234 {
0235     d->m_antonyms.append(antonym);
0236 }
0237 
0238 QList<KEduVocTranslation*> KEduVocTranslation::antonyms() const
0239 {
0240     return d->m_antonyms;
0241 }
0242 
0243 void KEduVocTranslation::removeAntonym(KEduVocTranslation * antonym)
0244 {
0245     d->m_antonyms.removeAt(d->m_antonyms.indexOf(antonym));
0246 }
0247 
0248 void KEduVocTranslation::setExample( const QString & expr )
0249 {
0250     d->m_example = expr.simplified();
0251 }
0252 
0253 
0254 QString KEduVocTranslation::example() const
0255 {
0256     return d->m_example;
0257 }
0258 
0259 
0260 void KEduVocTranslation::setParaphrase( const QString & expr )
0261 {
0262     d->m_paraphrase = expr.simplified();
0263 }
0264 
0265 
0266 QString KEduVocTranslation::paraphrase() const
0267 {
0268     return d->m_paraphrase;
0269 }
0270 
0271 
0272 void KEduVocTranslation::setConjugation( const QString& tense, const KEduVocConjugation& con )
0273 {
0274     d->m_conjugations[tense] = con;
0275 }
0276 
0277 
0278 KEduVocConjugation& KEduVocTranslation::conjugation(const QString &tense)
0279 {
0280     return d->m_conjugations[tense];
0281 }
0282 
0283 
0284 KEduVocConjugation KEduVocTranslation::getConjugation(const QString &tense) const
0285 {
0286     if (d->m_conjugations.contains(tense)) {
0287         return d->m_conjugations[tense];
0288     }
0289     return KEduVocConjugation();
0290 }
0291 
0292 
0293 QStringList& KEduVocTranslation::multipleChoice()
0294 {
0295     return d->m_multipleChoice;
0296 }
0297 
0298 
0299 QStringList KEduVocTranslation::getMultipleChoice() const
0300 {
0301     return d->m_multipleChoice;
0302 }
0303 
0304 
0305 void KEduVocTranslation::setMultipleChoice(const QStringList &choices)
0306 {
0307     d->m_multipleChoice = choices;
0308 }
0309 
0310 
0311 QString KEduVocTranslation::pronunciation() const
0312 {
0313     return d->m_pronunciation;
0314 }
0315 
0316 
0317 void KEduVocTranslation::setPronunciation( const QString & expr )
0318 {
0319     d->m_pronunciation = expr.simplified();
0320 }
0321 
0322 QStringList KEduVocTranslation::conjugationTenses() const
0323 {
0324     return d->m_conjugations.keys();
0325 }
0326 
0327 QMap< QString, KEduVocConjugation > KEduVocTranslation::conjugations() const
0328 {
0329     return d->m_conjugations;
0330 }
0331 
0332 void KEduVocTranslation::setConjugations(const QMap< QString, KEduVocConjugation > & conjugations)
0333 {
0334     d->m_conjugations = conjugations;
0335 }
0336 
0337 /** get the sound url for this translation if it exists */
0338 QUrl KEduVocTranslation::soundUrl()
0339 {
0340     return d->m_soundUrl;
0341 }
0342 
0343 /** set the sound url for this translation
0344  * @param url               url of the sound file */
0345 void KEduVocTranslation::setSoundUrl(const QUrl &url)
0346 {
0347     d->m_soundUrl = url;
0348 }
0349 
0350 /** get the image url for this translation if it exists */
0351 QUrl KEduVocTranslation::imageUrl()
0352 {
0353     return d->m_imageUrl;
0354 }
0355 
0356 /** set the image url for this translation
0357  * @param url               url of the image
0358  */
0359 void KEduVocTranslation::setImageUrl(const QUrl &url)
0360 {
0361     d->m_imageUrl = url;
0362 }
0363 
0364 KEduVocWordType * KEduVocTranslation::wordType() const
0365 {
0366     if (d) {
0367         return d->m_wordType;
0368     } else {
0369         return nullptr;
0370     }
0371 }
0372 
0373 void KEduVocTranslation::setWordType(KEduVocWordType * wordType)
0374 {
0375     if ( d->m_wordType ) {
0376         d->m_wordType->removeTranslation(this);
0377     }
0378     if ( wordType ) {
0379         wordType->addTranslation(this);
0380     }
0381     d->m_wordType = wordType;
0382 }
0383 
0384 KEduVocLeitnerBox * KEduVocTranslation::leitnerBox() const
0385 {
0386     return d->m_leitnerBox;
0387 }
0388 
0389 void KEduVocTranslation::setLeitnerBox(KEduVocLeitnerBox * leitnerBox)
0390 {
0391     if ( d->m_leitnerBox ) {
0392         d->m_leitnerBox->removeTranslation(this);
0393     }
0394     if ( leitnerBox ) {
0395         leitnerBox->addTranslation(this);
0396     }
0397     d->m_leitnerBox = leitnerBox;
0398 }
0399 
0400 KEduVocExpression * KEduVocTranslation::entry()
0401 {
0402     return d->m_entry;
0403 }
0404 
0405 QString KEduVocTranslation::comparative() const
0406 {
0407     if (d->m_comparative) {
0408         return d->m_comparative->text();
0409     }
0410     return QString();
0411 }
0412 
0413 void KEduVocTranslation::setComparative(const QString & comparative)
0414 {
0415     if (!d->m_comparative) {
0416         d->m_comparative = new KEduVocText(comparative);
0417     } else {
0418         d->m_comparative->setText(comparative);
0419     }
0420 }
0421 
0422 QString KEduVocTranslation::superlative() const
0423 {
0424     if (d->m_superlative) {
0425         return d->m_superlative->text();
0426     }
0427     return QString();
0428 }
0429 
0430 void KEduVocTranslation::setSuperlative(const QString & superlative)
0431 {
0432     if (!d->m_superlative) {
0433         d->m_superlative = new KEduVocText(superlative);
0434     } else {
0435         d->m_superlative->setText(superlative);
0436     }
0437 }
0438 
0439 KEduVocText KEduVocTranslation::comparativeForm() const
0440 {
0441     if (!d->m_comparative) {
0442         return KEduVocText();
0443     }
0444     KEduVocText t(*(d->m_comparative));
0445     return t;
0446 }
0447 
0448 void KEduVocTranslation::setComparativeForm(const KEduVocText& comparative)
0449 {
0450     if (!d->m_comparative) {
0451         d->m_comparative = new KEduVocText();
0452     }
0453     *(d->m_comparative) = comparative;
0454 }
0455 
0456 KEduVocText KEduVocTranslation::superlativeForm() const
0457 {
0458     if (!d->m_superlative) {
0459         return KEduVocText();
0460     }
0461     KEduVocText t(*d->m_superlative);
0462     return t;
0463 }
0464 
0465 void KEduVocTranslation::setSuperlativeForm(const KEduVocText& superlative)
0466 {
0467     if (!d->m_superlative) {
0468         d->m_superlative = new KEduVocText();
0469     }
0470     *d->m_superlative = superlative;
0471 }
0472 
0473 KEduVocText KEduVocTranslation::article() const
0474 {
0475     if (!d->m_articleGrade) {
0476         return KEduVocText();
0477     }
0478     KEduVocText t(*d->m_articleGrade);
0479     return t;
0480 }
0481 
0482 void KEduVocTranslation::setArticle(const KEduVocText& article)
0483 {
0484     if (!d->m_articleGrade) {
0485         d->m_articleGrade = new KEduVocText();
0486     }
0487     *d->m_articleGrade = article;
0488 }
0489 
0490 KEduVocDeclension * KEduVocTranslation::declension()
0491 {
0492     return d->m_declension;
0493 }
0494 
0495 void KEduVocTranslation::setDeclension(KEduVocDeclension * declension)
0496 {
0497     // remove the old declension object
0498     delete d->m_declension;
0499     d->m_declension = declension;
0500 }
0501 
0502 void KEduVocTranslation::toKVTML2(QDomElement & parent)
0503 {
0504     // text and grade
0505     KEduVocText::toKVTML2(parent);
0506 
0507     // declension
0508     if (d->m_declension) {
0509         d->m_declension->toKVTML2(parent);
0510     }
0511 
0512     // conjugation
0513     foreach ( const QString &tense, conjugationTenses() ) {
0514         QDomElement conjugationElement = parent.ownerDocument().createElement( KVTML_CONJUGATION );
0515         getConjugation(tense).toKVTML2(conjugationElement, tense);
0516         if (conjugationElement.hasChildNodes()) {
0517             parent.appendChild( conjugationElement );
0518         }
0519     }
0520 
0521     // <comment>
0522     KEduVocKvtml2Writer::appendTextElement( parent, KVTML_COMMENT, comment() );
0523 
0524     // <pronunciation>
0525     KEduVocKvtml2Writer::appendTextElement( parent, KVTML_PRONUNCIATION, pronunciation() );
0526 
0527     // <example>
0528     KEduVocKvtml2Writer::appendTextElement( parent, KVTML_EXAMPLE, example() );
0529 
0530     // <paraphrase>
0531     KEduVocKvtml2Writer::appendTextElement( parent, KVTML_PARAPHRASE, paraphrase() );
0532 
0533     ///@todo synonyms, antonyms
0534     ///@todo false friends
0535 }
0536 
0537 void KEduVocTranslation::fromKVTML2(QDomElement & parent)
0538 {
0539     KEduVocText::fromKVTML2(parent);
0540 
0541     setDeclension(KEduVocDeclension::fromKVTML2(parent));
0542 
0543     setComment( parent.firstChildElement( KVTML_COMMENT ).text() );
0544 
0545     setPronunciation( parent.firstChildElement( KVTML_PRONUNCIATION ).text() );
0546 
0547     //<example></example>
0548     setExample( parent.firstChildElement( KVTML_EXAMPLE ).text() );
0549 
0550     //<paraphrase></paraphrase>
0551     setParaphrase( parent.firstChildElement( KVTML_PARAPHRASE ).text() );
0552 
0553     // conjugations
0554     QDomElement conjugationElement = parent.firstChildElement( KVTML_CONJUGATION );
0555     while ( !conjugationElement.isNull() ) {
0556         QDomElement tenseElement = conjugationElement.firstChildElement( KVTML_TENSE );
0557         QString tense = tenseElement.text();
0558         KEduVocConjugation *conjugation = KEduVocConjugation::fromKVTML2(conjugationElement);
0559         setConjugation(tense, *conjugation);
0560         delete conjugation;
0561         conjugationElement = conjugationElement.nextSiblingElement( KVTML_CONJUGATION );
0562     }
0563 
0564     ///@todo synonyms, antonym
0565     ///@todo false friends
0566 }
0567 
0568 void KEduVocTranslation::setEntry(KEduVocExpression * entry)
0569 {
0570     d->m_entry = entry;
0571 }
0572 
0573 
0574