Back to index

fet  5.18.0
addconstraintbreaktimesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           addconstraintbreaktimesform.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 "longtextmessagebox.h"
00023 
00024 #include "addconstraintbreaktimesform.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("X"))
00035 #define NO    (QString(" "))
00036 
00037 AddConstraintBreakTimesForm::AddConstraintBreakTimesForm(QWidget* parent): QDialog(parent)
00038 {
00039        setupUi(this);
00040 
00041        addConstraintPushButton->setDefault(true);
00042 
00043        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addCurrentConstraint()));
00044        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00045        connect(notAllowedTimesTable, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));
00046        connect(setAllAllowedPushButton, SIGNAL(clicked()), this, SLOT(setAllAllowed()));
00047        connect(setAllBreakPushButton, SIGNAL(clicked()), this, SLOT(setAllBreak()));
00048 
00049        centerWidgetOnScreen(this);
00050        restoreFETDialogGeometry(this);
00051                                                  
00052        notAllowedTimesTable->setRowCount(gt.rules.nHoursPerDay);
00053        notAllowedTimesTable->setColumnCount(gt.rules.nDaysPerWeek);
00054 
00055        for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00056               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.daysOfTheWeek[j]);
00057               notAllowedTimesTable->setHorizontalHeaderItem(j, item);
00058        }
00059        for(int i=0; i<gt.rules.nHoursPerDay; i++){
00060               QTableWidgetItem* item=new QTableWidgetItem(gt.rules.hoursOfTheDay[i]);
00061               notAllowedTimesTable->setVerticalHeaderItem(i, item);
00062        }
00063 
00064        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00065               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00066                      QTableWidgetItem* item=new QTableWidgetItem(NO);
00067                      item->setTextAlignment(Qt::AlignCenter);
00068                      item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
00069                      colorItem(item);
00070                      notAllowedTimesTable->setItem(i, j, item);
00071               }
00072               
00073        notAllowedTimesTable->resizeRowsToContents();
00074        //notAllowedTimesTable->resizeColumnsToContents();
00075 
00076        connect(notAllowedTimesTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(horizontalHeaderClicked(int)));
00077        connect(notAllowedTimesTable->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(verticalHeaderClicked(int)));
00078 
00079        notAllowedTimesTable->setSelectionMode(QAbstractItemView::NoSelection);
00080        
00081        tableWidgetUpdateBug(notAllowedTimesTable);
00082        
00083        setStretchAvailabilityTableNicely(notAllowedTimesTable);
00084 }
00085 
00086 AddConstraintBreakTimesForm::~AddConstraintBreakTimesForm()
00087 {
00088        saveFETDialogGeometry(this);
00089 }
00090 
00091 void AddConstraintBreakTimesForm::colorItem(QTableWidgetItem* item)
00092 {
00093        if(USE_GUI_COLORS){
00094               if(item->text()==NO)
00095                      item->setBackground(QBrush(Qt::darkGreen));
00096               else
00097                      item->setBackground(QBrush(Qt::darkRed));
00098               item->setForeground(QBrush(Qt::lightGray));
00099        }
00100 }
00101 
00102 void AddConstraintBreakTimesForm::horizontalHeaderClicked(int col)
00103 {
00104        if(col>=0 && col<gt.rules.nDaysPerWeek){
00105               QString s=notAllowedTimesTable->item(0, col)->text();
00106               if(s==YES)
00107                      s=NO;
00108               else{
00109                      assert(s==NO);
00110                      s=YES;
00111               }
00112 
00113               for(int row=0; row<gt.rules.nHoursPerDay; row++){
00114                      notAllowedTimesTable->item(row, col)->setText(s);
00115                      colorItem(notAllowedTimesTable->item(row,col));
00116               }
00117               tableWidgetUpdateBug(notAllowedTimesTable);
00118        }
00119 }
00120 
00121 void AddConstraintBreakTimesForm::verticalHeaderClicked(int row)
00122 {
00123        if(row>=0 && row<gt.rules.nHoursPerDay){
00124               QString s=notAllowedTimesTable->item(row, 0)->text();
00125               if(s==YES)
00126                      s=NO;
00127               else{
00128                      assert(s==NO);
00129                      s=YES;
00130               }
00131        
00132               for(int col=0; col<gt.rules.nDaysPerWeek; col++){
00133                      notAllowedTimesTable->item(row, col)->setText(s);
00134                      colorItem(notAllowedTimesTable->item(row,col));
00135               }
00136               tableWidgetUpdateBug(notAllowedTimesTable);
00137        }
00138 }
00139 
00140 void AddConstraintBreakTimesForm::setAllAllowed()
00141 {
00142        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00143               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00144                      notAllowedTimesTable->item(i, j)->setText(NO);
00145                      colorItem(notAllowedTimesTable->item(i,j));
00146               }
00147        tableWidgetUpdateBug(notAllowedTimesTable);
00148 }
00149 
00150 void AddConstraintBreakTimesForm::setAllBreak()
00151 {
00152        for(int i=0; i<gt.rules.nHoursPerDay; i++)
00153               for(int j=0; j<gt.rules.nDaysPerWeek; j++){
00154                      notAllowedTimesTable->item(i, j)->setText(YES);
00155                      colorItem(notAllowedTimesTable->item(i,j));
00156               }
00157        tableWidgetUpdateBug(notAllowedTimesTable);
00158 }
00159 
00160 void AddConstraintBreakTimesForm::itemClicked(QTableWidgetItem* item)
00161 {
00162        QString s=item->text();
00163        if(s==YES)
00164               s=NO;
00165        else{
00166               assert(s==NO);
00167               s=YES;
00168        }
00169        item->setText(s);
00170        colorItem(item);
00171        tableWidgetUpdateBug(notAllowedTimesTable);
00172 }
00173 
00174 void AddConstraintBreakTimesForm::addCurrentConstraint()
00175 {
00176        TimeConstraint *ctr=NULL;
00177 
00178        double weight;
00179        QString tmp=weightLineEdit->text();
00180        weight_sscanf(tmp, "%lf", &weight);
00181        if(weight<100.0 || weight>100.0){
00182               QMessageBox::warning(this, tr("FET information"),
00183                      tr("Invalid weight (percentage). It has to be 100"));
00184               return;
00185        }
00186 
00187        QList<int> days;
00188        QList<int> hours;
00189        for(int j=0; j<gt.rules.nDaysPerWeek; j++)
00190               for(int i=0; i<gt.rules.nHoursPerDay; i++)
00191                      if(notAllowedTimesTable->item(i, j)->text()==YES){
00192                             days.append(j);
00193                             hours.append(i);
00194                      }
00195 
00196        ctr=new ConstraintBreakTimes(weight, days, hours);
00197 
00198        bool tmp2=gt.rules.addTimeConstraint(ctr);
00199        if(tmp2)
00200               LongTextMessageBox::information(this, tr("FET information"),
00201                      tr("Constraint added:")+"\n\n"+ctr->getDetailedDescription(gt.rules));
00202        else{
00203               QMessageBox::warning(this, tr("FET information"),
00204                      tr("Constraint NOT added - there must be another constraint of this "
00205                        "type. Please edit that one"));
00206               delete ctr;
00207        }
00208 }