Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <qlayout.h>
00019 #include <iostream>
00020
00021 #include "QmitkPlotWidget.h"
00022
00023 QmitkPlotWidget::QmitkPlotWidget(QWidget* parent, const char* title, const char* , Qt::WindowFlags f): QWidget(parent, f)
00024 {
00025 QVBoxLayout* boxLayout = new QVBoxLayout(this);
00026 m_Plot = new QwtPlot( QwtText(title), this ) ;
00027 m_Plot->setCanvasBackground(Qt::white);
00028 boxLayout->addWidget( m_Plot );
00029 }
00030
00031
00032 QmitkPlotWidget::~QmitkPlotWidget()
00033 {
00034 this->Clear();
00035 delete m_Plot;
00036 }
00037
00038
00039 QwtPlot* QmitkPlotWidget::GetPlot()
00040 {
00041 return m_Plot;
00042 }
00043
00044
00045 unsigned int QmitkPlotWidget::InsertCurve(const char* title)
00046 {
00047 QwtPlotCurve* curve = new QwtPlotCurve(QwtText(title));
00048 m_PlotCurveVector.push_back(curve);
00049 curve->attach(m_Plot);
00050 return static_cast<unsigned int> (m_PlotCurveVector.size() - 1);
00051 }
00052
00053 void QmitkPlotWidget::SetPlotTitle(const char* title)
00054 {
00055 m_Plot->setTitle(title);
00056 }
00057
00058 void QmitkPlotWidget::SetAxisTitle(int axis, const char* title)
00059 {
00060 m_Plot->setAxisTitle(axis, title);
00061 }
00062
00063
00064 bool QmitkPlotWidget::SetCurveData( unsigned int curveId, const QmitkPlotWidget::DataVector& xValues, const QmitkPlotWidget::DataVector& yValues )
00065 {
00066 if ( xValues.size() != yValues.size() )
00067 {
00068 std::cerr << "Sizes of data arrays don't match." << std::endl;
00069 return false;
00070 }
00071 double* rawDataX = ConvertToRawArray( xValues );
00072 double* rawDataY = ConvertToRawArray( yValues );
00073 m_PlotCurveVector[curveId]->setData( rawDataX, rawDataY, static_cast<int>(xValues.size()) );
00074 delete[] rawDataX;
00075 delete[] rawDataY;
00076 return true;
00077 }
00078
00079
00080 bool QmitkPlotWidget::SetCurveData( unsigned int curveId, const QmitkPlotWidget::XYDataVector& data )
00081 {
00082 double* rawDataX = ConvertToRawArray( data, 0 );
00083 double* rawDataY = ConvertToRawArray( data, 1 );
00084 m_PlotCurveVector[curveId]->setData( rawDataX, rawDataY, static_cast<int>(data.size()) );
00085 delete[] rawDataX;
00086 delete[] rawDataY;
00087 return true;
00088 }
00089
00090 void QmitkPlotWidget::SetCurvePen( unsigned int curveId, const QPen& pen )
00091 {
00092 m_PlotCurveVector[curveId]->setPen( pen );
00093 }
00094
00095
00096 void QmitkPlotWidget::SetCurveBrush( unsigned int curveId, const QBrush& brush )
00097 {
00098 m_PlotCurveVector[curveId]->setBrush( brush );
00099 }
00100
00101
00102 void QmitkPlotWidget::SetCurveTitle( unsigned int , const char* title )
00103 {
00104 m_Plot->setTitle( title );
00105 }
00106
00107 void QmitkPlotWidget::SetCurveStyle( unsigned int curveId, const QwtPlotCurve::CurveStyle style )
00108 {
00109 m_PlotCurveVector[curveId]->setStyle(style);
00110 }
00111
00112 void QmitkPlotWidget::SetCurveSymbol( unsigned int curveId, QwtSymbol* symbol )
00113 {
00114 m_PlotCurveVector[curveId]->setSymbol(*symbol);
00115 }
00116
00117 void QmitkPlotWidget::Replot()
00118 {
00119 m_Plot->replot();
00120 }
00121
00122
00123 void QmitkPlotWidget::Clear()
00124 {
00125 m_PlotCurveVector.clear();
00126 m_PlotCurveVector.resize(0);
00127 m_Plot->clear();
00128 }
00129
00130
00131 double* QmitkPlotWidget::ConvertToRawArray( const QmitkPlotWidget::DataVector& values )
00132 {
00133 double* raw = new double[ values.size() ];
00134 for( unsigned int i = 0; i < values.size(); ++i )
00135 raw[i] = values[i];
00136 return raw;
00137 }
00138
00139
00140 double* QmitkPlotWidget::ConvertToRawArray( const QmitkPlotWidget::XYDataVector& values, unsigned int component )
00141 {
00142 double* raw = new double[ values.size() ];
00143 for( unsigned int i = 0; i < values.size(); ++i )
00144 {
00145 switch (component)
00146 {
00147 case (0):
00148 raw[i] = values[i].first;
00149 break;
00150 case (1):
00151 raw[i] = values[i].second;
00152 break;
00153 default:
00154 std::cout << "Component must be either 0 or 1."<< std::endl;
00155 }
00156 }
00157 return raw;
00158 }