libkdeedu/keduvocdocument
keduvoctext.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "keduvoctext.h"
00015 #include "kvtml2defs.h"
00016 #include "keduvockvtml2writer.h"
00017
00018 #include <KDE/KDebug>
00019 #include <QtXml/QDomDocument>
00020
00021 class KEduVocText::KEduVocTextPrivate
00022 {
00023 public:
00025 QString m_text;
00026
00027 grade_t m_grade;
00028 count_t m_totalPracticeCount;
00029 count_t m_badCount;
00030 QDateTime m_practiceDate;
00031 };
00032
00033 KEduVocText::KEduVocText(const QString& text)
00034 :d( new KEduVocTextPrivate )
00035 {
00036 d->m_text = text;
00037 resetGrades();
00038 }
00039
00040 KEduVocText::KEduVocText( const KEduVocText &other )
00041 :d( new KEduVocTextPrivate )
00042 {
00043 d->m_text = other.d->m_text;
00044 setGrade( other.grade() );
00045 setPracticeCount( other.practiceCount() );
00046 setBadCount( other.badCount() );
00047 setPracticeDate( other.practiceDate() );
00048 }
00049
00050 KEduVocText::~KEduVocText()
00051 {
00052 delete d;
00053 }
00054
00055 QString KEduVocText::text() const
00056 {
00057 return d->m_text;
00058 }
00059
00060 void KEduVocText::setText( const QString & expr )
00061 {
00062 d->m_text = expr.simplified();
00063 }
00064
00065 void KEduVocText::resetGrades()
00066 {
00067 d->m_grade = KV_NORM_GRADE;
00068 d->m_totalPracticeCount = 0;
00069 d->m_badCount = 0;
00070
00071 QDateTime dt;
00072 dt.setTime_t( 0 );
00073 d->m_practiceDate = dt;
00074 }
00075
00076
00077 grade_t KEduVocText::grade() const
00078 {
00079 return d->m_grade;
00080 }
00081
00082
00083 void KEduVocText::setGrade( grade_t grade )
00084 {
00085 if ( grade > KV_MAX_GRADE ) {
00086 grade = KV_MAX_GRADE;
00087 }
00088 d->m_grade = grade;
00089 }
00090
00091
00092 void KEduVocText::incGrade()
00093 {
00094 setGrade( grade() + 1 );
00095 }
00096
00097
00098 void KEduVocText::decGrade()
00099 {
00100 if ( grade() == KV_MIN_GRADE ) {
00101 return;
00102 }
00103 setGrade( grade() - 1 );
00104 }
00105
00106
00107 count_t KEduVocText::practiceCount() const
00108 {
00109 return d->m_totalPracticeCount;
00110 }
00111
00112
00113 void KEduVocText::incPracticeCount()
00114 {
00115 setPracticeCount( practiceCount() + 1 );
00116 }
00117
00118
00119 void KEduVocText::incBadCount()
00120 {
00121 setBadCount( badCount() + 1 );
00122 }
00123
00124
00125 void KEduVocText::setPracticeCount( count_t count )
00126 {
00127 d->m_totalPracticeCount = count;
00128 }
00129
00130
00131 count_t KEduVocText::badCount() const
00132 {
00133 return d->m_badCount;
00134 }
00135
00136
00137 void KEduVocText::setBadCount( count_t count )
00138 {
00139 d->m_badCount = count;
00140 }
00141
00142
00143 QDateTime KEduVocText::practiceDate() const
00144 {
00145 return d->m_practiceDate;
00146 }
00147
00148
00149 void KEduVocText::setPracticeDate( const QDateTime & date )
00150 {
00151 d->m_practiceDate = date;
00152 }
00153
00154 KEduVocText & KEduVocText::operator =(const KEduVocText & other)
00155 {
00156 d->m_text = other.d->m_text;
00157 d->m_grade = other.d->m_grade;
00158 d->m_totalPracticeCount = other.d->m_totalPracticeCount;
00159 d->m_badCount = other.d->m_badCount;
00160 d->m_practiceDate = other.d->m_practiceDate;
00161
00162 return *this;
00163 }
00164
00165 bool KEduVocText::operator ==(const KEduVocText & other) const
00166 {
00167 return
00168 d->m_text == other.d->m_text &&
00169 d->m_grade == other.d->m_grade &&
00170 d->m_totalPracticeCount == other.d->m_totalPracticeCount &&
00171 d->m_badCount == other.d->m_badCount &&
00172 d->m_practiceDate == other.d->m_practiceDate;
00173 }
00174
00175 void KEduVocText::toKVTML2(QDomElement& parent)
00176 {
00177 QDomDocument domDoc = parent.ownerDocument();
00178 if (text().isEmpty()) {
00179 return;
00180 }
00181
00182
00183 KEduVocKvtml2Writer::appendTextElement( parent, KVTML_TEXT, text() );
00184
00185
00186 if ( practiceCount() > 0 ) {
00187 QDomElement gradeElement = domDoc.createElement( KVTML_GRADE );
00188
00189
00190 KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_CURRENTGRADE, QString::number( grade() ) );
00191
00192
00193 KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_COUNT, QString::number( practiceCount() ) );
00194
00195
00196 KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_ERRORCOUNT, QString::number( badCount() ) );
00197
00198
00199 KEduVocKvtml2Writer::appendTextElement( gradeElement, KVTML_DATE, practiceDate().toString( Qt::ISODate ) );
00200
00201 parent.appendChild( gradeElement );
00202 }
00203 }
00204
00205 void KEduVocText::fromKVTML2(QDomElement & parent)
00206 {
00207 setText( parent.firstChildElement( KVTML_TEXT ).text() );
00208
00209
00210 const QDomElement& gradeElement = parent.firstChildElement( KVTML_GRADE );
00211 if ( !gradeElement.isNull() ) {
00212
00213 setGrade( gradeElement.firstChildElement(KVTML_CURRENTGRADE).text().toInt() );
00214
00215 setPracticeCount( gradeElement.firstChildElement(KVTML_COUNT).text().toInt() );
00216
00217 setBadCount( gradeElement.firstChildElement(KVTML_ERRORCOUNT).text().toInt() );
00218
00219 QString dateString = gradeElement.firstChildElement(KVTML_DATE).text();
00220 if ( !dateString.isEmpty() ) {
00221 QDateTime value = QDateTime::fromString( dateString, Qt::ISODate );
00222 setPracticeDate( value );
00223 }
00224 }
00225 }
00226
00227 bool KEduVocText::isEmpty()
00228 {
00229 return d->m_text.isEmpty();
00230 }