Back to index

fet  5.18.0
addconstraintteachernotavailabletimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintteachernotavailabletimesform.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 "addconstraintteachernotavailabletimesform.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 AddConstraintTeacherNotAvailableTimesForm::AddConstraintTeacherNotAvailableTimesForm(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(pushButton5, 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 tmp1=teachersComboBox->minimumSizeHint();
00054        Q_UNUSED(tmp1);
00055 
00056        updateTeachersComboBox();
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 AddConstraintTeacherNotAvailableTimesForm::~AddConstraintTeacherNotAvailableTimesForm()
00092 {
00093        saveFETDialogGeometry(this);
00094 }
00095 
00096 void AddConstraintTeacherNotAvailableTimesForm::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 AddConstraintTeacherNotAvailableTimesForm::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 AddConstraintTeacherNotAvailableTimesForm::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 AddConstraintTeacherNotAvailableTimesForm::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 AddConstraintTeacherNotAvailableTimesForm::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 AddConstraintTeacherNotAvailableTimesForm::updateTeachersComboBox(){
00166        teachersComboBox->clear();
00167        for(int i=0; i<gt.rules.teachersList.size(); i++){
00168               Teacher* tch=gt.rules.teachersList[i];
00169               teachersComboBox->addItem(tch->name);
00170        }
00171 }
00172 
00173 void AddConstraintTeacherNotAvailableTimesForm::itemClicked(QTableWidgetItem* item)
00174 {
00175        QString s=item->text();
00176        if(s==YES)
00177               s=NO;
00178        else{
00179               assert(s==NO);
00180               s=YES;
00181        }
00182        item->setText(s);
00183        colorItem(item);
00184 
00185        tableWidgetUpdateBug(notAllowedTimesTable);
00186 }
00187 
00188 void AddConstraintTeacherNotAvailableTimesForm::addCurrentConstraint()
00189 {
00190        TimeConstraint *ctr=NULL;
00191 
00192        double weight;
00193        QString tmp=weightLineEdit->text();
00194        weight_sscanf(tmp, "%lf", &weight);
00195        if(weight<100.0 || weight>100.0){
00196               QMessageBox::warning(this, tr("FET information"),
00197                      tr("Invalid weight (percentage). It has to be 100"));
00198               return;
00199        }
00200 
00201        QString teacher_name=teachersComboBox->currentText();
00202        int teacher_ID=gt.rules.searchTeacher(teacher_name);
00203        if(teacher_ID<0){
00204               QMessageBox::warning(this, tr("FET information"),
00205                      tr("Invalid teacher"));
00206               return;
00207        }
00208 
00209        QList<int> days;
00210        QList<int> hours;
00211        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00212               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00213                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00214                             days.append(j);
00215                             hours.append(i);
00216                      }
00217 
00218        ctr=new ConstraintTeacherNotAvailableTimes(weight, teacher_name, days, hours);
00219 
00220        bool tmp2=gt.rules.addTimeConstraint(ctr);
00221        if(tmp2)
00222               LongTextMessageBox::information(this, tr("FET information"),
00223                      tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
00224        else{
00225               QMessageBox::warning(this, tr("FET information"),
00226                      tr("Constraint NOT added - there must be another constraint of this "
00227                        "type referring to the same teacher. Please edit that one"));
00228               delete ctr;
00229        }
00230 }
00231 
00232 void AddConstraintTeacherNotAvailableTimesForm::help()
00233 {
00234        QString s;
00235        
00236        s=tr("This constraint does not induce gaps for teachers. If a teacher has lessons"
00237         " before and after a not available period, gaps will not be counted.");
00238         
00239        s+="\n\n";
00240         
00241        s+=tr("If you really need to use weight under 100%, you can use activities preferred times with"
00242         " only the teacher specified, but this might generate problems, as possible gaps will be"
00243         " counted and you may obtain an impossible timetable.");
00244 
00245        QMessageBox::information(this, tr("FET help"), s);
00246 }