Back to index

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