Back to index

fet  5.18.0
modifyconstraintbreaktimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           modifyconstraintbreaktimesform.cpp  -  description
00003                              -------------------
00004     begin                : Feb 10, 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 "modifyconstraintbreaktimesform.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("X"))
00033 #define NO           (QString(" "))
00034 
00035 ModifyConstraintBreakTimesForm::ModifyConstraintBreakTimesForm(QWidget* parent, ConstraintBreakTimes* 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(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllAllowed()));
00045        connect(setAllBreakPushButton, SIGNAL(clicked()), this, SLOT(setAllBreak()));
00046 
00047        centerWidgetOnScreen(this);
00048        restoreFETDialogGeometry(this);
00049        
00050        this->_ctr=ctr;
00051        
00052        weightLineEdit->setText(CustomFETString::number(ctr->weightPercentage));
00053        
00054        notAllowedTimesTable->setRowCount(gt.rules.nHoursPerDay);
00055        notAllowedTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00056 
00057        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00058               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00059               notAllowedTimesTable->setHorizontalHeaderItem(j, item);
00060        }
00061        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00062               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00063               notAllowedTimesTable->setVerticalHeaderItem(i, item);
00064        }
00065 
00066        //bool currentMatrix[MAX_HOURS_PER_DAY][MAX_DAYS_PER_WEEK];
00067        Matrix2D<bool> currentMatrix;
00068        currentMatrix.resize(gt.rules.nHoursPerDay, gt.rules.nDaysPerWeek);
00069 
00070        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00071               for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00072                      currentMatrix[i][j]=false;                
00073        assert(ctr->days.count()==ctr->hours.count());   
00074        for(int k=0; k<ctr->days.count(); k++){
00075               if(ctr->hours.at(k)==-1 || ctr->days.at(k)==-1)
00076                      assert(0);
00077               int i=ctr->hours.at(k);
00078               int j=ctr->days.at(k);
00079               if(i>=0 && i<gt.rules.nHoursPerDay && j>=0 && j<gt.rules.nDaysPerWeek)
00080                      currentMatrix[i][j]=true;
00081        }
00082 
00083        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00084               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00085                      QTableWidgetItem* item= new QTableWidgetItem();
00086                      item->setTextAlignment(Qt::AlignCenter);
00087                      item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
00088                      notAllowedTimesTable->setItem(i, j, item);
00089 
00090                      if(!currentMatrix[i][j])
00091                             item->setText(NO);
00092                      else
00093                             item->setText(YES);
00094                             
00095                      colorItem(item);
00096               }
00097               
00098        notAllowedTimesTable->resizeRowsToContents();
00099 
00100        connect(notAllowedTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00101        connect(notAllowedTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00102 
00103        notAllowedTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00104        
00105        tableWidgetUpdateBug(notAllowedTimesTable);
00106        
00107        setStretchAvailabilityTableNicely(notAllowedTimesTable);
00108 }
00109 
00110 ModifyConstraintBreakTimesForm::~ModifyConstraintBreakTimesForm()
00111 {
00112        saveFETDialogGeometry(this);
00113 }
00114 
00115 void ModifyConstraintBreakTimesForm::colorItem(QTableWidgetItem* item)
00116 {
00117        if(USE_GUI_COLORS){
00118               if(item->text()==NO)
00119                      item->setBackground(QBrush(Qt::darkGreen));
00120               else
00121                      item->setBackground(QBrush(Qt::darkRed));
00122               item->setForeground(QBrush(Qt::lightGray));
00123        }
00124 }
00125 
00126 void ModifyConstraintBreakTimesForm::horizontalHeaderClicked(int col)
00127 {
00128        if(col>=0 && col<gt.rules.nDaysPerWeek){
00129               QString s=notAllowedTimesTable->item(0, col)->text();
00130               if(s==YES)
00131                      s=NO;
00132               else{
00133                      assert(s==NO);
00134                      s=YES;
00135               }
00136 
00137               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00138                      notAllowedTimesTable->item(row, col)->setText(s);
00139                      colorItem(notAllowedTimesTable->item(row,col));
00140               }
00141               tableWidgetUpdateBug(notAllowedTimesTable);
00142        }
00143 }
00144 
00145 void ModifyConstraintBreakTimesForm::verticalHeaderClicked(int row)
00146 {
00147        if(row>=0 && row<gt.rules.nHoursPerDay){
00148               QString s=notAllowedTimesTable->item(row, 0)->text();
00149               if(s==YES)
00150                      s=NO;
00151               else{
00152                      assert(s==NO);
00153                      s=YES;
00154               }
00155        
00156               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00157                      notAllowedTimesTable->item(row, col)->setText(s);
00158                      colorItem(notAllowedTimesTable->item(row,col));
00159               }
00160               tableWidgetUpdateBug(notAllowedTimesTable);
00161        }
00162 }
00163 
00164 void ModifyConstraintBreakTimesForm::setAllAllowed()
00165 {
00166        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00167               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00168                      notAllowedTimesTable->item(i, j)->setText(NO);
00169                      colorItem(notAllowedTimesTable->item(i,j));
00170               }
00171        tableWidgetUpdateBug(notAllowedTimesTable);
00172 }
00173 
00174 void ModifyConstraintBreakTimesForm::setAllBreak()
00175 {
00176        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00177               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00178                      notAllowedTimesTable->item(i, j)->setText(YES);
00179                      colorItem(notAllowedTimesTable->item(i,j));
00180               }
00181        tableWidgetUpdateBug(notAllowedTimesTable);
00182 }
00183 
00184 void ModifyConstraintBreakTimesForm::itemClicked(QTableWidgetItem* item)
00185 {
00186        QString s=item->text();
00187        if(s==YES)
00188               s=NO;
00189        else{
00190               assert(s==NO);
00191               s=YES;
00192        }
00193        item->setText(s);
00194        colorItem(item);
00195 
00196        tableWidgetUpdateBug(notAllowedTimesTable);
00197 }
00198 
00199 void ModifyConstraintBreakTimesForm::ok()
00200 {
00201        double weight;
00202        QString tmp=weightLineEdit->text();
00203        weight_sscanf(tmp, "%lf", &weight);
00204        if(weight<100.0 || weight>100.0){
00205               QMessageBox::warning(this, tr("FET information"),
00206                      tr("Invalid weight (percentage). It has to be 100"));
00207               return;
00208        }
00209 
00210        this->_ctr->weightPercentage=weight;
00211 
00212        QList<int> days;
00213        QList<int> hours;
00214        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00215               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00216                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00217                             days.append(j);
00218                             hours.append(i);
00219                      }
00220 
00221        this->_ctr->days=days;
00222        this->_ctr->hours=hours;
00223 
00224        gt.rules.internalStructureComputed=false;
00225        setRulesModifiedAndOtherThings(&gt.rules);
00226        
00227        this->close();
00228 }
00229 
00230 void ModifyConstraintBreakTimesForm::cancel()
00231 {
00232        this->close();
00233 }