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;
189  if (useMatrixFormula && d_matrix->canCalculate()){
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)
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 
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