]> Git trees. - libqmvoc.git/commitdiff
Change the way grades are handled fundamentally.
authorFrederik Gladhorn <gladhorn@kde.org>
Mon, 4 Feb 2008 21:20:42 +0000 (21:20 +0000)
committerFrederik Gladhorn <gladhorn@kde.org>
Mon, 4 Feb 2008 21:20:42 +0000 (21:20 +0000)
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
keduvocdocument/keduvocconjugation.h
keduvocdocument/keduvocgrade.cpp
keduvocdocument/keduvocgrade.h
keduvocdocument/keduvockvtml2reader.cpp
keduvocdocument/keduvockvtml2writer.cpp
keduvocdocument/keduvockvtmlreader.cpp
keduvocdocument/keduvoctranslation.cpp
keduvocdocument/keduvoctranslation.h
keduvocdocument/tests/keduvocdocumentvalidatortest.cpp

index 0c145d81c5c73ff70a0dfcce7e3f850490bb2bc1..325b01f45542016e8fa8627d362ab0f413393c3c 100644 (file)
@@ -29,7 +29,6 @@
 class KEduVocConjugation::Private
 {
 public:
-    KEduVocGrade m_grade;
     QMap<int, QString> 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();
index 69726aa7abeb20e90aa27bade97c72f5b28522e8..1867d46417f34baf3138f605678f559f8ff95c42 100644 (file)
@@ -76,8 +76,6 @@ public:
 
     bool isEmpty();
 
-    KEduVocGrade &grade();
-
     static int indexOf(ConjugationPerson person, ConjugationNumber number);
 
 private:
index c78e850a549525dcea70d70e5d8450abc4efc61a..5e18f187dd82e78575cc3e8d49c1e02643b02464 100644 (file)
 
 #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 &&
index 11f8f6cb5664fc7bcbf5f227afe4e534d569787e..700d8a0f59e58dd420f2278af1be28f7bab3b42b 100644 (file)
@@ -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 <frederik.gladhorn@kdemail.net>
 */
-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
index 9191694f0309c0172e1cd2ca4c6ce92d7c86ec5a..247cd69d7dce269911274c612eb9c6feba1b3e90 100644 (file)
@@ -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 );
         }
     }
 
index 048e23481931afbb70d605482828fddc9891833c..b01d461a46ea58598c58d7546e9d79e2078f176a 100644 (file)
@@ -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 ) );
-            //<currentgrade>2</currentgrade>
-            gradeElement.appendChild( newTextElement( KVTML_CURRENTGRADE, QString::number( thisGrade.grade() ) ) );
+    if ( translation->practiceCount() > 0 ) {
+        QDomElement gradeElement = m_domDoc.createElement( KVTML_GRADE );
 
-            //<count>6</count>
-            gradeElement.appendChild( newTextElement( KVTML_COUNT, QString::number( thisGrade.practiceCount() ) ) );
+        //<currentgrade>2</currentgrade>
+        gradeElement.appendChild( newTextElement( KVTML_CURRENTGRADE, QString::number( translation->grade() ) ) );
 
-            //<errorcount>1</errorcount>
-            gradeElement.appendChild( newTextElement( KVTML_ERRORCOUNT, QString::number( thisGrade.badCount() ) ) );
+        //<count>6</count>
+        gradeElement.appendChild( newTextElement( KVTML_COUNT, QString::number( translation->practiceCount() ) ) );
 
-            //<date>949757271</date>
-            gradeElement.appendChild( newTextElement( KVTML_DATE,  thisGrade.practiceDate().toString( Qt::ISODate ) ) );
+        //<errorcount>1</errorcount>
+        gradeElement.appendChild( newTextElement( KVTML_ERRORCOUNT, QString::number( translation->badCount() ) ) );
 
-            translationElement.appendChild( gradeElement );
-        }
+        //<date>949757271</date>
+        gradeElement.appendChild( newTextElement( KVTML_DATE,  translation->practiceDate().toString( Qt::ISODate ) ) );
+
+        translationElement.appendChild( gradeElement );
     }
 
     // conjugation
index ae4bb311f2824f89e9de6f00cb30200532f96a2d..88ca25f1c9d8c20754a6439199079e96693279b0 100644 (file)
@@ -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
index c387acdcd78cf89b28c5aca72f1e48d131bae7a3..73b824c3e8e20a47f384a5596c03eaf36b59902c 100644 (file)
@@ -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<int, KEduVocGrade> m_grades;
-
     /// One false friend string per other language
     QMap<int, QString> 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();
index 425c63a162e5ad655a6e952443b0e3e412961ae9..35b0589197d0831263421b4a0b387c2fceb9b580 100644 (file)
 #include <QtCore/QString>
 
 class KEduVocExpression;
-class KEduVocGrade;
+class KEduVocString;
 class KEduVocWordType;
 
 /**
  @author Frederik Gladhorn <frederik.gladhorn@kdemail.net>
 */
 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 );
index 37f49c9880c59c3bc5b20b86ce30a39c8540cea9..752d755d02a0c60a0f8a7517b0877cfebf618eb3 100644 (file)
@@ -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"