Back to index

fet  5.18.0
addconstraintactivitypreferredstartingtimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintactivitypreferredstartingtimesform.cpp  -  description
00003                              -------------------
00004     begin                : Wed Apr 23 2003
00005     copyright            : (C) 2003 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 "tablewidgetupdatebug.h"
00021 
00022 #include "longtextmessagebox.h"
00023 
00024 #include "addconstraintactivitypreferredstartingtimesform.h"
00025 #include "timeconstraint.h"
00026 
00027 #include <QHeaderView>
00028 #include <QTableWidget>
00029 #include <QTableWidgetItem>
00030 
00031 #include <QBrush>
00032 #include <QColor>
00033 
00034 #define YES          (QString(" "))
00035 #define NO           (QString("X"))
00036 
00037 AddConstraintActivityPreferredStartingTimesForm::AddConstraintActivityPreferredStartingTimesForm(QWidget* parent): QDialog(parent)
00038 {
00039        setupUi(this);
00040 
00041        addConstraintPushButton->setDefault(true);
00042 
00043        connect(preferredTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00044        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
00045        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00046        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00047        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00049        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00050        connect(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsAllowed()));
00051        connect(setAllNotAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsNotAllowed()));
00052 
00053        centerWidgetOnScreen(this);
00054        restoreFETDialogGeometry(this);
00055               
00056        QSize tmp1=teachersComboBox->minimumSizeHint();
00057        Q_UNUSED(tmp1);
00058        QSize tmp2=studentsComboBox->minimumSizeHint();
00059        Q_UNUSED(tmp2);
00060        QSize tmp3=subjectsComboBox->minimumSizeHint();
00061        Q_UNUSED(tmp3);
00062        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00063        Q_UNUSED(tmp4);
00064        
00065        QSize tmp5=activitiesComboBox->minimumSizeHint();
00066        Q_UNUSED(tmp5);
00067 
00068        activitiesComboBox->setMaximumWidth(maxRecommendedWidth(this));
00069        
00070        teachersComboBox->addItem("");
00071        for(int i=0; i<gt.rules.teachersList.size(); i++){
00072               Teacher* tch=gt.rules.teachersList[i];
00073               teachersComboBox->addItem(tch->name);
00074        }
00075        teachersComboBox->setCurrentIndex(0);
00076 
00077        subjectsComboBox->addItem("");
00078        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00079               Subject* sb=gt.rules.subjectsList[i];
00080               subjectsComboBox->addItem(sb->name);
00081        }
00082        subjectsComboBox->setCurrentIndex(0);
00083 
00084        activityTagsComboBox->addItem("");
00085        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00086               ActivityTag* st=gt.rules.activityTagsList[i];
00087               activityTagsComboBox->addItem(st->name);
00088        }
00089        activityTagsComboBox->setCurrentIndex(0);
00090 
00091        studentsComboBox->addItem("");
00092        for(int i=0; i<gt.rules.yearsList.size(); i++){
00093               StudentsYear* sty=gt.rules.yearsList[i];
00094               studentsComboBox->addItem(sty->name);
00095               for(int j=0; j<sty->groupsList.size(); j++){
00096                      StudentsGroup* stg=sty->groupsList[j];
00097                      studentsComboBox->addItem(stg->name);
00098                      for(int k=0; k<stg->subgroupsList.size(); k++){
00099                             StudentsSubgroup* sts=stg->subgroupsList[k];
00100                             studentsComboBox->addItem(sts->name);
00101                      }
00102               }
00103        }
00104        studentsComboBox->setCurrentIndex(0);
00105        
00106        updateActivitiesComboBox();
00107 
00108        preferredTimesTable->setRowCount(gt.rules.nHoursPerDay);
00109        preferredTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00110 
00111        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00112               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00113               preferredTimesTable->setHorizontalHeaderItem(j, item);
00114        }
00115        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00116               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00117               preferredTimesTable->setVerticalHeaderItem(i, item);
00118        }
00119 
00120        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00121               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00122                      QTableWidgetItem* item=new QTableWidgetItem(YES);
00123                      item->setTextAlignment(Qt::AlignCenter);
00124                      item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
00125                      colorItem(item);
00126                      preferredTimesTable->setItem(i, j, item);
00127               }
00128               
00129        preferredTimesTable->resizeRowsToContents();
00130 
00131        connect(preferredTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00132        connect(preferredTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00133 
00134        preferredTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00135 
00136        tableWidgetUpdateBug(preferredTimesTable);
00137        
00138        setStretchAvailabilityTableNicely(preferredTimesTable);
00139 }
00140 
00141 AddConstraintActivityPreferredStartingTimesForm::~AddConstraintActivityPreferredStartingTimesForm()
00142 {
00143        saveFETDialogGeometry(this);
00144 }
00145 
00146 void AddConstraintActivityPreferredStartingTimesForm::colorItem(QTableWidgetItem* item)
00147 {
00148        if(USE_GUI_COLORS){
00149               if(item->text()==YES)
00150                      item->setBackground(QBrush(Qt::darkGreen));
00151               else
00152                      item->setBackground(QBrush(Qt::darkRed));
00153               item->setForeground(QBrush(Qt::lightGray));
00154        }
00155 }
00156 
00157 void AddConstraintActivityPreferredStartingTimesForm::horizontalHeaderClicked(int col)
00158 {
00159        if(col>=0 && col<gt.rules.nDaysPerWeek){
00160               QString s=preferredTimesTable->item(0, col)->text();
00161               if(s==YES)
00162                      s=NO;
00163               else{
00164                      assert(s==NO);
00165                      s=YES;
00166               }
00167 
00168               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00169                      /*QString s=notAllowedTimesTable->text(row, col);
00170                      if(s==YES)
00171                             s=NO;
00172                      else{
00173                             assert(s==NO);
00174                             s=YES;
00175                      }*/
00176                      preferredTimesTable->item(row, col)->setText(s);
00177                      colorItem(preferredTimesTable->item(row,col));
00178               }
00179               tableWidgetUpdateBug(preferredTimesTable);
00180        }
00181 }
00182 
00183 void AddConstraintActivityPreferredStartingTimesForm::verticalHeaderClicked(int row)
00184 {
00185        if(row>=0 && row<gt.rules.nHoursPerDay){
00186               QString s=preferredTimesTable->item(row, 0)->text();
00187               if(s==YES)
00188                      s=NO;
00189               else{
00190                      assert(s==NO);
00191                      s=YES;
00192               }
00193        
00194               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00195                      /*QString s=notAllowedTimesTable->text(row, col);
00196                      if(s==YES)
00197                             s=NO;
00198                      else{
00199                             assert(s==NO);
00200                             s=YES;
00201                      }*/
00202                      preferredTimesTable->item(row, col)->setText(s);
00203                      colorItem(preferredTimesTable->item(row,col));
00204               }
00205               tableWidgetUpdateBug(preferredTimesTable);
00206        }
00207 }
00208 
00209 void AddConstraintActivityPreferredStartingTimesForm::setAllSlotsAllowed()
00210 {
00211        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00212               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00213                      preferredTimesTable->item(i, j)->setText(YES);
00214                      colorItem(preferredTimesTable->item(i, j));
00215               }
00216        tableWidgetUpdateBug(preferredTimesTable);
00217 }
00218 
00219 void AddConstraintActivityPreferredStartingTimesForm::setAllSlotsNotAllowed()
00220 {
00221        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00222               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00223                      preferredTimesTable->item(i, j)->setText(NO);
00224                      colorItem(preferredTimesTable->item(i, j));
00225               }
00226        tableWidgetUpdateBug(preferredTimesTable);
00227 }
00228 
00229 bool AddConstraintActivityPreferredStartingTimesForm::filterOk(Activity* act)
00230 {
00231        QString tn=teachersComboBox->currentText();
00232        QString stn=studentsComboBox->currentText();
00233        QString sbn=subjectsComboBox->currentText();
00234        QString sbtn=activityTagsComboBox->currentText();
00235        int ok=true;
00236 
00237        //teacher
00238        if(tn!=""){
00239               bool ok2=false;
00240               for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00241                      if(*it == tn){
00242                             ok2=true;
00243                             break;
00244                      }
00245               if(!ok2)
00246                      ok=false;
00247        }
00248 
00249        //subject
00250        if(sbn!="" && sbn!=act->subjectName)
00251               ok=false;
00252               
00253        //activity tag
00254 //     if(sbtn!="" && sbtn!=act->activityTagName)
00255        if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00256               ok=false;
00257               
00258        //students
00259        if(stn!=""){
00260               bool ok2=false;
00261               for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00262                      if(*it == stn){
00263                             ok2=true;
00264                             break;
00265                      }
00266               if(!ok2)
00267                      ok=false;
00268        }
00269        
00270        return ok;
00271 }
00272 
00273 void AddConstraintActivityPreferredStartingTimesForm::filterChanged()
00274 {
00275        this->updateActivitiesComboBox();
00276 }
00277 
00278 void AddConstraintActivityPreferredStartingTimesForm::itemClicked(QTableWidgetItem* item)
00279 {
00280        QString s=item->text();
00281        
00282        if(s==YES)
00283               s=NO;
00284        else{
00285               assert(s==NO);
00286               s=YES;
00287        }
00288        item->setText(s);
00289        colorItem(item);
00290 
00291        tableWidgetUpdateBug(preferredTimesTable);
00292 }
00293 
00294 void AddConstraintActivityPreferredStartingTimesForm::updateActivitiesComboBox(){
00295        activitiesComboBox->clear();
00296        activitiesList.clear();
00297        
00298        for(int i=0; i<gt.rules.activitiesList.size(); i++){
00299               Activity* act=gt.rules.activitiesList[i];
00300               
00301               if(filterOk(act)){
00302                      activitiesComboBox->addItem(act->getDescription(gt.rules));
00303                      this->activitiesList.append(act->id);
00304               }
00305        }
00306 }
00307 
00308 void AddConstraintActivityPreferredStartingTimesForm::addConstraint()
00309 {
00310        TimeConstraint *ctr=NULL;
00311 
00312        double weight;
00313        QString tmp=weightLineEdit->text();
00314        weight_sscanf(tmp, "%lf", &weight);
00315        if(weight<0.0 || weight>100.0){
00316               QMessageBox::warning(this, tr("FET information"),
00317                      tr("Invalid weight (percentage)"));
00318               return;
00319        }
00320 
00321        /*bool compulsory=false;
00322        if(compulsoryCheckBox->isChecked())
00323               compulsory=true;*/
00324 
00325        int i=activitiesComboBox->currentIndex();
00326        assert(i<activitiesList.size());
00327        if(i<0 || activitiesComboBox->count()<=0){
00328               QMessageBox::warning(this, tr("FET information"),
00329                      tr("Invalid activity"));
00330               return;
00331        }
00332        int id=activitiesList.at(i);
00333        //Activity* act=gt.rules.activitiesList.at(id);
00334        
00335        QList<int> days_L;
00336        QList<int> hours_L;
00337        //int days[MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES];
00338        //int hours[MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES];
00339        int n=0;
00340        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00341               for(i=0; i<gt.rules.nHoursPerDay; i++)
00342                      if(preferredTimesTable->item(i, j)->text()==YES){
00343                             /*if(n>=MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES){
00344                                    QString s=tr("Not enough slots (too many \"Yes\" values).");
00345                                    s+="\n";
00346                                    s+=tr("Please increase the variable MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES");
00347                                    s+="\n";
00348                                    s+=tr("Currently, it is %1").arg(MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES);
00349                                    QMessageBox::warning(this, tr("FET information"), s);
00350                                    
00351                                    return;
00352                             }*/
00353                             
00354                             days_L.append(j);
00355                             hours_L.append(i);
00356                             n++;
00357                      }
00358 
00359        if(n<=0){
00360               int t=QMessageBox::question(this, tr("FET question"),
00361                tr("Warning: 0 slots selected. Are you sure?"),
00362                QMessageBox::Yes, QMessageBox::Cancel);
00363                                            
00364               if(t==QMessageBox::Cancel)
00365                             return;
00366        }
00367 
00368        /*foreach(TimeConstraint* tc, gt.rules.timeConstraintsList){
00369               if(tc->type==CONSTRAINT_ACTIVITY_PREFERRED_STARTING_TIMES){
00370                      ConstraintActivityPreferredStartingTimes* c=(ConstraintActivityPreferredStartingTimes*) tc;
00371                      if(c->activityId==id){
00372                             QMessageBox::warning(this, tr("FET information"),
00373                              tr("This activity id has other constraint of this type attached\n"
00374                              "Please remove the other constraints of type activity preferred starting times\n"
00375                              "referring to this activity before proceeding"));
00376                             return;
00377                      }
00378               }
00379        }*/
00380 
00381        ctr=new ConstraintActivityPreferredStartingTimes(weight, /*compulsory,*/ /*act->*/id, n, days_L, hours_L);
00382 
00383        bool tmp2=gt.rules.addTimeConstraint(ctr);
00384        if(tmp2){
00385               QString s=tr("Constraint added:");
00386               s+="\n\n";
00387               s+=ctr->getDetailedDescription(gt.rules);
00388               LongTextMessageBox::information(this, tr("FET information"), s);
00389        }
00390        else{
00391               QMessageBox::warning(this, tr("FET information"),
00392                      tr("Constraint NOT added - duplicate?"));
00393               delete ctr;
00394        }
00395 }
00396 
00397 #undef YES
00398 #undef NO