Back to index

fet  5.18.0
modifyconstraintroomnotavailabletimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintroomnotavailabletimesform.cpp  -  description
00003                              -------------------
00004     begin                : Feb 13, 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 "modifyconstraintroomnotavailabletimesform.h"
00023 #include "spaceconstraint.h"
00024 
00025 #include <QHeaderView>
00026 #include <QTableWidget>
00027 #include <QTableWidgetItem>
00028 
00029 #include <QBrush>
00030 #include <QColor>
00031 
00032 #define YES          (QString("X"))
00033 #define NO           (QString(" "))
00034 
00035 ModifyConstraintRoomNotAvailableTimesForm::ModifyConstraintRoomNotAvailableTimesForm(QWidget* parent, ConstraintRoomNotAvailableTimes* ctr): QDialog(parent)
00036 {
00037        setupUi(this);
00038 
00039        okPushButton->setDefault(true);
00040 
00041        connect(okPushButton, SIGNAL(clicked()), this, SLOT(ok()));
00042        connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(cancel()));
00043        connect(notAllowedTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00044        connect(setAllAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllAvailable()));
00045        connect(setAllNotAvailablePushButton, SIGNAL(clicked()), this, SLOT(setAllNotAvailable()));
00046 
00047        centerWidgetOnScreen(this);
00048        restoreFETDialogGeometry(this);
00049 
00050        QSize tmp5=roomsComboBox->minimumSizeHint();
00051        Q_UNUSED(tmp5);
00052        
00053        this->_ctr=ctr;
00054        
00055        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00056 
00057        updateRoomsComboBox();
00058 
00059        notAllowedTimesTable->setRowCount(gt.rules.nHoursPerDay);
00060        notAllowedTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00061 
00062        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00063               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00064               notAllowedTimesTable->setHorizontalHeaderItem(j, item);
00065        }
00066        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00067               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00068               notAllowedTimesTable->setVerticalHeaderItem(i, item);
00069        }
00070 
00071        //bool currentMatrix[MAX_HOURS_PER_DAY][MAX_DAYS_PER_WEEK];
00072        Matrix2D<bool> currentMatrix;
00073        currentMatrix.resize(gt.rules.nHoursPerDay, gt.rules.nDaysPerWeek);
00074        
00075        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00076               for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00077                      currentMatrix[i][j]=false;                
00078        assert(ctr->days.count()==ctr->hours.count());   
00079        for(int k=0; k<ctr->days.count(); k++){
00080               if(ctr->hours.at(k)==-1 || ctr->days.at(k)==-1)
00081                      assert(0);
00082               int i=ctr->hours.at(k);
00083               int j=ctr->days.at(k);
00084               if(i>=0 && i<gt.rules.nHoursPerDay && j>=0 && j<gt.rules.nDaysPerWeek)
00085                      currentMatrix[i][j]=true;
00086        }
00087 
00088        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00089               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00090                      QTableWidgetItem* item= new QTableWidgetItem();
00091                      item->setTextAlignment(Qt::AlignCenter);
00092                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00093                      notAllowedTimesTable->setItem(i, j, item);
00094 
00095                      if(!currentMatrix[i][j])
00096                             item->setText(NO);
00097                      else
00098                             item->setText(YES);
00099                             
00100                      colorItem(item);
00101               }
00102               
00103        notAllowedTimesTable->resizeRowsToContents();
00104 
00105        connect(notAllowedTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00106        connect(notAllowedTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00107 
00108        notAllowedTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00109 
00110        tableWidgetUpdateBug(notAllowedTimesTable);
00111        
00112        setStretchAvailabilityTableNicely(notAllowedTimesTable);
00113 }
00114 
00115 ModifyConstraintRoomNotAvailableTimesForm::~ModifyConstraintRoomNotAvailableTimesForm()
00116 {
00117        saveFETDialogGeometry(this);
00118 }
00119 
00120 void ModifyConstraintRoomNotAvailableTimesForm::colorItem(QTableWidgetItem* item)
00121 {
00122        if(USE_GUI_COLORS){
00123               if(item->text()==NO)
00124                      item->setBackground(QBrush(Qt::darkGreen));
00125               else
00126                      item->setBackground(QBrush(Qt::darkRed));
00127               item->setForeground(QBrush(Qt::lightGray));
00128        }
00129 }
00130 
00131 void ModifyConstraintRoomNotAvailableTimesForm::horizontalHeaderClicked(int col)
00132 {
00133        if(col>=0 && col<gt.rules.nDaysPerWeek){
00134               QString s=notAllowedTimesTable->item(0, col)->text();
00135               if(s==YES)
00136                      s=NO;
00137               else{
00138                      assert(s==NO);
00139                      s=YES;
00140               }
00141 
00142               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00143                      notAllowedTimesTable->item(row, col)->setText(s);
00144                      colorItem(notAllowedTimesTable->item(row,col));
00145               }
00146               tableWidgetUpdateBug(notAllowedTimesTable);
00147        }
00148 }
00149 
00150 void ModifyConstraintRoomNotAvailableTimesForm::verticalHeaderClicked(int row)
00151 {
00152        if(row>=0 && row<gt.rules.nHoursPerDay){
00153               QString s=notAllowedTimesTable->item(row, 0)->text();
00154               if(s==YES)
00155                      s=NO;
00156               else{
00157                      assert(s==NO);
00158                      s=YES;
00159               }
00160        
00161               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00162                      notAllowedTimesTable->item(row, col)->setText(s);
00163                      colorItem(notAllowedTimesTable->item(row,col));
00164               }
00165               tableWidgetUpdateBug(notAllowedTimesTable);
00166        }
00167 }
00168 
00169 void ModifyConstraintRoomNotAvailableTimesForm::setAllAvailable()
00170 {
00171        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00172               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00173                      notAllowedTimesTable->item(i, j)->setText(NO);
00174                      colorItem(notAllowedTimesTable->item(i,j));
00175               }
00176        tableWidgetUpdateBug(notAllowedTimesTable);
00177 }
00178 
00179 void ModifyConstraintRoomNotAvailableTimesForm::setAllNotAvailable()
00180 {
00181        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00182               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00183                      notAllowedTimesTable->item(i, j)->setText(YES);
00184                      colorItem(notAllowedTimesTable->item(i,j));
00185               }
00186        tableWidgetUpdateBug(notAllowedTimesTable);
00187 }
00188 
00189 void ModifyConstraintRoomNotAvailableTimesForm::updateRoomsComboBox()
00190 {
00191        int i=0, j=-1;
00192        roomsComboBox->clear();
00193        for(int k=0; k<gt.rules.roomsList.size(); k++){
00194               Room* room=gt.rules.roomsList[k];
00195               //roomsComboBox->addItem(room->getDescription());
00196               roomsComboBox->addItem(room->name);
00197               if(room->name==this->_ctr->room)
00198                      j=i;
00199               i++;
00200        }
00201        assert(j>=0);
00202        roomsComboBox->setCurrentIndex(j);
00203 }
00204 
00205 void ModifyConstraintRoomNotAvailableTimesForm::itemClicked(QTableWidgetItem* item)
00206 {
00207        QString s=item->text();
00208        if(s==YES)
00209               s=NO;
00210        else{
00211               assert(s==NO);
00212               s=YES;
00213        }
00214        item->setText(s);
00215        colorItem(item);
00216 
00217        tableWidgetUpdateBug(notAllowedTimesTable);
00218 }
00219 
00220 void ModifyConstraintRoomNotAvailableTimesForm::cancel()
00221 {
00222        this->close();
00223 }
00224 
00225 void ModifyConstraintRoomNotAvailableTimesForm::ok()
00226 {
00227        double weight;
00228        QString tmp=weightLineEdit->text();
00229        weight_sscanf(tmp, "%lf", &weight);
00230        if(weight<=0.0 || weight>100){
00231               QMessageBox::warning(this, tr("FET information"),
00232                      tr("Invalid weight"));
00233               return;
00234        }
00235 
00236        int i=roomsComboBox->currentIndex();
00237        if(i<0 || roomsComboBox->count()<=0){
00238               QMessageBox::warning(this, tr("FET information"),
00239                      tr("Invalid room"));
00240               return;
00241        }
00242        
00243        Room* room=gt.rules.roomsList.at(i);
00244 
00245        this->_ctr->weightPercentage=weight;
00246        this->_ctr->room=room->name;
00247 
00248        QList<int> days;
00249        QList<int> hours;
00250        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00251               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00252                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00253                             days.append(j);
00254                             hours.append(i);
00255                      }
00256 
00257        this->_ctr->days=days;
00258        this->_ctr->hours=hours;
00259 
00260        gt.rules.internalStructureComputed=false;
00261        setRulesModifiedAndOtherThings(&gt.rules);
00262        
00263        this->close();
00264 }