Back to index

fet  5.18.0
constraintactivitiessamestartingtimeform.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           constraintactivitiessamestartingtimeform.cpp  -  description
00003                              -------------------
00004     begin                : 23 June 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 "longtextmessagebox.h"
00021 
00022 #include "constraintactivitiessamestartingtimeform.h"
00023 #include "addconstraintactivitiessamestartingtimeform.h"
00024 #include "modifyconstraintactivitiessamestartingtimeform.h"
00025 
00026 #include <QListWidget>
00027 #include <QScrollBar>
00028 #include <QAbstractItemView>
00029 
00030 ConstraintActivitiesSameStartingTimeForm::ConstraintActivitiesSameStartingTimeForm(QWidget* parent): QDialog(parent)
00031 {
00032        setupUi(this);
00033 
00034        currentConstraintTextEdit->setReadOnly(true);
00035 
00036        modifyConstraintPushButton->setDefault(true);
00037        
00038        constraintsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00039 
00040        connect(addConstraintPushButton, SIGNAL(clicked()), this, SLOT(addConstraint()));
00041        connect(removeConstraintPushButton, SIGNAL(clicked()), this, SLOT(removeConstraint()));
00042        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00043        connect(constraintsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(constraintChanged(int)));
00044        connect(modifyConstraintPushButton, SIGNAL(clicked()), this, SLOT(modifyConstraint()));
00045        connect(constraintsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyConstraint()));
00046        connect(teachersComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00047        connect(studentsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00048        connect(subjectsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00049        connect(activityTagsComboBox, SIGNAL(activated(QString)), this, SLOT(filterChanged()));
00050        connect(helpPushButton, SIGNAL(clicked()), this, SLOT(help()));
00051 
00052        centerWidgetOnScreen(this);
00053        restoreFETDialogGeometry(this);
00054 
00055        QSize tmp1=teachersComboBox->minimumSizeHint();
00056        Q_UNUSED(tmp1);
00057        QSize tmp2=studentsComboBox->minimumSizeHint();
00058        Q_UNUSED(tmp2);
00059        QSize tmp3=subjectsComboBox->minimumSizeHint();
00060        Q_UNUSED(tmp3);
00061        QSize tmp4=activityTagsComboBox->minimumSizeHint();
00062        Q_UNUSED(tmp4);
00063        
00065        teachersComboBox->addItem("");
00066        for(int i=0; i<gt.rules.teachersList.size(); i++){
00067               Teacher* tch=gt.rules.teachersList[i];
00068               teachersComboBox->addItem(tch->name);
00069        }
00070        teachersComboBox->setCurrentIndex(0);
00071 
00072        subjectsComboBox->addItem("");
00073        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00074               Subject* sb=gt.rules.subjectsList[i];
00075               subjectsComboBox->addItem(sb->name);
00076        }
00077        subjectsComboBox->setCurrentIndex(0);
00078 
00079        activityTagsComboBox->addItem("");
00080        for(int i=0; i<gt.rules.activityTagsList.size(); i++){
00081               ActivityTag* st=gt.rules.activityTagsList[i];
00082               activityTagsComboBox->addItem(st->name);
00083        }
00084        activityTagsComboBox->setCurrentIndex(0);
00085 
00086        studentsComboBox->addItem("");
00087        for(int i=0; i<gt.rules.yearsList.size(); i++){
00088               StudentsYear* sty=gt.rules.yearsList[i];
00089               studentsComboBox->addItem(sty->name);
00090               for(int j=0; j<sty->groupsList.size(); j++){
00091                      StudentsGroup* stg=sty->groupsList[j];
00092                      studentsComboBox->addItem(stg->name);
00093                      for(int k=0; k<stg->subgroupsList.size(); k++){
00094                             StudentsSubgroup* sts=stg->subgroupsList[k];
00095                             studentsComboBox->addItem(sts->name);
00096                      }
00097               }
00098        }
00099        studentsComboBox->setCurrentIndex(0);
00101 
00102        this->filterChanged();
00103 }
00104 
00105 ConstraintActivitiesSameStartingTimeForm::~ConstraintActivitiesSameStartingTimeForm()
00106 {
00107        saveFETDialogGeometry(this);
00108 }
00109 
00110 void ConstraintActivitiesSameStartingTimeForm::refreshConstraintsListWidget()
00111 {
00112        this->visibleConstraintsList.clear();
00113        constraintsListWidget->clear();
00114        for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
00115               TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
00116               if(filterOk(ctr)){
00117                      QString s;
00118                      s=ctr->getDescription(gt.rules);
00119                      visibleConstraintsList.append(ctr);
00120                      constraintsListWidget->addItem(s);
00121               }
00122        }
00123 
00124        if(constraintsListWidget->count()>0){
00125               constraintsListWidget->setCurrentRow(0);
00126               constraintChanged(constraintsListWidget->currentRow());
00127        }
00128        else
00129               constraintChanged(-1);
00130 }
00131 
00132 bool ConstraintActivitiesSameStartingTimeForm::filterOk(TimeConstraint* ctr)
00133 {
00134        if(ctr->type!=CONSTRAINT_ACTIVITIES_SAME_STARTING_TIME)
00135               return false;
00136               
00137        ConstraintActivitiesSameStartingTime* c=(ConstraintActivitiesSameStartingTime*) ctr;
00138        
00139        QString tn=teachersComboBox->currentText();
00140        QString sbn=subjectsComboBox->currentText();
00141        QString sbtn=activityTagsComboBox->currentText();
00142        QString stn=studentsComboBox->currentText();
00143        
00144        if(tn=="" && sbn=="" && sbtn=="" && stn=="")
00145               return true;
00146        
00147        bool foundTeacher=false, foundStudents=false, foundSubject=false, foundActivityTag=false;
00148               
00149        for(int i=0; i<c->n_activities; i++){
00150               //bool found=true;
00151        
00152               int id=c->activitiesId[i];
00153               Activity* act=NULL;
00154               foreach(Activity* a, gt.rules.activitiesList)
00155                      if(a->id==id)
00156                             act=a;
00157               
00158               if(act!=NULL){
00159                      //teacher
00160                      if(tn!=""){
00161                             bool ok2=false;
00162                             for(QStringList::Iterator it=act->teachersNames.begin(); it!=act->teachersNames.end(); it++)
00163                                    if(*it == tn){
00164                                           ok2=true;
00165                                           break;
00166                                    }
00167                             if(ok2)
00168                                    foundTeacher=true;
00169                      }
00170                      else
00171                             foundTeacher=true;
00172 
00173                      //subject
00174                      if(sbn!="" && sbn!=act->subjectName)
00175                             ;
00176                      else
00177                             foundSubject=true;
00178               
00179                      //activity tag
00180                      if(sbtn!="" && !act->activityTagsNames.contains(sbtn))
00181                             ;
00182                      else
00183                             foundActivityTag=true;
00184               
00185                      //students
00186                      if(stn!=""){
00187                             bool ok2=false;
00188                             for(QStringList::Iterator it=act->studentsNames.begin(); it!=act->studentsNames.end(); it++)
00189                                    if(*it == stn){
00190                                           ok2=true;
00191                                           break;
00192                             }
00193                             if(ok2)
00194                                    foundStudents=true;
00195                      }
00196                      else
00197                             foundStudents=true;
00198               }
00199        }
00200        
00201        if(foundTeacher && foundStudents && foundSubject && foundActivityTag)
00202               return true;
00203        else
00204               return false;
00205 }
00206 
00207 void ConstraintActivitiesSameStartingTimeForm::filterChanged()
00208 {
00209        this->visibleConstraintsList.clear();
00210        constraintsListWidget->clear();
00211        for(int i=0; i<gt.rules.timeConstraintsList.size(); i++){
00212               TimeConstraint* ctr=gt.rules.timeConstraintsList[i];
00213               if(filterOk(ctr)){
00214                      visibleConstraintsList.append(ctr);
00215                      constraintsListWidget->addItem(ctr->getDescription(gt.rules));
00216               }
00217        }
00218        
00219        if(constraintsListWidget->count()>0)
00220               constraintsListWidget->setCurrentRow(0);
00221        else
00222               constraintChanged(-1);
00223 }
00224 
00225 void ConstraintActivitiesSameStartingTimeForm::constraintChanged(int index)
00226 {
00227        if(index<0){
00228               currentConstraintTextEdit->setPlainText("");
00229               return;
00230        }
00231        QString s;
00232        assert(index<this->visibleConstraintsList.size());
00233        TimeConstraint* ctr=this->visibleConstraintsList.at(index);
00234        assert(ctr!=NULL);
00235        s=ctr->getDetailedDescription(gt.rules);
00236        currentConstraintTextEdit->setPlainText(s);
00237 }
00238 
00239 void ConstraintActivitiesSameStartingTimeForm::addConstraint()
00240 {
00241        AddConstraintActivitiesSameStartingTimeForm form(this);
00242        setParentAndOtherThings(&form, this);
00243        form.exec();
00244 
00245        this->refreshConstraintsListWidget();
00246        
00247        constraintsListWidget->setCurrentRow(constraintsListWidget->count()-1);
00248 }
00249 
00250 void ConstraintActivitiesSameStartingTimeForm::modifyConstraint()
00251 {
00252        int valv=constraintsListWidget->verticalScrollBar()->value();
00253        int valh=constraintsListWidget->horizontalScrollBar()->value();
00254 
00255        int i=constraintsListWidget->currentRow();
00256        if(i<0){
00257               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00258               return;
00259        }
00260        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00261 
00262        ModifyConstraintActivitiesSameStartingTimeForm form(this, (ConstraintActivitiesSameStartingTime*)ctr);
00263        setParentAndOtherThings(&form, this);
00264        form.exec();
00265 
00266        refreshConstraintsListWidget();
00267        
00268        constraintsListWidget->verticalScrollBar()->setValue(valv);
00269        constraintsListWidget->horizontalScrollBar()->setValue(valh);
00270        
00271        if(i>=constraintsListWidget->count())
00272               i=constraintsListWidget->count()-1;
00273        
00274        if(i>=0)
00275               constraintsListWidget->setCurrentRow(i);
00276        else
00277               this->constraintChanged(-1);
00278 }
00279 
00280 void ConstraintActivitiesSameStartingTimeForm::removeConstraint()
00281 {
00282        int i=constraintsListWidget->currentRow();
00283        if(i<0){
00284               QMessageBox::information(this, tr("FET information"), tr("Invalid selected constraint"));
00285               return;
00286        }
00287        TimeConstraint* ctr=this->visibleConstraintsList.at(i);
00288        QString s;
00289        s=tr("Remove constraint?");
00290        s+="\n\n";
00291        s+=ctr->getDetailedDescription(gt.rules);
00292        
00293        QListWidgetItem* item;
00294 
00295        switch( LongTextMessageBox::confirmation( this, tr("FET confirmation"),
00296               s, tr("Yes"), tr("No"), 0, 0, 1 ) ){
00297        case 0: // The user clicked the OK button or pressed Enter
00298               gt.rules.removeTimeConstraint(ctr);
00299               
00300               visibleConstraintsList.removeAt(i);
00301               constraintsListWidget->setCurrentRow(-1);
00302               item=constraintsListWidget->takeItem(i);
00303               delete item;
00304               
00305               break;
00306        case 1: // The user clicked the Cancel button or pressed Escape
00307               break;
00308        }
00309 
00310        if(i>=constraintsListWidget->count())
00311               i=constraintsListWidget->count()-1;
00312        if(i>=0)
00313               constraintsListWidget->setCurrentRow(i);
00314        else
00315               this->constraintChanged(-1);
00316 }
00317 
00318 void ConstraintActivitiesSameStartingTimeForm::help()
00319 {
00320        QString s;
00321 
00322        s+=tr("IMPORTANT: after adding such constraints, it is necessary (otherwise generation might be impossible) to remove redundant constraints"
00323        " min days between activities. If you are sure that you don't have redundant constraints, you can skip this step, but it doesn't hurt to do it as a precaution."
00324        " Also, you don't have to do that after each added constraint, but only once after adding more constraints of this type."
00325        " Please read Help/Important tips - tip number 2 for details");
00326 
00327        LongTextMessageBox::largeInformation(this, tr("FET help"), s);
00328 }