Back to index

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