Spectrogram.h
Go to the documentation of this file.
/***************************************************************************
File : Spectrogram.h
Project : QtiPlot
--------------------------------------------------------------------
Copyright : (C) 2006 by Ion Vasilief
Email (use @ for *) : ion_vasilief*yahoo.fr
Description : QtiPlot's Spectrogram Class
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#ifndef SPECTROGRAM_H
#define SPECTROGRAM_H
#include <QApplication>
#include <Matrix.h>
#include <muParserScript.h>
#include <LinearColorMap.h>
#include <qwt_raster_data.h>
#include <qwt_plot.h>
#include <qwt_plot_spectrogram.h>
#include <qwt_plot_marker.h>
class MatrixData;
class Graph;
class PlotMarker;
class Spectrogram: public QwtPlotSpectrogram
{
public:
Spectrogram(Graph *graph, Matrix *m);
enum ColorMapPolicy{GrayScale, Default, Custom};
Spectrogram* copy(Graph *g);
Graph * graph(){return d_graph;};
Matrix * matrix(){return d_matrix;};
bool setMatrix(Matrix *m, bool useFormula = false);
int levels(){return (int)contourLevels().size();};
void setLevelsNumber(int levels);
void setContourLevels (const QwtValueList & levels);
bool hasColorScale();
int colorScaleAxis(){return color_axis;};
void setColorScaleAxis(int axis){color_axis = axis;};
void showColorScale(int axis, bool on = true);
int colorBarWidth();
void setColorBarWidth(int width);
void setGrayScale();
void setDefaultColorMap();
LinearColorMap colorMap(){return color_map;};
LinearColorMap *colorMapPointer(){return &color_map;};
void setCustomColorMap(const LinearColorMap& map);
QString saveToString();
ColorMapPolicy colorMapPolicy(){return color_map_policy;};
virtual QwtDoubleRect boundingRect() const;
bool hasLabels(){return d_show_labels;};
QList <PlotMarker *> labelsList(){return d_labels_list;};
void showContourLineLabels(bool show = true);
QFont labelsFont(){return d_labels_font;};
void setLabelsFont(const QFont& font);
QColor labelsColor(){return d_labels_color;};
void setLabelsColor(const QColor& c);
bool labelsWhiteOut(){return d_white_out_labels;};
void setLabelsWhiteOut(bool whiteOut);
double labelsXOffset(){return d_labels_x_offset;};
double labelsYOffset(){return d_labels_y_offset;};
void setLabelsOffset(double x, double y);
void setLabelOffset(int index, double x, double y);
double labelsRotation(){return d_labels_angle;};
void setLabelsRotation(double angle);
bool selectedLabels(const QPoint& pos);
void selectLabel(bool on);
bool hasSelectedLabels();
void moveLabel(const QPoint& pos);
void clearLabels();
virtual void setVisible(bool on);
virtual QPen contourPen (double level) const;
void setColorMapPen(bool on = true);
bool useColorMapPen(){return d_color_map_pen;};
QList<QPen> contourPenList(){return d_pen_list;};
void setContourPenList(QList<QPen> lst);
void setContourLinePen(int index, const QPen &pen);
bool useMatrixFormula(){return d_use_matrix_formula;};
bool setUseMatrixFormula(bool on = true);
void updateData();
QwtDoubleInterval range() const;
protected:
virtual QImage renderImage(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtDoubleRect &rect) const;
virtual void drawContourLines (QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const;
void createLabels();
Graph *d_graph;
Matrix *d_matrix;
int color_axis;
ColorMapPolicy color_map_policy;
LinearColorMap color_map;
bool d_show_labels;
QColor d_labels_color;
QFont d_labels_font;
bool d_white_out_labels;
double d_labels_angle;
double d_labels_x_offset, d_labels_y_offset;
QList <PlotMarker *> d_labels_list;
PlotMarker *d_selected_label;
double d_click_pos_x, d_click_pos_y;
bool d_use_matrix_formula;
bool d_color_map_pen;
QList<QPen> d_pen_list;
};
class MatrixData: public QwtRasterData
{
public:
MatrixData(Matrix *m, bool useMatrixFormula = false):
QwtRasterData(m->boundingRect()),
d_matrix(m)
{
n_rows = d_matrix->numRows();
n_cols = d_matrix->numCols();
x_start = d_matrix->xStart();
dx = d_matrix->dx();
y_start = d_matrix->yStart();
dy = d_matrix->dy();
d_mup = NULL;
if (useMatrixFormula && d_matrix->canCalculate()){
d_mup = new muParserScript(d_matrix->scriptingEnv(), d_matrix->formula(),
d_matrix, QString("<%1>").arg(d_matrix->objectName()));
d_x = d_mup->defineVariable("x");
d_y = d_mup->defineVariable("y");
d_ri = d_mup->defineVariable("i");
d_rr = d_mup->defineVariable("row");
d_cj = d_mup->defineVariable("j");
d_cc = d_mup->defineVariable("col");
if (!d_mup->compile()){
delete d_mup;
d_mup = NULL;
}
if (d_mup){//calculate z range
*d_ri = 1.0;
*d_rr = 1.0;
*d_y = y_start;
*d_cj = 1.0;
*d_cc = 1.0;
*d_x = x_start;
if (d_mup->codeLines() == 1)
min_z = d_mup->evalSingleLine();
else
min_z = d_mup->eval().toDouble();
max_z = min_z;
if (d_mup->codeLines() == 1){
for(int row = 0; row < n_rows; row++){
double r = row + 1.0;
*d_ri = r; *d_rr = r;
*d_y = y_start + row*dy;
for(int col = 0; col < n_cols; col++){
double c = col + 1.0;
*d_cj = c; *d_cc = c;
*d_x = x_start + col*dx;
double aux = d_mup->evalSingleLine();
if (aux <= min_z)
min_z = aux;
if (aux >= max_z)
max_z = aux;
}
}
} else {
for(int row = 0; row < n_rows; row++){
double r = row + 1.0;
*d_ri = r; *d_rr = r;
*d_y = y_start + row*dy;
for(int col = 0; col < n_cols; col++){
double c = col + 1.0;
*d_cj = c; *d_cc = c;
*d_x = x_start + col*dx;
double aux = d_mup->eval().toDouble();
if (aux <= min_z)
min_z = aux;
if (aux >= max_z)
max_z = aux;
}
qApp->processEvents();
}
}
}
} else
m->range(&min_z, &max_z);
}
~MatrixData()
{
if (d_mup)
delete d_mup;
}
virtual QwtRasterData *copy() const
{
if (d_mup)
return new MatrixData(d_matrix, true);
return new MatrixData(d_matrix);
}
virtual QwtDoubleInterval range() const
{
return QwtDoubleInterval(min_z, max_z);
}
virtual QSize rasterHint (const QwtDoubleRect &) const
{
return QSize(n_cols, n_rows);
}
virtual double value(double x, double y) const;
private:
Matrix *d_matrix;
int n_rows, n_cols;
double min_z, max_z;
double dx, dy;
double x_start;
double y_start;
muParserScript *d_mup;
double *d_x, *d_y, *d_ri, *d_rr, *d_cj, *d_cc;
};
#endif
Spectrogram::labelsWhiteOut
bool labelsWhiteOut()
Definition: Spectrogram.h:96
Spectrogram::Custom
Definition: Spectrogram.h:51
Spectrogram::createLabels
void createLabels()
Definition: Spectrogram.cpp:384
Spectrogram::Default
Definition: Spectrogram.h:51
LinearColorMap.h
Spectrogram::hasLabels
bool hasLabels()
Definition: Spectrogram.h:85
Spectrogram::setColorMapPen
void setColorMapPen(bool on=true)
Definition: Spectrogram.cpp:692
Spectrogram::labelsList
QList< PlotMarker * > labelsList()
Definition: Spectrogram.h:86
Spectrogram::selectedLabels
bool selectedLabels(const QPoint &pos)
Definition: Spectrogram.cpp:571
Spectrogram::setLevelsNumber
void setLevelsNumber(int levels)
Definition: Spectrogram.cpp:131
Spectrogram::d_labels_y_offset
double d_labels_y_offset
Definition: Spectrogram.h:155
Graph
A 2D-plotting widget.
Definition: Graph.h:123
scripted::scriptingEnv
ScriptingEnv * scriptingEnv()
Definition: Script.h:158
Spectrogram::saveToString
QString saveToString()
Used when saving a project file.
Definition: Spectrogram.cpp:312
MatrixData::x_start
double x_start
X axis left value in the data matrix.
Definition: Spectrogram.h:299
Spectrogram::selectLabel
void selectLabel(bool on)
Definition: Spectrogram.cpp:596
Spectrogram::copy
Spectrogram * copy(Graph *g)
Definition: Spectrogram.cpp:230
Spectrogram::useMatrixFormula
bool useMatrixFormula()
Definition: Spectrogram.h:123
MatrixData::copy
virtual QwtRasterData * copy() const
Definition: Spectrogram.h:265
Spectrogram::color_map_policy
ColorMapPolicy color_map_policy
Flags.
Definition: Spectrogram.h:143
Spectrogram::labelsRotation
double labelsRotation()
Definition: Spectrogram.h:104
Spectrogram::updateLabels
void updateLabels(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const
Definition: Spectrogram.cpp:459
Spectrogram::d_graph
Graph * d_graph
Pointer to the parent plot.
Definition: Spectrogram.h:135
Spectrogram::setLabelsColor
void setLabelsColor(const QColor &c)
Definition: Spectrogram.cpp:508
Matrix::numCols
int numCols()
Return the number of columns.
Definition: Matrix.h:118
MatrixData::y_start
double y_start
Y axis bottom value in the data matrix.
Definition: Spectrogram.h:302
Spectrogram::d_labels_x_offset
double d_labels_x_offset
Definition: Spectrogram.h:155
Spectrogram
Definition: Spectrogram.h:46
muParserScript
TODO.
Definition: muParserScript.h:42
Spectrogram::d_use_matrix_formula
bool d_use_matrix_formula
Flag telling that we evaluate the matrix expression instead of using the matrix data.
Definition: Spectrogram.h:165
Spectrogram::d_color_map_pen
bool d_color_map_pen
Flag telling if we use the color map to calculate the pen (QwtPlotSpectrogram::contourPen()).
Definition: Spectrogram.h:168
MatrixData::d_matrix
Matrix * d_matrix
Pointer to the source data matrix.
Definition: Spectrogram.h:287
Spectrogram::setVisible
virtual void setVisible(bool on)
Definition: Spectrogram.cpp:672
Matrix::yStart
double yStart()
Returns the Y value corresponding to row 1.
Definition: Matrix.h:270
Spectrogram::d_click_pos_y
double d_click_pos_y
Definition: Spectrogram.h:162
Spectrogram::setDefaultColorMap
void setDefaultColorMap()
Definition: Spectrogram.cpp:284
Spectrogram::labelsXOffset
double labelsXOffset()
Definition: Spectrogram.h:99
Spectrogram::setLabelsOffset
void setLabelsOffset(double x, double y)
Definition: Spectrogram.cpp:539
Spectrogram::d_white_out_labels
bool d_white_out_labels
Flag telling if we paint a white background for the labels.
Definition: Spectrogram.h:153
Spectrogram::setContourPenList
void setContourPenList(QList< QPen > lst)
Definition: Spectrogram.cpp:701
Spectrogram::GrayScale
Definition: Spectrogram.h:51
Spectrogram::d_pen_list
QList< QPen > d_pen_list
Definition: Spectrogram.h:170
Spectrogram::labelsYOffset
double labelsYOffset()
Definition: Spectrogram.h:100
LinearColorMap
A customized QwtLinearColorMap.
Definition: LinearColorMap.h:39
Spectrogram::d_labels_color
QColor d_labels_color
Labels color.
Definition: Spectrogram.h:149
Spectrogram::color_map
LinearColorMap color_map
Definition: Spectrogram.h:145
MatrixData::d_mup
muParserScript * d_mup
Pointer to a muParserScript.
Definition: Spectrogram.h:305
Spectrogram::renderImage
virtual QImage renderImage(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtDoubleRect &rect) const
Definition: Spectrogram.cpp:740
Spectrogram::hasColorScale
bool hasColorScale()
Definition: Spectrogram.cpp:152
Spectrogram::setLabelsWhiteOut
void setLabelsWhiteOut(bool whiteOut)
Definition: Spectrogram.cpp:522
Spectrogram::colorMapPointer
LinearColorMap * colorMapPointer()
Definition: Spectrogram.h:75
Spectrogram::colorScaleAxis
int colorScaleAxis()
Definition: Spectrogram.h:64
Spectrogram::matrix
Matrix * matrix()
Definition: Spectrogram.h:56
Spectrogram::d_labels_font
QFont d_labels_font
Labels font.
Definition: Spectrogram.h:151
Spectrogram::contourPen
virtual QPen contourPen(double level) const
Definition: Spectrogram.cpp:679
Spectrogram::setMatrix
bool setMatrix(Matrix *m, bool useFormula=false)
Definition: Spectrogram.cpp:96
Matrix::numRows
int numRows()
Return the number of rows.
Definition: Matrix.h:114
Spectrogram::d_click_pos_x
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
Spectrogram::ColorMapPolicy
ColorMapPolicy
Definition: Spectrogram.h:51
Spectrogram::updateData
void updateData()
Definition: Spectrogram.cpp:71
Spectrogram::d_matrix
Matrix * d_matrix
Pointer to the source data matrix.
Definition: Spectrogram.h:137
Spectrogram::d_labels_list
QList< PlotMarker * > d_labels_list
List of the text labels associated to this spectrogram.
Definition: Spectrogram.h:158
Spectrogram::useColorMapPen
bool useColorMapPen()
Definition: Spectrogram.h:116
Spectrogram::setContourLevels
void setContourLevels(const QwtValueList &levels)
Definition: Spectrogram.cpp:146
MatrixData::min_z
double min_z
Min and max values in the source data matrix.
Definition: Spectrogram.h:293
MatrixData::rasterHint
virtual QSize rasterHint(const QwtDoubleRect &) const
Definition: Spectrogram.h:278
Spectrogram::colorMapPolicy
ColorMapPolicy colorMapPolicy()
Definition: Spectrogram.h:81
muParserScript.h
Matrix::formula
QString formula()
Return the matrix formula.
Definition: Matrix.h:231
Spectrogram::contourPenList
QList< QPen > contourPenList()
Definition: Spectrogram.h:118
Matrix.h
Matrix
Matrix worksheet class.
Definition: Matrix.h:57
Spectrogram::setColorBarWidth
void setColorBarWidth(int width)
Definition: Spectrogram.cpp:221
Spectrogram::colorBarWidth
int colorBarWidth()
Definition: Spectrogram.cpp:212
Spectrogram::clearLabels
void clearLabels()
Definition: Spectrogram.cpp:663
Spectrogram::labelsColor
QColor labelsColor()
Definition: Spectrogram.h:93
Spectrogram::setLabelsRotation
void setLabelsRotation(double angle)
Definition: Spectrogram.cpp:560
Spectrogram::setColorScaleAxis
void setColorScaleAxis(int axis)
Definition: Spectrogram.h:65
Spectrogram::setLabelsFont
void setLabelsFont(const QFont &font)
Definition: Spectrogram.cpp:494
PlotMarker
Definition: PlotCurve.h:237
Spectrogram::boundingRect
virtual QwtDoubleRect boundingRect() const
Definition: Spectrogram.cpp:379
Spectrogram::showContourLineLabels
void showContourLineLabels(bool show=true)
Definition: Spectrogram.cpp:413
Spectrogram::showColorScale
void showColorScale(int axis, bool on=true)
Definition: Spectrogram.cpp:167
Spectrogram::labelsFont
QFont labelsFont()
Definition: Spectrogram.h:90
MatrixData::n_rows
int n_rows
Data size.
Definition: Spectrogram.h:290
MatrixData::~MatrixData
~MatrixData()
Definition: Spectrogram.h:259
MatrixData::dy
double dy
Definition: Spectrogram.h:296
Spectrogram::d_show_labels
bool d_show_labels
Flag telling if we display the labels.
Definition: Spectrogram.h:147
Spectrogram::d_selected_label
PlotMarker * d_selected_label
Keeps track of the plot marker on which the user clicked when selecting the labels.
Definition: Spectrogram.h:160
Matrix::range
void range(double *min, double *max)
Min and max values of the matrix.
Definition: Matrix.cpp:1185
Spectrogram::hasSelectedLabels
bool hasSelectedLabels()
Definition: Spectrogram.cpp:620
Matrix::canCalculate
bool canCalculate(bool useMuParser=true)
Definition: Matrix.cpp:546
Spectrogram::setGrayScale
void setGrayScale()
Definition: Spectrogram.cpp:270
Spectrogram::setContourLinePen
void setContourLinePen(int index, const QPen &pen)
Definition: Spectrogram.cpp:708
Spectrogram::setUseMatrixFormula
bool setUseMatrixFormula(bool on=true)
Definition: Spectrogram.cpp:730
MatrixData::range
virtual QwtDoubleInterval range() const
Definition: Spectrogram.h:273
Spectrogram::d_labels_angle
double d_labels_angle
Definition: Spectrogram.h:154
MatrixData
Definition: Spectrogram.h:173
Spectrogram::setLabelOffset
void setLabelOffset(int index, double x, double y)
Definition: Spectrogram.cpp:548
Spectrogram::colorMap
LinearColorMap colorMap()
Definition: Spectrogram.h:74
Spectrogram::color_axis
int color_axis
Axis used to display the color scale.
Definition: Spectrogram.h:140
Matrix::dx
double dx()
Returns the step of the X axis.
Definition: Matrix.h:275
Spectrogram::setCustomColorMap
void setCustomColorMap(const LinearColorMap &map)
Definition: Spectrogram.cpp:298
MatrixData::d_y
double * d_y
Definition: Spectrogram.h:307
Spectrogram::drawContourLines
virtual void drawContourLines(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QwtRasterData::ContourLines &lines) const
Definition: Spectrogram.cpp:428
Matrix::dy
double dy()
Returns the step of the Y axis.
Definition: Matrix.h:277
Spectrogram::Spectrogram
Spectrogram(Graph *graph, Matrix *m)
Definition: Spectrogram.cpp:41
Spectrogram::graph
Graph * graph()
Definition: Spectrogram.h:54
Spectrogram::levels
int levels()
Definition: Spectrogram.h:59
Spectrogram::moveLabel
void moveLabel(const QPoint &pos)
Definition: Spectrogram.cpp:638
Matrix::xStart
double xStart()
Returns the X value corresponding to column 1.
Definition: Matrix.h:266
MatrixData::MatrixData
MatrixData(Matrix *m, bool useMatrixFormula=false)
Definition: Spectrogram.h:176
Spectrogram::range
QwtDoubleInterval range() const
Definition: Spectrogram.cpp:86