ThreadManager.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006-2008 by Antonello Lobianco                         *
00003  *   http://regmas.org                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 3 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef THREAD_H
00021 #define THREAD_H
00022 
00023 #include <iostream>
00024 #include <string>
00025 #include <sstream>
00026 
00027 #include <QtCore>
00028 #include <QThread>
00029 #include <QString>
00030 #include <QColor>
00031 #include <QImage>
00032 #include <QMutex>
00033 
00034 
00035 // regmas includes..
00036 #include "BaseRegmas.h"
00037 
00038 class MainWindow;
00039 class RegData;
00040 class Gis;
00041 class SuperAgentManager;
00042 class Init;
00043 class Scheduler;
00044 class Output;
00045 
00046 using namespace std;
00047 
00049 
00054 class ThreadManager : public QThread, public BaseRegmas
00055 {
00056     Q_OBJECT
00057 
00058 public:
00059                         ThreadManager();
00060     // pointers..
00061     RegData*            RD;   
00062     Gis*                GIS;  
00063     SuperAgentManager*  SAM;  
00064     Init*               INIT; 
00065     Scheduler*          SCD;  
00066     Output*             DO;   
00067 
00068     void                setMessage(const QString &message);
00069     void                stop();
00070     void                deleteDeadOldPointers(); 
00071     void                pauseOrResume();
00072     void                pause();
00073     void                resume();
00074     void                refreshGUI();
00075     void                msgOut(const int msgCode_h, const string message_h);
00076     void                addLayer(string layerName_h, string layerLabel_h);
00077     void                updatePixel(string layerName_h, int x_h, int y_h, QColor color);
00078     void                updateImage(string layerName_h, const QImage &image_h);
00079     void                upgradeMainSBLabel(const string message_h);
00080     void                upgradeYearSBLabel(int year);
00081     string              getBaseDirectory(){return baseDirectory.toStdString();};
00082     string              getInputFileName(){return inputFileName.toStdString();};
00083     string              getScenarioName() {return scenarioName.toStdString();};
00084     void                setOutputDirName(string outputDirname_h);
00085 
00087     void                setRDPointer(RegData *RD_h){RD=RD_h;};
00089     void                setGISPointer(Gis *GIS_h){GIS=GIS_h;};
00091     void                setSAMPointer(SuperAgentManager *SAM_h){SAM=SAM_h;};
00093     void                setINITPointer(Init *INIT_h){INIT=INIT_h;};
00095     void                setSCDPointer(Scheduler *SCD_h){SCD=SCD_h;};
00097     void                setDOPointer(Output *DO_h){DO=DO_h;};
00098 
00099     //public slots:
00100     void                setInputFileName(QString inputFileName_h);
00101     //void                setBaseDirectory(QString baseDirectory_h){baseDirectory =  baseDirectory_h;};
00102 
00103     // tree viewer operations...
00104     void               treeViewerAddManager(string name){emit treeViewerAddItemToGui("Manager "+name, "manager_"+name, "managers");};
00105     void               treeViewerAddAgent(int uniqueID){emit treeViewerAddItemToGui(i2s(uniqueID), "agent_"+i2s(uniqueID), "agents"); };
00106     void               treeViewerAddManagerProperty(string managerName, string propertyName){
00107                             emit treeViewerAddItemToGui(propertyName, "manager_"+managerName+"_"+propertyName, "manager_"+managerName);};
00108     void               treeViewerAddAgentProperty(int uniqueID, string propertyName){
00109                             emit treeViewerAddItemToGui(propertyName, "agent_"+i2s(uniqueID)+"_"+propertyName, "agent_"+i2s(uniqueID));};
00110     void               treeViewerManagerPropertyChangeValue(string managerName, string propertyName, string newValue){
00111                             emit treeViewerItemChangeValueToGui("manager_"+managerName+"_"+propertyName, newValue);};
00112     void               treeViewerAgentPropertyChangeValue(int uniqueID, string propertyName, string newValue){
00113                             emit treeViewerItemChangeValueToGui("agent_"+i2s(uniqueID)+"_"+propertyName, newValue);};
00114     void               treeViewerRemoveManager(string name){emit treeViewerItemRemoveToGui("manager_"+name);};
00115     void               treeViewerRemoveAgent(int uniqueID){emit treeViewerItemRemoveToGui("agent_"+i2s(uniqueID));};
00116     void               treeViewerChangeGeneralPropertyValue(string propertyName, string newValue){
00117                             emit treeViewerItemChangeValueToGui("general_"+propertyName, newValue);};
00118 
00119     void               fitInWindow() {emit fitInWindowToGui();}; 
00120     void                runFromConsole(QString inputFileName_h, QString scenarioName_h);
00121     bool                usingGUI(){return GUI;};
00122 
00123 signals:
00124     void                upgradeLogArea(const QString &logMessage);
00125     void                upgradeMainSBLabelToGui(const QString &logMessage);
00126     void                upgradeYearSBLabelToGui(const QString &logMessage);
00127     void                addLayerToGui(QString layerName, QString layerLabel);
00128     void                updatePixelToGui(QString layerName_h, int x_h, int y_h, QColor color);
00129     void                updateImageToGui(QString layerName_h, QImage image_h);
00130     void                setOutputDirNameToGui(string outputDirname_h);
00131     void                setGUIUnsavedStatus(bool status_h);
00132     void                setGUIMapDimension(int x_h, int y_h);
00133     void                treeViewerItemChangeValueToGui(string itemID, string newValue);
00134     void                treeViewerItemRemoveToGui(string itemID);
00135     void                treeViewerAddItemToGui(string text, string itemID, string parentID);
00136     void                fitInWindowToGui();
00137     void                queryRequestOnPx(int px_ID, int currentLayerIndex);
00138     void                publishQueryResults(const QString &results);
00139     void                activateTab(int pos_h);
00140     void                resetGUIForNewSimulation();
00141     void                sendScenarioOptionsToGUI(const QVector<QString> &scenarios_h);
00142 
00143 
00144 public slots:
00146     void                checkQuery(int px_ID, int currentLayerIndex, bool newRequest=true);
00148     void                computeQuery(int px_ID, int currentLayerIndex);
00149     void                retrieveScenarioNameFromGUI(const QString &scenarioName_h);
00150 
00151 protected:
00152     void                run();
00153 
00154 private:
00155     QString                           messageStr;
00156     volatile bool                        stopped;
00157     volatile bool                        running;
00158     QString                        inputFileName;
00159     QString                        baseDirectory;
00160     QString                         scenarioName;
00161     volatile int                       pxQueryID;
00162     volatile int                   layerQueryPos;
00163     QMutex                                 mutex;
00164     bool                                     GUI;
00165 
00166 };
00167 
00168 #endif
00169