Back to index

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