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     Matrix * matrix(){return d_matrix;};
00055     bool setMatrix(Matrix *m, bool useFormula = false);
00056 
00057     int levels(){return (int)contourLevels().size();};
00058     void setLevelsNumber(int levels);
00059     void setContourLevels (const QwtValueList & levels);
00060 
00061     bool hasColorScale();
00062     int colorScaleAxis(){return color_axis;};
00063     void setColorScaleAxis(int axis){color_axis = axis;};
00064     void showColorScale(int axis, bool on = true);
00065 
00066     int colorBarWidth();
00067     void setColorBarWidth(int width);
00068 
00069     void setGrayScale();
00070     void setDefaultColorMap();
00071 
00072     QwtLinearColorMap colorMap(){return color_map;};
00073     QwtLinearColorMap *colorMapPointer(){return &color_map;};
00074     void setCustomColorMap(const QwtLinearColorMap& map);
00075 
00077     QString saveToString();
00078 
00079     ColorMapPolicy colorMapPolicy(){return color_map_policy;};
00080 
00081     virtual QwtDoubleRect boundingRect() const;
00082 
00083     bool hasLabels(){return d_show_labels;};
00084     QList <PlotMarker *> labelsList(){return d_labels_list;};
00085 
00086     void showContourLineLabels(bool show = true);
00087 
00088     QFont labelsFont(){return d_labels_font;};
00089     void setLabelsFont(const QFont& font);
00090 
00091     QColor labelsColor(){return d_labels_color;};
00092     void setLabelsColor(const QColor& c);
00093 
00094     bool labelsWhiteOut(){return d_white_out_labels;};
00095     void setLabelsWhiteOut(bool whiteOut);
00096 
00097     double labelsXOffset(){return d_labels_x_offset;};
00098     double labelsYOffset(){return d_labels_y_offset;};
00099     void setLabelsOffset(double x, double y);
00100     void setLabelOffset(int index, double x, double y);
00101 
00102     double labelsRotation(){return d_labels_angle;};
00103     void setLabelsRotation(double angle);
00104 
00105     bool selectedLabels(const QPoint& pos);
00106     void selectLabel(bool on);
00107     bool hasSelectedLabels();
00108     void moveLabel(const QPoint& pos);
00109     void clearLabels();
00110 
00111     virtual void setVisible(bool on);
00112     virtual QPen contourPen (double level) const;
00113     void setColorMapPen(bool on = true);
00114     bool useColorMapPen(){return d_color_map_pen;};
00115 
00116     QList<QPen> contourPenList(){return d_pen_list;};
00117     void setContourPenList(QList<QPen> lst);
00118 
00119     void setContourLinePen(int index, const QPen &pen);
00120 
00121     bool useMatrixFormula(){return d_use_matrix_formula;};
00122     bool setUseMatrixFormula(bool on = true);
00123 
00124     void updateData();
00125     QwtDoubleInterval range();
00126     void setRange(double vmin, double vmax);
00127 
00128 protected:
00129     virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00130     void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00131     void createLabels();
00133     Graph *d_graph;
00135     Matrix *d_matrix;
00136 
00138     int color_axis;
00139 
00141     ColorMapPolicy color_map_policy;
00142 
00143     QwtLinearColorMap color_map;
00145     bool d_show_labels;
00147     QColor d_labels_color;
00149     QFont d_labels_font;
00151     bool d_white_out_labels;
00152     double d_labels_angle;
00153     double d_labels_x_offset, d_labels_y_offset;
00154 
00156     QList <PlotMarker *> d_labels_list;
00158     PlotMarker *d_selected_label;
00160     double d_click_pos_x, d_click_pos_y;
00161 
00163     bool d_use_matrix_formula;
00164 
00166     bool d_color_map_pen;
00167 
00168     QList<QPen> d_pen_list;
00169 
00170     bool d_impose_range;
00171     double d_min_value, d_max_value;
00172 };
00173 
00174 class MatrixData: public QwtRasterData
00175 {
00176 public:
00177     MatrixData(Matrix *m, bool useMatrixFormula = false):
00178         QwtRasterData(m->boundingRect()),
00179         d_matrix(m)
00180     {
00181         n_rows = d_matrix->numRows();
00182         n_cols = d_matrix->numCols();
00183 
00184         x_start = d_matrix->xStart();
00185         dx = d_matrix->dx();
00186         y_start = d_matrix->yStart();
00187         dy = d_matrix->dy();
00188 
00189         d_mup = NULL;
00190         if (useMatrixFormula && d_matrix->canCalculate()){
00191             d_mup = new muParserScript(d_matrix->scriptingEnv(), d_matrix->formula(),
00192                     d_matrix, QString("<%1>").arg(d_matrix->objectName()));
00193 
00194             d_x = d_mup->defineVariable("x");
00195             d_y = d_mup->defineVariable("y");
00196             d_ri = d_mup->defineVariable("i");
00197             d_rr = d_mup->defineVariable("row");
00198             d_cj = d_mup->defineVariable("j");
00199             d_cc = d_mup->defineVariable("col");
00200 
00201             if (!d_mup->compile()){
00202                 delete d_mup;
00203                 d_mup = NULL;
00204             }
00205 
00206             if (d_mup){//calculate z range
00207                 *d_ri = 1.0;
00208                 *d_rr = 1.0;
00209                 *d_y = y_start;
00210                 *d_cj = 1.0;
00211                 *d_cc = 1.0;
00212                 *d_x = x_start;
00213 
00214                 if (d_mup->codeLines() == 1)
00215                     min_z = d_mup->evalSingleLine();
00216                 else
00217                     min_z = d_mup->eval().toDouble();
00218 
00219                 max_z = min_z;
00220 
00221                 if (d_mup->codeLines() == 1){
00222                     for(int row = 0; row < n_rows; row++){
00223                         double r = row + 1.0;
00224                         *d_ri = r; *d_rr = r;
00225                         *d_y = y_start + row*dy;
00226                         for(int col = 0; col < n_cols; col++){
00227                             double c = col + 1.0;
00228                             *d_cj = c; *d_cc = c;
00229                             *d_x = x_start + col*dx;
00230                             double aux = d_mup->evalSingleLine();
00231                             if (aux <= min_z)
00232                                 min_z = aux;
00233                             if (aux >= max_z)
00234                                 max_z = aux;
00235                         }
00236                     }
00237                 } else {
00238                     for(int row = 0; row < n_rows; row++){
00239                         double r = row + 1.0;
00240                         *d_ri = r; *d_rr = r;
00241                         *d_y = y_start + row*dy;
00242                         for(int col = 0; col < n_cols; col++){
00243                             double c = col + 1.0;
00244                             *d_cj = c; *d_cc = c;
00245                             *d_x = x_start + col*dx;
00246                             double aux = d_mup->eval().toDouble();
00247                             if (aux <= min_z)
00248                                 min_z = aux;
00249                             if (aux >= max_z)
00250                                 max_z = aux;
00251                         }
00252                         qApp->processEvents();
00253                     }
00254                 }
00255             }
00256         } else
00257             m->range(&min_z, &max_z);
00258     }
00259 
00260     ~MatrixData()
00261     {
00262         if (d_mup)
00263             delete d_mup;
00264     }
00265 
00266     virtual QwtRasterData *copy() const
00267     {
00268         if (d_mup)
00269             return new MatrixData(d_matrix, true);
00270 
00271         return new MatrixData(d_matrix);
00272     }
00273 
00274     virtual QwtDoubleInterval range() const
00275     {
00276         return QwtDoubleInterval(min_z, max_z);
00277     }
00278 
00279     virtual QSize rasterHint (const QwtDoubleRect &) const
00280     {
00281         return QSize(n_cols, n_rows);
00282     }
00283 
00284     virtual double value(double x, double y) const;
00285 
00286 private:
00288     Matrix *d_matrix;
00289 
00291     int n_rows, n_cols;
00292 
00294     double min_z, max_z;
00295 
00297     double dx, dy;
00298 
00300     double x_start;
00301 
00303     double y_start;
00304 
00306     muParserScript *d_mup;
00308     double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
00309 };
00310 
00311 #endif