Back to index

fet  5.18.0
modifyconstraintactivitiespreferredstartingtimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintactivitiespreferredstartingtimesform.cpp  -  description
00003                              -------------------
00004     begin                : 15 May 2004
00005     copyright            : (C) 2004 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 "modifyconstraintactivitiespreferredstartingtimesform.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(" "))
00033 #define NO           (QString("X"))
00034 
00035 ModifyConstraintActivitiesPreferredStartingTimesForm::ModifyConstraintActivitiesPreferredStartingTimesForm(QWidget* parent, ConstraintActivitiesPreferredStartingTimes* ctr): QDialog(parent)
00036 {
00037        setupUi(this);
00038 
00039        okPushButton->setDefault(true);
00040 
00041        connect(preferredTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00042        connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
00043        connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
00044        connect(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsAllowed()));
00045        connect(setAllNotAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsNotAllowed()));
00046 
00047        centerWidgetOnScreen(this);
00048        restoreFETDialogGeometry(this);
00049 
00050        QSize tmp1=teachersComboBox->minimumSizeHint();
00051        Q_UNUSED(tmp1);
00052        QSize tmp2=studentsComboBox->minimumSizeHint();
00053        Q_UNUSED(tmp2);
00054        QSize tmp3=subjectsComboBox->minimumSizeHint();
00055        Q_UNUSED(tmp3);
00056        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00057        Q_UNUSED(tmp4);
00058        
00059        this->_ctr=ctr;
00060 
00061        updateTeachersComboBox();
00062        updateStudentsComboBox();
00063        updateSubjectsComboBox();
00064        updateActivityTagsComboBox();
00065 
00066        preferredTimesTable->setRowCount(gt.rules.nHoursPerDay);
00067        preferredTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00068 
00069        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00070               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00071               preferredTimesTable->setHorizontalHeaderItem(j, item);
00072        }
00073        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00074               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00075               preferredTimesTable->setVerticalHeaderItem(i, item);
00076        }
00077 
00078        //bool currentMatrix[MAX_HOURS_PER_DAY][MAX_DAYS_PER_WEEK];
00079        Matrix2D<bool> currentMatrix;
00080        currentMatrix.resize(gt.rules.nHoursPerDay, gt.rules.nDaysPerWeek);
00081        
00082        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00083               for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00084                      currentMatrix[i][j]=false;
00085        for(int k=0; k<ctr->nPreferredStartingTimes_L; k++){
00086               if(ctr->hours_L[k]==-1 || ctr->days_L[k]==-1)
00087                      assert(0);
00088               int i=ctr->hours_L[k];
00089               int j=ctr->days_L[k];
00090               if(i>=0 && i<gt.rules.nHoursPerDay && j>=0 && j<gt.rules.nDaysPerWeek)
00091                      currentMatrix[i][j]=true;
00092        }
00093 
00094        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00095               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00096                      QTableWidgetItem* item= new QTableWidgetItem();
00097                      item->setTextAlignment(Qt::AlignCenter);
00098                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00099                      preferredTimesTable->setItem(i, j, item);
00100                      
00101                      if(!currentMatrix[i][j])
00102                             item->setText(NO);
00103                      else
00104                             item->setText(YES);
00105                             
00106                      colorItem(item);
00107               }
00108               
00109        preferredTimesTable->resizeRowsToContents();
00110                             
00111        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00112 
00113        connect(preferredTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00114        connect(preferredTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00115 
00116        preferredTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00117 
00118        tableWidgetUpdateBug(preferredTimesTable);
00119        
00120        setStretchAvailabilityTableNicely(preferredTimesTable);
00121 }
00122 
00123 ModifyConstraintActivitiesPreferredStartingTimesForm::~ModifyConstraintActivitiesPreferredStartingTimesForm()
00124 {
00125        saveFETDialogGeometry(this);
00126 }
00127 
00128 void ModifyConstraintActivitiesPreferredStartingTimesForm::colorItem(QTableWidgetItem* item)
00129 {
00130        if(USE_GUI_COLORS){
00131               if(item->text()==YES)
00132                      item->setBackground(QBrush(Qt::darkGreen));
00133               else
00134                      item->setBackground(QBrush(Qt::darkRed));
00135               item->setForeground(QBrush(Qt::lightGray));
00136        }
00137 }
00138 
00139 void ModifyConstraintActivitiesPreferredStartingTimesForm::horizontalHeaderClicked(int col)
00140 {
00141        if(col>=0 && col<gt.rules.nDaysPerWeek){
00142               QString s=preferredTimesTable->item(0, col)->text();
00143               if(s==YES)
00144                      s=NO;
00145               else{
00146                      assert(s==NO);
00147                      s=YES;
00148               }
00149 
00150               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00151                      preferredTimesTable->item(row, col)->setText(s);
00152                      colorItem(preferredTimesTable->item(row,col));
00153               }
00154               tableWidgetUpdateBug(preferredTimesTable);
00155        }
00156 }
00157 
00158 void ModifyConstraintActivitiesPreferredStartingTimesForm::verticalHeaderClicked(int row)
00159 {
00160        if(row>=0 && row<gt.rules.nHoursPerDay){
00161               QString s=preferredTimesTable->item(row, 0)->text();
00162               if(s==YES)
00163                      s=NO;
00164               else{
00165                      assert(s==NO);
00166                      s=YES;
00167               }
00168        
00169               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00170                      preferredTimesTable->item(row, col)->setText(s);
00171                      colorItem(preferredTimesTable->item(row,col));
00172               }
00173               tableWidgetUpdateBug(preferredTimesTable);
00174        }
00175 }
00176 
00177 void ModifyConstraintActivitiesPreferredStartingTimesForm::setAllSlotsAllowed()
00178 {
00179        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00180               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00181                      preferredTimesTable->item(i, j)->setText(YES);
00182                      colorItem(preferredTimesTable->item(i,j));
00183               }
00184        tableWidgetUpdateBug(preferredTimesTable);
00185 }
00186 
00187 void ModifyConstraintActivitiesPreferredStartingTimesForm::setAllSlotsNotAllowed()
00188 {
00189        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00190               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00191                      preferredTimesTable->item(i, j)->setText(NO);
00192                      colorItem(preferredTimesTable->item(i,j));
00193               }
00194        tableWidgetUpdateBug(preferredTimesTable);
00195 }
00196 
00197 void ModifyConstraintActivitiesPreferredStartingTimesForm::itemClicked(QTableWidgetItem* item)
00198 {
00199        QString s=item->text();
00200        if(s==YES)
00201               s=NO;
00202        else{
00203               assert(s==NO);
00204               s=YES;
00205        }
00206        item->setText(s);
00207        colorItem(item);
00208 
00209        tableWidgetUpdateBug(preferredTimesTable);
00210 }
00211 
00212 void ModifyConstraintActivitiesPreferredStartingTimesForm::updateTeachersComboBox(){
00213        int i=0, j=-1;
00214        teachersComboBox->clear();
00215        teachersComboBox->addItem("");
00216        if(this->_ctr->teacherName=="")
00217               j=i;
00218        i++;
00219        for(int k=0; k<gt.rules.teachersList.size(); k++){
00220               Teacher* t=gt.rules.teachersList[k];
00221               teachersComboBox->addItem(t->name);
00222               if(t->name==this->_ctr->teacherName)
00223                      j=i;
00224               i++;
00225        }
00226        assert(j>=0);
00227        teachersComboBox->setCurrentIndex(j);
00228 }
00229 
00230 void ModifyConstraintActivitiesPreferredStartingTimesForm::updateStudentsComboBox(){
00231        int i=0, j=-1;
00232        studentsComboBox->clear();
00233        studentsComboBox->addItem("");
00234        if(this->_ctr->studentsName=="")
00235               j=i;
00236        i++;
00237        for(int m=0; m<gt.rules.yearsList.size(); m++){
00238               StudentsYear* sty=gt.rules.yearsList[m];
00239               studentsComboBox->addItem(sty->name);
00240               if(sty->name==this->_ctr->studentsName)
00241                      j=i;
00242               i++;
00243               for(int n=0; n<sty->groupsList.size(); n++){
00244                      StudentsGroup* stg=sty->groupsList[n];
00245                      studentsComboBox->addItem(stg->name);
00246                      if(stg->name==this->_ctr->studentsName)
00247                             j=i;
00248                      i++;
00249                      for(int p=0; p<stg->subgroupsList.size(); p++){
00250                             StudentsSubgroup* sts=stg->subgroupsList[p];
00251                             studentsComboBox->addItem(sts->name);
00252                             if(sts->name==this->_ctr->studentsName)
00253                                    j=i;
00254                             i++;
00255                      }
00256               }
00257        }
00258        assert(j>=0);
00259        studentsComboBox->setCurrentIndex(j);
00260 }
00261 
00262 void ModifyConstraintActivitiesPreferredStartingTimesForm::updateSubjectsComboBox(){
00263        int i=0, j=-1;
00264        subjectsComboBox->clear();
00265        subjectsComboBox->addItem("");
00266        if(this->_ctr->subjectName=="")
00267               j=i;
00268        i++;
00269        for(int k=0; k<gt.rules.subjectsList.size(); k++){
00270               Subject* s=gt.rules.subjectsList[k];
00271               subjectsComboBox->addItem(s->name);
00272               if(s->name==this->_ctr->subjectName)
00273                      j=i;
00274               i++;
00275        }
00276        assert(j>=0);
00277        subjectsComboBox->setCurrentIndex(j);
00278 }
00279 
00280 void ModifyConstraintActivitiesPreferredStartingTimesForm::updateActivityTagsComboBox(){
00281        int i=0, j=-1;
00282        activityTagsComboBox->clear();
00283        activityTagsComboBox->addItem("");
00284        if(this->_ctr->activityTagName=="")
00285               j=i;
00286        i++;
00287        for(int k=0; k<gt.rules.activityTagsList.size(); k++){
00288               ActivityTag* s=gt.rules.activityTagsList[k];
00289               activityTagsComboBox->addItem(s->name);
00290               if(s->name==this->_ctr->activityTagName)
00291                      j=i;
00292               i++;
00293        }
00294        assert(j>=0);
00295        activityTagsComboBox->setCurrentIndex(j);
00296 }
00297 
00298 void ModifyConstraintActivitiesPreferredStartingTimesForm::ok()
00299 {
00300        double weight;
00301        QString tmp=weightLineEdit->text();
00302        weight_sscanf(tmp, "%lf", &weight);
00303        if(weight<0.0 || weight>100.0){
00304               QMessageBox::warning(this, tr("FET information"),
00305                      tr("Invalid weight (percentage)"));
00306               return;
00307        }
00308 
00309        QString teacher=teachersComboBox->currentText();
00310        if(teacher!="")
00311               assert(gt.rules.searchTeacher(teacher)>=0);
00312 
00313        QString students=studentsComboBox->currentText();
00314        if(students!="")
00315               assert(gt.rules.searchStudentsSet(students)!=NULL);
00316 
00317        QString subject=subjectsComboBox->currentText();
00318        if(subject!="")
00319               assert(gt.rules.searchSubject(subject)>=0);
00320               
00321        QString activityTag=activityTagsComboBox->currentText();
00322        if(activityTag!="")
00323               assert(gt.rules.searchActivityTag(activityTag)>=0);
00324               
00325        if(teacher=="" && students=="" && subject=="" && activityTag==""){
00326               int t=QMessageBox::question(this, tr("FET question"),
00327                tr("You specified all the activities. This might be a small problem: if you specify"
00328                 " a not allowed slot between two allowed slots, this not allowed slot will"
00329                 " be counted as a gap in the teachers' and students' timetable.\n\n"
00330                 " The best practice would be to use constraint break times.\n\n"
00331                 " If you need weight under 100%, then you can use this constraint, but be careful"
00332                 " not to obtain an impossible timetable (if your teachers/students are constrained on gaps"
00333                 " or early gaps and if you leave a not allowed slot between 2 allowed slots or"
00334                 " a not allowed slot early in the day and more allowed slots after it,"
00335                 " this possible gap might be counted in teachers' and students' timetable)")
00336                 +"\n\n"+tr("Do you want to add current constraint?"),
00337                QMessageBox::Yes, QMessageBox::Cancel);
00338                                            
00339               if(t==QMessageBox::Cancel)
00340                             return;
00341        }
00342 
00343        if(teacher!="" && students=="" && subject=="" && activityTag==""){
00344               int t=QMessageBox::question(this, tr("FET question"),
00345                tr("You specified only the teacher. This might be a small problem: if you specify"
00346                 " a not allowed slot between two allowed slots, this not allowed slot will"
00347                 " be counted as a gap in the teacher's timetable.\n\n"
00348                 " The best practice would be to use constraint teacher not available times.\n\n"
00349                 " If you need weight under 100%, then you can use this constraint, but be careful"
00350                 " not to obtain an impossible timetable (if your teacher is constrained on gaps"
00351                 " and if you leave a not allowed slot between 2 allowed slots, this possible"
00352                 " gap might be counted in teacher's timetable)")
00353                 +"\n\n"+tr("Do you want to add current constraint?"),
00354                QMessageBox::Yes, QMessageBox::Cancel);
00355                                            
00356               if(t==QMessageBox::Cancel)
00357                             return;
00358        }
00359        if(teacher=="" && students!="" && subject=="" && activityTag==""){
00360               int t=QMessageBox::question(this, tr("FET question"),
00361                tr("You specified only the students set. This might be a small problem: if you specify"
00362                 " a not allowed slot between two allowed slots (or a not allowed slot before allowed slots),"
00363                 " this not allowed slot will"
00364                 " be counted as a gap (or early gap) in the students' timetable.\n\n"
00365                 " The best practice would be to use constraint students set not available times.\n\n"
00366                 " If you need weight under 100%, then you can use this constraint, but be careful"
00367                 " not to obtain an impossible timetable (if your students set is constrained on gaps or early gaps"
00368                 " and if you leave a not allowed slot between 2 allowed slots (or a not allowed slot before allowed slots), this possible"
00369                 " gap might be counted in students' timetable)")
00370                 +"\n\n"+tr("Do you want to add current constraint?"),
00371                QMessageBox::Yes, QMessageBox::Cancel);
00372                                            
00373               if(t==QMessageBox::Cancel)
00374                             return;
00375        }
00376 
00377        QList<int> days_L;
00378        QList<int> hours_L;
00379        //int days[MAX_N_CONSTRAINT_ACTIVITIES_PREFERRED_STARTING_TIMES];
00380        //int hours[MAX_N_CONSTRAINT_ACTIVITIES_PREFERRED_STARTING_TIMES];
00381        int n=0;
00382        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00383               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00384                      if(preferredTimesTable->item(i, j)->text()==YES){
00385                             days_L.append(j);
00386                             hours_L.append(i);
00387                             n++;
00388                      }
00389 
00390        if(n<=0){
00391               int t=QMessageBox::question(this, tr("FET question"),
00392                tr("Warning: 0 slots selected. Are you sure?"),
00393                QMessageBox::Yes, QMessageBox::Cancel);
00394                                            
00395               if(t==QMessageBox::Cancel)
00396                             return;
00397        }
00398 
00399        this->_ctr->weightPercentage=weight;
00400        this->_ctr->teacherName=teacher;
00401        this->_ctr->studentsName=students;
00402        this->_ctr->subjectName=subject;
00403        this->_ctr->activityTagName=activityTag;
00404        this->_ctr->nPreferredStartingTimes_L=n;
00405        this->_ctr->days_L=days_L;
00406        this->_ctr->hours_L=hours_L;
00407 
00408        gt.rules.internalStructureComputed=false;
00409        setRulesModifiedAndOtherThings(&gt.rules);
00410        
00411        this->close();
00412 }
00413 
00414 void ModifyConstraintActivitiesPreferredStartingTimesForm::cancel()
00415 {
00416        this->close();
00417 }
00418 
00419 #undef YES
00420 #undef NO