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 
00126 protected:
00127     virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00128     void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
00129     void createLabels();
00131     Graph *d_graph;
00133     Matrix *d_matrix;
00134 
00136     int color_axis;
00137 
00139     ColorMapPolicy color_map_policy;
00140 
00141     QwtLinearColorMap color_map;
00143     bool d_show_labels;
00145     QColor d_labels_color;
00147     QFont d_labels_font;
00149     bool d_white_out_labels;
00150     double d_labels_angle;
00151     double d_labels_x_offset, d_labels_y_offset;
00152 
00154     QList <PlotMarker *> d_labels_list;
00156     PlotMarker *d_selected_label;
00158     double d_click_pos_x, d_click_pos_y;
00159 
00161     bool d_use_matrix_formula;
00162 
00164     bool d_color_map_pen;
00165 
00166     QList<QPen> d_pen_list;
00167 };
00168 
00169 class MatrixData: public QwtRasterData
00170 {
00171 public:
00172     MatrixData(Matrix *m, bool useMatrixFormula = false):
00173         QwtRasterData(m->boundingRect()),
00174         d_matrix(m)
00175     {
00176         n_rows = d_matrix->numRows();
00177         n_cols = d_matrix->numCols();
00178 
00179         x_start = d_matrix->xStart();
00180         dx = d_matrix->dx();
00181         y_start = d_matrix->yStart();
00182         dy = d_matrix->dy();
00183 
00184         d_mup = NULL;
00185         if (useMatrixFormula && d_matrix->canCalculate()){
00186             d_mup = new muParserScript(d_matrix->scriptingEnv(), d_matrix->formula(),
00187                     d_matrix, QString("<%1>").arg(d_matrix->objectName()));
00188 
00189             d_x = d_mup->defineVariable("x");
00190             d_y = d_mup->defineVariable("y");
00191             d_ri = d_mup->defineVariable("i");
00192             d_rr = d_mup->defineVariable("row");
00193             d_cj = d_mup->defineVariable("j");
00194             d_cc = d_mup->defineVariable("col");
00195 
00196             if (!d_mup->compile()){
00197                 delete d_mup;
00198                 d_mup = NULL;
00199             }
00200 
00201             if (d_mup){//calculate z range
00202                 *d_ri = 1.0;
00203                 *d_rr = 1.0;
00204                 *d_y = y_start;
00205                 *d_cj = 1.0;
00206                 *d_cc = 1.0;
00207                 *d_x = x_start;
00208 
00209                 if (d_mup->codeLines() == 1)
00210                     min_z = d_mup->evalSingleLine();
00211                 else
00212                     min_z = d_mup->eval().toDouble();
00213 
00214                 max_z = min_z;
00215 
00216                 if (d_mup->codeLines() == 1){
00217                     for(int row = 0; row < n_rows; row++){
00218                         double r = row + 1.0;
00219                         *d_ri = r; *d_rr = r;
00220                         *d_y = y_start + row*dy;
00221                         for(int col = 0; col < n_cols; col++){
00222                             double c = col + 1.0;
00223                             *d_cj = c; *d_cc = c;
00224                             *d_x = x_start + col*dx;
00225                             double aux = d_mup->evalSingleLine();
00226                             if (aux <= min_z)
00227                                 min_z = aux;
00228                             if (aux >= max_z)
00229                                 max_z = aux;
00230                         }
00231                     }
00232                 } else {
00233                     for(int row = 0; row < n_rows; row++){
00234                         double r = row + 1.0;
00235                         *d_ri = r; *d_rr = r;
00236                         *d_y = y_start + row*dy;
00237                         for(int col = 0; col < n_cols; col++){
00238                             double c = col + 1.0;
00239                             *d_cj = c; *d_cc = c;
00240                             *d_x = x_start + col*dx;
00241                             double aux = d_mup->eval().toDouble();
00242                             if (aux <= min_z)
00243                                 min_z = aux;
00244                             if (aux >= max_z)
00245                                 max_z = aux;
00246                         }
00247                         qApp->processEvents();
00248                     }
00249                 }
00250             }
00251         } else
00252             m->range(&min_z, &max_z);
00253     }
00254 
00255     ~MatrixData()
00256     {
00257         if (d_mup)
00258             delete d_mup;
00259     }
00260 
00261     virtual QwtRasterData *copy() const
00262     {
00263         if (d_mup)
00264             return new MatrixData(d_matrix, true);
00265 
00266         return new MatrixData(d_matrix);
00267     }
00268 
00269     virtual QwtDoubleInterval range() const
00270     {
00271         return QwtDoubleInterval(min_z, max_z);
00272     }
00273 
00274     virtual QSize rasterHint (const QwtDoubleRect &) const
00275     {
00276         return QSize(n_cols, n_rows);
00277     }
00278 
00279     virtual double value(double x, double y) const;
00280 
00281 private:
00283     Matrix *d_matrix;
00284 
00286     int n_rows, n_cols;
00287 
00289     double min_z, max_z;
00290 
00292     double dx, dy;
00293 
00295     double x_start;
00296 
00298     double y_start;
00299 
00301     muParserScript *d_mup;
00303     double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
00304 };
00305 
00306 #endif

Generated by  doxygen 1.6.2