MNE-CPP  beta 1.0
plot.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "plot.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // Qt INCLUDES
48 //=============================================================================================================
49 
50 #include <QPoint>
51 #include <QPainter>
52 
53 
54 //*************************************************************************************************************
55 //=============================================================================================================
56 // USED NAMESPACES
57 //=============================================================================================================
58 
59 using namespace DISPLIB;
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // DEFINE MEMBER METHODS
65 //=============================================================================================================
66 
67 Plot::Plot(QWidget *parent)
68 : Graph(parent)
69 , m_bHoldOn(false)
70 {
71  init();
72 }
73 
74 
75 //*************************************************************************************************************
76 
77 Plot::Plot(VectorXd &p_dVec, QWidget *parent)
78 : Graph(parent)
79 , m_bHoldOn(false)
80 {
81  init();
82  updateData(p_dVec);
83 }
84 
85 
86 //*************************************************************************************************************
87 
89 {
90 
91 }
92 
93 
94 //*************************************************************************************************************
95 
96 void Plot::init()
97 {
98  //Parent init
99  Graph::init();
100 
101  m_qListVecPointFPaths.clear();
102 
103  m_dMinX = 0;
104  m_dMaxX = 0;
105  m_dMinY = 0;
106  m_dMaxY = 0;
107 }
108 
109 
110 //*************************************************************************************************************
111 
112 void Plot::updateData(VectorXd &p_dVec)
113 {
114  if(p_dVec.size() > 0)
115  {
116  if(!m_bHoldOn)
117  init();
118 
119  QVector<QPointF> t_qVecPointFPaths;
120  //No X data given
121  m_dMinX = 0 < m_dMinX ? 0 : m_dMinX;
122  m_dMaxX = p_dVec.size()-1 > m_dMaxX ? p_dVec.size()-1 : m_dMaxX;
123 
124  m_dMinY = p_dVec.minCoeff() < m_dMinY ? p_dVec.minCoeff() : m_dMinY;
125  m_dMaxY = p_dVec.maxCoeff() > m_dMaxY ? p_dVec.maxCoeff() : m_dMaxY;
126 
127  double t_dX = 0;
128  for(qint32 i = 0; i < p_dVec.size(); ++i)
129  {
130  t_qVecPointFPaths.append(QPointF(t_dX, p_dVec[i]));
131  t_dX += 1;
132  }
133 
134  m_qListVecPointFPaths.append(t_qVecPointFPaths);
135 
136  update();
137  }
138 }
139 
140 
141 //*************************************************************************************************************
142 
143 void Plot::paintEvent(QPaintEvent *)
144 {
145  QPainter painter(this);
146  if (m_qListVecPointFPaths.size() > 0)
147  {
148  QPoint t_qPointTopLeft(m_iBorderLeftRight,m_iBorderTopBottom);
149 
150 
151  QSize t_qSizePlot = m_qSizeWidget;
152 
153  t_qSizePlot.setWidth(t_qSizePlot.width() - 2*m_iBorderLeftRight);
154  t_qSizePlot.setHeight(t_qSizePlot.height() - 2*m_iBorderTopBottom);
155 
156  //Draw background
157  QPainter painter(this);
158  painter.fillRect(t_qPointTopLeft.x(), t_qPointTopLeft.y(), t_qSizePlot.width(), t_qSizePlot.height(), Qt::white);
159 
160  //Draw border
161  painter.drawRect(t_qPointTopLeft.x()-1, t_qPointTopLeft.y()-1, t_qSizePlot.width()+1, t_qSizePlot.height()+1);
162 
163  // -- Data --
164  painter.save();
165  QPen pen;
166  pen.setWidth(1);
167  pen.setBrush(Qt::blue);
168  painter.setPen(pen);
169  painter.translate(m_iBorderLeftRight-m_dMinX,m_iBorderTopBottom+t_qSizePlot.height()/2);
170  for(qint32 i = 0; i < m_qListVecPointFPaths.size(); ++i)
171  {
172  double scale_x = t_qSizePlot.width()/(m_dMaxX - m_dMinX);
173  double scale_y = (t_qSizePlot.height()-(t_qSizePlot.height()*0.1))/(m_dMaxY - m_dMinY);
174 
175  //scale
176  QVector<QPointF> t_qVecPointFPath;
177  QVector<QPointF>::ConstIterator it;
178  for(it = m_qListVecPointFPaths[i].begin(); it != m_qListVecPointFPaths[i].end(); ++it)
179  t_qVecPointFPath.append(QPointF(it->x()*scale_x, it->y()*scale_y));
180 
181  //draw
182  for(it = t_qVecPointFPath.begin()+1; it != t_qVecPointFPath.end(); ++it)
183  painter.drawLine(*(it-1), *it);
184  }
185  painter.restore();
186 
187 
188  //Draw title & axes
189  Graph::drawLabels(t_qSizePlot.width(), t_qSizePlot.height());
190  }
191 
192 }
193 
double m_dMinY
Definition: plot.h:150
bool m_bHoldOn
Definition: plot.h:144
qint32 m_iBorderLeftRight
Definition: graph.h:138
Plot class declaration.
qint32 m_iBorderTopBottom
Definition: graph.h:137
void updateData(VectorXd &p_dVec)
Definition: plot.cpp:112
Plot(QWidget *parent=0)
Definition: plot.cpp:67
Base class for graphs.
Definition: graph.h:95
double m_dMaxY
Definition: plot.h:151
void init()
Definition: plot.cpp:96
QSize m_qSizeWidget
Definition: graph.h:131
double m_dMinX
Definition: plot.h:148
double m_dMaxX
Definition: plot.h:149