Back to index

fet  5.18.0
addconstraintstudentssetnotavailabletimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintstudentssetnotavailabletimesform.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 "longtextmessagebox.h"
00023 
00024 #include "addconstraintstudentssetnotavailabletimesform.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("X"))
00035 #define NO           (QString(" "))
00036 
00037 AddConstraintStudentsSetNotAvailableTimesForm::AddConstraintStudentsSetNotAvailableTimesForm(QWidget* parent): QDialog(parent)
00038 {
00039        setupUi(this);
00040 
00041        addConstraintPushButton->setDefault(true);
00042 
00043        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addCurrentConstraint()));
00044        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00045        connect(notAllowedTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00046        connect(pushButton8, SIGNAL(clicked()), this, SLOT(help()));
00047        connect(setAllAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllAvailable()));
00048        connect(setAllNotAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllNotAvailable()));
00049 
00050        centerWidgetOnScreen(this);
00051        restoreFETDialogGeometry(this);
00052 
00053        QSize tmp2=studentsComboBox->minimumSizeHint();
00054        Q_UNUSED(tmp2);
00055        
00056        updateStudentsSetComboBox();
00057 
00058        notAllowedTimesTable->setRowCount(gt.rules.nHoursPerDay);
00059        notAllowedTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00060 
00061        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00062               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00063               notAllowedTimesTable->setHorizontalHeaderItem(j, item);
00064        }
00065        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00066               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00067               notAllowedTimesTable->setVerticalHeaderItem(i, item);
00068        }
00069 
00070        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00071               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00072                      QTableWidgetItem* item=new QTableWidgetItem(NO);
00073                      item->setTextAlignment(Qt::AlignCenter);
00074                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00075                      colorItem(item);
00076                      notAllowedTimesTable->setItem(i, j, item);
00077               }
00078               
00079        notAllowedTimesTable->resizeRowsToContents();
00080 
00081        connect(notAllowedTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00082        connect(notAllowedTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00083 
00084        notAllowedTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00085 
00086        tableWidgetUpdateBug(notAllowedTimesTable);
00087        
00088        setStretchAvailabilityTableNicely(notAllowedTimesTable);
00089 }
00090 
00091 AddConstraintStudentsSetNotAvailableTimesForm::~AddConstraintStudentsSetNotAvailableTimesForm()
00092 {
00093        saveFETDialogGeometry(this);
00094 }
00095 
00096 void AddConstraintStudentsSetNotAvailableTimesForm::colorItem(QTableWidgetItem* item)
00097 {
00098        if(USE_GUI_COLORS){
00099               if(item->text()==NO)
00100                      item->setBackground(QBrush(Qt::darkGreen));
00101               else
00102                      item->setBackground(QBrush(Qt::darkRed));
00103               item->setForeground(QBrush(Qt::lightGray));
00104        }
00105 }
00106 
00107 void AddConstraintStudentsSetNotAvailableTimesForm::horizontalHeaderClicked(int col)
00108 {
00109        if(col>=0 && col<gt.rules.nDaysPerWeek){
00110               QString s=notAllowedTimesTable->item(0, col)->text();
00111               if(s==YES)
00112                      s=NO;
00113               else{
00114                      assert(s==NO);
00115                      s=YES;
00116               }
00117 
00118               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00119                      notAllowedTimesTable->item(row, col)->setText(s);
00120                      colorItem(notAllowedTimesTable->item(row,col));
00121               }
00122               tableWidgetUpdateBug(notAllowedTimesTable);
00123        }
00124 }
00125 
00126 void AddConstraintStudentsSetNotAvailableTimesForm::verticalHeaderClicked(int row)
00127 {
00128        if(row>=0 && row<gt.rules.nHoursPerDay){
00129               QString s=notAllowedTimesTable->item(row, 0)->text();
00130               if(s==YES)
00131                      s=NO;
00132               else{
00133                      assert(s==NO);
00134                      s=YES;
00135               }
00136        
00137               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00138                      notAllowedTimesTable->item(row, col)->setText(s);
00139                      colorItem(notAllowedTimesTable->item(row,col));
00140               }
00141               tableWidgetUpdateBug(notAllowedTimesTable);
00142        }
00143 }
00144 
00145 void AddConstraintStudentsSetNotAvailableTimesForm::setAllAvailable()
00146 {
00147        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00148               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00149                      notAllowedTimesTable->item(i, j)->setText(NO);
00150                      colorItem(notAllowedTimesTable->item(i,j));
00151               }
00152        tableWidgetUpdateBug(notAllowedTimesTable);
00153 }
00154 
00155 void AddConstraintStudentsSetNotAvailableTimesForm::setAllNotAvailable()
00156 {
00157        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00158               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00159                      notAllowedTimesTable->item(i, j)->setText(YES);
00160                      colorItem(notAllowedTimesTable->item(i,j));
00161               }
00162        tableWidgetUpdateBug(notAllowedTimesTable);
00163 }
00164 
00165 void AddConstraintStudentsSetNotAvailableTimesForm::updateStudentsSetComboBox()
00166 {
00167        studentsComboBox->clear();  
00168        for(int i=0; i<gt.rules.yearsList.size(); i++){
00169               StudentsYear* sty=gt.rules.yearsList[i];
00170               studentsComboBox->addItem(sty->name);
00171               for(int j=0; j<sty->groupsList.size(); j++){
00172                      StudentsGroup* stg=sty->groupsList[j];
00173                      studentsComboBox->addItem(stg->name);
00174                      for(int k=0; k<stg->subgroupsList.size(); k++){
00175                             StudentsSubgroup* sts=stg->subgroupsList[k];
00176                             studentsComboBox->addItem(sts->name);
00177                      }
00178               }
00179        }
00180 }
00181 
00182 void AddConstraintStudentsSetNotAvailableTimesForm::itemClicked(QTableWidgetItem* item)
00183 {
00184        QString s=item->text();
00185        if(s==YES)
00186               s=NO;
00187        else{
00188               assert(s==NO);
00189               s=YES;
00190        }
00191        item->setText(s);
00192        colorItem(item);
00193        tableWidgetUpdateBug(notAllowedTimesTable);
00194 }
00195 
00196 void AddConstraintStudentsSetNotAvailableTimesForm::addCurrentConstraint()
00197 {
00198        TimeConstraint *ctr=NULL;
00199 
00200        double weight;
00201        QString tmp=weightLineEdit->text();
00202        weight_sscanf(tmp, "%lf", &weight);
00203        if(weight<100.0 || weight>100.0){
00204               QMessageBox::warning(this, tr("FET information"),
00205                      tr("Invalid weight (percentage). It has to be 100"));
00206               return;
00207        }
00208 
00209        QString students_name=studentsComboBox->currentText();
00210        StudentsSet* s=gt.rules.searchStudentsSet(students_name);
00211        if(s==NULL){
00212               QMessageBox::warning(this, tr("FET information"),
00213                      tr("Invalid students set"));
00214               return;
00215        }
00216 
00217        QList<int> days;
00218        QList<int> hours;
00219        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00220               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00221                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00222                             days.append(j);
00223                             hours.append(i);
00224                      }
00225 
00226        ctr=new ConstraintStudentsSetNotAvailableTimes(weight, students_name, days, hours);
00227 
00228        bool tmp2=gt.rules.addTimeConstraint(ctr);
00229        if(tmp2)
00230               LongTextMessageBox::information(this, tr("FET information"),
00231                      tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
00232        else{
00233               QMessageBox::warning(this, tr("FET information"),
00234                      tr("Constraint NOT added - there must be another constraint of this "
00235                        "type referring to the same students set. Please edit that one"));
00236               delete ctr;
00237        }
00238 }
00239 
00240 void AddConstraintStudentsSetNotAvailableTimesForm::help()
00241 {
00242        QString s;
00243        
00244        s=tr("This constraint does not induce gaps (or early gaps) for students. If a student set has lessons"
00245         " before and after a not available period, gaps will not be counted.");
00246         
00247        s+="\n\n";
00248         
00249        s+=tr("If you really need to use weight under 100%, you can use activities preferred times with"
00250         " only the students set specified, but this might generate problems, as possible gaps will be"
00251         " counted and you may obtain an impossible timetable");
00252 
00253        QMessageBox::information(this, tr("FET help"), s);
00254 }