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

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "keduvoctext.h"
0007 
0008 #include "kvtml2defs.h"
0009 #include "keduvockvtml2writer.h"
0010 
0011 #include <QDomDocument>
0012 
0013 class KEduVocText::KEduVocTextPrivate
0014 {
0015 public:
0016     /// This is the word itself. The vocabulary. This is what it is all about.
0017     QString m_text;
0018 
0019     grade_t m_preGrade;         // Before it gets to grade 1.
0020     grade_t m_grade;
0021     count_t m_totalPracticeCount;
0022     count_t m_badCount;
0023     QDateTime m_practiceDate;
0024     quint32 m_interval;         // Interval in seconds until next training is due.
0025 };
0026 
0027 KEduVocText::KEduVocText(const QString& text)
0028         :d( new KEduVocTextPrivate )
0029 {
0030     d->m_text = text;
0031     resetGrades();
0032 }
0033 
0034 KEduVocText::KEduVocText( const KEduVocText &other )
0035     : d( new KEduVocTextPrivate )
0036 {
0037     d->m_text = other.d->m_text;
0038     setPreGrade( other.preGrade() );
0039     setGrade( other.grade() );
0040     setPracticeCount( other.practiceCount() );
0041     setBadCount( other.badCount() );
0042     setPracticeDate( other.practiceDate() );
0043     setInterval( other.interval() );
0044 }
0045 
0046 KEduVocText::~KEduVocText()
0047 {
0048     delete d;
0049 }
0050 
0051 QString KEduVocText::text() const
0052 {
0053     return d->m_text;
0054 }
0055 
0056 void KEduVocText::setText( const QString & expr )
0057 {
0058     d->m_text = expr.simplified();
0059 }
0060 
0061 void KEduVocText::resetGrades()
0062 {
0063     d->m_preGrade = KV_NORM_GRADE;
0064     d->m_grade = KV_NORM_GRADE;
0065     d->m_totalPracticeCount = 0;
0066     d->m_badCount = 0;
0067 
0068     const QDateTime dt = QDateTime::fromSecsSinceEpoch(0);
0069     d->m_practiceDate = dt;
0070     d->m_interval = 0;
0071 }
0072 
0073 
0074 grade_t KEduVocText::preGrade() const
0075 {
0076     return d->m_preGrade;
0077 }
0078 
0079 
0080 void KEduVocText::setPreGrade( grade_t grade )
0081 {
0082     if ( grade > KV_MAX_GRADE ) {
0083         grade = KV_MAX_GRADE;
0084     }
0085     d->m_preGrade = grade;
0086 }
0087 
0088 
0089 grade_t KEduVocText::grade() const
0090 {
0091     return d->m_grade;
0092 }
0093 
0094 
0095 void KEduVocText::setGrade( grade_t grade )
0096 {
0097     if ( grade > KV_MAX_GRADE ) {
0098         grade = KV_MAX_GRADE;
0099     }
0100     d->m_grade = grade;
0101 }
0102 
0103 
0104 void KEduVocText::incGrade()
0105 {
0106     setGrade( qMax<grade_t>(grade() + 1, KV_LEV1_GRADE ) );
0107 }
0108 
0109 
0110 void KEduVocText::decGrade()
0111 {
0112     if ( grade() == KV_MIN_GRADE ) {
0113         return;
0114     }
0115     setGrade( grade() - 1 );
0116 }
0117 
0118 
0119 count_t KEduVocText::practiceCount()  const
0120 {
0121     return d->m_totalPracticeCount;
0122 }
0123 
0124 
0125 void KEduVocText::incPracticeCount()
0126 {
0127     setPracticeCount( practiceCount() + 1 );
0128 }
0129 
0130 
0131 void KEduVocText::incBadCount()
0132 {
0133     setBadCount( badCount() + 1 );
0134 }
0135 
0136 
0137 void KEduVocText::setPracticeCount( count_t count )
0138 {
0139     d->m_totalPracticeCount = count;
0140 }
0141 
0142 
0143 count_t KEduVocText::badCount() const
0144 {
0145     return d->m_badCount;
0146 }
0147 
0148 
0149 void KEduVocText::setBadCount( count_t count )
0150 {
0151     d->m_badCount = count;
0152 }
0153 
0154 
0155 QDateTime KEduVocText::practiceDate() const
0156 {
0157     return d->m_practiceDate;
0158 }
0159 
0160 
0161 void KEduVocText::setPracticeDate( const QDateTime & date )
0162 {
0163     d->m_practiceDate = date;
0164 }
0165 
0166 quint32 KEduVocText::interval() const
0167 {
0168     return d->m_interval;
0169 }
0170 
0171 
0172 void KEduVocText::setInterval( quint32 interval )
0173 {
0174     d->m_interval = interval;
0175 }
0176 
0177 
0178 KEduVocText &KEduVocText::operator=(const KEduVocText &other)
0179 {
0180     d->m_text = other.d->m_text;
0181     d->m_preGrade = other.d->m_preGrade;
0182     d->m_grade = other.d->m_grade;
0183     d->m_totalPracticeCount = other.d->m_totalPracticeCount;
0184     d->m_badCount = other.d->m_badCount;
0185     d->m_practiceDate = other.d->m_practiceDate;
0186     d->m_interval = other.d->m_interval;
0187 
0188     return *this;
0189 }
0190 
0191 bool KEduVocText::operator==(const KEduVocText &other) const
0192 {
0193     return
0194         d->m_text == other.d->m_text &&
0195         d->m_preGrade == other.d->m_preGrade &&
0196         d->m_grade == other.d->m_grade &&
0197         d->m_totalPracticeCount == other.d->m_totalPracticeCount &&
0198         d->m_badCount == other.d->m_badCount &&
0199         d->m_practiceDate == other.d->m_practiceDate &&
0200         d->m_interval == other.d->m_interval;
0201 }
0202 
0203 void KEduVocText::toKVTML2(QDomElement& parent)
0204 {
0205     QDomDocument domDoc = parent.ownerDocument();
0206     if (d->m_text.isEmpty() && d->m_totalPracticeCount == 0) {
0207         return;
0208     }
0209 
0210     // the text
0211     KEduVocKvtml2Writer::appendTextElement( parent, KVTML_TEXT, text() );
0212 
0213     // grades
0214     if ( d->m_totalPracticeCount > 0 ) {
0215         QDomElement gradeElement = domDoc.createElement( KVTML_GRADE );
0216 
0217             //<pregrade>2</pregrade>
0218         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_PREGRADE, QString::number( preGrade() ) );
0219 
0220             //<currentgrade>2</currentgrade>
0221         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_CURRENTGRADE, QString::number( grade() ) );
0222 
0223             //<count>6</count>
0224         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_COUNT, QString::number( practiceCount() ) );
0225 
0226             //<errorcount>1</errorcount>
0227         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_ERRORCOUNT, QString::number( badCount() ) );
0228 
0229             //<date>949757271</date>
0230         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_DATE,  practiceDate().toString( Qt::ISODate ) );
0231 
0232             //<interval>300</interval>
0233         KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_INTERVAL,  QString::number(interval()) );
0234 
0235 
0236         parent.appendChild( gradeElement );
0237     }
0238 }
0239 
0240 void KEduVocText::fromKVTML2(QDomElement & parent)
0241 {
0242     setText( parent.firstChildElement( KVTML_TEXT ).text() );
0243 
0244     // grade element
0245     const QDomElement& gradeElement = parent.firstChildElement( KVTML_GRADE );
0246     if ( !gradeElement.isNull() ) {
0247 
0248         setPreGrade( gradeElement.firstChildElement(KVTML_PREGRADE).text().toInt() );
0249         setGrade( gradeElement.firstChildElement(KVTML_CURRENTGRADE).text().toInt() );
0250 
0251         setPracticeCount( gradeElement.firstChildElement(KVTML_COUNT).text().toInt() );
0252 
0253         setBadCount( gradeElement.firstChildElement(KVTML_ERRORCOUNT).text().toInt() );
0254 
0255         QString dateString = gradeElement.firstChildElement(KVTML_DATE).text();
0256         if ( !dateString.isEmpty() ) {
0257             QDateTime value = QDateTime::fromString( dateString, Qt::ISODate );
0258             setPracticeDate( value );
0259         }
0260         setInterval( gradeElement.firstChildElement(KVTML_INTERVAL).text().toInt() );
0261     }
0262 }
0263 
0264 bool KEduVocText::isEmpty()
0265 {
0266     return d->m_text.isEmpty();
0267 }