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 #include <LinearColorMap.h>
00036 
00037 #include <qwt_raster_data.h>
00038 #include <qwt_plot.h>
00039 #include <qwt_plot_spectrogram.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     LinearColorMap colorMap(){return color_map;};
00075     LinearColorMap *colorMapPointer(){return &color_map;};
00076     void setCustomColorMap(const LinearColorMap& 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() const;
00128 
00129 protected:
00130     virtual QImage renderImage(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtDoubleRect &rect) const;
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     LinearColorMap 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 
00173 class MatrixData: public QwtRasterData
00174 {
00175 public:
00176     MatrixData(Matrix *m, bool useMatrixFormula = false):
00177         QwtRasterData(m->boundingRect()),
00178         d_matrix(m)
00179     {
00180         n_rows = d_matrix->numRows();
00181         n_cols = d_matrix->numCols();
00182 
00183         x_start = d_matrix->xStart();
00184         dx = d_matrix->dx();
00185         y_start = d_matrix->yStart();
00186         dy = d_matrix->dy();
00187 
00188         d_mup = NULL;
00189         if (useMatrixFormula && d_matrix->canCalculate()){
00190             d_mup = new muParserScript(d_matrix->scriptingEnv(), d_matrix->formula(),
00191                     d_matrix, QString("<%1>").arg(d_matrix->objectName()));
00192 
00193             d_x = d_mup->defineVariable("x");
00194             d_y = d_mup->defineVariable("y");
00195             d_ri = d_mup->defineVariable("i");
00196             d_rr = d_mup->defineVariable("row");
00197             d_cj = d_mup->defineVariable("j");
00198             d_cc = d_mup->defineVariable("col");
00199 
00200             if (!d_mup->compile()){
00201                 delete d_mup;
00202                 d_mup = NULL;
00203             }
00204 
00205             if (d_mup){//calculate z range
00206                 *d_ri = 1.0;
00207                 *d_rr = 1.0;
00208                 *d_y = y_start;
00209                 *d_cj = 1.0;
00210                 *d_cc = 1.0;
00211                 *d_x = x_start;
00212 
00213                 if (d_mup->codeLines() == 1)
00214                     min_z = d_mup->evalSingleLine();
00215                 else
00216                     min_z = d_mup->eval().toDouble();
00217 
00218                 max_z = min_z;
00219 
00220                 if (d_mup->codeLines() == 1){
00221                     for(int row = 0; row < n_rows; row++){
00222                         double r = row + 1.0;
00223                         *d_ri = r; *d_rr = r;
00224                         *d_y = y_start + row*dy;
00225                         for(int col = 0; col < n_cols; col++){
00226                             double c = col + 1.0;
00227                             *d_cj = c; *d_cc = c;
00228                             *d_x = x_start + col*dx;
00229                             double aux = d_mup->evalSingleLine();
00230                             if (aux <= min_z)
00231                                 min_z = aux;
00232                             if (aux >= max_z)
00233                                 max_z = aux;
00234                         }
00235                     }
00236                 } else {
00237                     for(int row = 0; row < n_rows; row++){
00238                         double r = row + 1.0;
00239                         *d_ri = r; *d_rr = r;
00240                         *d_y = y_start + row*dy;
00241                         for(int col = 0; col < n_cols; col++){
00242                             double c = col + 1.0;
00243                             *d_cj = c; *d_cc = c;
00244                             *d_x = x_start + col*dx;
00245                             double aux = d_mup->eval().toDouble();
00246                             if (aux <= min_z)
00247                                 min_z = aux;
00248                             if (aux >= max_z)
00249                                 max_z = aux;
00250                         }
00251                         qApp->processEvents();
00252                     }
00253                 }
00254             }
00255         } else
00256             m->range(&min_z, &max_z);
00257     }
00258 
00259     ~MatrixData()
00260     {
00261         if (d_mup)
00262             delete d_mup;
00263     }
00264 
00265     virtual QwtRasterData *copy() const
00266     {
00267         if (d_mup)
00268             return new MatrixData(d_matrix, true);
00269 
00270         return new MatrixData(d_matrix);
00271     }
00272 
00273     virtual QwtDoubleInterval range() const
00274     {
00275         return QwtDoubleInterval(min_z, max_z);
00276     }
00277 
00278     virtual QSize rasterHint (const QwtDoubleRect &) const
00279     {
00280         return QSize(n_cols, n_rows);
00281     }
00282 
00283     virtual double value(double x, double y) const;
00284 
00285 private:
00287     Matrix *d_matrix;
00288 
00290     int n_rows, n_cols;
00291 
00293     double min_z, max_z;
00294 
00296     double dx, dy;
00297 
00299     double x_start;
00300 
00302     double y_start;
00303 
00305     muParserScript *d_mup;
00307     double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
00308 };
00309 
00310 #endif