Back to index

fet  5.18.0
constraintactivitiesoccupymaxdifferentroomsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           constraintactivitiesoccupymaxdifferentroomsform.cpp  -  description
00003                              -------------------
00004     begin                : Apr 29, 2012
00005     copyright            : (C) 2012 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 "constraintactivitiesoccupymaxdifferentroomsform.h"
00023 #include "addconstraintactivitiesoccupymaxdifferentroomsform.h"
00024 #include "modifyconstraintactivitiesoccupymaxdifferentroomsform.h"
00025 
00026 #include <QListWidget>
00027 #include <QScrollBar>
00028 #include <QAbstractItemView>
00029 
00030 ConstraintActivitiesOccupyMaxDifferentRoomsForm::ConstraintActivitiesOccupyMaxDifferentRoomsForm(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 
00047        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00049        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00050        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00051 
00052        connect(helpPushButton, SIGNAL(clicked()), this, SLOT(help()));
00053 
00054        centerWidgetOnScreen(this);
00055        restoreFETDialogGeometry(this);
00056 
00057        QSize tmp1=teachersComboBox->minimumSizeHint();
00058        Q_UNUSED(tmp1);
00059        QSize tmp2=studentsComboBox->minimumSizeHint();
00060        Q_UNUSED(tmp2);
00061        QSize tmp3=subjectsComboBox->minimumSizeHint();
00062        Q_UNUSED(tmp3);
00063        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00064        Q_UNUSED(tmp4);
00065        
00067        teachersComboBox->addItem("");
00068        for(int i=0; i<gt.rules.teachersList.size(); i++){
00069               Teacher* tch=gt.rules.teachersList[i];
00070               teachersComboBox->addItem(tch->name);
00071        }
00072        teachersComboBox->setCurrentIndex(0);
00073 
00074        subjectsComboBox->addItem("");
00075        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00076               Subject* sb=gt.rules.subjectsList[i];
00077               subjectsComboBox->addItem(sb->name);
00078        }
00079        subjectsComboBox->setCurrentIndex(0);
00080 
00081        activityTagsComboBox->addItem("");
00082        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00083               ActivityTag* st=gt.rules.activityTagsList[i];
00084               activityTagsComboBox->addItem(st->name);
00085        }
00086        activityTagsComboBox->setCurrentIndex(0);
00087 
00088        studentsComboBox->addItem("");
00089        for(int i=0; i<gt.rules.yearsList.size(); i++){
00090               StudentsYear* sty=gt.rules.yearsList[i];
00091               studentsComboBox->addItem(sty->name);
00092               for(int j=0; j<sty->groupsList.size(); j++){
00093                      StudentsGroup* stg=sty->groupsList[j];
00094                      studentsComboBox->addItem(stg->name);
00095                      for(int k=0; k<stg->subgroupsList.size(); k++){
00096                             StudentsSubgroup* sts=stg->subgroupsList[k];
00097                             studentsComboBox->addItem(sts->name);
00098                      }
00099               }
00100        }
00101        studentsComboBox->setCurrentIndex(0);
00103 
00104        this->filterChanged();
00105 }
00106 
00107 ConstraintActivitiesOccupyMaxDifferentRoomsForm::~ConstraintActivitiesOccupyMaxDifferentRoomsForm()
00108 {
00109        saveFETDialogGeometry(this);
00110 }
00111 
00112 bool ConstraintActivitiesOccupyMaxDifferentRoomsForm::filterOk(SpaceConstraint* ctr)
00113 {
00114        if(ctr->type!=CONSTRAINT_ACTIVITIES_OCCUPY_MAX_DIFFERENT_ROOMS)
00115               return false;
00116 
00117        ConstraintActivitiesOccupyMaxDifferentRooms* c=(ConstraintActivitiesOccupyMaxDifferentRooms*) ctr;
00118        
00119        QString tn=teachersComboBox->currentText();
00120        QString sbn=subjectsComboBox->currentText();
00121        QString sbtn=activityTagsComboBox->currentText();
00122        QString stn=studentsComboBox->currentText();
00123        
00124        if(tn=="" && sbn=="" && sbtn=="" && stn=="")
00125               return true;
00126        
00127        bool foundTeacher=false, foundStudents=false, foundSubject=false, foundActivityTag=false;
00128               
00129        for(int i=0; i<c->activitiesIds.count(); i++){
00130               int id=c->activitiesIds.at(i);
00131               Activity* act=NULL;
00132               foreach(Activity* a, gt.rules.activitiesList)
00133                      if(a->id==id)
00134                             act=a;
00135               
00136               if(act!=NULL){
00137                      //teacher
00138                      if(tn!=""){
00139                             bool ok2=false;
00140                             for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00141                                    if(*it == tn){
00142                                           ok2=true;
00143                                           break;
00144                                    }
00145                             if(ok2)
00146                                    foundTeacher=true;
00147                      }
00148                      else
00149                             foundTeacher=true;
00150 
00151                      //subject
00152                      if(sbn!="" && sbn!=act->subjectName)
00153                             ;
00154                      else
00155                             foundSubject=true;
00156               
00157                      //activity tag
00158                      if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00159                             ;
00160                      else
00161                             foundActivityTag=true;
00162               
00163                      //students
00164                      if(stn!=""){
00165                             bool ok2=false;
00166                             for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00167                                    if(*it == stn){
00168                                           ok2=true;
00169                                           break;
00170                             }
00171                             if(ok2)
00172                                    foundStudents=true;
00173                      }
00174                      else
00175                             foundStudents=true;
00176               }
00177        }
00178        
00179        if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
00180               return true;
00181        else
00182               return false;
00183 }
00184 
00185 void ConstraintActivitiesOccupyMaxDifferentRoomsForm::filterChanged()
00186 {
00187        this->visibleConstraintsList.clear();
00188        constraintsListWidget->clear();
00189        for(int i=0; i<gt.rules.spaceConstraintsList.size(); i++){
00190               SpaceConstraint* ctr=gt.rules.spaceConstraintsList[i];
00191               if(filterOk(ctr)){
00192                      visibleConstraintsList.append(ctr);
00193                      constraintsListWidget->addItem(ctr->getDescription(gt.rules));
00194               }
00195        }
00196        
00197        if(constraintsListWidget->count()>0)
00198               constraintsListWidget->setCurrentRow(0);
00199        else
00200               currentConstraintTextEdit->setPlainText(QString(""));
00201 }
00202 
00203 void ConstraintActivitiesOccupyMaxDifferentRoomsForm::constraintChanged(int index)
00204 {
00205        if(index<0){
00206               currentConstraintTextEdit->setPlainText("");
00207               return;
00208        }
00209        assert(index<this->visibleConstraintsList.size());
00210        SpaceConstraint* ctr=this->visibleConstraintsList.at(index);
00211        assert(ctr!=NULL);
00212        currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
00213 }
00214 
00215 void ConstraintActivitiesOccupyMaxDifferentRoomsForm::addConstraint()
00216 {
00217        AddConstraintActivitiesOccupyMaxDifferentRoomsForm form(this);
00218        setParentAndOtherThings(&form, this);
00219        form.exec();
00220 
00221        filterChanged();
00222        
00223        constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
00224 }
00225 
00226 void ConstraintActivitiesOccupyMaxDifferentRoomsForm::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        SpaceConstraint* ctr=this->visibleConstraintsList.at(i);
00237 
00238        ModifyConstraintActivitiesOccupyMaxDifferentRoomsForm form(this, (ConstraintActivitiesOccupyMaxDifferentRooms*)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 ConstraintActivitiesOccupyMaxDifferentRoomsForm::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        SpaceConstraint* 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.removeSpaceConstraint(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 ConstraintActivitiesOccupyMaxDifferentRoomsForm::help()
00295 {
00296        QString s=QString("");
00297        
00298        s+=tr("IMPORTANT NOTE: Please be careful with this constraint, as its overuse may lead to a slowdown of generation or even to an impossible timetable.");
00299        s+=QString("\n\n");
00300        s+=tr("This constraint type was added on 29 April 2012.");
00301        s+=QString("\n\n");
00302        s+=tr("To use this constraint, you need to specify a set of activities and the maximum number of different rooms which can be occupied by these "
00303         "activities. Note that each activity can take place in a single room, and a single room can hold a single activity at a certain moment. "
00304         "This constraint may be used for instance to constrain all activities of type Physics of a certain students set to be in the same room "
00305         "throughout the week, by adding all Physics activities for this students set and making max different rooms = 1.");
00306 
00307        LongTextMessageBox::largeInformation(this, tr("FET help"), s);
00308 }