Back to index

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