Back to index

fet  5.18.0
addconstraintactivitiesoccupymaxdifferentroomsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintactivitiesoccupymaxdifferentroomsform.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 "addconstraintactivitiesoccupymaxdifferentroomsform.h"
00023 #include "spaceconstraint.h"
00024 
00025 #include <QListWidget>
00026 #include <QAbstractItemView>
00027 #include <QScrollBar>
00028 
00029 AddConstraintActivitiesOccupyMaxDifferentRoomsForm::AddConstraintActivitiesOccupyMaxDifferentRoomsForm(QWidget* parent): QDialog(parent)
00030 {
00031        setupUi(this);
00032 
00033        addConstraintPushButton->setDefault(true);
00034        
00035        allActivitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00036        selectedActivitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00037        
00038        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addCurrentConstraint()));
00039        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00040        connect(allActivitiesListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addActivity()));
00041        connect(addAllActivitiesPushButton, SIGNAL(clicked()), this, SLOT(addAllActivities()));
00042        connect(selectedActivitiesListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeActivity()));
00043        connect(clearPushButton, SIGNAL(clicked()), this, SLOT(clear()));
00044        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00045        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00046        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00047        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        
00049        centerWidgetOnScreen(this);
00050        restoreFETDialogGeometry(this);
00051                                                  
00052        maxDifferentRoomsSpinBox->setMinimum(1);
00053        maxDifferentRoomsSpinBox->setMaximum(MAX_ROOMS);
00054        maxDifferentRoomsSpinBox->setValue(1);
00055 
00056        //activities
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 
00066        teachersComboBox->addItem("");
00067        for(int i=0; i<gt.rules.teachersList.size(); i++){
00068               Teacher* tch=gt.rules.teachersList[i];
00069               teachersComboBox->addItem(tch->name);
00070        }
00071        teachersComboBox->setCurrentIndex(0);
00072 
00073        subjectsComboBox->addItem("");
00074        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00075               Subject* sb=gt.rules.subjectsList[i];
00076               subjectsComboBox->addItem(sb->name);
00077        }
00078        subjectsComboBox->setCurrentIndex(0);
00079 
00080        activityTagsComboBox->addItem("");
00081        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00082               ActivityTag* st=gt.rules.activityTagsList[i];
00083               activityTagsComboBox->addItem(st->name);
00084        }
00085        activityTagsComboBox->setCurrentIndex(0);
00086 
00087        studentsComboBox->addItem("");
00088        for(int i=0; i<gt.rules.yearsList.size(); i++){
00089               StudentsYear* sty=gt.rules.yearsList[i];
00090               studentsComboBox->addItem(sty->name);
00091               for(int j=0; j<sty->groupsList.size(); j++){
00092                      StudentsGroup* stg=sty->groupsList[j];
00093                      studentsComboBox->addItem(stg->name);
00094                      for(int k=0; k<stg->subgroupsList.size(); k++){
00095                             StudentsSubgroup* sts=stg->subgroupsList[k];
00096                             studentsComboBox->addItem(sts->name);
00097                      }
00098               }
00099        }
00100        studentsComboBox->setCurrentIndex(0);
00101 
00102        selectedActivitiesListWidget->clear();
00103        selectedActivitiesList.clear();
00104 
00105        filterChanged();
00106 }
00107 
00108 AddConstraintActivitiesOccupyMaxDifferentRoomsForm::~AddConstraintActivitiesOccupyMaxDifferentRoomsForm()
00109 {
00110        saveFETDialogGeometry(this);
00111 }
00112 
00113 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::addCurrentConstraint()
00114 {
00115        SpaceConstraint *ctr=NULL;
00116 
00117        double weight;
00118        QString tmp=weightLineEdit->text();
00119        weight_sscanf(tmp, "%lf", &weight);
00120        if(weight<100.0 || weight>100.0){
00121               QMessageBox::warning(this, tr("FET information"),
00122                      tr("Invalid weight (percentage). It has to be 100"));
00123               return;
00124        }
00125 
00126        int maxDifferentRooms=maxDifferentRoomsSpinBox->value();
00127        
00128        if(this->selectedActivitiesList.count()==0){
00129               QMessageBox::warning(this, tr("FET information"),
00130                tr("Empty list of activities"));
00131               return;
00132        }
00133        if(this->selectedActivitiesList.count()==1){
00134               QMessageBox::warning(this, tr("FET information"),
00135                tr("Only one selected activity"));
00136               return;
00137        }
00138 
00139        ctr=new ConstraintActivitiesOccupyMaxDifferentRooms(weight, selectedActivitiesList, maxDifferentRooms);
00140 
00141        bool tmp2=gt.rules.addSpaceConstraint(ctr);
00142        if(tmp2)
00143               LongTextMessageBox::information(this, tr("FET information"),
00144                      tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
00145        else{
00146               QMessageBox::warning(this, tr("FET information"),
00147                      tr("Constraint NOT added - please report error"));
00148               delete ctr;
00149        }
00150 }
00151 
00152 //activities
00153 bool AddConstraintActivitiesOccupyMaxDifferentRoomsForm::filterOk(Activity* act)
00154 {
00155        QString tn=teachersComboBox->currentText();
00156        QString stn=studentsComboBox->currentText();
00157        QString sbn=subjectsComboBox->currentText();
00158        QString sbtn=activityTagsComboBox->currentText();
00159        int ok=true;
00160 
00161        //teacher
00162        if(tn!=""){
00163               bool ok2=false;
00164               for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00165                      if(*it == tn){
00166                             ok2=true;
00167                             break;
00168                      }
00169               if(!ok2)
00170                      ok=false;
00171        }
00172 
00173        //subject
00174        if(sbn!="" && sbn!=act->subjectName)
00175               ok=false;
00176               
00177        //activity tag
00178        if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00179               ok=false;
00180               
00181        //students
00182        if(stn!=""){
00183               bool ok2=false;
00184               for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00185                      if(*it == stn){
00186                             ok2=true;
00187                             break;
00188                      }
00189               if(!ok2)
00190                      ok=false;
00191        }
00192        
00193        return ok;
00194 }
00195 
00196 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::filterChanged()
00197 {
00198        this->updateActivitiesListWidget();
00199 }
00200 
00201 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::updateActivitiesListWidget()
00202 {
00203        allActivitiesListWidget->clear();
00204        this->activitiesList.clear();
00205        
00206        for(int i=0; i<gt.rules.activitiesList.size(); i++){
00207               Activity* ac=gt.rules.activitiesList[i];
00208               if(filterOk(ac)){
00209                      allActivitiesListWidget->addItem(ac->getDescription(gt.rules));
00210                      this->activitiesList.append(ac->id);
00211               }
00212        }
00213        
00214        int q=allActivitiesListWidget->verticalScrollBar()->minimum();
00215        allActivitiesListWidget->verticalScrollBar()->setValue(q);
00216 }
00217 
00218 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::addActivity()
00219 {
00220        if(allActivitiesListWidget->currentRow()<0)
00221               return;
00222        int tmp=allActivitiesListWidget->currentRow();
00223        int _id=this->activitiesList.at(tmp);
00224        
00225        QString actName=allActivitiesListWidget->currentItem()->text();
00226        assert(actName!="");
00227        int i;
00228        //duplicate?
00229        for(i=0; i<selectedActivitiesListWidget->count(); i++)
00230               if(actName==selectedActivitiesListWidget->item(i)->text())
00231                      break;
00232        if(i<selectedActivitiesListWidget->count())
00233               return;
00234        selectedActivitiesListWidget->addItem(actName);
00235        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00236        
00237        this->selectedActivitiesList.append(_id);
00238 }
00239 
00240 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::addAllActivities()
00241 {
00242        for(int tmp=0; tmp<allActivitiesListWidget->count(); tmp++){
00243               //int tmp=allActivitiesListWidget->currentRow();
00244               int _id=this->activitiesList.at(tmp);
00245        
00246               QString actName=allActivitiesListWidget->item(tmp)->text();
00247               assert(actName!="");
00248               int i;
00249               //duplicate?
00250               for(i=0; i<selectedActivitiesList.count(); i++)
00251                      if(selectedActivitiesList.at(i)==_id)
00252                             break;
00253               if(i<selectedActivitiesList.count())
00254                      continue;
00255               
00256               /*for(i=0; i<selectedActivitiesListWidget->count(); i++)
00257                      if(actName==selectedActivitiesListWidget->item(i)->text())
00258                             break;
00259               if(i<selectedActivitiesListWidget->count())
00260                      continue;*/
00261                      
00262               selectedActivitiesListWidget->addItem(actName);
00263               this->selectedActivitiesList.append(_id);
00264        }
00265        
00266        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00267 }
00268 
00269 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::removeActivity()
00270 {
00271        if(selectedActivitiesListWidget->currentRow()<0 || selectedActivitiesListWidget->count()<=0)
00272               return;
00273        int tmp=selectedActivitiesListWidget->currentRow();
00274        
00275        selectedActivitiesList.removeAt(tmp);
00276 
00277        selectedActivitiesListWidget->setCurrentRow(-1);
00278        QListWidgetItem* item=selectedActivitiesListWidget->takeItem(tmp);
00279        delete item;
00280        if(tmp<selectedActivitiesListWidget->count())
00281               selectedActivitiesListWidget->setCurrentRow(tmp);
00282        else
00283               selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00284 }
00285 
00286 void AddConstraintActivitiesOccupyMaxDifferentRoomsForm::clear()
00287 {
00288        selectedActivitiesListWidget->clear();
00289        selectedActivitiesList.clear();
00290 }