Back to index

fet  5.18.0
modifyconstraintsubactivitiespreferredtimeslotsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintsubactivitiespreferredtimeslotsform.cpp  -  description
00003                              -------------------
00004     begin                : 2008
00005     copyright            : (C) 2008 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 "modifyconstraintsubactivitiespreferredtimeslotsform.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 ModifyConstraintSubactivitiesPreferredTimeSlotsForm::ModifyConstraintSubactivitiesPreferredTimeSlotsForm(QWidget* parent, ConstraintSubactivitiesPreferredTimeSlots* 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        componentNumberSpinBox->setMinimum(1);
00067        componentNumberSpinBox->setMaximum(MAX_SPLIT_OF_AN_ACTIVITY);
00068        componentNumberSpinBox->setValue(this->_ctr->componentNumber);
00069        
00070        preferredTimesTable->setRowCount(gt.rules.nHoursPerDay);
00071        preferredTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00072 
00073        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00074               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00075               preferredTimesTable->setHorizontalHeaderItem(j, item);
00076        }
00077        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00078               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00079               preferredTimesTable->setVerticalHeaderItem(i, item);
00080        }
00081 
00082        Matrix2D<bool> currentMatrix;
00083        currentMatrix.resize(gt.rules.nHoursPerDay, gt.rules.nDaysPerWeek);
00084        //bool currentMatrix[MAX_HOURS_PER_DAY][MAX_DAYS_PER_WEEK];
00085        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00086               for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00087                      currentMatrix[i][j]=false;
00088        for(int k=0; k<ctr->p_nPreferredTimeSlots_L; k++){
00089               if(ctr->p_hours_L[k]==-1 || ctr->p_days_L[k]==-1)
00090                      assert(0);
00091               int i=ctr->p_hours_L[k];
00092               int j=ctr->p_days_L[k];
00093               if(i>=0 && i<gt.rules.nHoursPerDay && j>=0 && j<gt.rules.nDaysPerWeek)
00094                      currentMatrix[i][j]=true;
00095        }
00096 
00097        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00098               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00099                      QTableWidgetItem* item= new QTableWidgetItem();
00100                      item->setTextAlignment(Qt::AlignCenter);
00101                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00102                      preferredTimesTable->setItem(i, j, item);
00103 
00104                      if(!currentMatrix[i][j])
00105                             item->setText(NO);
00106                      else
00107                             item->setText(YES);
00108                             
00109                      colorItem(item);
00110               }
00111               
00112        preferredTimesTable->resizeRowsToContents();
00113                             
00114        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00115 
00116        connect(preferredTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00117        connect(preferredTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00118 
00119        preferredTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00120        
00121        tableWidgetUpdateBug(preferredTimesTable);
00122 
00123        setStretchAvailabilityTableNicely(preferredTimesTable);
00124 }
00125 
00126 ModifyConstraintSubactivitiesPreferredTimeSlotsForm::~ModifyConstraintSubactivitiesPreferredTimeSlotsForm()
00127 {
00128        saveFETDialogGeometry(this);
00129 }
00130 
00131 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::colorItem(QTableWidgetItem* item)
00132 {
00133        if(USE_GUI_COLORS){
00134               if(item->text()==YES)
00135                      item->setBackground(QBrush(Qt::darkGreen));
00136               else
00137                      item->setBackground(QBrush(Qt::darkRed));
00138               item->setForeground(QBrush(Qt::lightGray));
00139        }
00140 }
00141 
00142 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::horizontalHeaderClicked(int col)
00143 {
00144        if(col>=0 && col<gt.rules.nDaysPerWeek){
00145               QString s=preferredTimesTable->item(0, col)->text();
00146               if(s==YES)
00147                      s=NO;
00148               else{
00149                      assert(s==NO);
00150                      s=YES;
00151               }
00152 
00153               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00154                      preferredTimesTable->item(row, col)->setText(s);
00155                      colorItem(preferredTimesTable->item(row,col));
00156               }
00157               tableWidgetUpdateBug(preferredTimesTable);
00158        }
00159 }
00160 
00161 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::verticalHeaderClicked(int row)
00162 {
00163        if(row>=0 && row<gt.rules.nHoursPerDay){
00164               QString s=preferredTimesTable->item(row, 0)->text();
00165               if(s==YES)
00166                      s=NO;
00167               else{
00168                      assert(s==NO);
00169                      s=YES;
00170               }
00171        
00172               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00173                      preferredTimesTable->item(row, col)->setText(s);
00174                      colorItem(preferredTimesTable->item(row,col));
00175               }
00176               tableWidgetUpdateBug(preferredTimesTable);
00177        }
00178 }
00179 
00180 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsAllowed()
00181 {
00182        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00183               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00184                      preferredTimesTable->item(i, j)->setText(YES);
00185                      colorItem(preferredTimesTable->item(i,j));
00186               }
00187        tableWidgetUpdateBug(preferredTimesTable);
00188 }
00189 
00190 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsNotAllowed()
00191 {
00192        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00193               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00194                      preferredTimesTable->item(i, j)->setText(NO);
00195                      colorItem(preferredTimesTable->item(i,j));
00196               }
00197        tableWidgetUpdateBug(preferredTimesTable);
00198 }
00199 
00200 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::itemClicked(QTableWidgetItem* item)
00201 {
00202        QString s=item->text();
00203        if(s==YES)
00204               s=NO;
00205        else{
00206               assert(s==NO);
00207               s=YES;
00208        }
00209        item->setText(s);
00210        colorItem(item);
00211 
00212        tableWidgetUpdateBug(preferredTimesTable);
00213 }
00214 
00215 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::updateTeachersComboBox(){
00216        int i=0, j=-1;
00217        teachersComboBox->clear();
00218        teachersComboBox->addItem("");
00219        if(this->_ctr->p_teacherName=="")
00220               j=i;
00221        i++;
00222        for(int k=0; k<gt.rules.teachersList.size(); k++){
00223               Teacher* t=gt.rules.teachersList[k];
00224               teachersComboBox->addItem(t->name);
00225               if(t->name==this->_ctr->p_teacherName)
00226                      j=i;
00227               i++;
00228        }
00229        assert(j>=0);
00230        teachersComboBox->setCurrentIndex(j);
00231 }
00232 
00233 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::updateStudentsComboBox(){
00234        int i=0, j=-1;
00235        studentsComboBox->clear();
00236        studentsComboBox->addItem("");
00237        if(this->_ctr->p_studentsName=="")
00238               j=i;
00239        i++;
00240        for(int m=0; m<gt.rules.yearsList.size(); m++){
00241               StudentsYear* sty=gt.rules.yearsList[m];
00242               studentsComboBox->addItem(sty->name);
00243               if(sty->name==this->_ctr->p_studentsName)
00244                      j=i;
00245               i++;
00246               for(int n=0; n<sty->groupsList.size(); n++){
00247                      StudentsGroup* stg=sty->groupsList[n];
00248                      studentsComboBox->addItem(stg->name);
00249                      if(stg->name==this->_ctr->p_studentsName)
00250                             j=i;
00251                      i++;
00252                      for(int p=0; p<stg->subgroupsList.size(); p++){
00253                             StudentsSubgroup* sts=stg->subgroupsList[p];
00254                             studentsComboBox->addItem(sts->name);
00255                             if(sts->name==this->_ctr->p_studentsName)
00256                                    j=i;
00257                             i++;
00258                      }
00259               }
00260        }
00261        assert(j>=0);
00262        studentsComboBox->setCurrentIndex(j);
00263 }
00264 
00265 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::updateSubjectsComboBox(){
00266        int i=0, j=-1;
00267        subjectsComboBox->clear();
00268        subjectsComboBox->addItem("");
00269        if(this->_ctr->p_subjectName=="")
00270               j=i;
00271        i++;
00272        for(int k=0; k<gt.rules.subjectsList.size(); k++){
00273               Subject* s=gt.rules.subjectsList[k];
00274               subjectsComboBox->addItem(s->name);
00275               if(s->name==this->_ctr->p_subjectName)
00276                      j=i;
00277               i++;
00278        }
00279        assert(j>=0);
00280        subjectsComboBox->setCurrentIndex(j);
00281 }
00282 
00283 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::updateActivityTagsComboBox(){
00284        int i=0, j=-1;
00285        activityTagsComboBox->clear();
00286        activityTagsComboBox->addItem("");
00287        if(this->_ctr->p_activityTagName=="")
00288               j=i;
00289        i++;
00290        for(int k=0; k<gt.rules.activityTagsList.size(); k++){
00291               ActivityTag* s=gt.rules.activityTagsList[k];
00292               activityTagsComboBox->addItem(s->name);
00293               if(s->name==this->_ctr->p_activityTagName)
00294                      j=i;
00295               i++;
00296        }
00297        assert(j>=0);
00298        activityTagsComboBox->setCurrentIndex(j);
00299 }
00300 
00301 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::ok()
00302 {
00303        double weight;
00304        QString tmp=weightLineEdit->text();
00305        weight_sscanf(tmp, "%lf", &weight);
00306        if(weight<0.0 || weight>100.0){
00307               QMessageBox::warning(this, tr("FET information"),
00308                      tr("Invalid weight (percentage)"));
00309               return;
00310        }
00311 
00312        QString teacher=teachersComboBox->currentText();
00313        if(teacher!="")
00314               assert(gt.rules.searchTeacher(teacher)>=0);
00315 
00316        QString students=studentsComboBox->currentText();
00317        if(students!="")
00318               assert(gt.rules.searchStudentsSet(students)!=NULL);
00319 
00320        QString subject=subjectsComboBox->currentText();
00321        if(subject!="")
00322               assert(gt.rules.searchSubject(subject)>=0);
00323               
00324        QString activityTag=activityTagsComboBox->currentText();
00325        if(activityTag!="")
00326               assert(gt.rules.searchActivityTag(activityTag)>=0);
00327               
00328        QList<int> days_L;
00329        QList<int> hours_L;
00330        //int days[MAX_N_CONSTRAINT_SUBACTIVITIES_PREFERRED_TIME_SLOTS];
00331        //int hours[MAX_N_CONSTRAINT_SUBACTIVITIES_PREFERRED_TIME_SLOTS];
00332        int n=0;
00333        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00334               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00335                      if(preferredTimesTable->item(i, j)->text()==YES){
00336                             days_L.append(j);
00337                             hours_L.append(i);
00338                             n++;
00339                      }
00340 
00341        if(n<=0){
00342               int t=QMessageBox::question(this, tr("FET question"),
00343                tr("Warning: 0 slots selected. Are you sure?"),
00344                QMessageBox::Yes, QMessageBox::Cancel);
00345                                            
00346               if(t==QMessageBox::Cancel)
00347                             return;
00348        }
00349 
00350        this->_ctr->weightPercentage=weight;
00351 
00352        this->_ctr->componentNumber=componentNumberSpinBox->value();
00353        
00354        this->_ctr->p_teacherName=teacher;
00355        this->_ctr->p_studentsName=students;
00356        this->_ctr->p_subjectName=subject;
00357        this->_ctr->p_activityTagName=activityTag;
00358        this->_ctr->p_nPreferredTimeSlots_L=n;
00359        this->_ctr->p_days_L=days_L;
00360        this->_ctr->p_hours_L=hours_L;
00361 
00362        gt.rules.internalStructureComputed=false;
00363        setRulesModifiedAndOtherThings(&gt.rules);
00364        
00365        this->close();
00366 }
00367 
00368 void ModifyConstraintSubactivitiesPreferredTimeSlotsForm::cancel()
00369 {
00370        this->close();
00371 }
00372 
00373 #undef YES
00374 #undef NO