Back to index

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