Back to index

fet  5.18.0
addconstraintmindaysbetweenactivitiesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintmindaysbetweenactivitiesform.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 "longtextmessagebox.h"
00021 
00022 #include "addconstraintmindaysbetweenactivitiesform.h"
00023 #include "spaceconstraint.h"
00024 
00025 #include <QListWidget>
00026 #include <QAbstractItemView>
00027 #include <QScrollBar>
00028 
00029 AddConstraintMinDaysBetweenActivitiesForm::AddConstraintMinDaysBetweenActivitiesForm(QWidget* parent): QDialog(parent)
00030 {
00031        setupUi(this);
00032 
00033        addConstraintPushButton->setDefault(true);
00034        
00035        activitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00036        selectedActivitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00037 
00038        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00039        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
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        connect(clearPushButton, SIGNAL(clicked()), this, SLOT(clear()));
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        minDaysSpinBox->setMinimum(1);
00062        minDaysSpinBox->setMaximum(gt.rules.nDaysPerWeek-1);
00063        minDaysSpinBox->setValue(1);
00064 
00065        teachersComboBox->addItem("");
00066        for(int i=0; i<gt.rules.teachersList.size(); i++){
00067               Teacher* tch=gt.rules.teachersList[i];
00068               teachersComboBox->addItem(tch->name);
00069        }
00070        teachersComboBox->setCurrentIndex(0);
00071 
00072        subjectsComboBox->addItem("");
00073        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00074               Subject* sb=gt.rules.subjectsList[i];
00075               subjectsComboBox->addItem(sb->name);
00076        }
00077        subjectsComboBox->setCurrentIndex(0);
00078 
00079        activityTagsComboBox->addItem("");
00080        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00081               ActivityTag* st=gt.rules.activityTagsList[i];
00082               activityTagsComboBox->addItem(st->name);
00083        }
00084        activityTagsComboBox->setCurrentIndex(0);
00085 
00086        studentsComboBox->addItem("");
00087        for(int i=0; i<gt.rules.yearsList.size(); i++){
00088               StudentsYear* sty=gt.rules.yearsList[i];
00089               studentsComboBox->addItem(sty->name);
00090               for(int j=0; j<sty->groupsList.size(); j++){
00091                      StudentsGroup* stg=sty->groupsList[j];
00092                      studentsComboBox->addItem(stg->name);
00093                      for(int k=0; k<stg->subgroupsList.size(); k++){
00094                             StudentsSubgroup* sts=stg->subgroupsList[k];
00095                             studentsComboBox->addItem(sts->name);
00096                      }
00097               }
00098        }
00099        studentsComboBox->setCurrentIndex(0);
00100 
00101        selectedActivitiesListWidget->clear();
00102        this->selectedActivitiesList.clear();
00103 
00104        filterChanged();
00105 }
00106 
00107 AddConstraintMinDaysBetweenActivitiesForm::~AddConstraintMinDaysBetweenActivitiesForm()
00108 {
00109        saveFETDialogGeometry(this);
00110 }
00111 
00112 bool AddConstraintMinDaysBetweenActivitiesForm::filterOk(Activity* act)
00113 {
00114        QString tn=teachersComboBox->currentText();
00115        QString stn=studentsComboBox->currentText();
00116        QString sbn=subjectsComboBox->currentText();
00117        QString sbtn=activityTagsComboBox->currentText();
00118        int ok=true;
00119 
00120        //teacher
00121        if(tn!=""){
00122               bool ok2=false;
00123               for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00124                      if(*it == tn){
00125                             ok2=true;
00126                             break;
00127                      }
00128               if(!ok2)
00129                      ok=false;
00130        }
00131 
00132        //subject
00133        if(sbn!="" && sbn!=act->subjectName)
00134               ok=false;
00135               
00136        //activity tag
00137        if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00138               ok=false;
00139               
00140        //students
00141        if(stn!=""){
00142               bool ok2=false;
00143               for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00144                      if(*it == stn){
00145                             ok2=true;
00146                             break;
00147                      }
00148               if(!ok2)
00149                      ok=false;
00150        }
00151        
00152        return ok;
00153 }
00154 
00155 void AddConstraintMinDaysBetweenActivitiesForm::filterChanged()
00156 {
00157        this->updateActivitiesListWidget();
00158 }
00159 
00160 void AddConstraintMinDaysBetweenActivitiesForm::updateActivitiesListWidget()
00161 {
00162        activitiesListWidget->clear();
00163 
00164        this->activitiesList.clear();
00165 
00166        for(int i=0; i<gt.rules.activitiesList.size(); i++){
00167               Activity* ac=gt.rules.activitiesList[i];
00168               if(filterOk(ac)){
00169                      activitiesListWidget->addItem(ac->getDescription(gt.rules));
00170                      this->activitiesList.append(ac->id);
00171               }
00172        }
00173        
00174        int q=activitiesListWidget->verticalScrollBar()->minimum();
00175        activitiesListWidget->verticalScrollBar()->setValue(q);
00176 }
00177 
00178 void AddConstraintMinDaysBetweenActivitiesForm::addConstraint()
00179 {
00180        TimeConstraint *ctr=NULL;
00181 
00182        double weight;
00183        QString tmp=weightLineEdit->text();
00184        weight_sscanf(tmp, "%lf", &weight);
00185        if(weight<0.0 || weight>100){
00186               QMessageBox::warning(this, tr("FET information"),
00187                      tr("Invalid weight (percentage)"));
00188               return;
00189        }
00190 
00191        if(this->selectedActivitiesList.count()==0){
00192               QMessageBox::warning(this, tr("FET information"),
00193                      tr("Empty list of selected activities"));
00194               return;
00195        }
00196        if(this->selectedActivitiesList.count()==1){
00197               QMessageBox::warning(this, tr("FET information"),
00198                      tr("Only one selected activity"));
00199               return;
00200        }
00201 
00202 #if 0
00203        if(0 && this->selectedActivitiesList.size()>gt.rules.nDaysPerWeek){
00204               QString s=tr("You want to add a constraint min days between activities for more activities than the number of days per week."
00205                 " 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).")+
00206                "\n\n"+
00207                tr("The best way to add the activities would be:")+
00208                "\n\n"+
00209 
00210                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"
00211                 ". 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"
00212                 " (possibly raising the weight of added constraint min days between activities up to 100%)")+
00213 
00214                 "\n\n"+
00215 
00216                tr("2. If you don't add 'force consecutive if same day', then add a larger activity splitted into a number of"
00217                 " activities equal with the number of days per week and the remaining components into other larger splitted activity."
00218                 " For example, suppose you need to add 7 activities with duration 1 in a 5 days week. Add 2 larger container activities,"
00219                 " first one splitted into 5 activities with duration 1 and second one splitted into 2 activities with duration 1"
00220                 " (possibly raising the weight of added constraints min days between activities for each of the 2 containers up to 100%)")+
00221 
00222                "\n\n"+
00223 
00224                tr("Do you want to add current constraint as it is now (not recommended) or cancel and edit as instructed?");
00225        
00226               int t=QMessageBox::warning(this, tr("FET warning"), s,
00227                QMessageBox::Yes, QMessageBox::Cancel);
00228               if(t==QMessageBox::Cancel)
00229                      return;
00230        }
00231 #endif
00232        
00233        QList<int> ids;
00234        int i;
00235        QList<int>::iterator it;
00236        ids.clear();
00237        for(i=0, it=this->selectedActivitiesList.begin(); it!=this->selectedActivitiesList.end(); it++, i++){
00238               ids.append(*it);
00239        }
00240        
00241        ctr=new ConstraintMinDaysBetweenActivities(weight, consecutiveIfSameDayCheckBox->isChecked(), this->selectedActivitiesList.count(), ids, minDaysSpinBox->value());
00242 
00243        bool tmp2=gt.rules.addTimeConstraint(ctr);
00244        
00245        if(tmp2){
00246               QString s=tr("Constraint added:");
00247               s+="\n\n";
00248               s+=ctr->getDetailedDescription(gt.rules);
00249               LongTextMessageBox::information(this, tr("FET information"), s);
00250        }
00251        else{
00252               QMessageBox::warning(this, tr("FET information"),
00253                      tr("Constraint NOT added - it must be a duplicate"));
00254               delete ctr;
00255        }
00256 }
00257 
00258 void AddConstraintMinDaysBetweenActivitiesForm::addActivity()
00259 {
00260        if(activitiesListWidget->currentRow()<0)
00261               return;
00262        int tmp=activitiesListWidget->currentRow();
00263        int _id=this->activitiesList.at(tmp);
00264        
00265        QString actName=activitiesListWidget->currentItem()->text();
00266        assert(actName!="");
00267        int i;
00268        //duplicate?
00269        for(i=0; i<selectedActivitiesListWidget->count(); i++)
00270               if(actName==selectedActivitiesListWidget->item(i)->text())
00271                      break;
00272        if(i<selectedActivitiesListWidget->count())
00273               return;
00274        selectedActivitiesListWidget->addItem(actName);
00275        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00276        
00277        this->selectedActivitiesList.append(_id);
00278 }
00279 
00280 void AddConstraintMinDaysBetweenActivitiesForm::addAllActivities()
00281 {
00282        for(int tmp=0; tmp<activitiesListWidget->count(); tmp++){
00283               int _id=this->activitiesList.at(tmp);
00284        
00285               QString actName=activitiesListWidget->item(tmp)->text();
00286               assert(actName!="");
00287               int i;
00288               //duplicate?
00289               for(i=0; i<selectedActivitiesList.count(); i++)
00290                      if(selectedActivitiesList.at(i)==_id)
00291                             break;
00292               if(i<selectedActivitiesList.count())
00293                      continue;
00294                      
00295               selectedActivitiesListWidget->addItem(actName);
00296               this->selectedActivitiesList.append(_id);
00297        }
00298        
00299        selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00300 }
00301 
00302 void AddConstraintMinDaysBetweenActivitiesForm::removeActivity()
00303 {
00304        if(selectedActivitiesListWidget->currentRow()<0 || selectedActivitiesListWidget->count()<=0)
00305               return;
00306        int tmp=selectedActivitiesListWidget->currentRow();
00307        
00308        selectedActivitiesList.removeAt(tmp);
00309        
00310        selectedActivitiesListWidget->setCurrentRow(-1);
00311        QListWidgetItem* item=selectedActivitiesListWidget->takeItem(tmp);
00312        delete item;
00313        if(tmp<selectedActivitiesListWidget->count())
00314               selectedActivitiesListWidget->setCurrentRow(tmp);
00315        else
00316               selectedActivitiesListWidget->setCurrentRow(selectedActivitiesListWidget->count()-1);
00317 }
00318 
00319 void AddConstraintMinDaysBetweenActivitiesForm::clear()
00320 {
00321        selectedActivitiesListWidget->clear();
00322        selectedActivitiesList.clear();
00323 }