Back to index

fet  5.18.0
modifyconstraintactivitiespreferredtimeslotsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintactivitiespreferredtimeslotsform.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 "modifyconstraintactivitiespreferredtimeslotsform.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 ModifyConstraintActivitiesPreferredTimeSlotsForm::ModifyConstraintActivitiesPreferredTimeSlotsForm(QWidget* parent, ConstraintActivitiesPreferredTimeSlots* 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->p_nPreferredTimeSlots_L; k++){
00086               if(ctr->p_hours_L[k]==-1 || ctr->p_days_L[k]==-1)
00087                      assert(0);
00088               int i=ctr->p_hours_L[k];
00089               int j=ctr->p_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 ModifyConstraintActivitiesPreferredTimeSlotsForm::~ModifyConstraintActivitiesPreferredTimeSlotsForm()
00124 {
00125        saveFETDialogGeometry(this);
00126 }
00127 
00128 void ModifyConstraintActivitiesPreferredTimeSlotsForm::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 ModifyConstraintActivitiesPreferredTimeSlotsForm::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 ModifyConstraintActivitiesPreferredTimeSlotsForm::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 ModifyConstraintActivitiesPreferredTimeSlotsForm::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 ModifyConstraintActivitiesPreferredTimeSlotsForm::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 ModifyConstraintActivitiesPreferredTimeSlotsForm::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        tableWidgetUpdateBug(preferredTimesTable);
00209 }
00210 
00211 void ModifyConstraintActivitiesPreferredTimeSlotsForm::updateTeachersComboBox(){
00212        int i=0, j=-1;
00213        teachersComboBox->clear();
00214        teachersComboBox->addItem("");
00215        if(this->_ctr->p_teacherName=="")
00216               j=i;
00217        i++;
00218        for(int k=0; k<gt.rules.teachersList.size(); k++){
00219               Teacher* t=gt.rules.teachersList[k];
00220               teachersComboBox->addItem(t->name);
00221               if(t->name==this->_ctr->p_teacherName)
00222                      j=i;
00223               i++;
00224        }
00225        assert(j>=0);
00226        teachersComboBox->setCurrentIndex(j);
00227 }
00228 
00229 void ModifyConstraintActivitiesPreferredTimeSlotsForm::updateStudentsComboBox(){
00230        int i=0, j=-1;
00231        studentsComboBox->clear();
00232        studentsComboBox->addItem("");
00233        if(this->_ctr->p_studentsName=="")
00234               j=i;
00235        i++;
00236        for(int m=0; m<gt.rules.yearsList.size(); m++){
00237               StudentsYear* sty=gt.rules.yearsList[m];
00238               studentsComboBox->addItem(sty->name);
00239               if(sty->name==this->_ctr->p_studentsName)
00240                      j=i;
00241               i++;
00242               for(int n=0; n<sty->groupsList.size(); n++){
00243                      StudentsGroup* stg=sty->groupsList[n];
00244                      studentsComboBox->addItem(stg->name);
00245                      if(stg->name==this->_ctr->p_studentsName)
00246                             j=i;
00247                      i++;
00248                      for(int p=0; p<stg->subgroupsList.size(); p++){
00249                             StudentsSubgroup* sts=stg->subgroupsList[p];
00250                             studentsComboBox->addItem(sts->name);
00251                             if(sts->name==this->_ctr->p_studentsName)
00252                                    j=i;
00253                             i++;
00254                      }
00255               }
00256        }
00257        assert(j>=0);
00258        studentsComboBox->setCurrentIndex(j);
00259 }
00260 
00261 void ModifyConstraintActivitiesPreferredTimeSlotsForm::updateSubjectsComboBox(){
00262        int i=0, j=-1;
00263        subjectsComboBox->clear();
00264        subjectsComboBox->addItem("");
00265        if(this->_ctr->p_subjectName=="")
00266               j=i;
00267        i++;
00268        for(int k=0; k<gt.rules.subjectsList.size(); k++){
00269               Subject* s=gt.rules.subjectsList[k];
00270               subjectsComboBox->addItem(s->name);
00271               if(s->name==this->_ctr->p_subjectName)
00272                      j=i;
00273               i++;
00274        }
00275        assert(j>=0);
00276        subjectsComboBox->setCurrentIndex(j);
00277 }
00278 
00279 void ModifyConstraintActivitiesPreferredTimeSlotsForm::updateActivityTagsComboBox(){
00280        int i=0, j=-1;
00281        activityTagsComboBox->clear();
00282        activityTagsComboBox->addItem("");
00283        if(this->_ctr->p_activityTagName=="")
00284               j=i;
00285        i++;
00286        for(int k=0; k<gt.rules.activityTagsList.size(); k++){
00287               ActivityTag* s=gt.rules.activityTagsList[k];
00288               activityTagsComboBox->addItem(s->name);
00289               if(s->name==this->_ctr->p_activityTagName)
00290                      j=i;
00291               i++;
00292        }
00293        assert(j>=0);
00294        activityTagsComboBox->setCurrentIndex(j);
00295 }
00296 
00297 void ModifyConstraintActivitiesPreferredTimeSlotsForm::ok()
00298 {
00299        double weight;
00300        QString tmp=weightLineEdit->text();
00301        weight_sscanf(tmp, "%lf", &weight);
00302        if(weight<0.0 || weight>100.0){
00303               QMessageBox::warning(this, tr("FET information"),
00304                      tr("Invalid weight (percentage)"));
00305               return;
00306        }
00307 
00308        QString teacher=teachersComboBox->currentText();
00309        if(teacher!="")
00310               assert(gt.rules.searchTeacher(teacher)>=0);
00311 
00312        QString students=studentsComboBox->currentText();
00313        if(students!="")
00314               assert(gt.rules.searchStudentsSet(students)!=NULL);
00315 
00316        QString subject=subjectsComboBox->currentText();
00317        if(subject!="")
00318               assert(gt.rules.searchSubject(subject)>=0);
00319               
00320        QString activityTag=activityTagsComboBox->currentText();
00321        if(activityTag!="")
00322               assert(gt.rules.searchActivityTag(activityTag)>=0);
00323               
00324        if(teacher=="" && students=="" && subject=="" && activityTag==""){
00325               int t=QMessageBox::question(this, tr("FET question"),
00326                tr("You specified all the activities. This might be a small problem: if you specify"
00327                 " a not allowed slot between two allowed slots, this not allowed slot will"
00328                 " be counted as a gap in the teachers' and students' timetable.\n\n"
00329                 " The best practice would be to use constraint break times.\n\n"
00330                 " If you need weight under 100%, then you can use this constraint, but be careful"
00331                 " not to obtain an impossible timetable (if your teachers/students are constrained on gaps"
00332                 " or early gaps and if you leave a not allowed slot between 2 allowed slots or"
00333                 " a not allowed slot early in the day and more allowed slots after it,"
00334                 " this possible gap might be counted in teachers' and students' timetable)")
00335                 +"\n\n"+tr("Do you want to add current constraint?"),
00336                QMessageBox::Yes, QMessageBox::Cancel);
00337                                            
00338               if(t==QMessageBox::Cancel)
00339                             return;
00340        }
00341 
00342        if(teacher!="" && students=="" && subject=="" && activityTag==""){
00343               int t=QMessageBox::question(this, tr("FET question"),
00344                tr("You specified only the teacher. This might be a small problem: if you specify"
00345                 " a not allowed slot between two allowed slots, this not allowed slot will"
00346                 " be counted as a gap in the teacher's timetable.\n\n"
00347                 " The best practice would be to use constraint teacher not available times.\n\n"
00348                 " If you need weight under 100%, then you can use this constraint, but be careful"
00349                 " not to obtain an impossible timetable (if your teacher is constrained on gaps"
00350                 " and if you leave a not allowed slot between 2 allowed slots, this possible"
00351                 " gap might be counted in teacher's timetable)")
00352                 +"\n\n"+tr("Do you want to add current constraint?"),
00353                QMessageBox::Yes, QMessageBox::Cancel);
00354                                            
00355               if(t==QMessageBox::Cancel)
00356                             return;
00357        }
00358        if(teacher=="" && students!="" && subject=="" && activityTag==""){
00359               int t=QMessageBox::question(this, tr("FET question"),
00360                tr("You specified only the students set. This might be a small problem: if you specify"
00361                 " a not allowed slot between two allowed slots (or a not allowed slot before allowed slots),"
00362                 " this not allowed slot will"
00363                 " be counted as a gap (or early gap) in the students' timetable.\n\n"
00364                 " The best practice would be to use constraint students set not available times.\n\n"
00365                 " If you need weight under 100%, then you can use this constraint, but be careful"
00366                 " not to obtain an impossible timetable (if your students set is constrained on gaps or early gaps"
00367                 " and if you leave a not allowed slot between 2 allowed slots (or a not allowed slot before allowed slots), this possible"
00368                 " gap might be counted in students' timetable)")
00369                 +"\n\n"+tr("Do you want to add current constraint?"),
00370                QMessageBox::Yes, QMessageBox::Cancel);
00371                                            
00372               if(t==QMessageBox::Cancel)
00373                             return;
00374        }
00375 
00376        QList<int> days_L;
00377        QList<int> hours_L;
00378        //int days[MAX_N_CONSTRAINT_ACTIVITIES_PREFERRED_TIME_SLOTS];
00379        //int hours[MAX_N_CONSTRAINT_ACTIVITIES_PREFERRED_TIME_SLOTS];
00380        int n=0;
00381        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00382               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00383                      if(preferredTimesTable->item(i, j)->text()==YES){
00384                             days_L.append(j);
00385                             hours_L.append(i);
00386                             n++;
00387                      }
00388 
00389        if(n<=0){
00390               int t=QMessageBox::question(this, tr("FET question"),
00391                tr("Warning: 0 slots selected. Are you sure?"),
00392                QMessageBox::Yes, QMessageBox::Cancel);
00393                                            
00394               if(t==QMessageBox::Cancel)
00395                             return;
00396        }
00397 
00398        this->_ctr->weightPercentage=weight;
00399        this->_ctr->p_teacherName=teacher;
00400        this->_ctr->p_studentsName=students;
00401        this->_ctr->p_subjectName=subject;
00402        this->_ctr->p_activityTagName=activityTag;
00403        this->_ctr->p_nPreferredTimeSlots_L=n;
00404        this->_ctr->p_days_L=days_L;
00405        this->_ctr->p_hours_L=hours_L;
00406 
00407        gt.rules.internalStructureComputed=false;
00408        setRulesModifiedAndOtherThings(&gt.rules);
00409        
00410        this->close();
00411 }
00412 
00413 void ModifyConstraintActivitiesPreferredTimeSlotsForm::cancel()
00414 {
00415        this->close();
00416 }
00417 
00418 #undef YES
00419 #undef NO