File indexing completed on 2024-04-21 03:48:22
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 "keduvockvtml2writer.h" 0009 #include "kvtml2defs.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 grade_t KEduVocText::preGrade() const 0074 { 0075 return d->m_preGrade; 0076 } 0077 0078 void KEduVocText::setPreGrade(grade_t grade) 0079 { 0080 if (grade > KV_MAX_GRADE) { 0081 grade = KV_MAX_GRADE; 0082 } 0083 d->m_preGrade = grade; 0084 } 0085 0086 grade_t KEduVocText::grade() const 0087 { 0088 return d->m_grade; 0089 } 0090 0091 void KEduVocText::setGrade(grade_t grade) 0092 { 0093 if (grade > KV_MAX_GRADE) { 0094 grade = KV_MAX_GRADE; 0095 } 0096 d->m_grade = grade; 0097 } 0098 0099 void KEduVocText::incGrade() 0100 { 0101 setGrade(qMax<grade_t>(grade() + 1, KV_LEV1_GRADE)); 0102 } 0103 0104 void KEduVocText::decGrade() 0105 { 0106 if (grade() == KV_MIN_GRADE) { 0107 return; 0108 } 0109 setGrade(grade() - 1); 0110 } 0111 0112 count_t KEduVocText::practiceCount() const 0113 { 0114 return d->m_totalPracticeCount; 0115 } 0116 0117 void KEduVocText::incPracticeCount() 0118 { 0119 setPracticeCount(practiceCount() + 1); 0120 } 0121 0122 void KEduVocText::incBadCount() 0123 { 0124 setBadCount(badCount() + 1); 0125 } 0126 0127 void KEduVocText::setPracticeCount(count_t count) 0128 { 0129 d->m_totalPracticeCount = count; 0130 } 0131 0132 count_t KEduVocText::badCount() const 0133 { 0134 return d->m_badCount; 0135 } 0136 0137 void KEduVocText::setBadCount(count_t count) 0138 { 0139 d->m_badCount = count; 0140 } 0141 0142 QDateTime KEduVocText::practiceDate() const 0143 { 0144 return d->m_practiceDate; 0145 } 0146 0147 void KEduVocText::setPracticeDate(const QDateTime &date) 0148 { 0149 d->m_practiceDate = date; 0150 } 0151 0152 quint32 KEduVocText::interval() const 0153 { 0154 return d->m_interval; 0155 } 0156 0157 void KEduVocText::setInterval(quint32 interval) 0158 { 0159 d->m_interval = interval; 0160 } 0161 0162 KEduVocText &KEduVocText::operator=(const KEduVocText &other) 0163 { 0164 d->m_text = other.d->m_text; 0165 d->m_preGrade = other.d->m_preGrade; 0166 d->m_grade = other.d->m_grade; 0167 d->m_totalPracticeCount = other.d->m_totalPracticeCount; 0168 d->m_badCount = other.d->m_badCount; 0169 d->m_practiceDate = other.d->m_practiceDate; 0170 d->m_interval = other.d->m_interval; 0171 0172 return *this; 0173 } 0174 0175 bool KEduVocText::operator==(const KEduVocText &other) const 0176 { 0177 return d->m_text == other.d->m_text && d->m_preGrade == other.d->m_preGrade && d->m_grade == other.d->m_grade 0178 && d->m_totalPracticeCount == other.d->m_totalPracticeCount && d->m_badCount == other.d->m_badCount && d->m_practiceDate == other.d->m_practiceDate 0179 && d->m_interval == other.d->m_interval; 0180 } 0181 0182 void KEduVocText::toKVTML2(QDomElement &parent) 0183 { 0184 QDomDocument domDoc = parent.ownerDocument(); 0185 if (d->m_text.isEmpty() && d->m_totalPracticeCount == 0) { 0186 return; 0187 } 0188 0189 // the text 0190 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_TEXT, text()); 0191 0192 // grades 0193 if (d->m_totalPracticeCount > 0) { 0194 QDomElement gradeElement = domDoc.createElement(KVTML_GRADE); 0195 0196 //<pregrade>2</pregrade> 0197 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_PREGRADE, QString::number(preGrade())); 0198 0199 //<currentgrade>2</currentgrade> 0200 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_CURRENTGRADE, QString::number(grade())); 0201 0202 //<count>6</count> 0203 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_COUNT, QString::number(practiceCount())); 0204 0205 //<errorcount>1</errorcount> 0206 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_ERRORCOUNT, QString::number(badCount())); 0207 0208 //<date>949757271</date> 0209 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_DATE, practiceDate().toString(Qt::ISODate)); 0210 0211 //<interval>300</interval> 0212 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_INTERVAL, QString::number(interval())); 0213 0214 parent.appendChild(gradeElement); 0215 } 0216 } 0217 0218 void KEduVocText::fromKVTML2(QDomElement &parent) 0219 { 0220 setText(parent.firstChildElement(KVTML_TEXT).text()); 0221 0222 // grade element 0223 const QDomElement &gradeElement = parent.firstChildElement(KVTML_GRADE); 0224 if (!gradeElement.isNull()) { 0225 setPreGrade(gradeElement.firstChildElement(KVTML_PREGRADE).text().toInt()); 0226 setGrade(gradeElement.firstChildElement(KVTML_CURRENTGRADE).text().toInt()); 0227 0228 setPracticeCount(gradeElement.firstChildElement(KVTML_COUNT).text().toInt()); 0229 0230 setBadCount(gradeElement.firstChildElement(KVTML_ERRORCOUNT).text().toInt()); 0231 0232 QString dateString = gradeElement.firstChildElement(KVTML_DATE).text(); 0233 if (!dateString.isEmpty()) { 0234 QDateTime value = QDateTime::fromString(dateString, Qt::ISODate); 0235 setPracticeDate(value); 0236 } 0237 setInterval(gradeElement.firstChildElement(KVTML_INTERVAL).text().toInt()); 0238 } 0239 } 0240 0241 bool KEduVocText::isEmpty() 0242 { 0243 return d->m_text.isEmpty(); 0244 }