Back to index

fet  5.18.0
constraintmingapsbetweenactivitiesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           constraintmingapsbetweenactivitiesform.cpp  -  description
00003                              -------------------
00004     begin                : July 10, 2008
00005     copyright            : (C) 2008 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 "constraintmingapsbetweenactivitiesform.h"
00023 #include "addconstraintmingapsbetweenactivitiesform.h"
00024 #include "modifyconstraintmingapsbetweenactivitiesform.h"
00025 
00026 #include <QListWidget>
00027 #include <QScrollBar>
00028 #include <QAbstractItemView>
00029 
00030 ConstraintMinGapsBetweenActivitiesForm::ConstraintMinGapsBetweenActivitiesForm(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 ConstraintMinGapsBetweenActivitiesForm::~ConstraintMinGapsBetweenActivitiesForm()
00106 {
00107        saveFETDialogGeometry(this);
00108 }
00109 
00110 bool ConstraintMinGapsBetweenActivitiesForm::filterOk(TimeConstraint* ctr)
00111 {
00112        if(ctr->type!=CONSTRAINT_MIN_GAPS_BETWEEN_ACTIVITIES)
00113               return false;
00114               
00115        ConstraintMinGapsBetweenActivities* c=(ConstraintMinGapsBetweenActivities*) 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        
00129               int id=c->activitiesId[i];
00130               Activity* act=NULL;
00131               foreach(Activity* a, gt.rules.activitiesList)
00132                      if(a->id==id)
00133                             act=a;
00134               
00135               if(act!=NULL){
00136                      //teacher
00137                      if(tn!=""){
00138                             bool ok2=false;
00139                             for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00140                                    if(*it == tn){
00141                                           ok2=true;
00142                                           break;
00143                                    }
00144                             if(ok2)
00145                                    foundTeacher=true;
00146                      }
00147                      else
00148                             foundTeacher=true;
00149 
00150                      //subject
00151                      if(sbn!="" && sbn!=act->subjectName)
00152                             ;
00153                      else
00154                             foundSubject=true;
00155               
00156                      //activity tag
00157                      if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00158                             ;
00159                      else
00160                             foundActivityTag=true;
00161               
00162                      //students
00163                      if(stn!=""){
00164                             bool ok2=false;
00165                             for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00166                                    if(*it == stn){
00167                                           ok2=true;
00168                                           break;
00169                             }
00170                             if(ok2)
00171                                    foundStudents=true;
00172                      }
00173                      else
00174                             foundStudents=true;
00175               }
00176        }
00177        
00178        if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
00179               return true;
00180        else
00181               return false;
00182 }
00183 
00184 void ConstraintMinGapsBetweenActivitiesForm::filterChanged()
00185 {
00186        this->visibleConstraintsList.clear();
00187        constraintsListWidget->clear();
00188        for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
00189               TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
00190               if(filterOk(ctr)){
00191                      visibleConstraintsList.append(ctr);
00192                      constraintsListWidget->addItem(ctr->getDescription(gt.rules));
00193               }
00194        }
00195 
00196        if(constraintsListWidget->count()>0)
00197               constraintsListWidget->setCurrentRow(0);
00198        else
00199               constraintChanged(-1);
00200 }
00201 
00202 void ConstraintMinGapsBetweenActivitiesForm::constraintChanged(int index)
00203 {
00204        if(index<0){
00205               currentConstraintTextEdit->setPlainText("");
00206        
00207               return;
00208        }
00209        assert(index<this->visibleConstraintsList.size());
00210        TimeConstraint* ctr=this->visibleConstraintsList.at(index);
00211        assert(ctr!=NULL);
00212        currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
00213 }
00214 
00215 void ConstraintMinGapsBetweenActivitiesForm::addConstraint()
00216 {
00217        AddConstraintMinGapsBetweenActivitiesForm form(this);
00218        setParentAndOtherThings(&form, this);
00219        form.exec();
00220 
00221        filterChanged();
00222        
00223        constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
00224 }
00225 
00226 void ConstraintMinGapsBetweenActivitiesForm::modifyConstraint()
00227 {
00228        int valv=constraintsListWidget->verticalScrollBar()->value();
00229        int valh=constraintsListWidget->horizontalScrollBar()->value();
00230 
00231        int i=constraintsListWidget->currentRow();
00232        if(i<0){
00233               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00234               return;
00235        }
00236        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00237 
00238        ModifyConstraintMinGapsBetweenActivitiesForm form(this, (ConstraintMinGapsBetweenActivities*)ctr);
00239        setParentAndOtherThings(&form, this);
00240        form.exec();
00241 
00242        filterChanged();
00243 
00244        constraintsListWidget->verticalScrollBar()->setValue(valv);
00245        constraintsListWidget->horizontalScrollBar()->setValue(valh);
00246 
00247        if(i>=constraintsListWidget->count())
00248               i=constraintsListWidget->count()-1;
00249 
00250        if(i>=0)
00251               constraintsListWidget->setCurrentRow(i);
00252        else
00253               this->constraintChanged(-1);
00254 }
00255 
00256 void ConstraintMinGapsBetweenActivitiesForm::removeConstraint()
00257 {
00258        int i=constraintsListWidget->currentRow();
00259        if(i<0){
00260               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00261               return;
00262        }
00263        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00264        QString s;
00265        s=tr("Remove constraint?");
00266        s+="\n\n";
00267        s+=ctr->getDetailedDescription(gt.rules);
00268        
00269        QListWidgetItem* item;
00270 
00271        switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
00272               s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
00273        case 0: // The user clicked the OK button or pressed Enter
00274               gt.rules.removeTimeConstraint(ctr);
00275               
00276               visibleConstraintsList.removeAt(i);
00277               constraintsListWidget->setCurrentRow(-1);
00278               item=constraintsListWidget->takeItem(i);
00279               delete item;
00280               
00281               break;
00282        case 1: // The user clicked the Cancel button or pressed Escape
00283               break;
00284        }
00285        
00286        if(i>=constraintsListWidget->count())
00287               i=constraintsListWidget->count()-1;
00288        if(i>=0)
00289               constraintsListWidget->setCurrentRow(i);
00290        else
00291               this->constraintChanged(-1);
00292 }
00293 
00294 void ConstraintMinGapsBetweenActivitiesForm::help()
00295 {
00296        QString s=tr("Please make sure that the selected activities are not forced to be"
00297               " consecutive by some other constraint 'min days between activities' (with"
00298               " 'consecutive if same day' true), by a constraint 'two activities consecutive',"
00299               " or by a constraint 'two activities grouped' (also, if you have a constraint"
00300               " 'three activities grouped' related to the selected activities, make sure that the"
00301               " constraints do not contradict).");
00302               
00303        LongTextMessageBox::information(this, tr("FET help"), s);
00304 }