Back to index

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