LibKEduVocDocument

keduvoctext.cpp
1/*
2 * SPDX-FileCopyrightText: 2007-2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "keduvoctext.h"
7
8#include "keduvockvtml2writer.h"
9#include "kvtml2defs.h"
10
11#include <QDomDocument>
12
13class KEduVocText::KEduVocTextPrivate
14{
15public:
16 /// This is the word itself. The vocabulary. This is what it is all about.
17 QString m_text;
18
19 grade_t m_preGrade; // Before it gets to grade 1.
20 grade_t m_grade;
21 count_t m_totalPracticeCount;
22 count_t m_badCount;
23 QDateTime m_practiceDate;
24 quint32 m_interval; // Interval in seconds until next training is due.
25};
26
28 : d(new KEduVocTextPrivate)
29{
30 d->m_text = text;
32}
33
35 : d(new KEduVocTextPrivate)
36{
37 d->m_text = other.d->m_text;
38 setPreGrade(other.preGrade());
39 setGrade(other.grade());
41 setBadCount(other.badCount());
43 setInterval(other.interval());
44}
45
47{
48 delete d;
49}
50
52{
53 return d->m_text;
54}
55
57{
58 d->m_text = expr.simplified();
59}
60
62{
63 d->m_preGrade = KV_NORM_GRADE;
64 d->m_grade = KV_NORM_GRADE;
65 d->m_totalPracticeCount = 0;
66 d->m_badCount = 0;
67
69 d->m_practiceDate = dt;
70 d->m_interval = 0;
71}
72
73grade_t KEduVocText::preGrade() const
74{
75 return d->m_preGrade;
76}
77
78void KEduVocText::setPreGrade(grade_t grade)
79{
80 if (grade > KV_MAX_GRADE) {
81 grade = KV_MAX_GRADE;
82 }
83 d->m_preGrade = grade;
84}
85
86grade_t KEduVocText::grade() const
87{
88 return d->m_grade;
89}
90
91void KEduVocText::setGrade(grade_t grade)
92{
93 if (grade > KV_MAX_GRADE) {
94 grade = KV_MAX_GRADE;
95 }
96 d->m_grade = grade;
97}
98
100{
101 setGrade(qMax<grade_t>(grade() + 1, KV_LEV1_GRADE));
102}
103
105{
106 if (grade() == KV_MIN_GRADE) {
107 return;
108 }
109 setGrade(grade() - 1);
110}
111
113{
114 return d->m_totalPracticeCount;
115}
116
121
123{
124 setBadCount(badCount() + 1);
125}
126
128{
129 d->m_totalPracticeCount = count;
130}
131
133{
134 return d->m_badCount;
135}
136
137void KEduVocText::setBadCount(count_t count)
138{
139 d->m_badCount = count;
140}
141
143{
144 return d->m_practiceDate;
145}
146
148{
149 d->m_practiceDate = date;
150}
151
153{
154 return d->m_interval;
155}
156
157void KEduVocText::setInterval(quint32 interval)
158{
159 d->m_interval = interval;
160}
161
163{
164 d->m_text = other.d->m_text;
165 d->m_preGrade = other.d->m_preGrade;
166 d->m_grade = other.d->m_grade;
167 d->m_totalPracticeCount = other.d->m_totalPracticeCount;
168 d->m_badCount = other.d->m_badCount;
169 d->m_practiceDate = other.d->m_practiceDate;
170 d->m_interval = other.d->m_interval;
171
172 return *this;
173}
174
175bool KEduVocText::operator==(const KEduVocText &other) const
176{
177 return d->m_text == other.d->m_text && d->m_preGrade == other.d->m_preGrade && d->m_grade == other.d->m_grade
178 && d->m_totalPracticeCount == other.d->m_totalPracticeCount && d->m_badCount == other.d->m_badCount && d->m_practiceDate == other.d->m_practiceDate
179 && d->m_interval == other.d->m_interval;
180}
181
182void KEduVocText::toKVTML2(QDomElement &parent)
183{
184 QDomDocument domDoc = parent.ownerDocument();
185 if (d->m_text.isEmpty() && d->m_totalPracticeCount == 0) {
186 return;
187 }
188
189 // the text
190 KEduVocKvtml2Writer::appendTextElement(parent, KVTML_TEXT, text());
191
192 // grades
193 if (d->m_totalPracticeCount > 0) {
194 QDomElement gradeElement = domDoc.createElement(KVTML_GRADE);
195
196 //<pregrade>2</pregrade>
197 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_PREGRADE, QString::number(preGrade()));
198
199 //<currentgrade>2</currentgrade>
200 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_CURRENTGRADE, QString::number(grade()));
201
202 //<count>6</count>
204
205 //<errorcount>1</errorcount>
206 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_ERRORCOUNT, QString::number(badCount()));
207
208 //<date>949757271</date>
210
211 //<interval>300</interval>
212 KEduVocKvtml2Writer::appendTextElement(gradeElement, KVTML_INTERVAL, QString::number(interval()));
213
214 parent.appendChild(gradeElement);
215 }
216}
217
218void KEduVocText::fromKVTML2(QDomElement &parent)
219{
220 setText(parent.firstChildElement(KVTML_TEXT).text());
221
222 // grade element
223 const QDomElement &gradeElement = parent.firstChildElement(KVTML_GRADE);
224 if (!gradeElement.isNull()) {
225 setPreGrade(gradeElement.firstChildElement(KVTML_PREGRADE).text().toInt());
226 setGrade(gradeElement.firstChildElement(KVTML_CURRENTGRADE).text().toInt());
227
228 setPracticeCount(gradeElement.firstChildElement(KVTML_COUNT).text().toInt());
229
230 setBadCount(gradeElement.firstChildElement(KVTML_ERRORCOUNT).text().toInt());
231
232 QString dateString = gradeElement.firstChildElement(KVTML_DATE).text();
233 if (!dateString.isEmpty()) {
234 QDateTime value = QDateTime::fromString(dateString, Qt::ISODate);
235 setPracticeDate(value);
236 }
237 setInterval(gradeElement.firstChildElement(KVTML_INTERVAL).text().toInt());
238 }
239}
240
242{
243 return d->m_text.isEmpty();
244}
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.
A text in vocabulary documents.
Definition keduvoctext.h:50
void setPracticeCount(count_t count)
set how often this entry has been practiced as int
bool operator==(const KEduVocText &other) const
Compare two sets of grades.
grade_t grade() const
returns grade as int
count_t practiceCount() const
returns how often this entry has been practiced as int
void setPreGrade(grade_t grade)
sets the pregrade
count_t badCount() const
returns bad query count as int
bool isEmpty()
If the string inside is empty this returns true.
QDateTime practiceDate() const
returns last practice date as int
void setBadCount(count_t count)
set bad query count as int
QString text() const
The translation as string (the word itself)
KEduVocText & operator=(const KEduVocText &other)
Equal operator to copy grades.
void setInterval(quint32 interval)
Set interval until next practice is due.
void resetGrades()
Clears grading and date information.
~KEduVocText()
default destructor
quint32 interval() const
returns interval until next practice is due
void setPracticeDate(const QDateTime &date)
Set last query date.
grade_t preGrade() const
returns pregrade
KEduVocText(const QString &text=QString())
default constructor
void incPracticeCount()
increment query count of given translation by 1
void incBadCount()
increment bad query count of given translation by 1
void incGrade()
increments grade
void setText(const QString &expr)
Sets the translation.
void setGrade(grade_t grade)
sets the grade
void decGrade()
decrements grade
char * toString(const EngineQuery &query)
QDateTime fromSecsSinceEpoch(qint64 secs)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
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 isNull() const const
QDomDocument ownerDocument() const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QString simplified() const const
int toInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.