Back to index

fet  5.18.0
teachersform.cpp
Go to the documentation of this file.
00001 //
00002 //
00003 // Description: This file is part of FET
00004 //
00005 //
00006 // Author: Lalescu Liviu <Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)>
00007 // Copyright (C) 2003 Liviu Lalescu <http://lalescu.ro/liviu/>
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 "timetable_defs.h"
00019 #include "timetable.h"
00020 #include "fet.h"
00021 #include "teachersform.h"
00022 #include "teacher.h"
00023 
00024 #include <QInputDialog>
00025 
00026 #include <QMessageBox>
00027 
00028 #include <QListWidget>
00029 #include <QAbstractItemView>
00030 
00031 #include <QSplitter>
00032 #include <QSettings>
00033 #include <QObject>
00034 #include <QMetaObject>
00035 
00036 extern const QString COMPANY;
00037 extern const QString PROGRAM;
00038 
00039 TeachersForm::TeachersForm(QWidget* parent): QDialog(parent)
00040 {
00041        setupUi(this);
00042        
00043        currentTeacherTextEdit->setReadOnly(true);
00044 
00045        renameTeacherPushButton->setDefault(true);
00046 
00047        teachersListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00048 
00049        connect(renameTeacherPushButton, SIGNAL(clicked()), this, SLOT(renameTeacher()));
00050        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00051        connect(addTeacherPushButton, SIGNAL(clicked()), this, SLOT(addTeacher()));
00052        connect(sortTeachersPushButton, SIGNAL(clicked()), this, SLOT(sortTeachers()));
00053        connect(removeTeacherPushButton, SIGNAL(clicked()), this, SLOT(removeTeacher()));
00054        connect(teachersListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(teacherChanged(int)));
00055        connect(activateTeacherPushButton, SIGNAL(clicked()), this, SLOT(activateTeacher()));
00056        connect(deactivateTeacherPushButton, SIGNAL(clicked()), this, SLOT(deactivateTeacher()));
00057        connect(teachersListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(renameTeacher()));
00058 
00059        centerWidgetOnScreen(this);
00060        restoreFETDialogGeometry(this);
00061        //restore splitter state
00062        QSettings settings(COMPANY, PROGRAM);
00063        if(settings.contains(this->metaObject()->className()+QString("/splitter-state")))
00064               splitter->restoreState(settings.value(this->metaObject()->className()+QString("/splitter-state")).toByteArray());
00065        
00066        teachersListWidget->clear();
00067        for(int i=0; i<gt.rules.teachersList.size(); i++){
00068               Teacher* tch=gt.rules.teachersList[i];
00069               teachersListWidget->addItem(tch->name);
00070        }
00071        
00072        if(teachersListWidget->count()>0)
00073               teachersListWidget->setCurrentRow(0);
00074 }
00075 
00076 
00077 TeachersForm::~TeachersForm()
00078 {
00079        saveFETDialogGeometry(this);
00080        //save splitter state
00081        QSettings settings(COMPANY, PROGRAM);
00082        settings.setValue(this->metaObject()->className()+QString("/splitter-state"), splitter->saveState());
00083 }
00084 
00085 void TeachersForm::addTeacher()
00086 {
00087        bool ok = FALSE;
00088        Teacher* tch=new Teacher();
00089        tch->name = QInputDialog::getText( this, tr("Add teacher"), tr("Please enter teacher's name") ,
00090         QLineEdit::Normal, QString(), &ok );
00091 
00092        if ( ok && !((tch->name).isEmpty()) ){
00093               // user entered something and pressed OK
00094               if(!gt.rules.addTeacher(tch)){
00095                      QMessageBox::information( this, tr("Teacher insertion dialog"),
00096                             tr("Could not insert item. Must be a duplicate"));
00097                      delete tch;
00098               }
00099               else{
00100                      teachersListWidget->addItem(tch->name);
00101                      teachersListWidget->setCurrentRow(teachersListWidget->count()-1);
00102               }
00103        }
00104        else{
00105               if(ok){ //the user entered nothing
00106                      QMessageBox::information(this, tr("FET information"), tr("Incorrect name"));
00107               }
00108               delete tch;// user entered nothing or pressed Cancel
00109        }
00110 }
00111 
00112 void TeachersForm::removeTeacher()
00113 {
00114        int i=teachersListWidget->currentRow();
00115        if(teachersListWidget->currentRow()<0){
00116               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00117               return;
00118        }
00119 
00120        QString text=teachersListWidget->currentItem()->text();
00121        int teacher_ID=gt.rules.searchTeacher(text);
00122        if(teacher_ID<0){
00123               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00124               return;
00125        }
00126 
00127        if(QMessageBox::warning( this, tr("FET"),
00128               tr("Are you sure you want to delete this teacher and all related activities and constraints?"),
00129               tr("Yes"), tr("No"), 0, 0, 1 ) == 1)
00130               return;
00131 
00132        int tmp=gt.rules.removeTeacher(text);
00133        if(tmp){
00134               teachersListWidget->setCurrentRow(-1);
00135               QListWidgetItem* item;
00136               item=teachersListWidget->takeItem(i);
00137               delete item;
00138 
00139               if(i>=teachersListWidget->count())
00140                      i=teachersListWidget->count()-1;
00141               if(i>=0)
00142                      teachersListWidget->setCurrentRow(i);
00143               else
00144                      currentTeacherTextEdit->setPlainText(QString(""));
00145        }
00146 }
00147 
00148 void TeachersForm::renameTeacher()
00149 {
00150        int i=teachersListWidget->currentRow();
00151        if(teachersListWidget->currentRow()<0){
00152               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00153               return;
00154        }
00155 
00156        QString initialTeacherName=teachersListWidget->currentItem()->text();
00157        int teacher_ID=gt.rules.searchTeacher(initialTeacherName);
00158        if(teacher_ID<0){
00159               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00160               return;
00161        }
00162 
00163        bool ok = FALSE;
00164        QString finalTeacherName;
00165        finalTeacherName = QInputDialog::getText( this, tr("Modify teacher"), tr("Please enter new teacher's name") ,
00166         QLineEdit::Normal, initialTeacherName, &ok);
00167 
00168        if ( ok && !(finalTeacherName.isEmpty())){
00169               // user entered something and pressed OK
00170               if(gt.rules.searchTeacher(finalTeacherName)>=0){
00171                      QMessageBox::information( this, tr("Teacher modification dialog"),
00172                             tr("Could not modify item. New name must be a duplicate"));
00173               }
00174               else{
00175                      gt.rules.modifyTeacher(initialTeacherName, finalTeacherName);
00176                      teachersListWidget->item(i)->setText(finalTeacherName);
00177                      teacherChanged(teachersListWidget->currentRow());
00178               }
00179        }
00180 }
00181 
00182 void TeachersForm::sortTeachers()
00183 {
00184        gt.rules.sortTeachersAlphabetically();
00185 
00186        teachersListWidget->clear();
00187        for(int i=0; i<gt.rules.teachersList.size(); i++){
00188               Teacher* tch=gt.rules.teachersList[i];
00189               teachersListWidget->addItem(tch->name);
00190        }
00191        
00192        if(teachersListWidget->count()>0)
00193               teachersListWidget->setCurrentRow(0);
00194 }
00195 
00196 void TeachersForm::teacherChanged(int index)
00197 {
00198        if(index<0){
00199               currentTeacherTextEdit->setPlainText("");
00200               return;
00201        }
00202        
00203        Teacher* t=gt.rules.teachersList.at(index);
00204        assert(t);
00205        QString s=t->getDetailedDescriptionWithConstraints(gt.rules);
00206        currentTeacherTextEdit->setPlainText(s);
00207 }
00208 
00209 void TeachersForm::activateTeacher()
00210 {
00211        if(teachersListWidget->currentRow()<0){
00212               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00213               return;
00214        }
00215 
00216        QString teacherName=teachersListWidget->currentItem()->text();
00217        int count=gt.rules.activateTeacher(teacherName);
00218        QMessageBox::information(this, tr("FET information"), tr("Activated a number of %1 activities").arg(count));
00219 }
00220 
00221 void TeachersForm::deactivateTeacher()
00222 {
00223        if(teachersListWidget->currentRow()<0){
00224               QMessageBox::information(this, tr("FET information"), tr("Invalid selected teacher"));
00225               return;
00226        }
00227 
00228        QString teacherName=teachersListWidget->currentItem()->text();
00229        int count=gt.rules.deactivateTeacher(teacherName);
00230        QMessageBox::information(this, tr("FET information"), tr("De-activated a number of %1 activities").arg(count));
00231 }