Back to index

fet  5.18.0
modifyconstraintmindaysbetweenactivitiesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintmindaysbetweenactivitiesform.cpp  -  description
00003                              -------------------
00004     begin                : Feb 11, 2005
00005     copyright            : (C) 2005 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 "modifyconstraintmindaysbetweenactivitiesform.h"
00021 #include "spaceconstraint.h"
00022 
00023 #include <QList>
00024 
00025 #include <QListWidget>
00026 #include <QAbstractItemView>
00027 #include <QScrollBar>
00028 
00029 ModifyConstraintMinDaysBetweenActivitiesForm::ModifyConstraintMinDaysBetweenActivitiesForm(QWidget* parent, ConstraintMinDaysBetweenActivities* 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               this->selectedActivitiesListWidget->addItem(act->getDescription(gt.rules));
00077        }
00078        
00079        minDaysSpinBox->setMinimum(1);
00080        minDaysSpinBox->setMaximum(gt.rules.nDaysPerWeek-1);
00081        minDaysSpinBox->setValue(ctr->minDays);
00082 
00083        consecutiveIfSameDayCheckBox->setChecked(ctr->consecutiveIfSameDay);
00084        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00085 
00087        teachersComboBox->addItem("");
00088        for(int i=0; i<gt.rules.teachersList.size(); i++){
00089               Teacher* tch=gt.rules.teachersList[i];
00090               teachersComboBox->addItem(tch->name);
00091        }
00092        teachersComboBox->setCurrentIndex(0);
00093 
00094        subjectsComboBox->addItem("");
00095        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00096               Subject* sb=gt.rules.subjectsList[i];
00097               subjectsComboBox->addItem(sb->name);
00098        }
00099        subjectsComboBox->setCurrentIndex(0);
00100 
00101        activityTagsComboBox->addItem("");
00102        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00103               ActivityTag* st=gt.rules.activityTagsList[i];
00104               activityTagsComboBox->addItem(st->name);
00105        }
00106        activityTagsComboBox->setCurrentIndex(0);
00107 
00108        studentsComboBox->addItem("");
00109        for(int i=0; i<gt.rules.yearsList.size(); i++){
00110               StudentsYear* sty=gt.rules.yearsList[i];
00111               studentsComboBox->addItem(sty->name);
00112               for(int j=0; j<sty->groupsList.size(); j++){
00113                      StudentsGroup* stg=sty->groupsList[j];
00114                      studentsComboBox->addItem(stg->name);
00115                      for(int k=0; k<stg->subgroupsList.size(); k++){
00116                             StudentsSubgroup* sts=stg->subgroupsList[k];
00117                             studentsComboBox->addItem(sts->name);
00118                      }
00119               }
00120        }
00121        studentsComboBox->setCurrentIndex(0);
00122 
00123        filterChanged();
00124 }
00125 
00126 ModifyConstraintMinDaysBetweenActivitiesForm::~ModifyConstraintMinDaysBetweenActivitiesForm()
00127 {
00128        saveFETDialogGeometry(this);
00129 }
00130 
00131 void ModifyConstraintMinDaysBetweenActivitiesForm::filterChanged()
00132 {
00133        activitiesListWidget->clear();
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 ModifyConstraintMinDaysBetweenActivitiesForm::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 ModifyConstraintMinDaysBetweenActivitiesForm::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 #if 0
00214        if(0 && this->selectedActivitiesList.size()>gt.rules.nDaysPerWeek){
00215               QString s=tr("You want to add a constraint min days between activities for more activities than the number of days per week."
00216                 " This is a very bad practice from the way the algorithm of generation works (it slows down the generation and makes it harder to find a solution).")+
00217                "\n\n"+
00218                tr("The best way to add the activities would be:")+
00219                "\n\n"+
00220 
00221                tr("1. If you add 'force consecutive if same day', then couple extra activities in pairs to obtain a number of activities equal to the number of days per week"
00222                 ". Example: 7 activities with duration 1 in a 5 days week, then transform into 5 activities with durations: 2,2,1,1,1 and add a single container activity with these 5 components"
00223                 " (possibly raising the weight of added constraint min days between activities up to 100%)")+
00224 
00225                 "\n\n"+
00226 
00227                tr("2. If you don't add 'force consecutive if same day', then add a larger activity splitted into a number of"
00228                 " activities equal with the number of days per week and the remaining components into other larger splitted activity."
00229                 " For example, suppose you need to add 7 activities with duration 1 in a 5 days week. Add 2 larger container activities,"
00230                 " first one splitted into 5 activities with duration 1 and second one splitted into 2 activities with duration 1"
00231                 " (possibly raising the weight of added constraints min days between activities for each of the 2 containers up to 100%)")+
00232 
00233                "\n\n"+
00234                tr("Do you want to add current constraint as it is now (not recommended) or cancel and edit as instructed?");
00235        
00236               int t=QMessageBox::warning(this, tr("FET warning"),     s,
00237                QMessageBox::Yes, QMessageBox::Cancel);
00238               if(t==QMessageBox::Cancel)
00239                      return;
00240        }
00241 #endif
00242 
00243        if(1){
00244               ConstraintMinDaysBetweenActivities adc;
00245 
00246               int i;
00247               QList<int>::iterator it;
00248               adc.activitiesId.clear();
00249               for(i=0, it=this->selectedActivitiesList.begin(); it!=this->selectedActivitiesList.end(); it++, i++){
00250                      adc.activitiesId.append(*it);
00251               }
00252               adc.n_activities=i;
00253               
00254               adc.weightPercentage=weight;
00255               adc.consecutiveIfSameDay=consecutiveIfSameDayCheckBox->isChecked();
00256               adc.minDays=minDaysSpinBox->value();
00257               
00258               bool duplicate=false;
00259               
00260               foreach(TimeConstraint* tc, gt.rules.timeConstraintsList)
00261                      if(tc!=this->_ctr && tc->type==CONSTRAINT_MIN_DAYS_BETWEEN_ACTIVITIES)
00262                             if( ( *((ConstraintMinDaysBetweenActivities*)tc) ) == adc){
00263                                    duplicate=true;
00264                                    break;
00265                             }
00266                             
00267               if(duplicate){
00268                      QMessageBox::warning(this, tr("FET information"), tr("Cannot proceed, current constraint is equal to another one (it is duplicated)"));
00269                      return;
00270               }
00271        }
00272        
00273        int i;
00274        QList<int>::iterator it;
00275        this->_ctr->activitiesId.clear();
00276        for(i=0, it=this->selectedActivitiesList.begin(); it!=this->selectedActivitiesList.end(); it++, i++){
00277               this->_ctr->activitiesId.append(*it);
00278        }
00279        this->_ctr->n_activities=i;
00280               
00281        this->_ctr->weightPercentage=weight;
00282        this->_ctr->consecutiveIfSameDay=consecutiveIfSameDayCheckBox->isChecked();
00283        this->_ctr->minDays=minDaysSpinBox->value();
00284        
00285        gt.rules.internalStructureComputed=false;
00286        setRulesModifiedAndOtherThings(&gt.rules);
00287        
00288        this->close();
00289 }
00290 
00291 void ModifyConstraintMinDaysBetweenActivitiesForm::cancel()
00292 {
00293        this->close();
00294 }
00295 
00296 void ModifyConstraintMinDaysBetweenActivitiesForm::addActivity()
00297 {
00298        if(activitiesListWidget->currentRow()<0)
00299               return;
00300        int tmp=activitiesListWidget->currentRow();
00301        int _id=this->activitiesList.at(tmp);
00302        
00303        QString actName=activitiesListWidget->currentItem()->text();
00304        assert(actName!="");
00305        int i;
00306        //duplicate?
00307        for(i=0; i<selectedActivitiesListWidget->count(); i++)
00308               if(actName==selectedActivitiesListWidget->item(i)->text())
00309                      break;
00310        if(i<selectedActivitiesListWidget->count())
00311               return;
00312        selectedActivitiesListWidget->addItem(actName);
00313        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00314        
00315        this->selectedActivitiesList.append(_id);
00316 }
00317 
00318 void ModifyConstraintMinDaysBetweenActivitiesForm::addAllActivities()
00319 {
00320        for(int tmp=0; tmp<activitiesListWidget->count(); tmp++){
00321               int _id=this->activitiesList.at(tmp);
00322        
00323               QString actName=activitiesListWidget->item(tmp)->text();
00324               assert(actName!="");
00325               int i;
00326               //duplicate?
00327               for(i=0; i<selectedActivitiesList.count(); i++)
00328                      if(selectedActivitiesList.at(i)==_id)
00329                             break;
00330               if(i<selectedActivitiesList.count())
00331                      continue;
00332                      
00333               selectedActivitiesListWidget->addItem(actName);
00334               this->selectedActivitiesList.append(_id);
00335        }
00336        
00337        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00338 }
00339 
00340 void ModifyConstraintMinDaysBetweenActivitiesForm::removeActivity()
00341 {
00342        if(selectedActivitiesListWidget->currentRow()<0 || selectedActivitiesListWidget->count()<=0)
00343               return;
00344        int tmp=selectedActivitiesListWidget->currentRow();
00345        
00346        this->selectedActivitiesList.removeAt(tmp);
00347 
00348        selectedActivitiesListWidget->setCurrentRow(-1);
00349        QListWidgetItem* item=selectedActivitiesListWidget->takeItem(tmp);
00350        delete item;
00351        if(tmp<selectedActivitiesListWidget->count())
00352               selectedActivitiesListWidget->setCurrentRow(tmp);
00353        else
00354               selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00355 }
00356 
00357 void ModifyConstraintMinDaysBetweenActivitiesForm::clear()
00358 {
00359        selectedActivitiesListWidget->clear();
00360        selectedActivitiesList.clear();
00361 }