LibKEduVocDocument

keduvoctranslation.cpp
1/*
2 * Vocabulary Expression Translation for KDE Edu
3 * SPDX-FileCopyrightText: 2007-2010 Frederik Gladhorn <gladhorn@kde.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "keduvoctranslation.h"
8
9#include "keduvocdeclension.h"
10#include "keduvockvtml2writer.h"
11#include "keduvocleitnerbox.h"
12#include "keduvocwordtype.h"
13#include "kvtml2defs.h"
14
15class KEduVocTranslation::KEduVocTranslationPrivate
16{
17public:
18 KEduVocTranslationPrivate(KEduVocExpression *parent);
19
20 ~KEduVocTranslationPrivate();
21
22 KEduVocExpression *m_entry;
23
24 /// Type of a word noun, verb, adjective etc
25 KEduVocWordType *m_wordType;
26
27 /// Leitner box of the translation.
28 KEduVocLeitnerBox *m_leitnerBox;
29
30 /// A comment giving additional information.
31 QString m_comment;
32 /// A hint, to make guessing the word easier.
33 QString m_hint;
34 /// Paraphrase
35 QString m_paraphrase;
36 /// An example
37 QString m_example;
38 /// Pronunciation
39 QString m_pronunciation;
40 /// Image url
41 QUrl m_imageUrl;
42 /// Sound url
43 QUrl m_soundUrl;
44
45 /// When creating multiple choice tests, these are possible answers. (otherwise other words are added randomly)
46 QStringList m_multipleChoice;
47
48 /// Conjugations of a word (I go, you go, he goes... boring in english)
50
51 /// The comparison forms of adjectives and adverbs: (fast), faster, fastest
52 KEduVocText *m_comparative;
53 KEduVocText *m_superlative;
54
55 /// The grade of an article. The text part should not be used.
56 KEduVocText *m_articleGrade;
57
58 KEduVocDeclension *m_declension;
59
60 // connections to other translations
61 /// Synonyms for a word: sick and ill, student and pupil
63 /// An antonym - the opposite: hot - cold
65 /// List of false friends
66 QList<KEduVocTranslation *> m_falseFriends;
67};
68
69KEduVocTranslation::KEduVocTranslationPrivate::KEduVocTranslationPrivate(KEduVocExpression *parent)
70{
71 m_entry = parent;
72 m_wordType = nullptr;
73 m_leitnerBox = nullptr;
74 m_declension = nullptr;
75
76 m_comparative = nullptr;
77 m_superlative = nullptr;
78 m_articleGrade = nullptr;
79}
80
81KEduVocTranslation::KEduVocTranslationPrivate::~KEduVocTranslationPrivate()
82{
83 delete m_declension;
84}
85
87 : d(new KEduVocTranslationPrivate(entry))
88{
89}
90
92 : d(new KEduVocTranslationPrivate(entry))
93{
94 setText(translation.simplified());
95}
96
98 : KEduVocText(other)
99 ,
100 // set the entry to 0, the translation will be put into a copied entry by the expression copy constructor
101 d(new KEduVocTranslationPrivate(nullptr))
102{
103 // better no word type copy as this is pointer copying
104 // will not work as this is not added to the word type container!
105 // d->m_wordType = other.d->m_wordType;
106 // d->m_leitnerBox = translation.d->m_leitnerBox;
107 d->m_comment = other.d->m_comment;
108 d->m_paraphrase = other.d->m_paraphrase;
109 d->m_example = other.d->m_example;
110 d->m_pronunciation = other.d->m_pronunciation;
111 d->m_conjugations = other.d->m_conjugations;
112 d->m_comparative = other.d->m_comparative;
113 d->m_superlative = other.d->m_superlative;
114 d->m_multipleChoice = other.d->m_multipleChoice;
115 d->m_imageUrl = other.d->m_imageUrl;
116 d->m_soundUrl = other.d->m_soundUrl;
117 // no copies of the following for now. we don't know enough to also add this as synonym/etc
118 // d->m_synonyms = other.d->m_synonyms;
119 // d->m_antonyms = other.d->m_antonyms;
120 // d->m_falseFriends = other.d->m_falseFriends;
121 if (other.d->m_declension) {
122 d->m_declension = new KEduVocDeclension(*other.d->m_declension);
123 }
124}
125
127{
128 setWordType(nullptr);
129 setLeitnerBox(nullptr);
130 foreach (KEduVocTranslation *synonym, d->m_synonyms) {
131 synonym->removeSynonym(this);
132 }
133 foreach (KEduVocTranslation *antonym, d->m_antonyms) {
134 antonym->removeAntonym(this);
135 }
136 foreach (KEduVocTranslation *falseFriend, d->m_falseFriends) {
137 falseFriend->removeFalseFriend(this);
138 }
139 delete d;
140}
141
143{
144 return KEduVocText::operator==(translation) && d->m_wordType == translation.d->m_wordType && d->m_leitnerBox == translation.d->m_leitnerBox
145 && d->m_comment == translation.d->m_comment && d->m_paraphrase == translation.d->m_paraphrase && d->m_example == translation.d->m_example
146 && d->m_pronunciation == translation.d->m_pronunciation && d->m_imageUrl == translation.d->m_imageUrl && d->m_soundUrl == translation.d->m_soundUrl
147 && d->m_comparative == translation.d->m_comparative && d->m_superlative == translation.d->m_superlative
148 && d->m_multipleChoice == translation.d->m_multipleChoice && d->m_synonyms == translation.d->m_synonyms && d->m_antonyms == translation.d->m_antonyms
149 && d->m_falseFriends == translation.d->m_falseFriends && d->m_conjugations == translation.d->m_conjugations;
150 /// @todo check and include declensions d->m_declension == translation.d->m_declension;
151}
152
154{
155 KEduVocText::operator=(translation);
156 d->m_entry = translation.d->m_entry;
157 // d->m_wordType = translation.d->m_wordType;
158 // d->m_leitnerBox = translation.d->m_leitnerBox;
159 d->m_comment = translation.d->m_comment;
160 d->m_paraphrase = translation.d->m_paraphrase;
161 d->m_example = translation.d->m_example;
162 d->m_pronunciation = translation.d->m_pronunciation;
163 d->m_imageUrl = translation.d->m_imageUrl;
164 d->m_soundUrl = translation.d->m_soundUrl;
165 d->m_comparative = translation.d->m_comparative;
166 d->m_superlative = translation.d->m_superlative;
167 d->m_multipleChoice = translation.d->m_multipleChoice;
168 d->m_falseFriends = translation.d->m_falseFriends;
169 d->m_synonyms = translation.d->m_synonyms;
170 d->m_antonyms = translation.d->m_antonyms;
171 d->m_conjugations = translation.d->m_conjugations;
172 if (translation.d->m_declension) {
173 d->m_declension = new KEduVocDeclension(*translation.d->m_declension);
174 }
175
176 return *this;
177}
178
180{
181 return d->m_comment;
182}
183
185{
186 d->m_comment = expr.simplified();
187}
188
190{
191 d->m_falseFriends.append(falseFriend);
192}
193
195{
196 d->m_falseFriends.removeAt(d->m_falseFriends.indexOf(falseFriend));
197}
198
200{
201 return d->m_falseFriends;
202}
203
205{
206 d->m_synonyms.append(synonym);
207}
208
210{
211 d->m_synonyms.removeAt(d->m_synonyms.indexOf(synonym));
212}
213
215{
216 return d->m_synonyms;
217}
218
220{
221 d->m_antonyms.append(antonym);
222}
223
225{
226 return d->m_antonyms;
227}
228
230{
231 d->m_antonyms.removeAt(d->m_antonyms.indexOf(antonym));
232}
233
235{
236 d->m_example = expr.simplified();
237}
238
240{
241 return d->m_example;
242}
243
245{
246 d->m_paraphrase = expr.simplified();
247}
248
250{
251 return d->m_paraphrase;
252}
253
255{
256 d->m_conjugations[tense] = con;
257}
258
260{
261 return d->m_conjugations[tense];
262}
263
265{
266 if (d->m_conjugations.contains(tense)) {
267 return d->m_conjugations[tense];
268 }
269 return KEduVocConjugation();
270}
271
273{
274 return d->m_multipleChoice;
275}
276
278{
279 return d->m_multipleChoice;
280}
281
283{
284 d->m_multipleChoice = choices;
285}
286
288{
289 return d->m_pronunciation;
290}
291
293{
294 d->m_pronunciation = expr.simplified();
295}
296
297QStringList KEduVocTranslation::conjugationTenses() const
298{
299 return d->m_conjugations.keys();
300}
301
303{
304 return d->m_conjugations;
305}
306
308{
309 d->m_conjugations = conjugations;
310}
311
312/** get the sound url for this translation if it exists */
314{
315 return d->m_soundUrl;
316}
317
318/** set the sound url for this translation
319 * @param url url of the sound file */
321{
322 d->m_soundUrl = url;
323}
324
325/** get the image url for this translation if it exists */
327{
328 return d->m_imageUrl;
329}
330
331/** set the image url for this translation
332 * @param url url of the image
333 */
335{
336 d->m_imageUrl = url;
337}
338
340{
341 if (d) {
342 return d->m_wordType;
343 } else {
344 return nullptr;
345 }
346}
347
349{
350 if (d->m_wordType) {
351 d->m_wordType->removeTranslation(this);
352 }
353 if (wordType) {
354 wordType->addTranslation(this);
355 }
356 d->m_wordType = wordType;
357}
358
360{
361 return d->m_leitnerBox;
362}
363
365{
366 if (d->m_leitnerBox) {
367 d->m_leitnerBox->removeTranslation(this);
368 }
369 if (leitnerBox) {
370 leitnerBox->addTranslation(this);
371 }
372 d->m_leitnerBox = leitnerBox;
373}
374
375KEduVocExpression *KEduVocTranslation::entry()
376{
377 return d->m_entry;
378}
379
381{
382 if (d->m_comparative) {
383 return d->m_comparative->text();
384 }
385 return QString();
386}
387
388void KEduVocTranslation::setComparative(const QString &comparative)
389{
390 if (!d->m_comparative) {
391 d->m_comparative = new KEduVocText(comparative);
392 } else {
393 d->m_comparative->setText(comparative);
394 }
395}
396
397QString KEduVocTranslation::superlative() const
398{
399 if (d->m_superlative) {
400 return d->m_superlative->text();
401 }
402 return QString();
403}
404
405void KEduVocTranslation::setSuperlative(const QString &superlative)
406{
407 if (!d->m_superlative) {
408 d->m_superlative = new KEduVocText(superlative);
409 } else {
410 d->m_superlative->setText(superlative);
411 }
412}
413
414KEduVocText KEduVocTranslation::comparativeForm() const
415{
416 if (!d->m_comparative) {
417 return KEduVocText();
418 }
419 KEduVocText t(*(d->m_comparative));
420 return t;
421}
422
423void KEduVocTranslation::setComparativeForm(const KEduVocText &comparative)
424{
425 if (!d->m_comparative) {
426 d->m_comparative = new KEduVocText();
427 }
428 *(d->m_comparative) = comparative;
429}
430
431KEduVocText KEduVocTranslation::superlativeForm() const
432{
433 if (!d->m_superlative) {
434 return KEduVocText();
435 }
436 KEduVocText t(*d->m_superlative);
437 return t;
438}
439
440void KEduVocTranslation::setSuperlativeForm(const KEduVocText &superlative)
441{
442 if (!d->m_superlative) {
443 d->m_superlative = new KEduVocText();
444 }
445 *d->m_superlative = superlative;
446}
447
448KEduVocText KEduVocTranslation::article() const
449{
450 if (!d->m_articleGrade) {
451 return KEduVocText();
452 }
453 KEduVocText t(*d->m_articleGrade);
454 return t;
455}
456
457void KEduVocTranslation::setArticle(const KEduVocText &article)
458{
459 if (!d->m_articleGrade) {
460 d->m_articleGrade = new KEduVocText();
461 }
462 *d->m_articleGrade = article;
463}
464
466{
467 return d->m_declension;
468}
469
471{
472 // remove the old declension object
473 delete d->m_declension;
474 d->m_declension = declension;
475}
476
478{
479 // text and grade
480 KEduVocText::toKVTML2(parent);
481
482 // declension
483 if (d->m_declension) {
484 d->m_declension->toKVTML2(parent);
485 }
486
487 // conjugation
488 foreach (const QString &tense, conjugationTenses()) {
489 QDomElement conjugationElement = parent.ownerDocument().createElement(KVTML_CONJUGATION);
490 getConjugation(tense).toKVTML2(conjugationElement, tense);
491 if (conjugationElement.hasChildNodes()) {
492 parent.appendChild(conjugationElement);
493 }
494 }
495
496 // <comment>
497 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_COMMENT, comment());
498
499 // <pronunciation>
500 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_PRONUNCIATION, pronunciation());
501
502 // <example>
503 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_EXAMPLE, example());
504
505 // <paraphrase>
506 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_PARAPHRASE, paraphrase());
507
508 ///@todo synonyms, antonyms
509 ///@todo false friends
510}
511
513{
514 KEduVocText::fromKVTML2(parent);
515
517
518 setComment(parent.firstChildElement(KVTML_COMMENT).text());
519
520 setPronunciation(parent.firstChildElement(KVTML_PRONUNCIATION).text());
521
522 //<example></example>
523 setExample(parent.firstChildElement(KVTML_EXAMPLE).text());
524
525 //<paraphrase></paraphrase>
526 setParaphrase(parent.firstChildElement(KVTML_PARAPHRASE).text());
527
528 // conjugations
529 QDomElement conjugationElement = parent.firstChildElement(KVTML_CONJUGATION);
530 while (!conjugationElement.isNull()) {
531 QDomElement tenseElement = conjugationElement.firstChildElement(KVTML_TENSE);
532 QString tense = tenseElement.text();
535 delete conjugation;
536 conjugationElement = conjugationElement.nextSiblingElement(KVTML_CONJUGATION);
537 }
538
539 ///@todo synonyms, antonym
540 ///@todo false friends
541}
542
543void KEduVocTranslation::setEntry(KEduVocExpression *entry)
544{
545 d->m_entry = entry;
546}
The conjugation of a verb.
void toKVTML2(QDomElement &parent, const QString &tense)
Create xml for this declension.
static KEduVocConjugation * fromKVTML2(QDomElement &parent)
Reads a declension from xml, returns 0 if it is empty.
A declension contains all forms that a NOUN possibly can have.
static KEduVocDeclension * fromKVTML2(QDomElement &parent)
Reads a declension from xml, returns 0 if it is empty.
void toKVTML2(QDomElement &parent)
Create xml for this declension.
This class contains one vocabulary expression as an original with one or more translations.
static void appendTextElement(QDomElement &parent, const QString &elementName, const QString &text)
Helper function, appends a new element AND a text child to parent Only appends if text is NOT empty.
Leitner Boxes are an alternative grading system.
A text in vocabulary documents.
Definition keduvoctext.h:50
bool operator==(const KEduVocText &other) const
Compare two sets of grades.
QString text() const
The translation as string (the word itself)
KEduVocText & operator=(const KEduVocText &other)
Equal operator to copy grades.
KEduVocText(const QString &text=QString())
default constructor
void setText(const QString &expr)
Sets the translation.
KEDUVOCDOCUMENT_DEPRECATED QString comparative() const
Comparison forms of adjectives/adverbs.
void toKVTML2(QDomElement &parent)
void setConjugations(const QMap< QString, KEduVocConjugation > &conjugations)
Bad, only used for tense entry page, will be deleted later.
KEDUVOCDOCUMENT_DEPRECATED QStringList & multipleChoice()
This method is deprecated, please use.
QString example() const
Returns example of this expression.
KEduVocConjugation getConjugation(const QString &tense) const
Returns a conjugation if available.
KEDUVOCDOCUMENT_DEPRECATED KEduVocConjugation & conjugation(const QString &tense)
This method is deprecated, please use.
void setLeitnerBox(KEduVocLeitnerBox *leitnerBox)
Sets the leitner box of this translation.
QString paraphrase() const
Returns paraphrase of this expression.
void setExample(const QString &expression)
Sets example this expression.
QString pronunciation() const
Returns the pronunciation of this expression.
void setSoundUrl(const QUrl &url)
Set the sound url for this translation.
void removeSynonym(KEduVocTranslation *synonym)
Remove a synonym.
void removeAntonym(KEduVocTranslation *antonym)
Remove a antonym.
QStringList getMultipleChoice() const
Returns multiple choice if available.
void addAntonym(KEduVocTranslation *antonym)
Add a antonym.
KEduVocLeitnerBox * leitnerBox() const
Returns the leitner box of this translation.
KEDUVOCDOCUMENT_DEPRECATED QMap< QString, KEduVocConjugation > conjugations() const
Bad, only compatibility.
void addFalseFriend(KEduVocTranslation *falseFriend)
Add a false friend.
KEduVocDeclension * declension()
Returns a pointer to the declension object of this translation.
QUrl soundUrl()
Get the sound url for this translation if it exists.
QList< KEduVocTranslation * > synonyms() const
Returns synonyms of this expression.
QString antonym() const
Returns antonym of this expression.
QList< KEduVocTranslation * > antonyms() const
Returns antonyms of this expression.
void setConjugation(const QString &tense, const KEduVocConjugation &conjugation)
adds conjugations or replaces them, if they exist.
void setMultipleChoice(const QStringList &choices)
Sets multiple choice.
void setParaphrase(const QString &expression)
Sets paraphrase of this expression.
QList< KEduVocTranslation * > falseFriends() const
Returns false friends of this expression.
bool operator==(const KEduVocTranslation &translation) const
Compare two translations, including word type etc.
void setDeclension(KEduVocDeclension *declension)
Set a new declension for a translation.
void addSynonym(KEduVocTranslation *synonym)
Add a synonym.
void setWordType(KEduVocWordType *wordType)
Sets the word type of this expression.
void fromKVTML2(QDomElement &parent)
void setImageUrl(const QUrl &url)
Set the image url for this translation.
KEduVocTranslation(KEduVocExpression *entry)
Default constructor for an empty translation.
void removeFalseFriend(KEduVocTranslation *falseFriend)
Remove a false friend.
QString comment() const
Returns comments of this expression.
void setPronunciation(const QString &expression)
Sets the pronunciation of this expression.
KEduVocWordType * wordType() const
Returns the word type of this expression, you will get a 0 pointer if wordtype is not set for the tra...
QUrl imageUrl()
Get the image url for this translation if it exists.
KEduVocTranslation & operator=(const KEduVocTranslation &translation)
Equal operator to assign a translation to another one.
void setComment(const QString &expr)
Sets comment of this expression.
class to store translation word types
QDomElement createElement(const QString &tagName)
QString text() const const
QDomNode appendChild(const QDomNode &newChild)
QDomElement firstChildElement(const QString &tagName, const QString &namespaceURI) const const
bool hasChildNodes() const const
bool isNull() const const
QDomElement nextSiblingElement(const QString &tagName, const QString &namespaceURI) const const
QDomDocument ownerDocument() const const
void append(QList< T > &&value)
qsizetype indexOf(const AT &value, qsizetype from) const const
void removeAt(qsizetype i)
bool contains(const Key &key) const const
QList< Key > keys() const const
QString simplified() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:55:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.