• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

parley

entryfilter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
00003  ***************************************************************************/
00004 
00005 /***************************************************************************
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include "entryfilter.h"
00015 
00016 #include "prefs.h"
00017 
00018 #include <keduvocdocument.h>
00019 #include <keduvocwordtype.h>
00020 #include <keduvocexpression.h>
00021 
00022 #include <KDialog>
00023 #include <KMessageBox>
00024 
00025 EntryFilter::EntryFilter(QObject * parent, KEduVocDocument* doc) :QObject(parent)
00026 {
00027     m_doc = doc;
00028     m_dialog = 0;
00029 
00030     // keep languages consistant between the edit, the filter, and practice
00031     switch (Prefs::testType())
00032     {
00033         // bilingual mode.
00034         case Prefs::EnumTestType::WrittenTest:
00035         case Prefs::EnumTestType::MixedLettersTest:
00036         case Prefs::EnumTestType::MultipleChoiceTest:
00037 //        case Prefs::EnumTestType::FlashCardsTest:
00038             m_fromTranslation = Prefs::questionLanguage();
00039             m_toTranslation = Prefs::solutionLanguage();
00040             break;
00041         // monolingual mode
00042         case Prefs::EnumTestType::ArticleTest:
00043         case Prefs::EnumTestType::ExampleTest:
00044         case Prefs::EnumTestType::ParaphraseTest:
00045         case Prefs::EnumTestType::SynonymTest:
00046         case Prefs::EnumTestType::AntonymTest:
00047         case Prefs::EnumTestType::ComparisonTest:
00048         case Prefs::EnumTestType::ConjugationTest:
00049         default:
00050             m_fromTranslation = m_toTranslation = Prefs::questionLanguage();
00051             break;
00052     }
00053     expireEntries();
00054 }
00055 
00056 EntryFilter::~ EntryFilter()
00057 {
00058 }
00059 
00060 void EntryFilter::expireEntries()
00061 {
00062     if ( Prefs::expire() ) {
00063         int counter = 0;
00064         foreach (KEduVocExpression* entry, m_entries) {
00065             int grade = entry->translation(m_toTranslation)->grade();
00066 
00067             const QDateTime &date =  entry->translation(m_toTranslation)->practiceDate();
00068 
00069             const QDateTime &expireDate = QDateTime::currentDateTime().addSecs( -Prefs::expireItem(grade) );
00070 
00071             if ( date < expireDate && grade > 0) {
00072                 // decrease the grade
00073                 entry->translation(m_toTranslation)->decGrade();
00074 
00075                 // prevent from endless dropping
00076                 entry->translation(m_toTranslation)->setPracticeDate( QDateTime::currentDateTime().addSecs( -Prefs::expireItem( grade - 2) ) );
00077                 counter++;
00078             }
00079         }
00080         kDebug() << "Expired words dropped their grade: " << counter;
00081     }
00082 }
00083 
00084 QList<KEduVocExpression*> EntryFilter::entries()
00085 {
00086     // set up the lists/sets of filtered vocabulary
00087     m_entries = m_doc->lesson()->entries(KEduVocLesson::Recursive).toSet();
00088     cleanupInvalid();
00089 
00090     kDebug() << "Document contains " << m_entries.count() << " valid entries.";
00091     if (m_entries.count() == 0) {
00092         KMessageBox::error(0, i18n("The vocabulary document contains no entries that can be used for the chosen type of practice."));
00093         return m_entries.toList();
00094     }
00095 
00096     if (Prefs::testType() == Prefs::EnumTestType::ArticleTest)
00097     {
00098         if (m_doc->identifier(m_toTranslation).article().isEmpty())
00099         {
00100             KMessageBox::error(0, i18n("The vocabulary document contains no articles for the current language. Please add some in the Edit->Grammar menu."));
00101             return QList<KEduVocExpression*>();
00102         }
00103     }
00104 
00105     lessonEntries();
00106     wordTypeEntries();
00107     blockedEntries();
00108     timesWrongEntries();
00109     timesPracticedEntries();
00110     minMaxGradeEntries();
00111 
00112     updateTotal();
00113 
00114     if (m_currentSelection.count() == 0) {
00115         kDebug() << "Creating practice filter dialog.";
00116         m_dialog = new KDialog;
00117         m_dialog->setCaption(i18n("Start Practice"));
00118         QWidget *widget = new QWidget(m_dialog);
00119         ui.setupUi(widget);
00120         m_dialog->setMainWidget(widget);
00121         m_dialog->setButtons( KDialog::Ok | KDialog::Cancel );
00122 
00123         ui.lessonLabel->setText(QString::number(m_entriesLesson.count()));
00124         ui.wordTypeLabel->setText(QString::number(m_entriesWordType.count()));
00125         ui.blockedLabel->setText(QString::number(m_entriesBlocked.count()));
00126         ui.timesWrongLabel->setText(QString::number(m_entriesTimesWrong.count()));
00127         ui.timesPracticedLabel->setText(QString::number(m_entriesTimesPracticed.count()));
00128         ui.minMaxGradeLabel->setText(QString::number(m_entriesMinMaxGrade.count()));
00129 
00130         ui.documentTotalLabel->setText(QString::number(m_entries.count()));
00131         updateTotal();
00132 
00133         ui.lessonCheckBox->setChecked(!m_entriesLesson.count());
00134         ui.wordTypeCheckBox->setChecked(!m_entriesWordType.count());
00135         ui.blockedCheckBox->setChecked(!m_entriesBlocked.count());
00136         ui.timesWrongCheckBox->setChecked(!m_entriesTimesWrong.count());
00137         ui.timesPracticedCheckBox->setChecked(!m_entriesTimesPracticed.count());
00138         ui.minMaxGradeCheckBox->setChecked(!m_entriesMinMaxGrade.count());
00139 
00140         connect( m_dialog, SIGNAL(okClicked()), this, SLOT(userSelectionAccepted()) );
00141         connect( m_dialog, SIGNAL(cancelClicked()), this, SLOT(userSelectionCanceled()) );
00142         connect( ui.lessonCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00143         connect( ui.wordTypeCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00144         connect( ui.blockedCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00145         connect( ui.timesWrongCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00146         connect( ui.timesPracticedCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00147         connect( ui.minMaxGradeCheckBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxChanged(bool)));
00148 
00149         updateTotal();
00150 
00151         if (!Prefs::wordTypesInPracticeEnabled()) {
00152             ui.wordTypeLabel->setVisible(false);
00153             ui.wordTypeCheckBox->setVisible(false);
00154         }
00155 
00156         if (m_dialog->exec() == QDialog::Rejected) {
00157             delete m_dialog;
00158             return QList<KEduVocExpression*>();
00159         }
00160         delete m_dialog;
00161 
00162     }
00163 
00164     return m_currentSelection.toList();
00165 }
00166 
00167 void EntryFilter::checkBoxChanged(bool filter)
00168 {
00169     updateTotal();
00170 }
00171 
00172 void EntryFilter::updateTotal()
00173 {
00174     QSet< KEduVocExpression * > selected = m_entries;
00175     if (!m_dialog || !ui.lessonCheckBox->isChecked()) {
00176         selected = selected.intersect(m_entriesLesson);
00177     }
00178     if (!m_dialog || !ui.wordTypeCheckBox->isChecked()) {
00179         selected = selected.intersect(m_entriesWordType);
00180     }
00181     if (!m_dialog || !ui.blockedCheckBox->isChecked()) {
00182         selected = selected.intersect(m_entriesBlocked);
00183     }
00184     if (!m_dialog || !ui.timesWrongCheckBox->isChecked()) {
00185         selected = selected.intersect(m_entriesTimesWrong);
00186     }
00187     if (!m_dialog || !ui.timesPracticedCheckBox->isChecked()) {
00188         selected = selected.intersect(m_entriesTimesPracticed);
00189     }
00190     if (!m_dialog || !ui.minMaxGradeCheckBox->isChecked()) {
00191         selected = selected.intersect(m_entriesMinMaxGrade);
00192     }
00193 
00194     if (m_dialog) {
00195         ui.totalLabel->setText(QString::number(selected.count()));
00196         m_dialog->enableButtonOk(selected.count() > 0);
00197     }
00198 
00199     m_currentSelection = selected;
00200 }
00201 
00202 void EntryFilter::lessonEntries()
00203 {
00205     foreach(KEduVocExpression* entry, m_entries) {
00206         if (entry->lesson()->inPractice()) {
00207             m_entriesLesson.insert(entry);
00208         }
00209     }
00210 
00211     //if ( Prefs::testOrderLesson() ) {
00212 
00213 }
00214 
00215 
00216 void EntryFilter::wordTypeEntries()
00217 {
00218     if (Prefs::wordTypesInPracticeEnabled()) {
00219         foreach(KEduVocExpression* entry, m_entries) {
00220             if (entry->translation(m_toTranslation)->wordType()) {
00221                 if(entry->translation(m_toTranslation)->wordType()->inPractice()) {
00222                     m_entriesWordType.insert(entry);
00223                 }
00224             }
00225         }
00226     } else {
00227         m_entriesWordType = m_entries;
00228     }
00229 }
00230 
00231 void EntryFilter::blockedEntries()
00232 {
00233     if (!Prefs::block()) {
00234         m_entriesBlocked = m_entries;
00235         return;
00236     }
00237 
00238     foreach(KEduVocExpression* entry, m_entries) {
00239         int grade = entry->translation(m_toTranslation)->grade();
00240         if (grade == KV_NORM_GRADE || Prefs::blockItem(grade) == 0) {
00241             m_entriesBlocked.insert(entry);
00242         } else {
00243             QDateTime date = entry->translation(m_toTranslation)->practiceDate();
00244             if (date.addSecs(Prefs::blockItem(grade)) < QDateTime::currentDateTime()) {
00245                 m_entriesBlocked.insert(entry);
00246             }
00247         }
00248     }
00249 }
00250 
00251 void EntryFilter::timesWrongEntries()
00252 {
00253     foreach(KEduVocExpression* entry, m_entries) {
00254         if (entry->translation(m_toTranslation)->badCount() >= Prefs::practiceMinimumWrongCount() && entry->translation(m_toTranslation)->badCount() <= Prefs::practiceMaximumWrongCount()) {
00255             m_entriesTimesWrong.insert(entry);
00256         }
00257     }
00258 }
00259 
00260 void EntryFilter::timesPracticedEntries()
00261 {
00262     foreach(KEduVocExpression* entry, m_entries) {
00263         if (entry->translation(m_toTranslation)->practiceCount() >= Prefs::practiceMinimumTimesAsked() && entry->translation(m_toTranslation)->practiceCount() <= Prefs::practiceMaximumTimesAsked()) {
00264             m_entriesTimesPracticed.insert(entry);
00265         }
00266     }
00267 }
00268 
00269 void EntryFilter::minMaxGradeEntries()
00270 {
00271     foreach(KEduVocExpression* entry, m_entries) {
00272         int grade =
00273                 entry->translation(m_toTranslation)->grade();
00274         if (grade >= Prefs::practiceMinimumGrade() && grade <= Prefs::practiceMaximumGrade()) {
00275             m_entriesMinMaxGrade.insert(entry);
00276         }
00277     }
00278 }
00279  /*
00280     if (m_testType == Prefs::EnumTestType::ArticleTest) {
00281     KMessageBox::information(0,
00282     i18n("You selected to practice the genders of nouns, but no appropriate nouns could be found. Use \"Edit Entry\" and select Noun as word type and the gender."),
00283     i18n("No valid word type found"));
00284     return;
00285 }
00286     if (m_testType == Prefs::EnumTestType::ComparisonTest) {
00287     KMessageBox::information(0,
00288     i18n("You selected to practice comparison forms, but no adjectives or adverbs containing comparison forms could be found. Use \"Edit Entry\" and select Adverb or Adjective as word type and enter the comparison forms."),
00289     i18n("No valid word type found"));
00290     return;
00291 }
00292     if (m_testType == Prefs::EnumTestType::ConjugationTest) {
00293     KMessageBox::information(0, i18n("You selected to practice conjugations, but no vocabulary containing conjugations in the tenses you selected could be found. Use \"Edit Entry\" and select Verb as word type and enter the conjugation forms."), i18n("No valid word type found"));
00294     return;
00295 }
00296 }
00297 
00298     if ( removeTestEntryList.count() == m_entries.count() ) {
00299     if ( KMessageBox::questionYesNo(0, i18n("<p>The lessons you selected for the practice contain no entries when the threshold settings are respected.</p><p>Hint: To configure the thresholds use the \"Threshold Page\" in the \"Configure Practice\" dialog.</p><p>Would you like to ignore the threshold setting?</p>"), i18n("No Entries with Current Threshold Settings") ) == KMessageBox::No ) {
00300     return;
00301 }
00302 
00303  */
00304 
00305 void EntryFilter::cleanupInvalid()
00306 {
00307     bool typeTest = Prefs::testType() == Prefs::EnumTestType::ArticleTest
00308             || Prefs::testType() == Prefs::EnumTestType::ComparisonTest
00309             || Prefs::testType() == Prefs::EnumTestType::ConjugationTest
00310             || Prefs::testType() == Prefs::EnumTestType::SynonymTest
00311             || Prefs::testType() == Prefs::EnumTestType::AntonymTest
00312             || Prefs::testType() == Prefs::EnumTestType::ParaphraseTest
00313             || Prefs::testType() == Prefs::EnumTestType::ExampleTest;
00314 
00315     QSet<KEduVocExpression*>::iterator i = m_entries.begin();
00316     while (i != m_entries.end()) {
00317         // remove empty entries
00318         if ((*i)->translation(m_toTranslation)->text().isEmpty()
00319               || (*i)->translation(m_fromTranslation)->text().isEmpty()) {
00320             i = m_entries.erase(i);
00321         } else if (typeTest) {
00322             if(!(*i)->translation(m_toTranslation)->wordType()) {
00323                 i = m_entries.erase(i);
00324             } else {
00325                 switch (Prefs::testType()) {
00326     // if we do a grammar test, check if the grammar type is valid
00327                 case Prefs::EnumTestType::ArticleTest:
00328                     if (! ((*i)->translation(m_toTranslation)->wordType()->wordType() & KEduVocWordFlag::Noun)) {
00329                         i = m_entries.erase(i);
00330                     } else i++;
00331                     break;
00332                 case Prefs::EnumTestType::ComparisonTest:
00333                     if (! ((Prefs::comparisonIncludeAdjective() &&(*i)->translation(m_toTranslation)->wordType()->wordType()
00334                                 == KEduVocWordFlag::Adjective)
00335                            || (Prefs::comparisonIncludeAdverb() &&(*i)->translation(m_toTranslation)->wordType()->wordType()
00336                            == KEduVocWordFlag::Adverb))) {
00337                         i = m_entries.erase(i);
00338                     } else {
00339                         if ((*i)->translation(m_toTranslation)->comparative().isEmpty() &&
00340                                     (*i)->translation(m_toTranslation)->superlative().isEmpty()) {
00341                             i = m_entries.erase(i);
00342                         } else i++;
00343                     }
00344                     break;
00345                 case Prefs::EnumTestType::ConjugationTest:
00346                     if ( (*i)->translation(m_toTranslation)->wordType()->wordType() != KEduVocWordFlag::Verb || (*i)->translation(m_toTranslation)->conjugations().count() == 0) {
00347                         i = m_entries.erase(i);
00348                     } else i++; // conjugation
00349                     break;
00350 
00351                 case Prefs::EnumTestType::SynonymTest:
00352                     if ((*i)->translation(m_toTranslation)->synonyms().isEmpty()){
00353                         i = m_entries.erase(i);
00354                     } else i++;
00355                     break;
00356                 case Prefs::EnumTestType::AntonymTest:
00357                     if ((*i)->translation(m_toTranslation)->antonyms().isEmpty()){
00358                         i = m_entries.erase(i);
00359                     } else i++;
00360                     break;
00361                 case Prefs::EnumTestType::ParaphraseTest:
00362                     if ((*i)->translation(m_toTranslation)->paraphrase().isEmpty()){
00363                         i = m_entries.erase(i);
00364                     } else i++;
00365                     break;
00366                 case Prefs::EnumTestType::ExampleTest:
00367                     if ((*i)->translation(m_toTranslation)->example().simplified().isEmpty()){
00368                         i = m_entries.erase(i);
00369                     } else i++;
00370                     break;
00371                 } // switch
00372             } // type valid
00373         } else { // if typeTest
00374             i++;
00375         }
00376     } // for
00377 
00378     kDebug() << "Invalid items removed. Remaining: " << m_entries.count();
00379 }
00380 
00381 
00382 #include "entryfilter.moc"

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  •     lib
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal