From ad579723ddc3da6b2cbbd07827ff8c135aa592d0 Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Mon, 4 Feb 2008 21:20:42 +0000 Subject: [PATCH] Change the way grades are handled fundamentally. Too bad, I didn't have the idea long ago. Much simpler: one grade for each word, no longer the fromTranslation. So whenever the user gets abc right, abc gets it's grade increased. Translation inherits from the new Text class. The new class KEduVocText will be used instead of QString in many places to for ex allow grades for individual conjugation forms etc. Add initial unittests for translation class. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=770972 --- keduvocdocument/keduvocconjugation.cpp | 12 +--- keduvocdocument/keduvocconjugation.h | 2 - keduvocdocument/keduvocgrade.cpp | 57 ++++++++++++------- keduvocdocument/keduvocgrade.h | 33 +++++++---- keduvocdocument/keduvockvtml2reader.cpp | 9 ++- keduvocdocument/keduvockvtml2writer.cpp | 27 ++++----- keduvocdocument/keduvockvtmlreader.cpp | 16 +++--- keduvocdocument/keduvoctranslation.cpp | 52 +++-------------- keduvocdocument/keduvoctranslation.h | 25 +------- .../tests/keduvocdocumentvalidatortest.cpp | 16 ++++++ 10 files changed, 108 insertions(+), 141 deletions(-) diff --git a/keduvocdocument/keduvocconjugation.cpp b/keduvocdocument/keduvocconjugation.cpp index 0c145d8..325b01f 100644 --- a/keduvocdocument/keduvocconjugation.cpp +++ b/keduvocdocument/keduvocconjugation.cpp @@ -29,7 +29,6 @@ class KEduVocConjugation::Private { public: - KEduVocGrade m_grade; QMap m_conjugations; }; @@ -42,7 +41,6 @@ KEduVocConjugation::KEduVocConjugation() KEduVocConjugation::KEduVocConjugation( const KEduVocConjugation& other ) : d( new Private ) { - d->m_grade = other.d->m_grade; d->m_conjugations = other.d->m_conjugations; } @@ -55,15 +53,13 @@ KEduVocConjugation::~KEduVocConjugation() KEduVocConjugation& KEduVocConjugation::operator = ( const KEduVocConjugation& other ) { - d->m_grade = other.d->m_grade; d->m_conjugations = other.d->m_conjugations; return *this; } bool KEduVocConjugation::operator ==(const KEduVocConjugation& other) const { - return d->m_conjugations == other.d->m_conjugations && - d->m_grade == other.d->m_grade; + return d->m_conjugations == other.d->m_conjugations; } @@ -111,12 +107,6 @@ bool KEduVocConjugation::isEmpty() return d->m_conjugations.count() == 0; } - -KEduVocGrade & KEduVocConjugation::grade() -{ - return d->m_grade; -} - QList< int > KEduVocConjugation::keys() { return d->m_conjugations.keys(); diff --git a/keduvocdocument/keduvocconjugation.h b/keduvocdocument/keduvocconjugation.h index 69726aa..1867d46 100644 --- a/keduvocdocument/keduvocconjugation.h +++ b/keduvocdocument/keduvocconjugation.h @@ -76,8 +76,6 @@ public: bool isEmpty(); - KEduVocGrade &grade(); - static int indexOf(ConjugationPerson person, ConjugationNumber number); private: diff --git a/keduvocdocument/keduvocgrade.cpp b/keduvocdocument/keduvocgrade.cpp index c78e850..5e18f18 100644 --- a/keduvocdocument/keduvocgrade.cpp +++ b/keduvocdocument/keduvocgrade.cpp @@ -15,36 +15,51 @@ #include "keduvocgrade.h" -class KEduVocGrade::KEduVocGradePrivate +class KEduVocText::KEduVocTextPrivate { public: + /// This is the word itself. The vocabulary. This is what it is all about. + QString m_text; + grade_t m_grade; count_t m_totalPracticeCount; count_t m_badCount; QDateTime m_practiceDate; }; -KEduVocGrade::KEduVocGrade() - :d( new KEduVocGradePrivate ) +KEduVocText::KEduVocText(const QString& text) + :d( new KEduVocTextPrivate ) { + d->m_text = text; resetGrades(); } -KEduVocGrade::KEduVocGrade( const KEduVocGrade &other ) - :d( new KEduVocGradePrivate ) +KEduVocText::KEduVocText( const KEduVocText &other ) + :d( new KEduVocTextPrivate ) { + d->m_text = other.d->m_text; setGrade( other.grade() ); setPracticeCount( other.practiceCount() ); setBadCount( other.badCount() ); setPracticeDate( other.practiceDate() ); } -KEduVocGrade::~KEduVocGrade() +KEduVocText::~KEduVocText() { delete d; } -void KEduVocGrade::resetGrades() +QString KEduVocText::text() const +{ + return d->m_text; +} + +void KEduVocText::setText( const QString & expr ) +{ + d->m_text = expr.simplified(); +} + +void KEduVocText::resetGrades() { d->m_grade = KV_NORM_GRADE; d->m_totalPracticeCount = 0; @@ -56,13 +71,13 @@ void KEduVocGrade::resetGrades() } -grade_t KEduVocGrade::grade() const +grade_t KEduVocText::grade() const { return d->m_grade; } -void KEduVocGrade::setGrade( grade_t grade ) +void KEduVocText::setGrade( grade_t grade ) { if ( grade > KV_MAX_GRADE ) { grade = KV_MAX_GRADE; @@ -71,13 +86,13 @@ void KEduVocGrade::setGrade( grade_t grade ) } -void KEduVocGrade::incGrade() +void KEduVocText::incGrade() { setGrade( grade() + 1 ); } -void KEduVocGrade::decGrade() +void KEduVocText::decGrade() { if ( grade() == KV_MIN_GRADE ) { return; @@ -86,54 +101,54 @@ void KEduVocGrade::decGrade() } -count_t KEduVocGrade::practiceCount() const +count_t KEduVocText::practiceCount() const { return d->m_totalPracticeCount; } -void KEduVocGrade::incPracticeCount() +void KEduVocText::incPracticeCount() { setPracticeCount( practiceCount() + 1 ); } -void KEduVocGrade::incBadCount() +void KEduVocText::incBadCount() { setBadCount( badCount() + 1 ); } -void KEduVocGrade::setPracticeCount( count_t count ) +void KEduVocText::setPracticeCount( count_t count ) { d->m_totalPracticeCount = count; } -count_t KEduVocGrade::badCount() const +count_t KEduVocText::badCount() const { return d->m_badCount; } -void KEduVocGrade::setBadCount( count_t count ) +void KEduVocText::setBadCount( count_t count ) { d->m_badCount = count; } -QDateTime KEduVocGrade::practiceDate() const +QDateTime KEduVocText::practiceDate() const { return d->m_practiceDate; } -void KEduVocGrade::setPracticeDate( const QDateTime & date ) +void KEduVocText::setPracticeDate( const QDateTime & date ) { d->m_practiceDate = date; } -KEduVocGrade & KEduVocGrade::operator =(const KEduVocGrade & other) +KEduVocText & KEduVocText::operator =(const KEduVocText & other) { d->m_grade = other.d->m_grade; d->m_totalPracticeCount = other.d->m_totalPracticeCount; @@ -143,7 +158,7 @@ KEduVocGrade & KEduVocGrade::operator =(const KEduVocGrade & other) return *this; } -bool KEduVocGrade::operator ==(const KEduVocGrade & other) const +bool KEduVocText::operator ==(const KEduVocText & other) const { return d->m_grade == other.d->m_grade && d->m_totalPracticeCount == other.d->m_totalPracticeCount && diff --git a/keduvocdocument/keduvocgrade.h b/keduvocdocument/keduvocgrade.h index 11f8f6c..700d8a0 100644 --- a/keduvocdocument/keduvocgrade.h +++ b/keduvocdocument/keduvocgrade.h @@ -53,37 +53,49 @@ typedef unsigned short count_t; /** -Contains grading information (query date, bad count) for one language with respect to another. - + * A text in vocabulary documents. Associated with it are grade and date information. + * This should be used instead of strings for all things that can be tested and thus get a grade. @author Frederik Gladhorn */ -class KEDUVOCDOCUMENT_EXPORT KEduVocGrade +class KEDUVOCDOCUMENT_EXPORT KEduVocText { public: /** default constructor */ - KEduVocGrade(); + KEduVocText(const QString& text = QString()); /** copy constructor * provides safe copy of d pointer * @param other object to copy from */ - KEduVocGrade( const KEduVocGrade &other ); + KEduVocText( const KEduVocText &other ); /** default destructor */ - ~KEduVocGrade(); + ~KEduVocText(); + + /** + * The translation as string (the word itself) + * @return the translation + */ + QString text() const; + + /** + * Sets the translation + * @param expr + */ + void setText( const QString & expr ); /** * Equal operator to copy grades. * @param other grades copied * @return reference to the new grades */ - KEduVocGrade& operator= ( const KEduVocGrade &other ); + KEduVocText& operator= ( const KEduVocText &other ); /** * Compare two sets of grades. * @param other * @return true if equal */ - bool operator== ( const KEduVocGrade &other ) const; + bool operator== ( const KEduVocText &other ) const; /** returns how often this entry has been practiced as int @@ -112,7 +124,6 @@ public: /** increment query count of given translation by 1 */ void incPracticeCount(); - /** * Clears grading and date information. */ @@ -144,8 +155,8 @@ public: void setPracticeDate( const QDateTime & date ); private: - class KEduVocGradePrivate; - KEduVocGradePrivate * const d; + class KEduVocTextPrivate; + KEduVocTextPrivate * const d; }; #endif diff --git a/keduvocdocument/keduvockvtml2reader.cpp b/keduvocdocument/keduvockvtml2reader.cpp index 9191694..247cd69 100644 --- a/keduvocdocument/keduvockvtml2reader.cpp +++ b/keduvocdocument/keduvockvtml2reader.cpp @@ -656,7 +656,6 @@ bool KEduVocKvtml2Reader::readMultipleChoice( QDomElement &multipleChoiceElement bool KEduVocKvtml2Reader::readGrade( QDomElement &gradeElement, KEduVocExpression *expr, int index ) { bool result = true; - int id = gradeElement.attribute( KVTML_FROMID ).toInt( &result ); if ( !result ) { m_errorMessage = i18n( "identifier missing id" ); return false; @@ -665,19 +664,19 @@ bool KEduVocKvtml2Reader::readGrade( QDomElement &gradeElement, KEduVocExpressio QDomElement currentElement = gradeElement.firstChildElement( KVTML_CURRENTGRADE ); if ( !currentElement.isNull() ) { int value = currentElement.text().toInt(); - expr->translation(index)->gradeFrom( id ).setGrade( value ); + expr->translation(index)->setGrade( value ); } currentElement = gradeElement.firstChildElement( KVTML_COUNT ); if ( !currentElement.isNull() ) { int value = currentElement.text().toInt(); - expr->translation(index)->gradeFrom( id ).setPracticeCount( value ); + expr->translation(index)->setPracticeCount( value ); } currentElement = gradeElement.firstChildElement( KVTML_ERRORCOUNT ); if ( !currentElement.isNull() ) { int value = currentElement.text().toInt(); - expr->translation(index)->gradeFrom( id ).setBadCount( value ); + expr->translation(index)->setBadCount( value ); } currentElement = gradeElement.firstChildElement( KVTML_DATE ); @@ -685,7 +684,7 @@ bool KEduVocKvtml2Reader::readGrade( QDomElement &gradeElement, KEduVocExpressio QString dateString = currentElement.text(); if ( !dateString.isEmpty() ) { QDateTime value = QDateTime::fromString( dateString, Qt::ISODate ); - expr->translation(index)->gradeFrom( id ).setPracticeDate( value ); + expr->translation(index)->setPracticeDate( value ); } } diff --git a/keduvocdocument/keduvockvtml2writer.cpp b/keduvocdocument/keduvockvtml2writer.cpp index 048e234..b01d461 100644 --- a/keduvocdocument/keduvockvtml2writer.cpp +++ b/keduvocdocument/keduvockvtml2writer.cpp @@ -422,25 +422,22 @@ kDebug() << "write tranlation:" << translation->text(); } // grades - for ( int i = 0; i < m_doc->identifierCount(); ++i ) { - KEduVocGrade thisGrade = translation->gradeFrom( i ); - if ( thisGrade.practiceCount() > 0 ) { - QDomElement gradeElement = m_domDoc.createElement( KVTML_GRADE ); - gradeElement.setAttribute( KVTML_FROMID, QString::number( i ) ); - //2 - gradeElement.appendChild( newTextElement( KVTML_CURRENTGRADE, QString::number( thisGrade.grade() ) ) ); + if ( translation->practiceCount() > 0 ) { + QDomElement gradeElement = m_domDoc.createElement( KVTML_GRADE ); - //6 - gradeElement.appendChild( newTextElement( KVTML_COUNT, QString::number( thisGrade.practiceCount() ) ) ); + //2 + gradeElement.appendChild( newTextElement( KVTML_CURRENTGRADE, QString::number( translation->grade() ) ) ); - //1 - gradeElement.appendChild( newTextElement( KVTML_ERRORCOUNT, QString::number( thisGrade.badCount() ) ) ); + //6 + gradeElement.appendChild( newTextElement( KVTML_COUNT, QString::number( translation->practiceCount() ) ) ); - //949757271 - gradeElement.appendChild( newTextElement( KVTML_DATE, thisGrade.practiceDate().toString( Qt::ISODate ) ) ); + //1 + gradeElement.appendChild( newTextElement( KVTML_ERRORCOUNT, QString::number( translation->badCount() ) ) ); - translationElement.appendChild( gradeElement ); - } + //949757271 + gradeElement.appendChild( newTextElement( KVTML_DATE, translation->practiceDate().toString( Qt::ISODate ) ) ); + + translationElement.appendChild( gradeElement ); } // conjugation diff --git a/keduvocdocument/keduvockvtmlreader.cpp b/keduvocdocument/keduvockvtmlreader.cpp index ae4bb31..88ca25f 100644 --- a/keduvocdocument/keduvockvtmlreader.cpp +++ b/keduvocdocument/keduvockvtmlreader.cpp @@ -1033,14 +1033,14 @@ bool KEduVocKvtmlReader::readExpression( QDomElement &domElementParent ) entry->translation( i )->setAntonym( antonym ); if ( i != 0 ) { - entry->translation( i )->gradeFrom( 0 ).setGrade( grade ); - entry->translation( 0 )->gradeFrom( i ).setGrade( r_grade ); - entry->translation( i )->gradeFrom( 0 ).setPracticeCount( qcount ); - entry->translation( 0 )->gradeFrom( i ).setPracticeCount( r_qcount ); - entry->translation( i )->gradeFrom( 0 ).setBadCount( bcount ); - entry->translation( 0 )->gradeFrom( i ).setBadCount( r_bcount ); - entry->translation( i )->gradeFrom( 0 ).setPracticeDate( qdate ); - entry->translation( 0 )->gradeFrom( i ).setPracticeDate( r_qdate ); + entry->translation( i )->setGrade( grade ); + entry->translation( 0 )->setGrade( r_grade ); + entry->translation( i )->setPracticeCount( qcount ); + entry->translation( 0 )->setPracticeCount( r_qcount ); + entry->translation( i )->setBadCount( bcount ); + entry->translation( 0 )->setBadCount( r_bcount ); + entry->translation( i )->setPracticeDate( qdate ); + entry->translation( 0 )->setPracticeDate( r_qdate ); } // Next translation diff --git a/keduvocdocument/keduvoctranslation.cpp b/keduvocdocument/keduvoctranslation.cpp index c387acd..73b824c 100644 --- a/keduvocdocument/keduvoctranslation.cpp +++ b/keduvocdocument/keduvoctranslation.cpp @@ -34,9 +34,6 @@ public: KEduVocExpression* m_entry; - /// This is the word itself. The vocabulary. This is what it is all about. - QString m_translation; - /// Type of a word noun, verb, adjective etc KEduVocWordType* m_wordType; @@ -71,13 +68,6 @@ public: KEduVocDeclination* m_declination; - // Here come all int indexFrom grades. (If you want, imagine the TO grades as int indexFrom of the other translation. That is where they belong. ) - // User is asked to give THIS here as answer, than the grades go here. - // User answers, this is the source, grades go to the other translation. - // Grades go to the translation the user has to type/choose/whatever. - // not all have to be supplied - QMap m_grades; - /// One false friend string per other language QMap m_falseFriends; }; @@ -102,12 +92,13 @@ KEduVocTranslation::KEduVocTranslation(KEduVocExpression* entry) : d( new KEduVo KEduVocTranslation::KEduVocTranslation(KEduVocExpression* entry, const QString &translation ) : d( new KEduVocTranslationPrivate(entry) ) { - d->m_translation = translation.simplified(); + setText(translation.simplified()); } -KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other ) : d( new KEduVocTranslationPrivate(other.d->m_entry) ) +KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other ) + : d( new KEduVocTranslationPrivate(other.d->m_entry) ), + KEduVocText(other) { - d->m_translation = other.d->m_translation; d->m_wordType = other.d->m_wordType; d->m_comment = other.d->m_comment; d->m_paraphrase = other.d->m_paraphrase; @@ -119,7 +110,6 @@ KEduVocTranslation::KEduVocTranslation( const KEduVocTranslation &other ) : d( n d->m_comparative = other.d->m_comparative; d->m_superlative = other.d->m_superlative; d->m_multipleChoice = other.d->m_multipleChoice; - d->m_grades = other.d->m_grades; d->m_falseFriends = other.d->m_falseFriends; d->m_imageUrl = other.d->m_imageUrl; d->m_soundUrl = other.d->m_soundUrl; @@ -132,12 +122,9 @@ KEduVocTranslation::~KEduVocTranslation() delete d; } - bool KEduVocTranslation::operator == ( const KEduVocTranslation & translation ) const { - return d->m_entry == translation.d->m_entry && - d->m_translation == translation.d->m_translation && - d->m_wordType == translation.d->m_wordType && + return d->m_wordType == translation.d->m_wordType && d->m_comment == translation.d->m_comment && d->m_paraphrase == translation.d->m_paraphrase && d->m_synonym == translation.d->m_synonym && @@ -150,15 +137,13 @@ bool KEduVocTranslation::operator == ( const KEduVocTranslation & translation ) d->m_superlative == translation.d->m_superlative && d->m_multipleChoice == translation.d->m_multipleChoice && d->m_falseFriends == translation.d->m_falseFriends && - d->m_conjugations == translation.d->m_conjugations && - d->m_grades == translation.d->m_grades; + d->m_conjugations == translation.d->m_conjugations; } - KEduVocTranslation & KEduVocTranslation::operator = ( const KEduVocTranslation & translation ) { + KEduVocText::operator=(translation); d->m_entry = translation.d->m_entry; - d->m_translation = translation.d->m_translation; d->m_wordType = translation.d->m_wordType; d->m_comment = translation.d->m_comment; d->m_paraphrase = translation.d->m_paraphrase; @@ -172,24 +157,11 @@ KEduVocTranslation & KEduVocTranslation::operator = ( const KEduVocTranslation & d->m_superlative = translation.d->m_superlative; d->m_multipleChoice = translation.d->m_multipleChoice; d->m_falseFriends = translation.d->m_falseFriends; - d->m_grades == translation.d->m_grades; d->m_conjugations = translation.d->m_conjugations; return *this; } -QString KEduVocTranslation::text() const -{ - return d->m_translation; -} - - -void KEduVocTranslation::setText( const QString & expr ) -{ - d->m_translation = expr.simplified(); -} - - QString KEduVocTranslation::comment() const { return d->m_comment; @@ -291,16 +263,6 @@ void KEduVocTranslation::setPronunciation( const QString & expr ) d->m_pronunciation = expr.simplified(); } -void KEduVocTranslation::resetGrades() -{ - d->m_grades.clear(); -} - -KEduVocGrade& KEduVocTranslation::gradeFrom( int indexFrom ) -{ - return d->m_grades[indexFrom]; -} - QStringList KEduVocTranslation::conjugationTenses() const { return d->m_conjugations.keys(); diff --git a/keduvocdocument/keduvoctranslation.h b/keduvocdocument/keduvoctranslation.h index 425c63a..35b0589 100644 --- a/keduvocdocument/keduvoctranslation.h +++ b/keduvocdocument/keduvoctranslation.h @@ -24,13 +24,14 @@ #include class KEduVocExpression; -class KEduVocGrade; +class KEduVocString; class KEduVocWordType; /** @author Frederik Gladhorn */ class KEDUVOCDOCUMENT_EXPORT KEduVocTranslation + :public KEduVocText { public: /** @@ -54,18 +55,6 @@ public: KEduVocExpression* entry(); - /** - * The translation as string (the word itself) - * @return the translation - */ - QString text() const; - - /** - * Sets the translation - * @param expr - */ - void setText( const QString & expr ); - /** sets the pronunciation of this expression * @param expression pronunciation of this index @@ -163,16 +152,6 @@ public: */ void setWordType( KEduVocWordType* wordType ); - - /** reset the grades for this translation */ - void resetGrades(); - - /** get the gradeobject from given identifier - * @param indexFrom which identifier to get the grade from - * @returns the grade object - */ - KEduVocGrade & gradeFrom( int indexFrom ); - /** returns a conjugation if available */ KEduVocConjugation& conjugation( const QString& tense ); diff --git a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp index 37f49c9..752d755 100644 --- a/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp +++ b/keduvocdocument/tests/keduvocdocumentvalidatortest.cpp @@ -42,6 +42,7 @@ private slots: void testDocumentAboutInfo(); void testLessons(); void testWordTypes(); + void testTranslations(); }; void KEduVocDocumentValidatorTest::testDocumentAboutInfo() @@ -155,6 +156,21 @@ void KEduVocDocumentValidatorTest::testWordTypes() QCOMPARE(doc.wordTypeContainer()->childContainerCount(), 1); } +void KEduVocDocumentValidatorTest::testTranslations() +{ + KEduVocTranslation *trans1 = new KEduVocTranslation(0, QString("My word")); + QCOMPARE(trans1->text(), QString("My word")); + + // copy ctor + KEduVocTranslation *trans2 = new KEduVocTranslation(*trans1); + QCOMPARE(trans2->text(), QString("My word")); + + // operator = + KEduVocTranslation *trans3 = new KEduVocTranslation(0); + trans3 = trans1; + QCOMPARE(trans3->text(), QString("My word")); +} + QTEST_KDEMAIN_CORE( KEduVocDocumentValidatorTest ) #include "keduvocdocumentvalidatortest.moc" -- 2.47.3