Back to index

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