QtiPlot 0.9.8.2
Spectrogram.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     File                 : Spectrogram.h
00003     Project              : QtiPlot
00004 --------------------------------------------------------------------
00005     Copyright            : (C) 2006 by Ion Vasilief
00006     Email (use @ for *)  : ion_vasilief*yahoo.fr
00007     Description          : QtiPlot's Spectrogram Class
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  *  This program is distributed in the hope that it will be useful,        *
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00020  *  GNU General Public License for more details.                           *
00021  *                                                                         *
00022  *   You should have received a copy of the GNU General Public License     *
00023  *   along with this program; if not, write to the Free Software           *
00024  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
00025  *   Boston, MA  02110-1301  USA                                           *
00026  *                                                                         *
00027  ***************************************************************************/
00028 
00029 #ifndef SPECTROGRAM_H
00030 #define SPECTROGRAM_H
00031 
00032 #include <QApplication>
00033 #include <Matrix.h>
00034 #include <muParserScript.h>
00035 
00036 #include <qwt_raster_data.h>
00037 #include <qwt_plot.h>
00038 #include <qwt_plot_spectrogram.h>
00039 #include <qwt_color_map.h>
00040 #include <qwt_plot_marker.h>
00041 
00042 class MatrixData;
00043 class Graph;
00044 class PlotMarker;
00045 
00046 class Spectrogram: public QwtPlotSpectrogram
00047 {
00048 public:
00049     Spectrogram(Graph *graph, Matrix *m);
00050 
00051     enum ColorMapPolicy{GrayScale, Default, Custom};
00052 
00053     Spectrogram* copy(Graph *g);
00054     Graph * graph(){return d_graph;};
00055 
00056     Matrix * matrix(){return d_matrix;};
00057     bool setMatrix(Matrix *m, bool useFormula = false);
00058 
00059     int levels(){return (int)contourLevels().size();};
00060     void setLevelsNumber(int levels);
00061     void setContourLevels (const QwtValueList & levels);
00062 
00063     bool hasColorScale();
00064     int colorScaleAxis(){return color_axis;};
00065     void setColorScaleAxis(int axis){color_axis = axis;};
00066     void showColorScale(int axis, bool on = true);
00067 
00068     int colorBarWidth();
00069     void setColorBarWidth(int width);
00070 
00071     void setGrayScale();
00072     void setDefaultColorMap();
00073 
00074     QwtLinearColorMap colorMap(){return color_map;};
00075     QwtLinearColorMap *colorMapPointer(){return &color_map;};
00076     void setCustomColorMap(const QwtLinearColorMap& map);
00077 
00079     QString saveToString();
00080 
00081     ColorMapPolicy colorMapPolicy(){return color_map_policy;};
00082 
00083     virtual QwtDoubleRect boundingRect() const;
00084 
00085     bool hasLabels(){return d_show_labels;};
00086     QList <PlotMarker *> labelsList(){return d_labels_list;};
00087 
00088     void showContourLineLabels(bool show = true);
00089 
00090     QFont labelsFont(){return d_labels_font;};
00091     void setLabelsFont(const QFont& font);
00092 
00093     QColor labelsColor(){return d_labels_color;};
00094     void setLabelsColor(const QColor& c);
00095 
00096     bool labelsWhiteOut(){return d_white_out_labels;};
00097     void setLabelsWhiteOut(bool whiteOut);
00098 
00099     double labelsXOffset(){return d_labels_x_offset;};
00100     double labelsYOffset(){return d_labels_y_offset;};
00101     void setLabelsOffset(double x, double y);
00102     void setLabelOffset(int index, double x, double y);
00103 
00104     double labelsRotation(){return d_labels_angle;};
00105     void setLabelsRotation(double angle);
00106 
00107     bool selectedLabels(const QPoint& pos);
00108     void selectLabel(bool on);
00109     bool hasSelectedLabels();
00110     void moveLabel(const QPoint& pos);
00111     void clearLabels();
00112 
00113     virtual void setVisible(bool on);
00114     virtual QPen contourPen (double level) const;
00115     void setColorMapPen(bool on = true);
00116     bool useColorMapPen(){return d_color_map_pen;};
00117 
00118     QList<QPen> contourPenList(){return d_pen_list;};
00119     void setContourPenList(QList<QPen> lst);
00120 
00121     void setContourLinePen(int index, const QPen &pen);
00122 
00123     bool useMatrixFormula(){return d_use_matrix_formula;};
00124     bool setUseMatrixFormula(bool on = true);
00125 
00126     void updateData();
00127     QwtDoubleInterval range();
00128     void setRange(double vmin, double vmax);
00129 
00130 protected:
00131     virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00132     void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00133     void createLabels();
00135     Graph *d_graph;
00137     Matrix *d_matrix;
00138 
00140     int color_axis;
00141 
00143     ColorMapPolicy color_map_policy;
00144 
00145     QwtLinearColorMap color_map;
00147     bool d_show_labels;
00149     QColor d_labels_color;
00151     QFont d_labels_font;
00153     bool d_white_out_labels;
00154     double d_labels_angle;
00155     double d_labels_x_offset, d_labels_y_offset;
00156 
00158     QList <PlotMarker *> d_labels_list;
00160     PlotMarker *d_selected_label;
00162     double d_click_pos_x, d_click_pos_y;
00163 
00165     bool d_use_matrix_formula;
00166 
00168     bool d_color_map_pen;
00169 
00170     QList<QPen> d_pen_list;
00171 
00172     bool d_impose_range;
00173     double d_min_value, d_max_value;
00174 };
00175 
00176 class MatrixData: public QwtRasterData
00177 {
00178 public:
00179     MatrixData(Matrix *m, bool useMatrixFormula = false):
00180         QwtRasterData(m->boundingRect()),
00181         d_matrix(m)
00182     {
00183         n_rows = d_matrix->numRows();
00184         n_cols = d_matrix->numCols();
00185 
00186         x_start = d_matrix->xStart();
00187         dx = d_matrix->dx();
00188         y_start = d_matrix->yStart();
00189         dy = d_matrix->dy();
00190 
00191         d_mup = NULL;
00192         if (useMatrixFormula && d_matrix->canCalculate()){
00193             d_mup = new muParserScript(d_matrix->scriptingEnv(), d_matrix->formula(),
00194                     d_matrix, QString("<%1>").arg(d_matrix->objectName()));
00195 
00196             d_x = d_mup->defineVariable("x");
00197             d_y = d_mup->defineVariable("y");
00198             d_ri = d_mup->defineVariable("i");
00199             d_rr = d_mup->defineVariable("row");
00200             d_cj = d_mup->defineVariable("j");
00201             d_cc = d_mup->defineVariable("col");
00202 
00203             if (!d_mup->compile()){
00204                 delete d_mup;
00205                 d_mup = NULL;
00206             }
00207 
00208             if (d_mup){//calculate z range
00209                 *d_ri = 1.0;
00210                 *d_rr = 1.0;
00211                 *d_y = y_start;
00212                 *d_cj = 1.0;
00213                 *d_cc = 1.0;
00214                 *d_x = x_start;
00215 
00216                 if (d_mup->codeLines() == 1)
00217                     min_z = d_mup->evalSingleLine();
00218                 else
00219                     min_z = d_mup->eval().toDouble();
00220 
00221                 max_z = min_z;
00222 
00223                 if (d_mup->codeLines() == 1){
00224                     for(int row = 0; row < n_rows; row++){
00225                         double r = row + 1.0;
00226                         *d_ri = r; *d_rr = r;
00227                         *d_y = y_start + row*dy;
00228                         for(int col = 0; col < n_cols; col++){
00229                             double c = col + 1.0;
00230                             *d_cj = c; *d_cc = c;
00231                             *d_x = x_start + col*dx;
00232                             double aux = d_mup->evalSingleLine();
00233                             if (aux <= min_z)
00234                                 min_z = aux;
00235                             if (aux >= max_z)
00236                                 max_z = aux;
00237                         }
00238                     }
00239                 } else {
00240                     for(int row = 0; row < n_rows; row++){
00241                         double r = row + 1.0;
00242                         *d_ri = r; *d_rr = r;
00243                         *d_y = y_start + row*dy;
00244                         for(int col = 0; col < n_cols; col++){
00245                             double c = col + 1.0;
00246                             *d_cj = c; *d_cc = c;
00247                             *d_x = x_start + col*dx;
00248                             double aux = d_mup->eval().toDouble();
00249                             if (aux <= min_z)
00250                                 min_z = aux;
00251                             if (aux >= max_z)
00252                                 max_z = aux;
00253                         }
00254                         qApp->processEvents();
00255                     }
00256                 }
00257             }
00258         } else
00259             m->range(&min_z, &max_z);
00260     }
00261 
00262     ~MatrixData()
00263     {
00264         if (d_mup)
00265             delete d_mup;
00266     }
00267 
00268     virtual QwtRasterData *copy() const
00269     {
00270         if (d_mup)
00271             return new MatrixData(d_matrix, true);
00272 
00273         return new MatrixData(d_matrix);
00274     }
00275 
00276     virtual QwtDoubleInterval range() const
00277     {
00278         return QwtDoubleInterval(min_z, max_z);
00279     }
00280 
00281     virtual QSize rasterHint (const QwtDoubleRect &) const
00282     {
00283         return QSize(n_cols, n_rows);
00284     }
00285 
00286     virtual double value(double x, double y) const;
00287 
00288 private:
00290     Matrix *d_matrix;
00291 
00293     int n_rows, n_cols;
00294 
00296     double min_z, max_z;
00297 
00299     double dx, dy;
00300 
00302     double x_start;
00303 
00305     double y_start;
00306 
00308     muParserScript *d_mup;
00310     double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
00311 };
00312 
00313 #endif