Back to index

fet  5.18.0
buildingsform.cpp
Go to the documentation of this file.
00001 //
00002 //
00003 // Description: This file is part of FET
00004 //
00005 //
00006 // Author: Liviu Lalescu <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  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "timetable_defs.h"
00020 #include "fet.h"
00021 #include "buildingsform.h"
00022 #include "addbuildingform.h"
00023 #include "modifybuildingform.h"
00024 
00025 #include <QMessageBox>
00026 
00027 #include <QListWidget>
00028 #include <QScrollBar>
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 BuildingsForm::BuildingsForm(QWidget* parent): QDialog(parent)
00040 {
00041        setupUi(this);
00042        
00043        currentBuildingTextEdit->setReadOnly(true);
00044 
00045        modifyBuildingPushButton->setDefault(true);
00046 
00047        buildingsListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
00048 
00049        connect(addBuildingPushButton, SIGNAL(clicked()), this, SLOT(addBuilding()));
00050        connect(removeBuildingPushButton, SIGNAL(clicked()), this, SLOT(removeBuilding()));
00051        connect(buildingsListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(buildingChanged(int)));
00052        connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
00053        connect(modifyBuildingPushButton, SIGNAL(clicked()), this, SLOT(modifyBuilding()));
00054        connect(sortBuildingsPushButton, SIGNAL(clicked()), this, SLOT(sortBuildings()));
00055        connect(buildingsListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(modifyBuilding()));
00056 
00057        centerWidgetOnScreen(this);
00058        restoreFETDialogGeometry(this);
00059        //restore splitter state
00060        QSettings settings(COMPANY, PROGRAM);
00061        if(settings.contains(this->metaObject()->className()+QString("/splitter-state")))
00062               splitter->restoreState(settings.value(this->metaObject()->className()+QString("/splitter-state")).toByteArray());
00063        
00064        this->filterChanged();
00065 }
00066 
00067 BuildingsForm::~BuildingsForm()
00068 {
00069        saveFETDialogGeometry(this);
00070        //save splitter state
00071        QSettings settings(COMPANY, PROGRAM);
00072        settings.setValue(this->metaObject()->className()+QString("/splitter-state"), splitter->saveState());
00073 }
00074 
00075 bool BuildingsForm::filterOk(Building* bu)
00076 {
00077        Q_UNUSED(bu);
00078 
00079        bool ok=true;
00080 
00081        return ok;
00082 }
00083 
00084 void BuildingsForm::filterChanged()
00085 {
00086        QString s;
00087        buildingsListWidget->clear();
00088        visibleBuildingsList.clear();
00089        for(int i=0; i<gt.rules.buildingsList.size(); i++){
00090               Building* bu=gt.rules.buildingsList[i];
00091               if(this->filterOk(bu)){
00092                      s=bu->name;
00093                      visibleBuildingsList.append(bu);
00094                      buildingsListWidget->addItem(s);
00095               }
00096        }
00097        
00098        if(buildingsListWidget->count()>0)
00099               buildingsListWidget->setCurrentRow(0);
00100        else
00101               buildingChanged(-1);
00102 }
00103 
00104 void BuildingsForm::addBuilding()
00105 {
00106        AddBuildingForm addBuildingForm(this);
00107        setParentAndOtherThings(&addBuildingForm, this);
00108        addBuildingForm.exec();
00109        
00110        filterChanged();
00111        
00112        buildingsListWidget->setCurrentRow(buildingsListWidget->count()-1);
00113 }
00114 
00115 void BuildingsForm::removeBuilding()
00116 {
00117        int ind=buildingsListWidget->currentRow();
00118        if(ind<0){
00119               QMessageBox::information(this, tr("FET information"), tr("Invalid selected building"));
00120               return;
00121        }
00122        
00123        Building* bu=visibleBuildingsList.at(ind);
00124        assert(bu!=NULL);
00125 
00126        if(QMessageBox::warning( this, tr("FET"),
00127               tr("Are you sure you want to delete this building?"),
00128               tr("Yes"), tr("No"), 0, 0, 1 ) == 1)
00129               return;
00130               
00131        bool tmp=gt.rules.removeBuilding(bu->name);
00132        assert(tmp);
00133        
00134        visibleBuildingsList.removeAt(ind);
00135        buildingsListWidget->setCurrentRow(-1);
00136        QListWidgetItem* item=buildingsListWidget->takeItem(ind);
00137        delete item;
00138        
00139        if(ind>=buildingsListWidget->count())
00140               ind=buildingsListWidget->count()-1;
00141        if(ind>=0)
00142               buildingsListWidget->setCurrentRow(ind);
00143        else
00144               currentBuildingTextEdit->setPlainText(QString(""));
00145 }
00146 
00147 void BuildingsForm::buildingChanged(int index)
00148 {
00149        if(index<0){
00150               currentBuildingTextEdit->setPlainText("");
00151               return;
00152        }
00153 
00154        QString s;
00155        Building* building=visibleBuildingsList.at(index);
00156 
00157        assert(building!=NULL);
00158        s=building->getDetailedDescriptionWithConstraints(gt.rules);
00159        currentBuildingTextEdit->setPlainText(s);
00160 }
00161 
00162 void BuildingsForm::sortBuildings()
00163 {
00164        gt.rules.sortBuildingsAlphabetically();
00165 
00166        filterChanged();
00167 }
00168 
00169 void BuildingsForm::modifyBuilding()
00170 {
00171        int valv=buildingsListWidget->verticalScrollBar()->value();
00172        int valh=buildingsListWidget->horizontalScrollBar()->value();
00173 
00174        int ci=buildingsListWidget->currentRow();
00175        if(ci<0){
00176               QMessageBox::information(this, tr("FET information"), tr("Invalid selected building"));
00177               return;
00178        }
00179        
00180        Building* bu=visibleBuildingsList.at(ci);
00181        ModifyBuildingForm form(this, bu->name);
00182        setParentAndOtherThings(&form, this);
00183        form.exec();
00184 
00185        filterChanged();
00186        
00187        buildingsListWidget->verticalScrollBar()->setValue(valv);
00188        buildingsListWidget->horizontalScrollBar()->setValue(valh);
00189 
00190        if(ci>=buildingsListWidget->count())
00191               ci=buildingsListWidget->count()-1;
00192 
00193        if(ci>=0)
00194               buildingsListWidget->setCurrentRow(ci);
00195 }