Public Types | Public Member Functions | Protected Member Functions | Protected Attributes

QmitkPlotWidget Class Reference

#include <QmitkPlotWidget.h>

Collaboration diagram for QmitkPlotWidget:
Collaboration graph
[legend]

List of all members.

Public Types

typedef double ScalarType
typedef std::vector< ScalarTypeDataVector
typedef std::vector< std::pair
< double, double > > 
XYDataVector

Public Member Functions

 QmitkPlotWidget (QWidget *parent=0, const char *title=0, const char *name=0, Qt::WindowFlags f=0)
virtual ~QmitkPlotWidget ()
QwtPlotGetPlot ()
void SetPlotTitle (const char *title)
unsigned int InsertCurve (const char *title)
void SetAxisTitle (int axis, const char *title)
bool SetCurveData (unsigned int curveId, const DataVector &xValues, const DataVector &yValues)
bool SetCurveData (unsigned int curveId, const XYDataVector &data)
void SetCurvePen (unsigned int curveId, const QPen &pen)
void SetCurveBrush (unsigned int curveId, const QBrush &brush)
void SetCurveStyle (unsigned int curveId, const QwtPlotCurve::CurveStyle style)
void SetCurveSymbol (unsigned int curveId, QwtSymbol *symbol)
void SetCurveTitle (unsigned int curveId, const char *title)
void Replot ()
void Clear ()

Protected Member Functions

double * ConvertToRawArray (const DataVector &values)
double * ConvertToRawArray (const XYDataVector &values, unsigned int component)

Protected Attributes

QwtPlotm_Plot
std::vector< QwtPlotCurve * > m_PlotCurveVector

Detailed Description

Provides a convenient interface for plotting curves using qwt. Designed for qwt version 5.2.1. Can be used with a QmitkPlotDialog, which provides a "Close" button.

See also:
QmitkPlotDialog

To plot data do the following: 1. Create two QmitkPlotWidget::DataVector Objects and fill them with corresponding x/y values. DataVectors are simple stl-vectors of type std::vector<double>. Please note that the xValues vector and the yValues vector MUST have the same size. 2. Instantiate the widget for example like that: QmitkPlotWidget* widget = new QmitkPlotWidget( this, "widget" ); widget->SetAxisTitle( QwtPlot::xBottom, "My x asis [mm]" ); widget->SetAxisTitle( QwtPlot::yLeft, "My y axis [mm]" ); int curveId = widget->InsertCurve( "My sophisticated data" ); widget->SetCurveData( curveId, xValues, yValues ); widget->SetCurvePen( curveId, QPen( red ) ); widget->SetCurveTitle( curveId, "My curve description" ); widget->Replot(); 3. You can modify the behavior of the plot by directly referencing the QwtPlot instance using the method GetPlot().

See also:
QwtPlot

Definition at line 54 of file QmitkPlotWidget.h.


Member Typedef Documentation

This type may be used to store a set of scalar values representing either x or y coordinates of the data points that should be rendered.

Definition at line 70 of file QmitkPlotWidget.h.

represents the data type used for scalar values stored in data arrays. This type is provided by qwt and may not be changed.

Definition at line 63 of file QmitkPlotWidget.h.

typedef std::vector< std::pair< double, double > > QmitkPlotWidget::XYDataVector

convenience type used to store pairs representing x/y coordinates that should be rendered as a curve by the plot widget

Definition at line 76 of file QmitkPlotWidget.h.


Constructor & Destructor Documentation

QmitkPlotWidget::QmitkPlotWidget ( QWidget *  parent = 0,
const char *  title = 0,
const char *  name = 0,
Qt::WindowFlags  f = 0 
)

Standard qt constructor

Definition at line 23 of file QmitkPlotWidget.cpp.

References m_Plot, and QwtPlot::setCanvasBackground().

                                                                                                          : QWidget(parent, f) 
{
  QVBoxLayout* boxLayout = new QVBoxLayout(this);
  m_Plot = new QwtPlot( QwtText(title), this ) ;
  m_Plot->setCanvasBackground(Qt::white);
  boxLayout->addWidget( m_Plot );
} 
QmitkPlotWidget::~QmitkPlotWidget (  ) [virtual]

Virtual destructor

Definition at line 32 of file QmitkPlotWidget.cpp.

References Clear(), and m_Plot.

{
  this->Clear();
  delete m_Plot;
}

Member Function Documentation

void QmitkPlotWidget::Clear (  )

Resets the plot into an empty state

Definition at line 123 of file QmitkPlotWidget.cpp.

References QwtPlot::clear(), m_Plot, and m_PlotCurveVector.

Referenced by ~QmitkPlotWidget().

{
  m_PlotCurveVector.clear();
  m_PlotCurveVector.resize(0);
  m_Plot->clear();
}
double * QmitkPlotWidget::ConvertToRawArray ( const DataVector values ) [protected]

Converts the given values into a raw double* array. A new array is allocated via new and must be deleted[] by the caller.

Definition at line 131 of file QmitkPlotWidget.cpp.

Referenced by SetCurveData().

{
  double* raw = new double[ values.size() ];
  for( unsigned int i = 0; i < values.size(); ++i )
    raw[i] = values[i];
  return raw;
}
double * QmitkPlotWidget::ConvertToRawArray ( const XYDataVector values,
unsigned int  component 
) [protected]

Converts the given values into a raw double* array. A new array is allocated via new and must be deleted[] by the caller.

Parameters:
valuesthe x/y values to convert to an array
componentdefines if the x values (0) or the y values(1) should be converted. Other values than 0 and 1 will not be accepted.

Definition at line 140 of file QmitkPlotWidget.cpp.

{
  double* raw = new double[ values.size() ];
  for( unsigned int i = 0; i < values.size(); ++i )
  {
    switch (component) 
    {
    case (0): 
      raw[i] = values[i].first;
      break;
    case (1):
      raw[i] = values[i].second;
      break;
    default:
      std::cout << "Component must be either 0 or 1."<< std::endl;
    }
  }
  return raw;
}
QwtPlot * QmitkPlotWidget::GetPlot (  )

Returns the instance of the plot-widget. This may be used to modify any detail of the appearance of the plot.

Definition at line 39 of file QmitkPlotWidget.cpp.

References m_Plot.

Referenced by QmitkPlotDialog::GetQwtPlot().

{
  return m_Plot;
}
unsigned int QmitkPlotWidget::InsertCurve ( const char *  title )

Inserts a new curve into the plot-window.

Parameters:
titlethe name of the curve
Returns:
the id of the curve. Use this id to refer to the curve, if you want to modify or add data.

Definition at line 45 of file QmitkPlotWidget.cpp.

References QwtPlotItem::attach(), m_Plot, and m_PlotCurveVector.

Referenced by QmitkIGTExampleView::OnShowErrorPlot().

{
  QwtPlotCurve* curve = new QwtPlotCurve(QwtText(title));
  m_PlotCurveVector.push_back(curve);
  curve->attach(m_Plot);
  return static_cast<unsigned int> (m_PlotCurveVector.size() - 1);
}
void QmitkPlotWidget::Replot (  )

Triggers a replot of the curve. Replot should be called once after setting new data.

Definition at line 117 of file QmitkPlotWidget.cpp.

References m_Plot, and QwtPlot::replot().

Referenced by QmitkIGTExampleView::OnErrorValueChanged().

{
  m_Plot->replot();
}
void QmitkPlotWidget::SetAxisTitle ( int  axis,
const char *  title 
)

Sets the title of the given axis. For the set of available axes

See also:
QwtPlot::Axis.
Parameters:
axisthe axis for which the description should be set.
titlethe name of the axis.

Definition at line 58 of file QmitkPlotWidget.cpp.

References m_Plot, and QwtPlot::setAxisTitle().

Referenced by QmitkIGTExampleView::OnShowErrorPlot().

{
  m_Plot->setAxisTitle(axis, title);
}
void QmitkPlotWidget::SetCurveBrush ( unsigned int  curveId,
const QBrush &  brush 
)

Assign a brush, which defines the fill pattern of shapes drawn by a QPainter. In case of brush.style() != QBrush::NoBrush and * style() != QwtPlotCurve::Sticks the area between the curve and the baseline will be filled. In case !brush.color().isValid() the area will be filled by pen.color(). The fill algorithm simply connects the first and the last curve point to the baseline. So the curve data has to be sorted (ascending or descending).

Parameters:
curveIdthe id of the curve for which appearance should be changed
brusha QBrush (
See also:
QBrush) defining the line style

Definition at line 96 of file QmitkPlotWidget.cpp.

References m_PlotCurveVector.

{
  m_PlotCurveVector[curveId]->setBrush( brush );
}
bool QmitkPlotWidget::SetCurveData ( unsigned int  curveId,
const DataVector xValues,
const DataVector yValues 
)

Sets the data for a previously added curve. Data is provided as two vectors of double. The first vector represents the x coordinates, the second vector represents the y coordinates.

Parameters:
curveIdthe id of the curve for which data should be added.
xValuesthe x coordinates of the points that define the curve
yValuesthe y coordinates of the points that define the curve
Returns:
whether data was added successfully or not

Definition at line 64 of file QmitkPlotWidget.cpp.

References ConvertToRawArray(), and m_PlotCurveVector.

Referenced by QmitkIGTExampleView::OnErrorValueChanged().

{
  if ( xValues.size() != yValues.size() )
  {
    std::cerr << "Sizes of data arrays don't match." << std::endl;
    return false;
  }
  double* rawDataX = ConvertToRawArray( xValues );
  double* rawDataY = ConvertToRawArray( yValues );
  m_PlotCurveVector[curveId]->setData( rawDataX, rawDataY, static_cast<int>(xValues.size()) );
  delete[] rawDataX;
  delete[] rawDataY;
  return true;
}
bool QmitkPlotWidget::SetCurveData ( unsigned int  curveId,
const XYDataVector data 
)

Sets the data for a previously added curve. Data is provided as a vectors of pairs. The pairs represent x/y coordinates of the points that define the curve.

Parameters:
curveIdthe id of the curve for which data should be added.
datathe coordinates of the points that define the curve
Returns:
whether data was added successfully or not

Definition at line 80 of file QmitkPlotWidget.cpp.

References ConvertToRawArray(), and m_PlotCurveVector.

{
  double* rawDataX = ConvertToRawArray( data, 0 );
  double* rawDataY = ConvertToRawArray( data, 1 );
  m_PlotCurveVector[curveId]->setData( rawDataX, rawDataY, static_cast<int>(data.size()) );
  delete[] rawDataX;
  delete[] rawDataY;
  return true;
}
void QmitkPlotWidget::SetCurvePen ( unsigned int  curveId,
const QPen &  pen 
)

Defines how a curve should be drawn. For drawing a curve, a QPen is used.

Parameters:
curveIdthe id of the curve for which appearance should be changed
pena QPen (
See also:
QPen) defining the line style

Definition at line 90 of file QmitkPlotWidget.cpp.

References m_PlotCurveVector.

Referenced by QmitkIGTExampleView::OnShowErrorPlot().

{
  m_PlotCurveVector[curveId]->setPen( pen );
}
void QmitkPlotWidget::SetCurveStyle ( unsigned int  curveId,
const QwtPlotCurve::CurveStyle  style 
)

Sets the style how the line is drawn for the curve; like, plain line, or with the data points marked with a symbol;

Parameters:
style A QwtPlotCurve::CurveStyle

Definition at line 107 of file QmitkPlotWidget.cpp.

References m_PlotCurveVector.

{
  m_PlotCurveVector[curveId]->setStyle(style);
}
void QmitkPlotWidget::SetCurveSymbol ( unsigned int  curveId,
QwtSymbol symbol 
)

Sets the style data points are drawn for the curve; like, a line, or dots;

Parameters:
symbol A QwtSymbol

Definition at line 112 of file QmitkPlotWidget.cpp.

References m_PlotCurveVector.

{
  m_PlotCurveVector[curveId]->setSymbol(*symbol);
}
void QmitkPlotWidget::SetCurveTitle ( unsigned int  curveId,
const char *  title 
)

Sets the title of the given curve. The title will be shown in the legend of the QwtPlot.

Parameters:
curveIdthe id of the curve for which the title should be set
titlethe description of the curve that will be shown in the legend.

Definition at line 102 of file QmitkPlotWidget.cpp.

References m_Plot, and QwtPlot::setTitle().

{
  m_Plot->setTitle( title );
}
void QmitkPlotWidget::SetPlotTitle ( const char *  title )

Definition at line 53 of file QmitkPlotWidget.cpp.

References m_Plot, and QwtPlot::setTitle().

Referenced by QmitkIGTExampleView::OnShowErrorPlot().

{
  m_Plot->setTitle(title);
}

Member Data Documentation


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines