Back to index

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