parley
entryfilter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "entryfilter.h"
00015
00016 #include "prefs.h"
00017
00018 #include <keduvocexpression.h>
00019 #include <keduvoctranslation.h>
00020 #include <keduvocdocument.h>
00021 #include <keduvocwordtype.h>
00022
00023 #include <KLocalizedString>
00024 #include <KMessageBox>
00025 #include <KDialog>
00026
00027 EntryFilter::EntryFilter(QObject * parent, KEduVocDocument* doc) :QObject(parent)
00028 {
00029 m_doc = doc;
00030 m_dialog = 0;
00031 m_fromTranslation = Prefs::questionLanguage();
00032 m_toTranslation = Prefs::solutionLanguage();
00033 expireEntries();
00034 }
00035
00036 EntryFilter::~ EntryFilter()
00037 {
00038 }
00039
00040 void EntryFilter::expireEntries()
00041 {
00042 if ( Prefs::expire() ) {
00043 int counter = 0;
00044 foreach (KEduVocExpression* entry, m_entries) {
00045 int grade = entry->translation(m_toTranslation)->grade();
00046
00047 const QDateTime &date = entry->translation(m_toTranslation)->practiceDate();
00048
00049 const QDateTime &expireDate = QDateTime::currentDateTime().addSecs( -Prefs::expireItem(grade) );
00050
00051 if ( date < expireDate && grade > 0) {
00052
00053 entry->translation(m_toTranslation)->decGrade();
00054
00055
00056 entry->translation(m_toTranslation)->setPracticeDate( QDateTime::currentDateTime().addSecs( -Prefs::expireItem( grade - 2) ) );
00057 counter++;
00058 }
00059 }
00060 kDebug() << "Expired words dropped their grade: " << counter;
00061 }
00062 }
00063
00064 QList<KEduVocExpression*> EntryFilter::entries()
00065 {
00066
00067 m_entries = m_doc->lesson()->entries(KEduVocLesson::Recursive).toSet();
00068 cleanupInvalid();
00069
00070 kDebug() << "Document contains " << m_entries.count() << " valid entries.";
00071 if (m_entries.count() == 0) {
00072 KMessageBox::error(0, i18n("The vocabulary document contains no entries that can be used for the chosen type of practice."));
00073 return m_entries.toList();
00074 }
00075
00076 lessonEntries();
00077 wordTypeEntries();
00078 blockedEntries();
00079 timesWrongEntries();
00080 timesPracticedEntries();
00081 minMaxGradeEntries();
00082
00083 updateTotal();
00084
00085 if (m_currentSelection.count() == 0) {
00086 kDebug() << "Creating practice filter dialog.";
00087 m_dialog = new KDialog;
00088 m_dialog->setCaption(i18n("Start Practice"));
00089 QWidget *widget = new QWidget(m_dialog);
00090 ui.setupUi(widget);
00091 m_dialog->setMainWidget(widget);
00092 m_dialog->setButtons( KDialog::Ok | KDialog::Cancel );
00093
00094 ui.lessonLabel->setText(QString::number(m_entriesLesson.count()));
00095 ui.wordTypeLabel->setText(QString::number(m_entriesWordType.count()));
00096 ui.blockedLabel->setText(QString::number(m_entriesBlocked.count()));
00097 ui.timesWrongLabel->setText(QString::number(m_entriesTimesWrong.count()));
00098 ui.timesPracticedLabel->setText(QString::number(m_entriesTimesPracticed.count()));
00099 ui.minMaxGradeLabel->setText(QString::number(m_entriesMinMaxGrade.count()));
00100
00101 ui.documentTotalLabel->setText(QString::number(m_entries.count()));
00102 updateTotal();
00103
00104 ui.lessonCheckBox->setChecked(!m_entriesLesson.count());
00105 ui.wordTypeCheckBox->setChecked(!m_entriesWordType.count());
00106 ui.blockedCheckBox->setChecked(!m_entriesBlocked.count());
00107 ui.timesWrongCheckBox->setChecked(!m_entriesTimesWrong.count());
00108 ui.timesPracticedCheckBox->setChecked(!m_entriesTimesPracticed.count());
00109 ui.minMaxGradeCheckBox->setChecked(!m_entriesMinMaxGrade.count());
00110
00111 connect( m_dialog, SIGNAL(okClicked()), this, SLOT(userSelectionAccepted()) );
00112 connect( m_dialog, SIGNAL(cancelClicked()), this, SLOT(userSelectionCanceled()) );
00113 connect( ui.lessonCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00114 connect( ui.wordTypeCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00115 connect( ui.blockedCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00116 connect( ui.timesWrongCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00117 connect( ui.timesPracticedCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00118 connect( ui.minMaxGradeCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00119
00120 updateTotal();
00121
00122 if (!Prefs::wordTypesInPracticeEnabled()) {
00123 ui.wordTypeLabel->setVisible(false);
00124 ui.wordTypeCheckBox->setVisible(false);
00125 }
00126
00127 if (m_dialog->exec() == QDialog::Rejected) {
00128 delete m_dialog;
00129 return QList<KEduVocExpression*>();
00130 }
00131 delete m_dialog;
00132
00133 }
00134
00135 return m_currentSelection.toList();
00136 }
00137
00138 void EntryFilter::checkBoxChanged(bool filter)
00139 {
00140 updateTotal();
00141 }
00142
00143 void EntryFilter::updateTotal()
00144 {
00145 QSet< KEduVocExpression * > selected = m_entries;
00146 if (!m_dialog || !ui.lessonCheckBox->isChecked()) {
00147 selected = selected.intersect(m_entriesLesson);
00148 }
00149 if (!m_dialog || !ui.wordTypeCheckBox->isChecked()) {
00150 selected = selected.intersect(m_entriesWordType);
00151 }
00152 if (!m_dialog || !ui.blockedCheckBox->isChecked()) {
00153 selected = selected.intersect(m_entriesBlocked);
00154 }
00155 if (!m_dialog || !ui.timesWrongCheckBox->isChecked()) {
00156 selected = selected.intersect(m_entriesTimesWrong);
00157 }
00158 if (!m_dialog || !ui.timesPracticedCheckBox->isChecked()) {
00159 selected = selected.intersect(m_entriesTimesPracticed);
00160 }
00161 if (!m_dialog || !ui.minMaxGradeCheckBox->isChecked()) {
00162 selected = selected.intersect(m_entriesMinMaxGrade);
00163 }
00164
00165 if (m_dialog) {
00166 ui.totalLabel->setText(QString::number(selected.count()));
00167 m_dialog->enableButtonOk(selected.count() > 0);
00168 }
00169
00170 m_currentSelection = selected;
00171 }
00172
00173 void EntryFilter::lessonEntries()
00174 {
00175 foreach(KEduVocExpression* entry, m_entries) {
00176 if (entry->lesson()->inPractice()) {
00177 m_entriesLesson.insert(entry);
00178 }
00179 }
00180
00181
00182
00183 }
00184
00185
00186 void EntryFilter::wordTypeEntries()
00187 {
00188 if (Prefs::wordTypesInPracticeEnabled()) {
00189 foreach(KEduVocExpression* entry, m_entries) {
00190 if (entry->translation(m_toTranslation)->wordType()) {
00191 if(entry->translation(m_toTranslation)->wordType()->inPractice()) {
00192 m_entriesWordType.insert(entry);
00193 }
00194 }
00195 }
00196 } else {
00197 m_entriesWordType = m_entries;
00198 }
00199 }
00200
00201 void EntryFilter::blockedEntries()
00202 {
00203 if (!Prefs::block()) {
00204 m_entriesBlocked = m_entries;
00205 return;
00206 }
00207
00208 foreach(KEduVocExpression* entry, m_entries) {
00209 int grade = entry->translation(m_toTranslation)->grade();
00210 if (grade == KV_NORM_GRADE || Prefs::blockItem(grade) == 0) {
00211 m_entriesBlocked.insert(entry);
00212 } else {
00213 QDateTime date = entry->translation(m_toTranslation)->practiceDate();
00214 if (date.addSecs(Prefs::blockItem(grade)) < QDateTime::currentDateTime()) {
00215 m_entriesBlocked.insert(entry);
00216 }
00217 }
00218 }
00219 }
00220
00221 void EntryFilter::timesWrongEntries()
00222 {
00223 foreach(KEduVocExpression* entry, m_entries) {
00224 if (entry->translation(m_toTranslation)->badCount() >= Prefs::practiceMinimumWrongCount() && entry->translation(m_toTranslation)->badCount() <= Prefs::practiceMaximumWrongCount()) {
00225 m_entriesTimesWrong.insert(entry);
00226 }
00227 }
00228 }
00229
00230 void EntryFilter::timesPracticedEntries()
00231 {
00232 foreach(KEduVocExpression* entry, m_entries) {
00233 if (entry->translation(m_toTranslation)->practiceCount() >= Prefs::practiceMinimumTimesAsked() && entry->translation(m_toTranslation)->practiceCount() <= Prefs::practiceMaximumTimesAsked()) {
00234 m_entriesTimesPracticed.insert(entry);
00235 }
00236 }
00237 }
00238
00239 void EntryFilter::minMaxGradeEntries()
00240 {
00241 foreach(KEduVocExpression* entry, m_entries) {
00242 int grade =
00243 entry->translation(m_toTranslation)->grade();
00244 if (grade >= Prefs::practiceMinimumGrade() && grade <= Prefs::practiceMaximumGrade()) {
00245 m_entriesMinMaxGrade.insert(entry);
00246 }
00247 }
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 void EntryFilter::cleanupInvalid()
00276 {
00277 bool typeTest = Prefs::testType() == Prefs::EnumTestType::ArticleTest
00278 || Prefs::testType() == Prefs::EnumTestType::ComparisonTest
00279 || Prefs::testType() == Prefs::EnumTestType::ConjugationTest
00280 || Prefs::testType() == Prefs::EnumTestType::SynonymTest
00281 || Prefs::testType() == Prefs::EnumTestType::AntonymTest
00282 || Prefs::testType() == Prefs::EnumTestType::ParaphraseTest
00283 || Prefs::testType() == Prefs::EnumTestType::ExampleTest;
00284
00285 QSet<KEduVocExpression*>::iterator i = m_entries.begin();
00286 while (i != m_entries.end()) {
00287
00288 if ((*i)->translation(m_toTranslation)->text().isEmpty()
00289 || (*i)->translation(m_fromTranslation)->text().isEmpty()) {
00290 i = m_entries.erase(i);
00291 } else if (typeTest) {
00292 if(!(*i)->translation(m_toTranslation)->wordType()) {
00293 i = m_entries.erase(i);
00294 } else {
00295 switch (Prefs::testType()) {
00296
00297 case Prefs::EnumTestType::ArticleTest:
00298 if (! ((*i)->translation(m_toTranslation)->wordType()->wordType() & KEduVocWordFlag::Noun)) {
00299 i = m_entries.erase(i);
00300 } else i++;
00301 break;
00302 case Prefs::EnumTestType::ComparisonTest:
00303 if (! ((Prefs::comparisonIncludeAdjective() &&(*i)->translation(m_toTranslation)->wordType()->wordType()
00304 == KEduVocWordFlag::Adjective)
00305 || (Prefs::comparisonIncludeAdverb() &&(*i)->translation(m_toTranslation)->wordType()->wordType()
00306 == KEduVocWordFlag::Adverb))) {
00307 i = m_entries.erase(i);
00308 } else {
00309 if ((*i)->translation(m_toTranslation)->comparative().isEmpty() &&
00310 (*i)->translation(m_toTranslation)->superlative().isEmpty()) {
00311 i = m_entries.erase(i);
00312 } else i++;
00313 }
00314 break;
00315 case Prefs::EnumTestType::ConjugationTest:
00316 if ( (*i)->translation(m_toTranslation)->wordType()->wordType() != KEduVocWordFlag::Verb || (*i)->translation(m_toTranslation)->conjugations().count() == 0) {
00317 i = m_entries.erase(i);
00318 } else i++;
00319 break;
00320
00321 case Prefs::EnumTestType::SynonymTest:
00322 if ((*i)->translation(m_toTranslation)->synonym().simplified().isEmpty()){
00323 i = m_entries.erase(i);
00324 } else i++;
00325 break;
00326 case Prefs::EnumTestType::AntonymTest:
00327 if ((*i)->translation(m_toTranslation)->antonym().simplified().isEmpty()){
00328 i = m_entries.erase(i);
00329 } else i++;
00330 break;
00331 case Prefs::EnumTestType::ParaphraseTest:
00332 if ((*i)->translation(m_toTranslation)->paraphrase().simplified().isEmpty()){
00333 i = m_entries.erase(i);
00334 } else i++;
00335 break;
00336 case Prefs::EnumTestType::ExampleTest:
00337 if ((*i)->translation(m_toTranslation)->example().simplified().isEmpty()){
00338 i = m_entries.erase(i);
00339 } else i++;
00340 break;
00341 }
00342 }
00343 } else {
00344 i++;
00345 }
00346 }
00347
00348 kDebug() << "Invalid items removed. Remaining: " << m_entries.count();
00349 }
00350
00351
00352 #include "entryfilter.moc"