Back to index

fet  5.18.0
addconstraintsubactivitiespreferredtimeslotsform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintsubactivitiespreferredtimeslotsform.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 "longtextmessagebox.h"
00023 
00024 #include "addconstraintsubactivitiespreferredtimeslotsform.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 AddConstraintSubactivitiesPreferredTimeSlotsForm::AddConstraintSubactivitiesPreferredTimeSlotsForm(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        componentNumberSpinBox->setMinimum(1);
00095        componentNumberSpinBox->setMaximum(MAX_SPLIT_OF_AN_ACTIVITY);
00096        componentNumberSpinBox->setValue(1);
00097 
00098        tableWidgetUpdateBug(preferredTimesTable);
00099        
00100        setStretchAvailabilityTableNicely(preferredTimesTable);
00101 }
00102 
00103 AddConstraintSubactivitiesPreferredTimeSlotsForm::~AddConstraintSubactivitiesPreferredTimeSlotsForm()
00104 {
00105        saveFETDialogGeometry(this);
00106 }
00107 
00108 void AddConstraintSubactivitiesPreferredTimeSlotsForm::colorItem(QTableWidgetItem* item)
00109 {
00110        if(USE_GUI_COLORS){
00111               if(item->text()==YES)
00112                      item->setBackground(QBrush(Qt::darkGreen));
00113               else
00114                      item->setBackground(QBrush(Qt::darkRed));
00115               item->setForeground(QBrush(Qt::lightGray));
00116        }
00117 }
00118 
00119 void AddConstraintSubactivitiesPreferredTimeSlotsForm::horizontalHeaderClicked(int col)
00120 {
00121        if(col>=0 && col<gt.rules.nDaysPerWeek){
00122               QString s=preferredTimesTable->item(0, col)->text();
00123               if(s==YES)
00124                      s=NO;
00125               else{
00126                      assert(s==NO);
00127                      s=YES;
00128               }
00129 
00130               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00131                      preferredTimesTable->item(row, col)->setText(s);
00132                      colorItem(preferredTimesTable->item(row,col));
00133               }
00134               tableWidgetUpdateBug(preferredTimesTable);
00135        }
00136 }
00137 
00138 void AddConstraintSubactivitiesPreferredTimeSlotsForm::verticalHeaderClicked(int row)
00139 {
00140        if(row>=0 && row<gt.rules.nHoursPerDay){
00141               QString s=preferredTimesTable->item(row, 0)->text();
00142               if(s==YES)
00143                      s=NO;
00144               else{
00145                      assert(s==NO);
00146                      s=YES;
00147               }
00148        
00149               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00150                      preferredTimesTable->item(row, col)->setText(s);
00151                      colorItem(preferredTimesTable->item(row,col));
00152               }
00153               tableWidgetUpdateBug(preferredTimesTable);
00154        }
00155 }
00156 
00157 void AddConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsAllowed()
00158 {
00159        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00160               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00161                      preferredTimesTable->item(i, j)->setText(YES);
00162                      colorItem(preferredTimesTable->item(i,j));
00163               }
00164        tableWidgetUpdateBug(preferredTimesTable);
00165 }
00166 
00167 void AddConstraintSubactivitiesPreferredTimeSlotsForm::setAllSlotsNotAllowed()
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(NO);
00172                      colorItem(preferredTimesTable->item(i,j));
00173               }
00174        tableWidgetUpdateBug(preferredTimesTable);
00175 }
00176 
00177 void AddConstraintSubactivitiesPreferredTimeSlotsForm::itemClicked(QTableWidgetItem* item)
00178 {
00179        QString s=item->text();
00180        if(s==YES)
00181               s=NO;
00182        else{
00183               assert(s==NO);
00184               s=YES;
00185        }
00186        item->setText(s);
00187        colorItem(item);
00188 
00189        tableWidgetUpdateBug(preferredTimesTable);
00190 }
00191 
00192 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateTeachersComboBox(){
00193        teachersComboBox->clear();
00194        teachersComboBox->addItem("");
00195        for(int i=0; i<gt.rules.teachersList.size(); i++){
00196               Teacher* t=gt.rules.teachersList[i];
00197               teachersComboBox->addItem(t->name);
00198        }
00199 }
00200 
00201 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateStudentsComboBox(){
00202        studentsComboBox->clear();
00203        studentsComboBox->addItem("");
00204        for(int i=0; i<gt.rules.yearsList.size(); i++){
00205               StudentsYear* sty=gt.rules.yearsList[i];
00206               studentsComboBox->addItem(sty->name);
00207               for(int j=0; j<sty->groupsList.size(); j++){
00208                      StudentsGroup* stg=sty->groupsList[j];
00209                      studentsComboBox->addItem(stg->name);
00210                      for(int k=0; k<stg->subgroupsList.size(); k++){
00211                             StudentsSubgroup* sts=stg->subgroupsList[k];
00212                             studentsComboBox->addItem(sts->name);
00213                      }
00214               }
00215        }
00216 }
00217 
00218 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateSubjectsComboBox(){
00219        subjectsComboBox->clear();
00220        subjectsComboBox->addItem("");
00221        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00222               Subject* s=gt.rules.subjectsList[i];
00223               subjectsComboBox->addItem(s->name);
00224        }
00225 }
00226 
00227 void AddConstraintSubactivitiesPreferredTimeSlotsForm::updateActivityTagsComboBox(){
00228        activityTagsComboBox->clear();
00229        activityTagsComboBox->addItem("");
00230        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00231               ActivityTag* s=gt.rules.activityTagsList[i];
00232               activityTagsComboBox->addItem(s->name);
00233        }
00234 }
00235 
00236 void AddConstraintSubactivitiesPreferredTimeSlotsForm::addConstraint()
00237 {
00238        TimeConstraint *ctr=NULL;
00239 
00240        double weight;
00241        QString tmp=weightLineEdit->text();
00242        weight_sscanf(tmp, "%lf", &weight);
00243        if(weight<0.0 || weight>100.0){
00244               QMessageBox::warning(this, tr("FET information"),
00245                      tr("Invalid weight (percentage)"));
00246               return;
00247        }
00248 
00249        QString teacher=teachersComboBox->currentText();
00250        if(teacher!="")
00251               assert(gt.rules.searchTeacher(teacher)>=0);
00252 
00253        QString students=studentsComboBox->currentText();
00254        if(students!="")
00255               assert(gt.rules.searchStudentsSet(students)!=NULL);
00256 
00257        QString subject=subjectsComboBox->currentText();
00258        if(subject!="")
00259               assert(gt.rules.searchSubject(subject)>=0);
00260               
00261        QString activityTag=activityTagsComboBox->currentText();
00262        if(activityTag!="")
00263               assert(gt.rules.searchActivityTag(activityTag)>=0);
00264        
00265        QList<int> days_L;
00266        QList<int> hours_L;
00267        //int days[MAX_N_CONSTRAINT_SUBACTIVITIES_PREFERRED_TIME_SLOTS];
00268        //int hours[MAX_N_CONSTRAINT_SUBACTIVITIES_PREFERRED_TIME_SLOTS];
00269        int n=0;
00270        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00271               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00272                      if(preferredTimesTable->item(i, j)->text()==YES){
00273                             days_L.append(j);
00274                             hours_L.append(i);
00275                             n++;
00276                      }
00277 
00278        if(n<=0){
00279               int t=QMessageBox::question(this, tr("FET question"),
00280                tr("Warning: 0 slots selected. Are you sure?"),
00281                QMessageBox::Yes, QMessageBox::Cancel);
00282                                            
00283               if(t==QMessageBox::Cancel)
00284                             return;
00285        }
00286 
00287        ctr=new ConstraintSubactivitiesPreferredTimeSlots(weight, componentNumberSpinBox->value(),/*compulsory,*/ teacher, students, subject, activityTag, n, days_L, hours_L);
00288 
00289        bool tmp2=gt.rules.addTimeConstraint(ctr);
00290        if(tmp2){
00291               QString s=tr("Constraint added:");
00292               s+="\n\n";
00293               s+=ctr->getDetailedDescription(gt.rules);
00294               LongTextMessageBox::information(this, tr("FET information"), s);
00295        }
00296        else{
00297               QMessageBox::warning(this, tr("FET information"),
00298                      tr("Constraint NOT added - duplicate?"));
00299               delete ctr;
00300        }
00301 }
00302 
00303 #undef YES
00304 #undef NO