Back to index

fet  5.18.0
subjectsform.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 "subjectsform.h"
00022 #include "studentsset.h"
00023 #include "teacher.h"
00024 #include "subject.h"
00025 
00026 #include <QInputDialog>
00027 
00028 #include <QMessageBox>
00029 
00030 #include <QListWidget>
00031 #include <QAbstractItemView>
00032 
00033 #include <QSplitter>
00034 #include <QSettings>
00035 #include <QObject>
00036 #include <QMetaObject>
00037 
00038 extern const QString COMPANY;
00039 extern const QString PROGRAM;
00040 
00041 SubjectsForm::SubjectsForm(QWidget* parent): QDialog(parent)
00042 {
00043        setupUi(this);
00044        
00045        currentSubjectTextEdit->setReadOnly(true);
00046        
00047        renameSubjectPushButton->setDefault(true);
00048 
00049        subjectsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00050 
00051        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00052        connect(addSubjectPushButton, SIGNAL(clicked()), this, SLOT(addSubject()));
00053        connect(removeSubjectPushButton, SIGNAL(clicked()), this, SLOT(removeSubject()));
00054        connect(renameSubjectPushButton, SIGNAL(clicked()), this, SLOT(renameSubject()));
00055        connect(sortSubjectsPushButton, SIGNAL(clicked()), this, SLOT(sortSubjects()));
00056        connect(subjectsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(subjectChanged(int)));
00057        connect(activateSubjectPushButton, SIGNAL(clicked()), this, SLOT(activateSubject()));
00058        connect(deactivateSubjectPushButton, SIGNAL(clicked()), this, SLOT(deactivateSubject()));
00059        connect(subjectsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(renameSubject()));
00060 
00061        centerWidgetOnScreen(this);
00062        restoreFETDialogGeometry(this);
00063        //restore splitter state
00064        QSettings settings(COMPANY, PROGRAM);
00065        if(settings.contains(this->metaObject()->className()+QString("/splitter-state")))
00066               splitter->restoreState(settings.value(this->metaObject()->className()+QString("/splitter-state")).toByteArray());
00067        
00068        subjectsListWidget->clear();
00069        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00070               Subject* sbj=gt.rules.subjectsList[i];
00071               subjectsListWidget->addItem(sbj->name);
00072        }
00073        
00074        if(subjectsListWidget->count()>0)
00075               subjectsListWidget->setCurrentRow(0);
00076 }
00077 
00078 
00079 SubjectsForm::~SubjectsForm()
00080 {
00081        saveFETDialogGeometry(this);
00082        //save splitter state
00083        QSettings settings(COMPANY, PROGRAM);
00084        settings.setValue(this->metaObject()->className()+QString("/splitter-state"), splitter->saveState());
00085 }
00086 
00087 void SubjectsForm::addSubject()
00088 {
00089        bool ok = FALSE;
00090        Subject* sbj=new Subject();
00091        sbj->name = QInputDialog::getText( this, tr("Add subject"), tr("Please enter subject's name") ,
00092         QLineEdit::Normal, QString(), &ok );
00093 
00094        if ( ok && !((sbj->name).isEmpty()) ){
00095               // user entered something and pressed OK
00096               if(!gt.rules.addSubject(sbj)){
00097                      QMessageBox::information( this, tr("Subject insertion dialog"),
00098                             tr("Could not insert item. Must be a duplicate"));
00099                      delete sbj;
00100               }
00101               else{
00102                      subjectsListWidget->addItem(sbj->name);
00103                      subjectsListWidget->setCurrentRow(subjectsListWidget->count()-1);
00104               }
00105        }
00106        else{
00107               if(ok){ //the user entered nothing
00108                      QMessageBox::information(this, tr("FET information"), tr("Incorrect name"));
00109               }
00110               delete sbj;// user entered nothing or pressed Cancel
00111        }
00112 }
00113 
00114 void SubjectsForm::removeSubject()
00115 {
00116        int i=subjectsListWidget->currentRow();
00117        if(subjectsListWidget->currentRow()<0){
00118               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00119               return;
00120        }
00121 
00122        QString text=subjectsListWidget->currentItem()->text();
00123        int subject_ID=gt.rules.searchSubject(text);
00124        if(subject_ID<0){
00125               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00126               return;
00127        }
00128 
00129        if(QMessageBox::warning( this, tr("FET"),
00130               tr("Are you sure you want to delete this subject and all related activities and constraints?"),
00131               tr("Yes"), tr("No"), 0, 0, 1 ) == 1)
00132               return;
00133 
00134        int tmp=gt.rules.removeSubject(text);
00135        if(tmp){
00136               subjectsListWidget->setCurrentRow(-1);
00137               QListWidgetItem* item;
00138               item=subjectsListWidget->takeItem(i);
00139               delete item;
00140 
00141               if(i>=subjectsListWidget->count())
00142                      i=subjectsListWidget->count()-1;
00143               if(i>=0)
00144                      subjectsListWidget->setCurrentRow(i);
00145               else
00146                      currentSubjectTextEdit->setPlainText(QString(""));
00147        }
00148 }
00149 
00150 void SubjectsForm::renameSubject()
00151 {
00152        int i=subjectsListWidget->currentRow();
00153        if(subjectsListWidget->currentRow()<0){
00154               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00155               return;
00156        }
00157        
00158        QString initialSubjectName=subjectsListWidget->currentItem()->text();
00159 
00160        int subject_ID=gt.rules.searchSubject(initialSubjectName);
00161        if(subject_ID<0){
00162               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00163               return;
00164        }
00165 
00166        bool ok = FALSE;
00167        QString finalSubjectName;
00168        finalSubjectName = QInputDialog::getText( this, tr("Modify subject"), tr("Please enter new subject's name") ,
00169         QLineEdit::Normal, initialSubjectName, &ok);
00170 
00171        if ( ok && !(finalSubjectName.isEmpty()) ){
00172               // user entered something and pressed OK
00173               if(gt.rules.searchSubject(finalSubjectName)>=0){
00174                      QMessageBox::information( this, tr("Subject insertion dialog"),
00175                             tr("Could not modify item. New name must be a duplicate"));
00176               }
00177               else{
00178                      gt.rules.modifySubject(initialSubjectName, finalSubjectName);
00179                      subjectsListWidget->item(i)->setText(finalSubjectName);
00180                      subjectChanged(subjectsListWidget->currentRow());
00181               }
00182        }
00183 }
00184 
00185 void SubjectsForm::sortSubjects()
00186 {
00187        gt.rules.sortSubjectsAlphabetically();
00188        
00189        subjectsListWidget->clear();
00190        for(int i=0; i<gt.rules.subjectsList.size(); i++){
00191               Subject* sbj=gt.rules.subjectsList[i];
00192               subjectsListWidget->addItem(sbj->name);
00193        }
00194        
00195        if(subjectsListWidget->count()>0)
00196               subjectsListWidget->setCurrentRow(0);
00197 }
00198 
00199 void SubjectsForm::subjectChanged(int index)
00200 {
00201        if(index<0){
00202               currentSubjectTextEdit->setPlainText(QString(""));
00203               return;
00204        }
00205        
00206        Subject* sb=gt.rules.subjectsList.at(index);
00207        assert(sb);
00208        QString s=sb->getDetailedDescriptionWithConstraints(gt.rules);
00209        currentSubjectTextEdit->setPlainText(s);
00210 }
00211 
00212 void SubjectsForm::activateSubject()
00213 {
00214        if(subjectsListWidget->currentRow()<0){
00215               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00216               return;
00217        }
00218        
00219        QString subjectName=subjectsListWidget->currentItem()->text();
00220        
00221        int count=gt.rules.activateSubject(subjectName);
00222        QMessageBox::information(this, tr("FET information"), tr("Activated a number of %1 activities").arg(count));
00223 }
00224 
00225 void SubjectsForm::deactivateSubject()
00226 {
00227        if(subjectsListWidget->currentRow()<0){
00228               QMessageBox::information(this, tr("FET information"), tr("Invalid selected subject"));
00229               return;
00230        }
00231        
00232        QString subjectName=subjectsListWidget->currentItem()->text();
00233        
00234        int count=gt.rules.deactivateSubject(subjectName);
00235        QMessageBox::information(this, tr("FET information"), tr("De-activated a number of %1 activities").arg(count));
00236 }