experiments/evorobot/include/evodataviewer.h

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00005  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00006  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00007  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #ifndef EVODATAVIEWER_H
00025 #define EVODATAVIEWER_H
00026 
00027 #include "experimentsconfig.h"
00028 #include <QWidget>
00029 #include <QPixmap>
00030 #include <QTimer>
00031 #include <QListWidget>
00032 #include <QGridLayout>
00033 #include <QPushButton>
00034 #include <QFile>
00035 #include <QTextStream>
00036 
00037 namespace farsa {
00038 
00039 
00040 //DataChunk class
00041 class FARSA_EXPERIMENTS_API DataChunk
00042 {
00043 
00044 
00045 
00046 
00047 private:
00048     QColor  color;
00049     QString label;
00050     int size; //actual size of the array
00051     double dpratio;// dat pixel ratio data size/pixels
00052     bool visible;
00053     int index;
00054     double min, max;//range in case we need a normalization
00055     double maxValue;
00056     
00057     
00058     int style; //different visualization style 0: full line, 1: edge
00059     double* data; //to be sized on size value;
00060     double zeroValue;
00061     void checkMaxValue(double val);
00062     
00063 
00064 public:
00065     DataChunk(const QString& label, const QColor& color, int size, bool visible);
00066     ~DataChunk();
00067     void setColor(QColor color);
00068     void setData(double value);
00069     void setDataRaw(double value); //not ranged
00070     void setDataRaw(int ind, double value); //not ranged
00071     
00072 
00073     void setRange(double min, double max);
00074     void setLabel(const QString& label);
00075     void setStyle(int style);
00076     void setVisible(bool vis);
00077     bool isVisible();
00078     const QString& getLabel();
00079     QColor& getColor();
00080     
00081     double getValueToDraw();
00082     double getValue(int ind);
00083     int getIndex();
00084     double linearMap( double x, double rmin, double rmax,
00085                 double outMin = 0, double outMax = 1 );
00086     
00087 
00088     double getRangeMin();
00089     double getRangeMax();
00090     double getMaxValue();
00091     double getZeroValue();
00092     void setDPRatio(double val);
00093     double getDPRatio();
00094     //loads space separated columns from a txt file
00095     bool loadRawData(const QString& filename, int column);
00096     
00097 
00098 
00099 
00100 };
00101 //listwindow triggered by right mouse click ---------------------------------------------------------------------------------------
00102 class FARSA_EXPERIMENTS_API EvoListViewer : public QWidget
00103 {
00104     Q_OBJECT
00105 public:
00106     EvoListViewer(DataChunk** dataChunks, int n, bool *nviewChange , QWidget *parent = 0);
00107     ~EvoListViewer();
00108     private slots:
00109         void okSelected();
00110     public slots:
00111         void restoreSelected();
00112         void allOnOrAllOff();
00113     
00114 
00115 private:
00116     int nchunks;
00117     DataChunk** dataChunks;
00118     QListWidget *listwidget;
00119     QGridLayout *layout;
00120     QPushButton* deselectAll;
00121     bool *nvchange;
00122     //EvoDataViewr *edv;
00123     //void resizeEvent(QResizeEvent* evt);
00124 
00125 
00126 
00127 };
00128 
00129 
00130 // EvodataViewer ----------------------------------------------------------------------------------------------------------------
00131 
00132 class FARSA_EXPERIMENTS_API EvoDataViewer : public QWidget
00133 {
00134     Q_OBJECT
00135 public:
00136     //EvoDataViewer();
00137     EvoDataViewer(int nchunks, int chunksize, int style=0, QWidget* parent=0, Qt::WindowFlags flags=0); 
00138     ~EvoDataViewer();
00139     
00140     void setChunkLabel(int ch, const QString& label);
00141     void setChunkValue(int ch, double value);              //setting value by id
00142     bool setChunkValue(const QString& name, double value); //setting value by chunk name
00143     void setChunkColor(int ch, QColor color);
00144     void setCurrentStep(int step);
00145     void setStyle(int style);
00146     void setChunkRange(int ch, double mn, double mx);
00147     void setChunkProperties(int ch, double rangeMin, double rangeMax, const QString& label, QColor color, bool visible);
00148     int visibleChunks();
00149     void listVisibleChunks();
00150     void reset();
00151 private slots:
00152     void pickUnvisible();
00153 protected:
00154     void paintEvent(QPaintEvent* evt);
00155     void resizeEvent(QResizeEvent* evt);
00156     void mousePressEvent(QMouseEvent* evt);
00157 private:
00158     void updateGraphic(int ch);//draws only latest data on the pixmap. ch is the chunk to update
00159     QPixmap* qpixmap;
00160     int dheight;
00161     int dwidth;
00162     int nchunks;
00163     int nvchunks; //# of visible chunks
00164     int chunksize;
00165     DataChunk** dataChunks;
00166     DataChunk*  stepChunk; //here we set the current step/cycle
00167     int vertical_step;
00168     int label_width;    //no pixels of the label column
00169     int panning_width;  //width between label column and data column in which we draw 3 pixels large line
00170     int style;
00171     double pickValue;
00172     int    pickStep;
00173     bool   pickValueVisible;
00174     int pickX, pickY;
00175     QTimer *qtimer;
00176     EvoListViewer *elw;
00177     
00178     int *vchunks; //visible chunks
00179 public:
00180     bool nviewChange;
00181 private:
00182     void evoDataPaint();
00183 
00184 
00185 };
00186 
00187 // Fitviewer
00188 class FARSA_EXPERIMENTS_API FitViewer : public QWidget
00189 {
00190 
00191 private:
00192     DataChunk** dataChunks;
00193     int nchunks;
00194     int chunksize;
00195 
00196     double fitVal[4000][3];
00197     int currentGen;
00198     int padding;
00199     double zerox, zeroy;
00200     double vmin;
00201     double vmax;
00202     double wyaxes;
00203     double xstep;
00204     int*   sortedIndex;
00205     QString gtitle;
00206     QString xlabel;
00207     QString ylabel;
00208 
00209 public:
00210     void setValues(int gen, double min, double average, double max);
00211     FitViewer( QWidget* parent=0, Qt::WindowFlags flags=0 );
00212     FitViewer(int nchunks, int chunkSize, QWidget* parent=0, Qt::WindowFlags flags=0);
00213     ~FitViewer();
00214 private:
00215     double getYnormValue(double val);
00216     int checkGraphRange(double val);
00217 
00218 protected:
00219 void paintEvent(QPaintEvent* evt);
00220 public:
00221     void setChunkLabel(int ch, const QString &label);
00222     void setChunkProperties(int ch, const QString &label, QColor color, bool visible);
00223     void setChunkValue(int ch,int ind, double value);              //setting value by id
00224     bool setChunkValue(const QString& name,int ind, double value); //setting value by chunk name
00225     void diplayUntilStep(int st);
00226     void setLabels(const QString &title, const QString &xlabel, const QString &ylabel);
00227     bool loadRawData(int nchunk, const QString &filename, int column);
00228     void checkChunkRange(int chunk);
00229     void reset();
00230     int  getCurrentGeneration();
00231     void sortchunks();
00232 
00233 
00234 };
00235 
00236 } //end namespace farsa
00237 
00238 #endif