Back to index

fet  5.18.0
constraintmindaysbetweenactivitiesform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           constraintmindaysbetweenactivitiesform.cpp  -  description
00003                              -------------------
00004     begin                : Feb 11, 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 
00021 
00022 #include "longtextmessagebox.h"
00023 
00024 #include "constraintmindaysbetweenactivitiesform.h"
00025 #include "addconstraintmindaysbetweenactivitiesform.h"
00026 #include "modifyconstraintmindaysbetweenactivitiesform.h"
00027 
00028 #include "changemindaysselectivelyform.h"
00029 
00030 #include <QListWidget>
00031 #include <QScrollBar>
00032 #include <QAbstractItemView>
00033 
00034 ConstraintMinDaysBetweenActivitiesForm::ConstraintMinDaysBetweenActivitiesForm(QWidget* parent): QDialog(parent)
00035 {
00036        setupUi(this);
00037 
00038        currentConstraintTextEdit->setReadOnly(true);
00039        
00040        modifyConstraintPushButton->setDefault(true);
00041 
00042        constraintsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00043 
00044        connect(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
00045        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
00046        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00047        connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
00048        connect(modifyConstraintPushButton, SIGNAL(clicked()), this, SLOT(modifyConstraint()));
00049        connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
00050        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00051        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00052        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00053        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00054        connect(changeSelectivelyPushButton, SIGNAL(clicked()), this, SLOT(changeSelectively()));
00055 
00056        centerWidgetOnScreen(this);
00057        restoreFETDialogGeometry(this);
00058 
00059        QSize tmp1=teachersComboBox->minimumSizeHint();
00060        Q_UNUSED(tmp1);
00061        QSize tmp2=studentsComboBox->minimumSizeHint();
00062        Q_UNUSED(tmp2);
00063        QSize tmp3=subjectsComboBox->minimumSizeHint();
00064        Q_UNUSED(tmp3);
00065        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00066        Q_UNUSED(tmp4);
00067        
00069        teachersComboBox->addItem("");
00070        for(int i=0; i<gt.rules.teachersList.size(); i++){
00071               Teacher* tch=gt.rules.teachersList[i];
00072               teachersComboBox->addItem(tch->name);
00073        }
00074        teachersComboBox->setCurrentIndex(0);
00075 
00076        subjectsComboBox->addItem("");
00077        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00078               Subject* sb=gt.rules.subjectsList[i];
00079               subjectsComboBox->addItem(sb->name);
00080        }
00081        subjectsComboBox->setCurrentIndex(0);
00082 
00083        activityTagsComboBox->addItem("");
00084        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00085               ActivityTag* st=gt.rules.activityTagsList[i];
00086               activityTagsComboBox->addItem(st->name);
00087        }
00088        activityTagsComboBox->setCurrentIndex(0);
00089 
00090        studentsComboBox->addItem("");
00091        for(int i=0; i<gt.rules.yearsList.size(); i++){
00092               StudentsYear* sty=gt.rules.yearsList[i];
00093               studentsComboBox->addItem(sty->name);
00094               for(int j=0; j<sty->groupsList.size(); j++){
00095                      StudentsGroup* stg=sty->groupsList[j];
00096                      studentsComboBox->addItem(stg->name);
00097                      for(int k=0; k<stg->subgroupsList.size(); k++){
00098                             StudentsSubgroup* sts=stg->subgroupsList[k];
00099                             studentsComboBox->addItem(sts->name);
00100                      }
00101               }
00102        }
00103        studentsComboBox->setCurrentIndex(0);
00105 
00106        this->filterChanged();
00107 }
00108 
00109 ConstraintMinDaysBetweenActivitiesForm::~ConstraintMinDaysBetweenActivitiesForm()
00110 {
00111        saveFETDialogGeometry(this);
00112 }
00113 
00114 bool ConstraintMinDaysBetweenActivitiesForm::filterOk(TimeConstraint* ctr)
00115 {
00116        if(ctr->type!=CONSTRAINT_MIN_DAYS_BETWEEN_ACTIVITIES)
00117               return false;
00118               
00119        ConstraintMinDaysBetweenActivities* c=(ConstraintMinDaysBetweenActivities*) ctr;
00120        
00121        QString tn=teachersComboBox->currentText();
00122        QString sbn=subjectsComboBox->currentText();
00123        QString sbtn=activityTagsComboBox->currentText();
00124        QString stn=studentsComboBox->currentText();
00125        
00126        if(tn=="" && sbn=="" && sbtn=="" && stn=="")
00127               return true;
00128        
00129        bool foundTeacher=false, foundStudents=false, foundSubject=false, foundActivityTag=false;
00130               
00131        for(int i=0; i<c->n_activities; i++){
00132               int id=c->activitiesId[i];
00133               Activity* act=NULL;
00134               foreach(Activity* a, gt.rules.activitiesList)
00135                      if(a->id==id)
00136                             act=a;
00137               
00138               if(act!=NULL){
00139                      //teacher
00140                      if(tn!=""){
00141                             bool ok2=false;
00142                             for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00143                                    if(*it == tn){
00144                                           ok2=true;
00145                                           break;
00146                                    }
00147                             if(ok2)
00148                                    foundTeacher=true;
00149                      }
00150                      else
00151                             foundTeacher=true;
00152 
00153                      //subject
00154                      if(sbn!="" && sbn!=act->subjectName)
00155                             ;
00156                      else
00157                             foundSubject=true;
00158               
00159                      //activity tag
00160                      if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00161                             ;
00162                      else
00163                             foundActivityTag=true;
00164               
00165                      //students
00166                      if(stn!=""){
00167                             bool ok2=false;
00168                             for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00169                                    if(*it == stn){
00170                                           ok2=true;
00171                                           break;
00172                             }
00173                             if(ok2)
00174                                    foundStudents=true;
00175                      }
00176                      else
00177                             foundStudents=true;
00178               }
00179        }
00180        
00181        if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
00182               return true;
00183        else
00184               return false;
00185 }
00186 
00187 void ConstraintMinDaysBetweenActivitiesForm::filterChanged()
00188 {
00189        this->visibleConstraintsList.clear();
00190        constraintsListWidget->clear();
00191        for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
00192               TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
00193               if(filterOk(ctr)){
00194                      visibleConstraintsList.append(ctr);
00195                      constraintsListWidget->addItem(ctr->getDescription(gt.rules));
00196               }
00197        }
00198        
00199        if(constraintsListWidget->count()>0)
00200               constraintsListWidget->setCurrentRow(0);
00201        else
00202               currentConstraintTextEdit->setPlainText(QString(""));
00203 }
00204 
00205 void ConstraintMinDaysBetweenActivitiesForm::constraintChanged(int index)
00206 {
00207        if(index<0){
00208               currentConstraintTextEdit->setPlainText("");
00209        
00210               return;
00211        }
00212        assert(index<this->visibleConstraintsList.size());
00213        TimeConstraint* ctr=this->visibleConstraintsList.at(index);
00214        assert(ctr!=NULL);
00215        currentConstraintTextEdit->setPlainText(ctr->getDetailedDescription(gt.rules));
00216 }
00217 
00218 void ConstraintMinDaysBetweenActivitiesForm::addConstraint()
00219 {
00220        AddConstraintMinDaysBetweenActivitiesForm form(this);
00221        setParentAndOtherThings(&form, this);
00222        form.exec();
00223 
00224        filterChanged();
00225        
00226        constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
00227 }
00228 
00229 void ConstraintMinDaysBetweenActivitiesForm::modifyConstraint()
00230 {
00231        int valv=constraintsListWidget->verticalScrollBar()->value();
00232        int valh=constraintsListWidget->horizontalScrollBar()->value();
00233 
00234        int i=constraintsListWidget->currentRow();
00235        if(i<0){
00236               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00237               return;
00238        }
00239        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00240 
00241        ModifyConstraintMinDaysBetweenActivitiesForm form(this, (ConstraintMinDaysBetweenActivities*)ctr);
00242        setParentAndOtherThings(&form, this);
00243        form.exec();
00244 
00245        filterChanged();
00246 
00247        constraintsListWidget->verticalScrollBar()->setValue(valv);
00248        constraintsListWidget->horizontalScrollBar()->setValue(valh);
00249        
00250        if(i>=constraintsListWidget->count())
00251               i=constraintsListWidget->count()-1;
00252 
00253        if(i>=0)
00254               constraintsListWidget->setCurrentRow(i);
00255        else
00256               this->constraintChanged(-1);
00257 }
00258 
00259 void ConstraintMinDaysBetweenActivitiesForm::removeConstraint()
00260 {
00261        int i=constraintsListWidget->currentRow();
00262        if(i<0){
00263               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00264               return;
00265        }
00266        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00267        QString s;
00268        s=tr("Remove constraint?");
00269        s+="\n\n";
00270        s+=ctr->getDetailedDescription(gt.rules);
00271        
00272        QListWidgetItem* item;
00273 
00274        switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
00275               s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
00276        case 0: // The user clicked the OK button or pressed Enter
00277               gt.rules.removeTimeConstraint(ctr);
00278 
00279               visibleConstraintsList.removeAt(i);
00280               constraintsListWidget->setCurrentRow(-1);
00281               item=constraintsListWidget->takeItem(i);
00282               delete item;
00283 
00284               break;
00285        case 1: // The user clicked the Cancel button or pressed Escape
00286               break;
00287        }
00288        
00289        if(i>=constraintsListWidget->count())
00290               i=constraintsListWidget->count()-1;
00291        if(i>=0)
00292               constraintsListWidget->setCurrentRow(i);
00293        else
00294               this->constraintChanged(-1);
00295 }
00296 
00297 void ConstraintMinDaysBetweenActivitiesForm::changeSelectively()
00298 {
00299        ChangeMinDaysSelectivelyForm dialog(this);
00300        
00301        setParentAndOtherThings(&dialog, this);
00302        bool result=dialog.exec();
00303 
00304        if(result==QDialog::Accepted){
00305               double oldWeight=dialog.oldWeight;
00306               double newWeight=dialog.newWeight;
00307               int oldConsecutive=dialog.oldConsecutive;
00308               int newConsecutive=dialog.newConsecutive;
00309               int oldDays=dialog.oldDays;
00310               int newDays=dialog.newDays;
00311               int oldNActs=dialog.oldNActs;
00312               if(oldWeight==-1){
00313               }
00314               else if(oldWeight>=0 && oldWeight<=100.0){
00315               }
00316               else{
00317                      QMessageBox::critical(this, tr("FET information"),
00318                      tr("FET has meet a critical error - aborting current operation, please report bug (old weight is not -1 and not (>=0.0 and <=100.0)"));
00319                      return;
00320               }
00321 
00322               if(newWeight==-1){
00323               }
00324               else if(newWeight>=0 && newWeight<=100.0){
00325               }
00326               else{
00327                      QMessageBox::critical(this, tr("FET information"),
00328                      tr("FET has met a critical error - aborting current operation, please report bug (new weight is not -1 and not (>=0.0 and <=100.0)"));
00329                      return;
00330               }
00331               
00332               enum {ANY=0, YES=1, NO=2};
00333               enum {NOCHANGE=0};
00334               
00335               if(oldConsecutive<0 || oldConsecutive>2){
00336                      QMessageBox::critical(this, tr("FET information"),
00337                      tr("FET has met a critical error - aborting current operation, please report bug (old consecutive is not any, yes or no)"));
00338                      return;
00339               }
00340               
00341               if(newConsecutive<0 || newConsecutive>2){
00342                      QMessageBox::critical(this, tr("FET information"),
00343                      tr("FET has met a critical error - aborting current operation, please report bug (new consecutive is not no_change, yes or no)"));
00344                      return;
00345               }
00346               
00347               if(oldDays==-1){
00348               }
00349               else if(oldDays>=1 && oldDays<=gt.rules.nDaysPerWeek){
00350               }
00351               else{
00352                      QMessageBox::critical(this, tr("FET information"),
00353                      tr("FET has met a critical error - aborting current operation, please report bug (old min days is not -1 or 1..ndaysperweek)"));
00354                      return;
00355               }
00356               
00357               if(newDays==-1){
00358               }
00359               else if(newDays>=1 && newDays<=gt.rules.nDaysPerWeek){
00360               }
00361               else{
00362                      QMessageBox::critical(this, tr("FET information"),
00363                      tr("FET has met a critical error - aborting current operation, please report bug (new min days is not -1 or 1..ndaysperweek)"));
00364                      return;
00365               }
00366               
00367               if(oldNActs==-1){
00368               }
00369               else if(oldNActs>=1){
00370               }
00371               else{
00372                      QMessageBox::critical(this, tr("FET information"),
00373                      tr("FET has met a critical error - aborting current operation, please report bug (old nActivities is not -1 or >=1)"));
00374                      return;
00375               }
00376               
00377               int count=0;
00378 
00379               foreach(TimeConstraint* tc, gt.rules.timeConstraintsList)
00380                      if(tc->type==CONSTRAINT_MIN_DAYS_BETWEEN_ACTIVITIES){
00381                             ConstraintMinDaysBetweenActivities* mc=(ConstraintMinDaysBetweenActivities*)tc;
00382                             bool okw, okd, okc, okn;
00383                             if(oldWeight==-1)
00384                                    okw=true;
00385                             else if(oldWeight==mc->weightPercentage)
00386                                    okw=true;
00387                             else
00388                                    okw=false;
00389                                    
00390                             if(oldConsecutive==ANY)
00391                                    okc=true;
00392                             else if(oldConsecutive==YES && mc->consecutiveIfSameDay==true)
00393                                    okc=true;
00394                             else if(oldConsecutive==NO && mc->consecutiveIfSameDay==false)
00395                                    okc=true;
00396                             else
00397                                    okc=false;
00398                                    
00399                             if(oldDays==-1)
00400                                    okd=true;
00401                             else if(oldDays==mc->minDays)
00402                                    okd=true;
00403                             else
00404                                    okd=false;
00405                                    
00406                             if(oldNActs==-1)
00407                                    okn=true;
00408                             else if(mc->n_activities==oldNActs)
00409                                    okn=true;
00410                             else
00411                                    okn=false;
00412                                    
00413                             if(okw && okc && okd && okn){
00414                                    if(newWeight>=0)
00415                                           mc->weightPercentage=newWeight;
00416                                           
00417                                    if(newConsecutive==YES)
00418                                           mc->consecutiveIfSameDay=true;
00419                                    else if(newConsecutive==NO)
00420                                           mc->consecutiveIfSameDay=false;
00421                                           
00422                                    if(newDays>=0)
00423                                           mc->minDays=newDays;
00424                                    
00425                                    count++;
00426                             }
00427                      }
00428 
00429               QMessageBox::information(this, tr("FET information"), tr("There were inspected (and possibly modified) %1 constraints min days between activities matching your criteria")
00430                .arg(count)+"\n\n"+
00431                      tr("NOTE: If you are using constraints of type activities same starting time or activities same starting day, it is important"
00432                       " (after current operation) to apply the operation of removing redundant constraints.")
00433                      +" "+tr("Read Help/Important tips - tip 2) for details.")
00434                );
00435 
00436               gt.rules.internalStructureComputed=false;
00437               setRulesModifiedAndOtherThings(&gt.rules);
00438 
00439               this->filterChanged();
00440        }
00441 }