Back to index

fet  5.18.0
addconstraintactivitiessamestartingtimeform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintactivitiessamestartingtimeform.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 "addconstraintactivitiessamestartingtimeform.h"
00023 
00024 #include "matrix.h"
00025 
00026 #include <QListWidget>
00027 #include <QAbstractItemView>
00028 #include <QScrollBar>
00029 
00030 AddConstraintActivitiesSameStartingTimeForm::AddConstraintActivitiesSameStartingTimeForm(QWidget* parent): QDialog(parent)
00031 {
00032        setupUi(this);
00033 
00034        addConstraintPushButton->setDefault(true);
00035        
00036        activitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00037        simultaneousActivitiesListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00038 
00039        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00040        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
00041        connect(helpPushButton, SIGNAL(clicked()), this, SLOT(help()));
00042        connect(blockCheckBox, SIGNAL(toggled(bool)), this, SLOT(blockChanged()));
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        connect(activitiesListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(addActivity()));
00049        connect(addAllActivitiesPushButton, SIGNAL(clicked()), this, SLOT(addAllActivities()));
00050        connect(simultaneousActivitiesListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeActivity()));
00051 
00052        centerWidgetOnScreen(this);
00053        restoreFETDialogGeometry(this);
00054        
00055        QSize tmp1=teachersComboBox->minimumSizeHint();
00056        Q_UNUSED(tmp1);
00057        QSize tmp2=studentsComboBox->minimumSizeHint();
00058        Q_UNUSED(tmp2);
00059        QSize tmp3=subjectsComboBox->minimumSizeHint();
00060        Q_UNUSED(tmp3);
00061        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00062        Q_UNUSED(tmp4);
00063        
00064        teachersComboBox->addItem("");
00065        for(int i=0; i<gt.rules.teachersList.size(); i++){
00066               Teacher* tch=gt.rules.teachersList[i];
00067               teachersComboBox->addItem(tch->name);
00068        }
00069        teachersComboBox->setCurrentIndex(0);
00070 
00071        subjectsComboBox->addItem("");
00072        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00073               Subject* sb=gt.rules.subjectsList[i];
00074               subjectsComboBox->addItem(sb->name);
00075        }
00076        subjectsComboBox->setCurrentIndex(0);
00077 
00078        activityTagsComboBox->addItem("");
00079        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00080               ActivityTag* st=gt.rules.activityTagsList[i];
00081               activityTagsComboBox->addItem(st->name);
00082        }
00083        activityTagsComboBox->setCurrentIndex(0);
00084 
00085        studentsComboBox->addItem("");
00086        for(int i=0; i<gt.rules.yearsList.size(); i++){
00087               StudentsYear* sty=gt.rules.yearsList[i];
00088               studentsComboBox->addItem(sty->name);
00089               for(int j=0; j<sty->groupsList.size(); j++){
00090                      StudentsGroup* stg=sty->groupsList[j];
00091                      studentsComboBox->addItem(stg->name);
00092                      for(int k=0; k<stg->subgroupsList.size(); k++){
00093                             StudentsSubgroup* sts=stg->subgroupsList[k];
00094                             studentsComboBox->addItem(sts->name);
00095                      }
00096               }
00097        }
00098        studentsComboBox->setCurrentIndex(0);
00099        
00100        simultaneousActivitiesListWidget->clear();
00101        this->simultaneousActivitiesList.clear();
00102 
00103        updateActivitiesListWidget();
00104 }
00105 
00106 AddConstraintActivitiesSameStartingTimeForm::~AddConstraintActivitiesSameStartingTimeForm()
00107 {
00108        saveFETDialogGeometry(this);
00109 }
00110 
00111 bool AddConstraintActivitiesSameStartingTimeForm::filterOk(Activity* act)
00112 {
00113        QString tn=teachersComboBox->currentText();
00114        QString stn=studentsComboBox->currentText();
00115        QString sbn=subjectsComboBox->currentText();
00116        QString sbtn=activityTagsComboBox->currentText();
00117        int ok=true;
00118 
00119        //teacher
00120        if(tn!=""){
00121               bool ok2=false;
00122               for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00123                      if(*it == tn){
00124                             ok2=true;
00125                             break;
00126                      }
00127               if(!ok2)
00128                      ok=false;
00129        }
00130 
00131        //subject
00132        if(sbn!="" && sbn!=act->subjectName)
00133               ok=false;
00134               
00135        //activity tag
00136        if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00137               ok=false;
00138               
00139        //students
00140        if(stn!=""){
00141               bool ok2=false;
00142               for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00143                      if(*it == stn){
00144                             ok2=true;
00145                             break;
00146                      }
00147               if(!ok2)
00148                      ok=false;
00149        }
00150        
00151        return ok;
00152 }
00153 
00154 void AddConstraintActivitiesSameStartingTimeForm::filterChanged()
00155 {
00156        this->updateActivitiesListWidget();
00157 }
00158 
00159 void AddConstraintActivitiesSameStartingTimeForm::updateActivitiesListWidget()
00160 {
00161        activitiesListWidget->clear();
00162        this->activitiesList.clear();
00163 
00164        if(blockCheckBox->isChecked())
00165               //show only non-split activities and split activities which are the representatives
00166               for(int i=0; i<gt.rules.activitiesList.size(); i++){
00167                      Activity* ac=gt.rules.activitiesList[i];
00168                      if(filterOk(ac)){
00169                             if(ac->activityGroupId==0){
00170                                    activitiesListWidget->addItem(ac->getDescription(gt.rules));
00171                                    this->activitiesList.append(ac->id);
00172                             }
00173                             else if(ac->id==ac->activityGroupId){
00174                                    activitiesListWidget->addItem(ac->getDescription(gt.rules));
00175                                    this->activitiesList.append(ac->id);
00176                             }
00177                      }
00178               }
00179        else
00180               for(int i=0; i<gt.rules.activitiesList.size(); i++){
00181                      Activity* ac=gt.rules.activitiesList[i];
00182                      if(filterOk(ac)){
00183                             activitiesListWidget->addItem(ac->getDescription(gt.rules));
00184                             this->activitiesList.append(ac->id);
00185                      }
00186               }
00187        
00188        int q=activitiesListWidget->verticalScrollBar()->minimum();
00189        activitiesListWidget->verticalScrollBar()->setValue(q);
00190 }
00191 
00192 void AddConstraintActivitiesSameStartingTimeForm::blockChanged()
00193 {
00194        simultaneousActivitiesListWidget->clear();
00195        this->simultaneousActivitiesList.clear();
00196 
00197        updateActivitiesListWidget();
00198 }
00199 
00200 void AddConstraintActivitiesSameStartingTimeForm::addConstraint()
00201 {
00202        TimeConstraint *ctr=NULL;
00203 
00204        double weight;
00205        QString tmp=weightLineEdit->text();
00206        weight_sscanf(tmp, "%lf", &weight);
00207        if(weight<0.0 || weight>100.0){
00208               QMessageBox::warning(this, tr("FET information"),
00209                      tr("Invalid weight (percentage)"));
00210               return;
00211        }
00212 
00213        if(this->simultaneousActivitiesList.count()==0){
00214               QMessageBox::warning(this, tr("FET information"),
00215                      tr("Empty list of simultaneous activities"));
00216               return;
00217        }
00218        if(this->simultaneousActivitiesList.count()==1){
00219               QMessageBox::warning(this, tr("FET information"),
00220                      tr("Only one selected activity - impossible"));
00221               return;
00222        }
00223        
00224        if(blockCheckBox->isChecked()){ //block constraints
00226               int nConstraints=0;
00227               QList<int>::iterator it;
00228               for(it=this->simultaneousActivitiesList.begin(); it!=this->simultaneousActivitiesList.end(); it++){
00229                      int _id=(*it);
00230                      int tmp=0; //tmp represents the number of sub-activities represented by the current (sub)activity
00231 
00232                      for(int i=0; i<gt.rules.activitiesList.size(); i++){
00233                             Activity* act=gt.rules.activitiesList[i];
00234                             if(act->activityGroupId==0){
00235                                    if(act->id==_id){
00236                                           assert(tmp==0);
00237                                           tmp=1;
00238                                    }
00239                             }
00240                             else{
00241                                    if(act->id==_id){
00242                                           assert(act->activityGroupId==act->id);
00243                                           assert(tmp==0);
00244                                           tmp=1;
00245                                    }
00246                                    else if(act->activityGroupId==_id)
00247                                           tmp++;
00248                             }
00249                      }
00250 
00251                      if(nConstraints==0){
00252                             nConstraints=tmp;
00253                      }
00254                      else{
00255                             if(tmp!=nConstraints){
00256                                    QString s=tr("Sub-activities do not correspond. Mistake:");
00257                                    s+="\n";
00258                                    s+=tr("1. First (sub)activity has id=%1 and represents %2 sub-activities")
00259                                           .arg(this->simultaneousActivitiesList.at(0))
00260                                           .arg(nConstraints);
00261                                    s+="\n";
00262                                    s+=tr("2. Current (sub)activity has id=%1 and represents %2 sub-activities")
00263                                           .arg(_id)
00264                                           .arg(tmp);
00265                                    QMessageBox::warning(this, tr("FET information"), s);
00266                                    return;                            
00267                             }
00268                      }
00269               }
00270        
00272               Matrix1D<QList<int> > ids;
00273               ids.resize(nConstraints);
00274 
00275               for(int i=0; i<nConstraints; i++)
00276                      ids[i].clear();
00277               int k;
00278               for(k=0, it=this->simultaneousActivitiesList.begin(); it!=this->simultaneousActivitiesList.end(); k++, it++){
00279                      int _id=(*it);
00280                      int tmp=0; //tmp represents the number of sub-activities represented by the current (sub)activity
00281 
00282                      for(int i=0; i<gt.rules.activitiesList.size(); i++){
00283                             Activity* act=gt.rules.activitiesList[i];
00284                             if(act->activityGroupId==0){
00285                                    if(act->id==_id){
00286                                           assert(tmp==0);
00287                                           assert(ids[tmp].count()==k);
00288                                           ids[tmp].append(_id);
00289                                           tmp=1;
00290                                    }
00291                             }
00292                             else{
00293                                    if(act->id==_id){
00294                                           assert(act->activityGroupId==act->id);
00295                                           assert(tmp==0);
00296                                           assert(ids[tmp].count()==k);
00297                                           ids[tmp].append(_id);
00298                                           tmp=1;
00299                                    }
00300                                    else if(act->activityGroupId==_id){
00301                                           assert(ids[tmp].count()==k);
00302                                           ids[tmp].append(act->id);
00303                                           tmp++;
00304                                    }
00305                             }
00306                      }
00307               }
00308        
00310               for(k=0; k<nConstraints; k++){
00311                      ctr=new ConstraintActivitiesSameStartingTime(weight, this->simultaneousActivitiesList.count(), ids[k]);
00312                      bool tmp2=gt.rules.addTimeConstraint(ctr);
00313                      
00314                      if(tmp2){
00315                             QString s;
00316 
00317                             s+=tr("Constraint added:");
00318                             s+="\n\n";
00319                             s+=ctr->getDetailedDescription(gt.rules);
00320                             LongTextMessageBox::information(this, tr("FET information"), s);
00321                      }
00322                      else{
00323                             QMessageBox::warning(this, tr("FET information"),
00324                                    tr("Constraint NOT added - please report error"));
00325                             delete ctr;
00326                      }
00327               }
00328        }
00329        else{
00330               QList<int> ids;
00331               QList<int>::iterator it;
00332               int i;
00333               ids.clear();
00334               for(i=0, it=this->simultaneousActivitiesList.begin(); it!=this->simultaneousActivitiesList.end(); i++,it++){
00335                      ids.append(*it);
00336               }
00337               ctr=new ConstraintActivitiesSameStartingTime(weight, this->simultaneousActivitiesList.count(), ids);
00338 
00339               bool tmp2=gt.rules.addTimeConstraint(ctr);
00340               
00341               if(tmp2){
00342                      QString s;
00343 
00344                      s+=tr("Constraint added:");
00345                      s+="\n\n";
00346                      s+=ctr->getDetailedDescription(gt.rules);
00347                      LongTextMessageBox::information(this, tr("FET information"), s);
00348               }
00349               else{
00350                      QMessageBox::warning(this, tr("FET information"),
00351                             tr("Constraint NOT added - please report error"));
00352                      delete ctr;
00353               }
00354        }
00355 }
00356 
00357 void AddConstraintActivitiesSameStartingTimeForm::addActivity()
00358 {
00359        if(activitiesListWidget->currentRow()<0)
00360               return;
00361        int tmp=activitiesListWidget->currentRow();
00362        int _id=this->activitiesList.at(tmp);
00363        
00364        QString actName=activitiesListWidget->currentItem()->text();
00365        assert(actName!="");
00366        int i;
00367        //duplicate?
00368        for(i=0; i<simultaneousActivitiesListWidget->count(); i++)
00369               if(actName==simultaneousActivitiesListWidget->item(i)->text())
00370                      break;
00371        if(i<simultaneousActivitiesListWidget->count())
00372               return;
00373        
00374        simultaneousActivitiesListWidget->addItem(actName);
00375        simultaneousActivitiesListWidget->setCurrentRow(simultaneousActivitiesListWidget->count()-1);
00376        
00377        this->simultaneousActivitiesList.append(_id);
00378 }
00379 
00380 void AddConstraintActivitiesSameStartingTimeForm::addAllActivities()
00381 {
00382        for(int tmp=0; tmp<activitiesListWidget->count(); tmp++){
00383               int _id=this->activitiesList.at(tmp);
00384        
00385               QString actName=activitiesListWidget->item(tmp)->text();
00386               assert(actName!="");
00387               int i;
00388               //duplicate?
00389               for(i=0; i<simultaneousActivitiesList.count(); i++)
00390                      if(simultaneousActivitiesList.at(i)==_id)
00391                             break;
00392               if(i<simultaneousActivitiesList.count())
00393                      continue;
00394                      
00395               simultaneousActivitiesListWidget->addItem(actName);
00396               this->simultaneousActivitiesList.append(_id);
00397        }
00398        
00399        simultaneousActivitiesListWidget->setCurrentRow(simultaneousActivitiesListWidget->count()-1);
00400 }
00401 
00402 void AddConstraintActivitiesSameStartingTimeForm::removeActivity()
00403 {
00404        if(simultaneousActivitiesListWidget->currentRow()<0 || simultaneousActivitiesListWidget->count()<=0)
00405               return;
00406        int tmp=simultaneousActivitiesListWidget->currentRow();
00407        
00408        this->simultaneousActivitiesList.removeAt(tmp);
00409        
00410        simultaneousActivitiesListWidget->setCurrentRow(-1);
00411        QListWidgetItem* item=simultaneousActivitiesListWidget->takeItem(tmp);
00412        delete item;
00413        if(tmp<simultaneousActivitiesListWidget->count())
00414               simultaneousActivitiesListWidget->setCurrentRow(tmp);
00415        else
00416               simultaneousActivitiesListWidget->setCurrentRow(simultaneousActivitiesListWidget->count()-1);
00417 }
00418 
00419 void AddConstraintActivitiesSameStartingTimeForm::clear()
00420 {
00421        simultaneousActivitiesListWidget->clear();
00422        simultaneousActivitiesList.clear();
00423 }
00424 
00425 void AddConstraintActivitiesSameStartingTimeForm::help()
00426 {
00427        QString s;
00428        
00429        s=tr("Add multiple constraints: this is a check box. Select this if you want to input only the representatives of sub-activities and FET to add multiple constraints,"
00430        " for all sub-activities from the same components, in turn, respectively."
00431        " There will be added more constraints activities same starting time, one for each corresponding tuple. The number of"
00432        " sub-activities must match for the representants and be careful to the order, to be what you need");
00433 
00434        LongTextMessageBox::largeInformation(this, tr("FET help"), s);
00435 }