Back to index

fet  5.18.0
modifyconstraintstudentssetnotavailabletimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintstudentssetnotavailabletimesform.cpp  -  description
00003                              -------------------
00004     begin                : Feb 10, 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 "tablewidgetupdatebug.h"
00021 
00022 #include "modifyconstraintstudentssetnotavailabletimesform.h"
00023 #include "timeconstraint.h"
00024 
00025 #include <QHeaderView>
00026 #include <QTableWidget>
00027 #include <QTableWidgetItem>
00028 
00029 #include <QBrush>
00030 #include <QColor>
00031 
00032 #define YES          (QString("X"))
00033 #define NO           (QString(" "))
00034 
00035 ModifyConstraintStudentsSetNotAvailableTimesForm::ModifyConstraintStudentsSetNotAvailableTimesForm(QWidget* parent, ConstraintStudentsSetNotAvailableTimes* ctr): QDialog(parent)
00036 {
00037        setupUi(this);
00038 
00039        okPushButton->setDefault(true);
00040 
00041        connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
00042        connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
00043        connect(notAllowedTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00044        connect(setAllAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllAvailable()));
00045        connect(setAllNotAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllNotAvailable()));
00046 
00047        centerWidgetOnScreen(this);
00048        restoreFETDialogGeometry(this);
00049 
00050        QSize tmp2=studentsComboBox->minimumSizeHint();
00051        Q_UNUSED(tmp2);
00052               
00053        this->_ctr=ctr;
00054        
00055        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00056        
00057        updateStudentsComboBox();
00058 
00059        notAllowedTimesTable->setRowCount(gt.rules.nHoursPerDay);
00060        notAllowedTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00061 
00062        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00063               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00064               notAllowedTimesTable->setHorizontalHeaderItem(j, item);
00065        }
00066        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00067               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00068               notAllowedTimesTable->setVerticalHeaderItem(i, item);
00069        }
00070 
00071        //bool currentMatrix[MAX_HOURS_PER_DAY][MAX_DAYS_PER_WEEK];
00072        Matrix2D<bool> currentMatrix;
00073        currentMatrix.resize(gt.rules.nHoursPerDay, gt.rules.nDaysPerWeek);
00074 
00075        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00076               for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00077                      currentMatrix[i][j]=false;                
00078        assert(ctr->days.count()==ctr->hours.count());   
00079        for(int k=0; k<ctr->days.count(); k++){
00080               if(ctr->hours.at(k)==-1 || ctr->days.at(k)==-1)
00081                      assert(0);
00082               int i=ctr->hours.at(k);
00083               int j=ctr->days.at(k);
00084               if(i>=0 && i<gt.rules.nHoursPerDay && j>=0 && j<gt.rules.nDaysPerWeek)
00085                      currentMatrix[i][j]=true;
00086        }
00087 
00088        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00089               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00090                      QTableWidgetItem* item= new QTableWidgetItem();
00091                      item->setTextAlignment(Qt::AlignCenter);
00092                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00093                      notAllowedTimesTable->setItem(i, j, item);
00094 
00095                      if(!currentMatrix[i][j])
00096                             item->setText(NO);
00097                      else
00098                             item->setText(YES);
00099                             
00100                      colorItem(item);
00101               }
00102               
00103        notAllowedTimesTable->resizeRowsToContents();
00104 
00105        connect(notAllowedTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00106        connect(notAllowedTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00107 
00108        notAllowedTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00109        
00110        tableWidgetUpdateBug(notAllowedTimesTable);
00111        
00112        setStretchAvailabilityTableNicely(notAllowedTimesTable);
00113 }
00114 
00115 ModifyConstraintStudentsSetNotAvailableTimesForm::~ModifyConstraintStudentsSetNotAvailableTimesForm()
00116 {
00117        saveFETDialogGeometry(this);
00118 }
00119 
00120 void ModifyConstraintStudentsSetNotAvailableTimesForm::colorItem(QTableWidgetItem* item)
00121 {
00122        if(USE_GUI_COLORS){
00123               if(item->text()==NO)
00124                      item->setBackground(QBrush(Qt::darkGreen));
00125               else
00126                      item->setBackground(QBrush(Qt::darkRed));
00127               item->setForeground(QBrush(Qt::lightGray));
00128        }
00129 }
00130 
00131 void ModifyConstraintStudentsSetNotAvailableTimesForm::horizontalHeaderClicked(int col)
00132 {
00133        if(col>=0 && col<gt.rules.nDaysPerWeek){
00134               QString s=notAllowedTimesTable->item(0, col)->text();
00135               if(s==YES)
00136                      s=NO;
00137               else{
00138                      assert(s==NO);
00139                      s=YES;
00140               }
00141 
00142               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00143                      notAllowedTimesTable->item(row, col)->setText(s);
00144                      colorItem(notAllowedTimesTable->item(row,col));
00145               }
00146               tableWidgetUpdateBug(notAllowedTimesTable);
00147        }
00148 }
00149 
00150 void ModifyConstraintStudentsSetNotAvailableTimesForm::verticalHeaderClicked(int row)
00151 {
00152        if(row>=0 && row<gt.rules.nHoursPerDay){
00153               QString s=notAllowedTimesTable->item(row, 0)->text();
00154               if(s==YES)
00155                      s=NO;
00156               else{
00157                      assert(s==NO);
00158                      s=YES;
00159               }
00160        
00161               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00162                      notAllowedTimesTable->item(row, col)->setText(s);
00163                      colorItem(notAllowedTimesTable->item(row,col));
00164               }
00165        tableWidgetUpdateBug(notAllowedTimesTable);
00166        }
00167 }
00168 
00169 void ModifyConstraintStudentsSetNotAvailableTimesForm::setAllAvailable()
00170 {
00171        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00172               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00173                      notAllowedTimesTable->item(i, j)->setText(NO);
00174                      colorItem(notAllowedTimesTable->item(i,j));
00175               }
00176        tableWidgetUpdateBug(notAllowedTimesTable);
00177 }
00178 
00179 void ModifyConstraintStudentsSetNotAvailableTimesForm::setAllNotAvailable()
00180 {
00181        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00182               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00183                      notAllowedTimesTable->item(i, j)->setText(YES);
00184                      colorItem(notAllowedTimesTable->item(i,j));
00185               }
00186        tableWidgetUpdateBug(notAllowedTimesTable);
00187 }
00188 
00189 void ModifyConstraintStudentsSetNotAvailableTimesForm::itemClicked(QTableWidgetItem* item)
00190 {
00191        QString s=item->text();
00192        if(s==YES)
00193               s=NO;
00194        else{
00195               assert(s==NO);
00196               s=YES;
00197        }
00198        item->setText(s);
00199        colorItem(item);
00200 
00201        tableWidgetUpdateBug(notAllowedTimesTable);
00202 }
00203 
00204 void ModifyConstraintStudentsSetNotAvailableTimesForm::updateStudentsComboBox(){
00205        studentsComboBox->clear();
00206        int i=0, j=-1;
00207        for(int m=0; m<gt.rules.yearsList.size(); m++){
00208               StudentsYear* sty=gt.rules.yearsList[m];
00209               studentsComboBox->addItem(sty->name);
00210               if(sty->name==this->_ctr->students)
00211                      j=i;
00212               i++;
00213               for(int n=0; n<sty->groupsList.size(); n++){
00214                      StudentsGroup* stg=sty->groupsList[n];
00215                      studentsComboBox->addItem(stg->name);
00216                      if(stg->name==this->_ctr->students)
00217                             j=i;
00218                      i++;
00219                      for(int p=0; p<stg->subgroupsList.size(); p++){
00220                             StudentsSubgroup* sts=stg->subgroupsList[p];
00221                             studentsComboBox->addItem(sts->name);
00222                             if(sts->name==this->_ctr->students)
00223                                    j=i;
00224                             i++;
00225                      }
00226               }
00227        }
00228        assert(j>=0);
00229        studentsComboBox->setCurrentIndex(j);                                                                                                              
00230 }
00231 
00232 void ModifyConstraintStudentsSetNotAvailableTimesForm::ok()
00233 {
00234        double weight;
00235        QString tmp=weightLineEdit->text();
00236        weight_sscanf(tmp, "%lf", &weight);
00237        if(weight<100.0 || weight>100.0){
00238               QMessageBox::warning(this, tr("FET information"),
00239                      tr("Invalid weight (percentage). It has to be 100"));
00240               return;
00241        }
00242 
00243        QString students_name=studentsComboBox->currentText();
00244        StudentsSet* s=gt.rules.searchStudentsSet(students_name);
00245        if(s==NULL){
00246               QMessageBox::warning(this, tr("FET information"),
00247                      tr("Invalid students set"));
00248               return;
00249        }
00250 
00251        foreach(TimeConstraint* c, gt.rules.timeConstraintsList)
00252               if(c!=this->_ctr && c->type==CONSTRAINT_STUDENTS_SET_NOT_AVAILABLE_TIMES){
00253                      ConstraintStudentsSetNotAvailableTimes* cc=(ConstraintStudentsSetNotAvailableTimes*)c;
00254                      if(cc->students==students_name){
00255                             QMessageBox::warning(this, tr("FET information"),
00256                             tr("A constraint of this type exists for the same students set - cannot proceed"));
00257                             return;
00258                      }
00259               }                                                                                                                                                                                                   
00260                                                                                                                                                                                              
00261        this->_ctr->weightPercentage=weight;
00262        this->_ctr->students=students_name;
00263 
00264        QList<int> days;
00265        QList<int> hours;
00266        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00267               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00268                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00269                             days.append(j);
00270                             hours.append(i);
00271                      }
00272 
00273        this->_ctr->days=days;
00274        this->_ctr->hours=hours;
00275 
00276        gt.rules.internalStructureComputed=false;
00277        setRulesModifiedAndOtherThings(&gt.rules);
00278        
00279        this->close();
00280 }
00281 
00282 void ModifyConstraintStudentsSetNotAvailableTimesForm::cancel()
00283 {
00284        this->close();
00285 }