Back to index

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