Back to index

fet  5.18.0
constraintmaxdaysbetweenactivitiesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           constraintmaxdaysbetweenactivitiesform.cpp  -  description
00003                              -------------------
00004     begin                : 2009
00005     copyright            : (C) 2009 by Lalescu Liviu
00006     email                : Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <QMessageBox>
00019 
00020 #include "longtextmessagebox.h"
00021 
00022 #include "constraintmaxdaysbetweenactivitiesform.h"
00023 #include "addconstraintmaxdaysbetweenactivitiesform.h"
00024 #include "modifyconstraintmaxdaysbetweenactivitiesform.h"
00025 
00026 #include <QListWidget>
00027 #include <QScrollBar>
00028 #include <QAbstractItemView>
00029 
00030 ConstraintMaxDaysBetweenActivitiesForm::ConstraintMaxDaysBetweenActivitiesForm(QWidget* parent): QDialog(parent)
00031 {
00032        setupUi(this);
00033 
00034        currentConstraintTextEdit->setReadOnly(true);
00035        
00036        modifyConstraintPushButton->setDefault(true);
00037 
00038        constraintsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00039 
00040        connect(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
00041        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
00042        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00043        connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
00044        connect(modifyConstraintPushButton, SIGNAL(clicked()), this, SLOT(modifyConstraint()));
00045        connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
00046        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00047        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00049        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00050        connect(helpPushButton, SIGNAL(clicked()), this, SLOT(help()));
00051 
00052        centerWidgetOnScreen(this);
00053        restoreFETDialogGeometry(this);
00054 
00055        QSize tmp1=teachersComboBox->minimumSizeHint();
00056        Q_UNUSED(tmp1);
00057        QSize tmp2=studentsComboBox->minimumSizeHint();
00058        Q_UNUSED(tmp2);
00059        QSize tmp3=subjectsComboBox->minimumSizeHint();
00060        Q_UNUSED(tmp3);
00061        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00062        Q_UNUSED(tmp4);
00063        
00065        teachersComboBox->addItem("");
00066        for(int i=0; i<gt.rules.teachersList.size(); i++){
00067               Teacher* tch=gt.rules.teachersList[i];
00068               teachersComboBox->addItem(tch->name);
00069        }
00070        teachersComboBox->setCurrentIndex(0);
00071 
00072        subjectsComboBox->addItem("");
00073        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00074               Subject* sb=gt.rules.subjectsList[i];
00075               subjectsComboBox->addItem(sb->name);
00076        }
00077        subjectsComboBox->setCurrentIndex(0);
00078 
00079        activityTagsComboBox->addItem("");
00080        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00081               ActivityTag* st=gt.rules.activityTagsList[i];
00082               activityTagsComboBox->addItem(st->name);
00083        }
00084        activityTagsComboBox->setCurrentIndex(0);
00085 
00086        studentsComboBox->addItem("");
00087        for(int i=0; i<gt.rules.yearsList.size(); i++){
00088               StudentsYear* sty=gt.rules.yearsList[i];
00089               studentsComboBox->addItem(sty->name);
00090               for(int j=0; j<sty->groupsList.size(); j++){
00091                      StudentsGroup* stg=sty->groupsList[j];
00092                      studentsComboBox->addItem(stg->name);
00093                      for(int k=0; k<stg->subgroupsList.size(); k++){
00094                             StudentsSubgroup* sts=stg->subgroupsList[k];
00095                             studentsComboBox->addItem(sts->name);
00096                      }
00097               }
00098        }
00099        studentsComboBox->setCurrentIndex(0);
00101 
00102        this->filterChanged();
00103 }
00104 
00105 ConstraintMaxDaysBetweenActivitiesForm::~ConstraintMaxDaysBetweenActivitiesForm()
00106 {
00107        saveFETDialogGeometry(this);
00108 }
00109 
00110 bool ConstraintMaxDaysBetweenActivitiesForm::filterOk(TimeConstraint* ctr)
00111 {
00112        if(ctr->type!=CONSTRAINT_MAX_DAYS_BETWEEN_ACTIVITIES)
00113               return false;
00114               
00115        ConstraintMaxDaysBetweenActivities* c=(ConstraintMaxDaysBetweenActivities*) ctr;
00116        
00117        QString tn=teachersComboBox->currentText();
00118        QString sbn=subjectsComboBox->currentText();
00119        QString sbtn=activityTagsComboBox->currentText();
00120        QString stn=studentsComboBox->currentText();
00121        
00122        if(tn=="" && sbn=="" && sbtn=="" && stn=="")
00123               return true;
00124        
00125        bool foundTeacher=false, foundStudents=false, foundSubject=false, foundActivityTag=false;
00126               
00127        for(int i=0; i<c->n_activities; i++){
00128               int id=c->activitiesId[i];
00129               Activity* act=NULL;
00130               foreach(Activity* a, gt.rules.activitiesList)
00131                      if(a->id==id)
00132                             act=a;
00133               
00134               if(act!=NULL){
00135                      //teacher
00136                      if(tn!=""){
00137                             bool ok2=false;
00138                             for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00139                                    if(*it == tn){
00140                                           ok2=true;
00141                                           break;
00142                                    }
00143                             if(ok2)
00144                                    foundTeacher=true;
00145                      }
00146                      else
00147                             foundTeacher=true;
00148 
00149                      //subject
00150                      if(sbn!="" && sbn!=act->subjectName)
00151                             ;
00152                      else
00153                             foundSubject=true;
00154               
00155                      //activity tag
00156                      if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00157                             ;
00158                      else
00159                             foundActivityTag=true;
00160               
00161                      //students
00162                      if(stn!=""){
00163                             bool ok2=false;
00164                             for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00165                                    if(*it == stn){
00166                                           ok2=true;
00167                                           break;
00168                             }
00169                             if(ok2)
00170                                    foundStudents=true;
00171                      }
00172                      else
00173                             foundStudents=true;
00174               }
00175        }
00176        
00177        if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
00178               return true;
00179        else
00180               return false;
00181 }
00182 
00183 void ConstraintMaxDaysBetweenActivitiesForm::filterChanged()
00184 {
00185        this->visibleConstraintsList.clear();
00186        constraintsListWidget->clear();
00187        for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
00188               TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
00189               if(filterOk(ctr)){
00190                      visibleConstraintsList.append(ctr);
00191                      constraintsListWidget->addItem(ctr->getDescription(gt.rules));
00192               }
00193        }
00194        
00195        if(constraintsListWidget->count()>0)
00196               constraintsListWidget->setCurrentRow(0);
00197        else
00198               currentConstraintTextEdit->setPlainText(QString(""));
00199 }
00200 
00201 void ConstraintMaxDaysBetweenActivitiesForm::constraintChanged(int index)
00202 {
00203        if(index<0){
00204               currentConstraintTextEdit->setPlainText("");
00205        
00206               return;
00207        }
00208        assert(index<this->visibleConstraintsList.size());
00209        TimeConstraint* ctr=this->visibleConstraintsList.at(index);
00210        assert(ctr!=NULL);
00211        currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
00212 }
00213 
00214 void ConstraintMaxDaysBetweenActivitiesForm::addConstraint()
00215 {
00216        AddConstraintMaxDaysBetweenActivitiesForm form(this);
00217        setParentAndOtherThings(&form, this);
00218        form.exec();
00219 
00220        filterChanged();
00221        
00222        constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
00223 }
00224 
00225 void ConstraintMaxDaysBetweenActivitiesForm::modifyConstraint()
00226 {
00227        int valv=constraintsListWidget->verticalScrollBar()->value();
00228        int valh=constraintsListWidget->horizontalScrollBar()->value();
00229 
00230        int i=constraintsListWidget->currentRow();
00231        if(i<0){
00232               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00233               return;
00234        }
00235        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00236 
00237        ModifyConstraintMaxDaysBetweenActivitiesForm form(this, (ConstraintMaxDaysBetweenActivities*)ctr);
00238        setParentAndOtherThings(&form, this);
00239        form.exec();
00240 
00241        filterChanged();
00242        
00243        constraintsListWidget->verticalScrollBar()->setValue(valv);
00244        constraintsListWidget->horizontalScrollBar()->setValue(valh);
00245 
00246        if(i>=constraintsListWidget->count())
00247               i=constraintsListWidget->count()-1;
00248 
00249        if(i>=0)
00250               constraintsListWidget->setCurrentRow(i);
00251        else
00252               this->constraintChanged(-1);
00253 }
00254 
00255 void ConstraintMaxDaysBetweenActivitiesForm::removeConstraint()
00256 {
00257        int i=constraintsListWidget->currentRow();
00258        if(i<0){
00259               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00260               return;
00261        }
00262        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00263        QString s;
00264        s=tr("Remove constraint?");
00265        s+="\n\n";
00266        s+=ctr->getDetailedDescription(gt.rules);
00267        
00268        QListWidgetItem* item;
00269 
00270        switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
00271               s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
00272        case 0: // The user clicked the OK button or pressed Enter
00273               gt.rules.removeTimeConstraint(ctr);
00274               
00275               visibleConstraintsList.removeAt(i);
00276               constraintsListWidget->setCurrentRow(-1);
00277               item=constraintsListWidget->takeItem(i);
00278               delete item;
00279 
00280               break;
00281        case 1: // The user clicked the Cancel button or pressed Escape
00282               break;
00283        }
00284        
00285        if(i>=constraintsListWidget->count())
00286               i=constraintsListWidget->count()-1;
00287        if(i>=0)
00288               constraintsListWidget->setCurrentRow(i);
00289        else
00290               this->constraintChanged(-1);
00291 }
00292 
00293 void ConstraintMaxDaysBetweenActivitiesForm::help()
00294 {
00295        QString s;
00296        
00297        s+=tr("Help about the constraint max days between activities:");
00298        s+="\n\n";
00299        s+=tr("This constraint was suggested for the following situation: a user needed that activities A1, A2 and A3 to be in follower days"
00300         " (like: A1 on Tuesday, A2 on Wednesday and A3 on Thursday. So, they must be in 3 consecutive days). This is simple: add a constraint"
00301         " max days between activities for A1, A2 and A3, with max 2 days between them. It is supposed that these activities are constrained"
00302         " not to be in the same day by a constraint min days between activities.");
00303        s+="\n\n";
00304        s+=tr("So, the general situation: this constraint ensures that between each pair from the selected activities, the distance in days is at most the selected value."
00305        " Distance = 1 day between a pair A1 and A2 means that A1 and A2 are in consecutive days (like Thursday and Friday)."
00306        " Distance = 3 days means that A1 and A2 are 3 days apart, for instance Monday and Thursday.");
00307        s+="\n\n";
00308        s+=tr("Another example: teacher T wants to ensure that his activities take place in at most 4 consecutive days (so, from Monday to Thursday or from"
00309        " Tuesday to Friday). Then, add all his activities and max days between them = 3.");
00310 
00311        LongTextMessageBox::largeInformation(this, tr("FET help"), s);
00312 }