Back to index

fet  5.18.0
addconstraintactivitypreferredtimeslotsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintactivitypreferredtimeslotsform.cpp  -  description
00003                              -------------------
00004     begin                : Wed Apr 23 2003
00005     copyright            : (C) 2003 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 "tablewidgetupdatebug.h"
00019 
00020 #include "longtextmessagebox.h"
00021 
00022 #include "addconstraintactivitypreferredtimeslotsform.h"
00023 #include "timeconstraint.h"
00024 
00025 #include <QHeaderView>
00026 #include <QTableWidget>
00027 #include <QTableWidgetItem>
00028 
00029 #include <QMessageBox>
00030 
00031 #include <QBrush>
00032 #include <QColor>
00033 
00034 #define YES          (QString(" "))
00035 #define NO           (QString("X"))
00036 
00037 AddConstraintActivityPreferredTimeSlotsForm::AddConstraintActivityPreferredTimeSlotsForm(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(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00047        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00049        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00050        connect(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsAllowed()));
00051        connect(setAllNotAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllSlotsNotAllowed()));
00052 
00053        centerWidgetOnScreen(this);
00054        restoreFETDialogGeometry(this);
00055               
00056        QSize tmp1=teachersComboBox->minimumSizeHint();
00057        Q_UNUSED(tmp1);
00058        QSize tmp2=studentsComboBox->minimumSizeHint();
00059        Q_UNUSED(tmp2);
00060        QSize tmp3=subjectsComboBox->minimumSizeHint();
00061        Q_UNUSED(tmp3);
00062        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00063        Q_UNUSED(tmp4);
00064        
00065        QSize tmp5=activitiesComboBox->minimumSizeHint();
00066        Q_UNUSED(tmp5);
00067 
00068        activitiesComboBox->setMaximumWidth(maxRecommendedWidth(this));
00069        
00070        teachersComboBox->addItem("");
00071        for(int i=0; i<gt.rules.teachersList.size(); i++){
00072               Teacher* tch=gt.rules.teachersList[i];
00073               teachersComboBox->addItem(tch->name);
00074        }
00075        teachersComboBox->setCurrentIndex(0);
00076 
00077        subjectsComboBox->addItem("");
00078        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00079               Subject* sb=gt.rules.subjectsList[i];
00080               subjectsComboBox->addItem(sb->name);
00081        }
00082        subjectsComboBox->setCurrentIndex(0);
00083 
00084        activityTagsComboBox->addItem("");
00085        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00086               ActivityTag* st=gt.rules.activityTagsList[i];
00087               activityTagsComboBox->addItem(st->name);
00088        }
00089        activityTagsComboBox->setCurrentIndex(0);
00090 
00091        studentsComboBox->addItem("");
00092        for(int i=0; i<gt.rules.yearsList.size(); i++){
00093               StudentsYear* sty=gt.rules.yearsList[i];
00094               studentsComboBox->addItem(sty->name);
00095               for(int j=0; j<sty->groupsList.size(); j++){
00096                      StudentsGroup* stg=sty->groupsList[j];
00097                      studentsComboBox->addItem(stg->name);
00098                      for(int k=0; k<stg->subgroupsList.size(); k++){
00099                             StudentsSubgroup* sts=stg->subgroupsList[k];
00100                             studentsComboBox->addItem(sts->name);
00101                      }
00102               }
00103        }
00104        studentsComboBox->setCurrentIndex(0);
00105        
00106        updateActivitiesComboBox();
00107 
00108        preferredTimesTable->setRowCount(gt.rules.nHoursPerDay);
00109        preferredTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00110 
00111        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00112               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00113               preferredTimesTable->setHorizontalHeaderItem(j, item);
00114        }
00115        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00116               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00117               preferredTimesTable->setVerticalHeaderItem(i, item);
00118        }
00119 
00120        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00121               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00122                      QTableWidgetItem* item=new QTableWidgetItem(YES);
00123                      item->setTextAlignment(Qt::AlignCenter);
00124                      item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
00125                      colorItem(item);
00126                      preferredTimesTable->setItem(i, j, item);
00127               }
00128               
00129        preferredTimesTable->resizeRowsToContents();
00130 
00131        connect(preferredTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00132        connect(preferredTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00133 
00134        preferredTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00135        
00136        tableWidgetUpdateBug(preferredTimesTable);
00137        
00138        setStretchAvailabilityTableNicely(preferredTimesTable);
00139 }
00140 
00141 AddConstraintActivityPreferredTimeSlotsForm::~AddConstraintActivityPreferredTimeSlotsForm()
00142 {
00143        saveFETDialogGeometry(this);
00144 }
00145 
00146 void AddConstraintActivityPreferredTimeSlotsForm::colorItem(QTableWidgetItem* item)
00147 {
00148        if(USE_GUI_COLORS){
00149               if(item->text()==YES)
00150                      item->setBackground(QBrush(Qt::darkGreen));
00151               else
00152                      item->setBackground(QBrush(Qt::darkRed));
00153               item->setForeground(QBrush(Qt::lightGray));
00154        }
00155 }
00156 
00157 void AddConstraintActivityPreferredTimeSlotsForm::horizontalHeaderClicked(int col)
00158 {
00159        if(col>=0 && col<gt.rules.nDaysPerWeek){
00160               QString s=preferredTimesTable->item(0, col)->text();
00161               if(s==YES)
00162                      s=NO;
00163               else{
00164                      assert(s==NO);
00165                      s=YES;
00166               }
00167 
00168               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00169                      /*QString s=notAllowedTimesTable->text(row, col);
00170                      if(s==YES)
00171                             s=NO;
00172                      else{
00173                             assert(s==NO);
00174                             s=YES;
00175                      }*/
00176                      preferredTimesTable->item(row, col)->setText(s);
00177                      colorItem(preferredTimesTable->item(row,col));
00178               }
00179               tableWidgetUpdateBug(preferredTimesTable);
00180        }
00181 }
00182 
00183 void AddConstraintActivityPreferredTimeSlotsForm::verticalHeaderClicked(int row)
00184 {
00185        if(row>=0 && row<gt.rules.nHoursPerDay){
00186               QString s=preferredTimesTable->item(row, 0)->text();
00187               if(s==YES)
00188                      s=NO;
00189               else{
00190                      assert(s==NO);
00191                      s=YES;
00192               }
00193        
00194               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00195                      /*QString s=notAllowedTimesTable->text(row, col);
00196                      if(s==YES)
00197                             s=NO;
00198                      else{
00199                             assert(s==NO);
00200                             s=YES;
00201                      }*/
00202                      preferredTimesTable->item(row, col)->setText(s);
00203                      colorItem(preferredTimesTable->item(row,col));
00204               }
00205               tableWidgetUpdateBug(preferredTimesTable);
00206        }
00207 }
00208 
00209 void AddConstraintActivityPreferredTimeSlotsForm::setAllSlotsAllowed()
00210 {
00211        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00212               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00213                      preferredTimesTable->item(i, j)->setText(YES);
00214                      colorItem(preferredTimesTable->item(i,j));
00215               }
00216        tableWidgetUpdateBug(preferredTimesTable);
00217 }
00218 
00219 void AddConstraintActivityPreferredTimeSlotsForm::setAllSlotsNotAllowed()
00220 {
00221        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00222               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00223                      preferredTimesTable->item(i, j)->setText(NO);
00224                      colorItem(preferredTimesTable->item(i,j));
00225               }
00226        tableWidgetUpdateBug(preferredTimesTable);
00227 }
00228 
00229 bool AddConstraintActivityPreferredTimeSlotsForm::filterOk(Activity* act)
00230 {
00231        QString tn=teachersComboBox->currentText();
00232        QString stn=studentsComboBox->currentText();
00233        QString sbn=subjectsComboBox->currentText();
00234        QString sbtn=activityTagsComboBox->currentText();
00235        int ok=true;
00236 
00237        //teacher
00238        if(tn!=""){
00239               bool ok2=false;
00240               for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00241                      if(*it == tn){
00242                             ok2=true;
00243                             break;
00244                      }
00245               if(!ok2)
00246                      ok=false;
00247        }
00248 
00249        //subject
00250        if(sbn!="" && sbn!=act->subjectName)
00251               ok=false;
00252               
00253        //activity tag
00254        if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00255               ok=false;
00256               
00257        //students
00258        if(stn!=""){
00259               bool ok2=false;
00260               for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00261                      if(*it == stn){
00262                             ok2=true;
00263                             break;
00264                      }
00265               if(!ok2)
00266                      ok=false;
00267        }
00268        
00269        return ok;
00270 }
00271 
00272 void AddConstraintActivityPreferredTimeSlotsForm::filterChanged()
00273 {
00274        this->updateActivitiesComboBox();
00275 }
00276 
00277 void AddConstraintActivityPreferredTimeSlotsForm::itemClicked(QTableWidgetItem* item)
00278 {
00279        QString s=item->text();
00280 
00281        if(s==YES)
00282               s=NO;
00283        else{
00284               assert(s==NO);
00285               s=YES;
00286        }
00287        item->setText(s);
00288        colorItem(item);
00289 
00290        tableWidgetUpdateBug(preferredTimesTable);
00291 }
00292 
00293 void AddConstraintActivityPreferredTimeSlotsForm::updateActivitiesComboBox(){
00294        activitiesComboBox->clear();
00295        activitiesList.clear();
00296        
00297        for(int i=0; i<gt.rules.activitiesList.size(); i++){
00298               Activity* act=gt.rules.activitiesList[i];
00299               
00300               if(filterOk(act)){
00301                      activitiesComboBox->addItem(act->getDescription(gt.rules));
00302                      this->activitiesList.append(act->id);
00303               }
00304        }
00305 }
00306 
00307 void AddConstraintActivityPreferredTimeSlotsForm::addConstraint()
00308 {
00309        TimeConstraint *ctr=NULL;
00310 
00311        double weight;
00312        QString tmp=weightLineEdit->text();
00313        weight_sscanf(tmp, "%lf", &weight);
00314        if(weight<0.0 || weight>100.0){
00315               QMessageBox::warning(this, tr("FET information"),
00316                      tr("Invalid weight (percentage)"));
00317               return;
00318        }
00319 
00320        /*bool compulsory=false;
00321        if(compulsoryCheckBox->isChecked())
00322               compulsory=true;*/
00323 
00324        int i=activitiesComboBox->currentIndex();
00325        assert(i<activitiesList.size());
00326        if(i<0 || activitiesComboBox->count()<=0){
00327               QMessageBox::warning(this, tr("FET information"),
00328                      tr("Invalid activity"));
00329               return;
00330        }
00331        int id=activitiesList.at(i);
00332        //Activity* act=gt.rules.activitiesList.at(id);
00333        
00334        QList<int> days_L;
00335        QList<int> hours_L;
00336        //int days[MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_TIME_SLOTS];
00337        //int hours[MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_TIME_SLOTS];
00338        int n=0;
00339        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00340               for(i=0; i<gt.rules.nHoursPerDay; i++)
00341                      if(preferredTimesTable->item(i, j)->text()==YES){
00342                             /*if(n>=MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_TIME_SLOTS){
00343                                    QString s=tr("Not enough slots (too many \"Yes\" values).");
00344                                    s+="\n";
00345                                    s+=tr("Please increase the variable MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_TIME_SLOTS");
00346                                    s+="\n";
00347                                    s+=tr("Currently, it is %1").arg(MAX_N_CONSTRAINT_ACTIVITY_PREFERRED_TIME_SLOTS);
00348                                    QMessageBox::warning(this, tr("FET information"), s);
00349                                    
00350                                    return;
00351                             }*/
00352                             
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        /*foreach(TimeConstraint* tc, gt.rules.timeConstraintsList){
00368               if(tc->type==CONSTRAINT_ACTIVITY_PREFERRED_TIMES){
00369                      ConstraintActivityPreferredTimes* c=(ConstraintActivityPreferredTimes*) tc;
00370                      if(c->activityId==id){
00371                             QMessageBox::warning(this, tr("FET information"),
00372                              tr("This activity id has other constraint of this type attached\n"
00373                              "Please remove the other constraints of type activity preferred times\n"
00374                              "referring to this activity before proceeding"));
00375                             return;
00376                      }
00377               }
00378        }*/
00379 
00380        ctr=new ConstraintActivityPreferredTimeSlots(weight, /*compulsory,*/ /*act->*/id, n, days_L, hours_L);
00381 
00382        bool tmp2=gt.rules.addTimeConstraint(ctr);
00383        if(tmp2){
00384               QString s=tr("Constraint added:");
00385               s+="\n\n";
00386               s+=ctr->getDetailedDescription(gt.rules);
00387               LongTextMessageBox::information(this, tr("FET information"), s);
00388        }
00389        else{
00390               QMessageBox::warning(this, tr("FET information"),
00391                      tr("Constraint NOT added - duplicate?"));
00392               delete ctr;
00393        }
00394 }
00395 
00396 #undef YES
00397 #undef NO