Back to index

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