QtiPlot  0.9.8.2
Spectrogram.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Spectrogram.h
3  Project : QtiPlot
4 --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief
6  Email (use @ for *) : ion_vasilief*yahoo.fr
7  Description : QtiPlot's Spectrogram Class
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  * This program is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  * You should have received a copy of the GNU General Public License *
23  * along with this program; if not, write to the Free Software *
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25  * Boston, MA 02110-1301 USA *
26  * *
27  ***************************************************************************/
28 
29 #ifndef SPECTROGRAM_H
30 #define SPECTROGRAM_H
31 
32 #include <QApplication>
33 #include <Matrix.h>
34 #include <muParserScript.h>
35 #include <LinearColorMap.h>
36 
37 #include <qwt_raster_data.h>
38 #include <qwt_plot.h>
39 #include <qwt_plot_spectrogram.h>
40 #include <qwt_plot_marker.h>
41 
42 class MatrixData;
43 class Graph;
44 class PlotMarker;
45 
46 class Spectrogram: public QwtPlotSpectrogram
47 {
48 public:
50 
52 
53  Spectrogram* copy(Graph *g);
54  Graph * graph(){return d_graph;};
55 
56  Matrix * matrix(){return d_matrix;};
57  bool setMatrix(Matrix *m, bool useFormula = false);
58 
59  int levels(){return (int)contourLevels().size();};
60  void setLevelsNumber(int levels);
61  void setContourLevels (const QwtValueList & levels);
62 
63  bool hasColorScale();
64  int colorScaleAxis(){return color_axis;};
65  void setColorScaleAxis(int axis){color_axis = axis;};
66  void showColorScale(int axis, bool on = true);
67 
68  int colorBarWidth();
69  void setColorBarWidth(int width);
70 
71  void setGrayScale();
72  void setDefaultColorMap();
73 
76  void setCustomColorMap(const LinearColorMap& map);
77 
79  QString saveToString();
80 
82 
83  virtual QwtDoubleRect boundingRect() const;
84 
85  bool hasLabels(){return d_show_labels;};
86  QList <PlotMarker *> labelsList(){return d_labels_list;};
87 
88  void showContourLineLabels(bool show = true);
89 
90  QFont labelsFont(){return d_labels_font;};
91  void setLabelsFont(const QFont& font);
92 
93  QColor labelsColor(){return d_labels_color;};
94  void setLabelsColor(const QColor& c);
95 
97  void setLabelsWhiteOut(bool whiteOut);
98 
99  double labelsXOffset(){return d_labels_x_offset;};
100  double labelsYOffset(){return d_labels_y_offset;};
101  void setLabelsOffset(double x, double y);
102  void setLabelOffset(int index, double x, double y);
103 
104  double labelsRotation(){return d_labels_angle;};
105  void setLabelsRotation(double angle);
106 
107  bool selectedLabels(const QPoint& pos);
108  void selectLabel(bool on);
109  bool hasSelectedLabels();
110  void moveLabel(const QPoint& pos);
111  void clearLabels();
112 
113  virtual void setVisible(bool on);
114  virtual QPen contourPen (double level) const;
115  void setColorMapPen(bool on = true);
117 
118  QList<QPen> contourPenList(){return d_pen_list;};
119  void setContourPenList(QList<QPen> lst);
120 
121  void setContourLinePen(int index, const QPen &pen);
122 
124  bool setUseMatrixFormula(bool on = true);
125 
126  void updateData();
127  QwtDoubleInterval range() const;
128 
129 protected:
130  virtual QImage renderImage(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtDoubleRect &rect) const;
131  virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
132  void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
133  void createLabels();
138 
141 
144 
156 
158  QList <PlotMarker *> d_labels_list;
163 
166 
169 
170  QList<QPen> d_pen_list;
171 };
172 
173 class MatrixData: public QwtRasterData
174 {
175 public:
176  MatrixData(Matrix *m, bool useMatrixFormula = false):
177  QwtRasterData(m->boundingRect()),
178  d_matrix(m)
179  {
180  n_rows = d_matrix->numRows();
181  n_cols = d_matrix->numCols();
182 
183  x_start = d_matrix->xStart();
184  dx = d_matrix->dx();
185  y_start = d_matrix->yStart();
186  dy = d_matrix->dy();
187 
188  d_mup = NULL;
191  d_matrix, QString("<%1>").arg(d_matrix->objectName()));
192 
193  d_x = d_mup->defineVariable("x");
194  d_y = d_mup->defineVariable("y");
195  d_ri = d_mup->defineVariable("i");
196  d_rr = d_mup->defineVariable("row");
197  d_cj = d_mup->defineVariable("j");
198  d_cc = d_mup->defineVariable("col");
199 
200  if (!d_mup->compile()){
201  delete d_mup;
202  d_mup = NULL;
203  }
204 
205  if (d_mup){//calculate z range
206  *d_ri = 1.0;
207  *d_rr = 1.0;
208  *d_y = y_start;
209  *d_cj = 1.0;
210  *d_cc = 1.0;
211  *d_x = x_start;
212 
213  if (d_mup->codeLines() == 1)
214  min_z = d_mup->evalSingleLine();
215  else
216  min_z = d_mup->eval().toDouble();
217 
218  max_z = min_z;
219 
220  if (d_mup->codeLines() == 1){
221  for(int row = 0; row < n_rows; row++){
222  double r = row + 1.0;
223  *d_ri = r; *d_rr = r;
224  *d_y = y_start + row*dy;
225  for(int col = 0; col < n_cols; col++){
226  double c = col + 1.0;
227  *d_cj = c; *d_cc = c;
228  *d_x = x_start + col*dx;
229  double aux = d_mup->evalSingleLine();
230  if (aux <= min_z)
231  min_z = aux;
232  if (aux >= max_z)
233  max_z = aux;
234  }
235  }
236  } else {
237  for(int row = 0; row < n_rows; row++){
238  double r = row + 1.0;
239  *d_ri = r; *d_rr = r;
240  *d_y = y_start + row*dy;
241  for(int col = 0; col < n_cols; col++){
242  double c = col + 1.0;
243  *d_cj = c; *d_cc = c;
244  *d_x = x_start + col*dx;
245  double aux = d_mup->eval().toDouble();
246  if (aux <= min_z)
247  min_z = aux;
248  if (aux >= max_z)
249  max_z = aux;
250  }
251  qApp->processEvents();
252  }
253  }
254  }
255  } else
256  m->range(&min_z, &max_z);
257  }
258 
260  {
261  if (d_mup)
262  delete d_mup;
263  }
264 
265  virtual QwtRasterData *copy() const
266  {
267  if (d_mup)
268  return new MatrixData(d_matrix, true);
269 
270  return new MatrixData(d_matrix);
271  }
272 
273  virtual QwtDoubleInterval range() const
274  {
275  return QwtDoubleInterval(min_z, max_z);
276  }
277 
278  virtual QSize rasterHint (const QwtDoubleRect &) const
279  {
280  return QSize(n_cols, n_rows);
281  }
282 
283  virtual double value(double x, double y) const;
284 
285 private:
288 
290  int n_rows, n_cols;
291 
293  double min_z, max_z;
294 
296  double dx, dy;
297 
299  double x_start;
300 
302  double y_start;
303 
307  double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
308 };
309 
310 #endif
bool labelsWhiteOut()
Definition: Spectrogram.h:96
Definition: Spectrogram.h:51
void createLabels()
Definition: Spectrogram.cpp:384
Definition: Spectrogram.h:51
bool hasLabels()
Definition: Spectrogram.h:85
void setColorMapPen(bool on=true)
Definition: Spectrogram.cpp:692
QList< PlotMarker * > labelsList()
Definition: Spectrogram.h:86
bool selectedLabels(const QPoint &pos)
Definition: Spectrogram.cpp:571
void setLevelsNumber(int levels)
Definition: Spectrogram.cpp:131
double d_labels_y_offset
Definition: Spectrogram.h:155
A 2D-plotting widget.
Definition: Graph.h:123
ScriptingEnv * scriptingEnv()
Definition: Script.h:158
QString saveToString()
Used when saving a project file.
Definition: Spectrogram.cpp:312
double x_start
X axis left value in the data matrix.
Definition: Spectrogram.h:299
void selectLabel(bool on)
Definition: Spectrogram.cpp:596
Spectrogram * copy(Graph *g)
Definition: Spectrogram.cpp:230
bool useMatrixFormula()
Definition: Spectrogram.h:123
ColorMapPolicy color_map_policy
Flags.
Definition: Spectrogram.h:143
double labelsRotation()
Definition: Spectrogram.h:104
Graph * d_graph
Pointer to the parent plot.
Definition: Spectrogram.h:135
void setLabelsColor(const QColor &c)
Definition: Spectrogram.cpp:508
int numCols()
Return the number of columns.
Definition: Matrix.h:118
double y_start
Y axis bottom value in the data matrix.
Definition: Spectrogram.h:302
double d_labels_x_offset
Definition: Spectrogram.h:155
Definition: Spectrogram.h:46
TODO.
Definition: muParserScript.h:42
bool d_use_matrix_formula
Flag telling that we evaluate the matrix expression instead of using the matrix data.
Definition: Spectrogram.h:165
bool d_color_map_pen
Flag telling if we use the color map to calculate the pen (QwtPlotSpectrogram::contourPen()).
Definition: Spectrogram.h:168
Matrix * d_matrix
Pointer to the source data matrix.
Definition: Spectrogram.h:287
virtual void setVisible(bool on)
Definition: Spectrogram.cpp:672
double yStart()
Returns the Y value corresponding to row 1.
Definition: Matrix.h:270
double d_click_pos_y
Definition: Spectrogram.h:162
void setDefaultColorMap()
Definition: Spectrogram.cpp:284
double labelsXOffset()
Definition: Spectrogram.h:99
void setLabelsOffset(double x, double y)
Definition: Spectrogram.cpp:539
bool d_white_out_labels
Flag telling if we paint a white background for the labels.
Definition: Spectrogram.h:153
void setContourPenList(QList< QPen > lst)
Definition: Spectrogram.cpp:701
Definition: Spectrogram.h:51
virtual QwtDoubleRect boundingRect() const
Definition: Spectrogram.cpp:379
QList< QPen > d_pen_list
Definition: Spectrogram.h:170
double labelsYOffset()
Definition: Spectrogram.h:100
A customized QwtLinearColorMap.
Definition: LinearColorMap.h:39
QColor d_labels_color
Labels color.
Definition: Spectrogram.h:149
LinearColorMap color_map
Definition: Spectrogram.h:145
muParserScript * d_mup
Pointer to a muParserScript.
Definition: Spectrogram.h:305
virtual QwtDoubleInterval range() const
Definition: Spectrogram.h:273
bool hasColorScale()
Definition: Spectrogram.cpp:152
void setLabelsWhiteOut(bool whiteOut)
Definition: Spectrogram.cpp:522
LinearColorMap * colorMapPointer()
Definition: Spectrogram.h:75
int colorScaleAxis()
Definition: Spectrogram.h:64
Matrix * matrix()
Definition: Spectrogram.h:56
QFont d_labels_font
Labels font.
Definition: Spectrogram.h:151
bool setMatrix(Matrix *m, bool useFormula=false)
Definition: Spectrogram.cpp:96
int numRows()
Return the number of rows.
Definition: Matrix.h:114
double d_click_pos_x
Keep track of the coordinates of the point where the user clicked when selecting the labels...
Definition: Spectrogram.h:162
ColorMapPolicy
Definition: Spectrogram.h:51
void updateData()
Definition: Spectrogram.cpp:71
Matrix * d_matrix
Pointer to the source data matrix.
Definition: Spectrogram.h:137
virtual QwtRasterData * copy() const
Definition: Spectrogram.h:265
QList< PlotMarker * > d_labels_list
List of the text labels associated to this spectrogram.
Definition: Spectrogram.h:158
bool useColorMapPen()
Definition: Spectrogram.h:116
void setContourLevels(const QwtValueList &levels)
Definition: Spectrogram.cpp:146
QwtDoubleInterval range() const
Definition: Spectrogram.cpp:86
double min_z
Min and max values in the source data matrix.
Definition: Spectrogram.h:293
ColorMapPolicy colorMapPolicy()
Definition: Spectrogram.h:81
QString formula()
Return the matrix formula.
Definition: Matrix.h:231
QList< QPen > contourPenList()
Definition: Spectrogram.h:118
Matrix worksheet class.
Definition: Matrix.h:57
void setColorBarWidth(int width)
Definition: Spectrogram.cpp:221
int colorBarWidth()
Definition: Spectrogram.cpp:212
void clearLabels()
Definition: Spectrogram.cpp:663
QColor labelsColor()
Definition: Spectrogram.h:93
void setLabelsRotation(double angle)
Definition: Spectrogram.cpp:560
virtual QPen contourPen(double level) const
Definition: Spectrogram.cpp:679
void setColorScaleAxis(int axis)
Definition: Spectrogram.h:65
void setLabelsFont(const QFont &font)
Definition: Spectrogram.cpp:494
Definition: PlotCurve.h:237
void showContourLineLabels(bool show=true)
Definition: Spectrogram.cpp:413
virtual QImage renderImage(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtDoubleRect &rect) const
Definition: Spectrogram.cpp:740
void showColorScale(int axis, bool on=true)
Definition: Spectrogram.cpp:167
QFont labelsFont()
Definition: Spectrogram.h:90
int n_rows
Data size.
Definition: Spectrogram.h:290
~MatrixData()
Definition: Spectrogram.h:259
double dy
Definition: Spectrogram.h:296
bool d_show_labels
Flag telling if we display the labels.
Definition: Spectrogram.h:147
PlotMarker * d_selected_label
Keeps track of the plot marker on which the user clicked when selecting the labels.
Definition: Spectrogram.h:160
void range(double *min, double *max)
Min and max values of the matrix.
Definition: Matrix.cpp:1185
bool hasSelectedLabels()
Definition: Spectrogram.cpp:620
bool canCalculate(bool useMuParser=true)
Definition: Matrix.cpp:546
void setGrayScale()
Definition: Spectrogram.cpp:270
virtual QSize rasterHint(const QwtDoubleRect &) const
Definition: Spectrogram.h:278
void setContourLinePen(int index, const QPen &pen)
Definition: Spectrogram.cpp:708
bool setUseMatrixFormula(bool on=true)
Definition: Spectrogram.cpp:730
double d_labels_angle
Definition: Spectrogram.h:154
Definition: Spectrogram.h:173
void setLabelOffset(int index, double x, double y)
Definition: Spectrogram.cpp:548
LinearColorMap colorMap()
Definition: Spectrogram.h:74
virtual void drawContourLines(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const
Definition: Spectrogram.cpp:428
int color_axis
Axis used to display the color scale.
Definition: Spectrogram.h:140
double dx()
Returns the step of the X axis.
Definition: Matrix.h:275
void setCustomColorMap(const LinearColorMap &map)
Definition: Spectrogram.cpp:298
double * d_y
Definition: Spectrogram.h:307
double dy()
Returns the step of the Y axis.
Definition: Matrix.h:277
Spectrogram(Graph *graph, Matrix *m)
Definition: Spectrogram.cpp:41
Graph * graph()
Definition: Spectrogram.h:54
void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const
Definition: Spectrogram.cpp:459
int levels()
Definition: Spectrogram.h:59
void moveLabel(const QPoint &pos)
Definition: Spectrogram.cpp:638
double xStart()
Returns the X value corresponding to column 1.
Definition: Matrix.h:266
MatrixData(Matrix *m, bool useMatrixFormula=false)
Definition: Spectrogram.h:176