MNE-CPP  beta 0.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
glwidget_OnDisp.cpp
1 
2 #include <QtWidgets>
3 #include <QtOpenGL>
4 #include <QDebug>
5 #include <qmath.h>
6 
7 #include "glwidget_OnDisp.h"
8 
9 #define LINES_BUTT 3
10 
11 
12 GLWidget_OnDisp::GLWidget_OnDisp(QWidget *parent)
13  : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
14 {
15  qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
16  qtPurple = QColor::fromCmykF(0.0, 0.0, 0.0, 0.0);
17  xl = -200.0;
18  xr = 200.0;
19  yt = 200.0;
20  yd = -200.0;
21  xCoordScale = 1.0;
22  yCoordScale = 1.0;
23  m_row = 0;
24  m_col = 0;
25  linePlot = false;
26  axPlot = false;
27 }
28 
29 GLWidget_OnDisp::~GLWidget_OnDisp()
30 {
31 }
32 
33 QSize GLWidget_OnDisp::minimumSizeHint() const
34 {
35  return QSize(0, 0);
36 }
37 
38 QSize GLWidget_OnDisp::sizeHint() const
39 {
40  //return QSize(400, 400);
41  qDebug()<<"GL:Width="<<this->width();
42  qDebug()<<"GL:Height="<<this->height();
43  return QSize(this->width(), this->height());
44 
45 }
46 
47 void GLWidget_OnDisp::initializeGL()
48 {
49  //qglClearColor(qtPurple.dark());
50  glClearColor(1.0f, 1.0f,1.0f, 0.0);
51  glLoadIdentity();
52 }
53 
54 void GLWidget_OnDisp::paintGL()
55 {
56  glClear(GL_COLOR_BUFFER_BIT);
57 
58  glPushMatrix(); /* GL_MODELVIEW is default */
59  glScalef(1.0, 1.0, 1.0);
60 
61  /*
62  // horizontal line
63  glBegin(GL_LINE_STRIP);
64  glColor3f(0.0f,0.0,0.0);
65  glVertex2f(100, 100); //left middle
66  glVertex2f(-100,-100); //right middle
67  glEnd();
68  */
69  if (axPlot)
70  {
71  glColor3f(0.0f,0.0f,0.0f);
72  drawAx(m_x, m_x+m_w, m_y, m_y-m_h);
73  }
74  if (linePlot)
75  {
76  glColor3f(0.0f,0.0f,1.0f);
77  //glCallList(LINES_BUTT);
78  drawLines(m_samples, m_row, m_col, m_wise_type, m_x, m_y, m_w, m_h);
79  }
80  glPopMatrix();
81 }
82 
83 void GLWidget_OnDisp::resizeGL(int width, int height)
84 {
85  glViewport( 0, 0, (GLint)width, (GLint)height );
86  glMatrixMode( GL_PROJECTION );
87  glLoadIdentity();
88  glOrtho( xl, xr, yd, yt, -1.0, 1.0 );
89  glMatrixMode( GL_MODELVIEW );
90 }
91 
92 void GLWidget_OnDisp::SetGLView(float xleft, float xright, float ydown, float ytop)
93 {
94  xl = xleft;
95  xr = xright;
96  yd = ydown;
97  yt = ytop;
98 
99  // reset port view
100  glMatrixMode( GL_PROJECTION );
101  glLoadIdentity();
102  glOrtho( xl, xr, yd, yt, -1.0, 1.0 );
103  glMatrixMode( GL_MODELVIEW );
104 }
105 void GLWidget_OnDisp::SetdrawLines(MatrixXf tmp,int wise_type, int chnind, float x, float y, float w, float h)
106 {
107  Q_UNUSED(x);
108  Q_UNUSED(y);
109  Q_UNUSED(w);
110  Q_UNUSED(h);
111 
112  m_tmp = tmp;
113  m_row = m_tmp.rows();
114  m_col = m_tmp.cols();
115 
116  m_chnind = chnind;
117 
118  //set the opengl window according to the current data
119  float xleft = -200;
120  float xright = 200;
121  float ydown = -150;//m_tmp(chnind,0) - 2*m_tmp(chnind,0);
122  float ytop = 150;//m_tmp(chnind,0) + 2*m_tmp(chnind,0);
123 
124  SetGLView(xleft, xright, ydown, ytop);
125 
126  m_wise_type = wise_type;
127  m_x = -150;//x;
128  m_y = 100;//ytop-m_tmp(chnind,0);//y;
129  m_w = 300;
130  m_h = 200;//2*m_tmp(chnind,0);
131 
132  for(int i = 0; i<m_col;i++)
133  m_tmp(chnind,i) -= m_tmp(chnind,0);
134 
135  m_samples = m_tmp.data();
136 
137 
138  linePlot = true;
139  axPlot = true;
140  updateGL();
141 }
142 void GLWidget_OnDisp::SetChnInd(int chnind)
143 {
144  m_chnind = chnind;
145  qDebug()<<"GLWidget_OnDisp:chnind : "<<chnind;
146  if (m_row == 0)
147  linePlot = false;
148  else
149  {
150  linePlot = true;
151  updateGL();
152  }
153 }
154 void GLWidget_OnDisp::drawAx(float xleft, float xright, float ytop, float ydown)
155 {
156 
157  glLineWidth(2.0f);
158  glBegin(GL_LINE_STRIP);
159  glVertex2f(xleft, ytop);
160  glVertex2f(xright, ytop);
161  glVertex2f(xright, ydown);
162  glVertex2f(xleft, ydown);
163  glVertex2f(xleft, ytop);
164  glEnd();
165  glLineWidth(1.0f);
166 
167  glBegin(GL_LINES);
168 
169  glVertex2f(xleft, ytop-(ytop-ydown)/3);
170  glVertex2f(xleft+(xright-xleft)/20, ytop-(ytop-ydown)/3);
171 
172  glVertex2f(xleft, ytop-2*(ytop-ydown)/3);
173  glVertex2f(xleft+(xright-xleft)/20, ytop-2*(ytop-ydown)/3);
174 
175  glVertex2f(xleft+(xright-xleft)/3, ydown);
176  glVertex2f(xleft+(xright-xleft)/3, ydown+(ytop-ydown)/20);
177 
178  glVertex2f(xleft+2*(xright-xleft)/3, ydown);
179  glVertex2f(xleft+2*(xright-xleft)/3, ydown+(ytop-ydown)/20);
180 
181  glEnd();
182 
183 }
184 
185 void GLWidget_OnDisp::drawLines(float * samples, int row, int col, int wise_type,
186  float x, float y, float w, float h)
187 {
188  NumLines = row;
189  if (NumLines>0){
190  //glNewList(LINES_BUTT, GL_COMPILE);
191  int chanIndx = m_chnind;
192  //for (int chanIndx=0; chanIndx < row; chanIndx++)
193  //qDebug()<<"GLWidget_OnDisp:drawLines (chnind) : "<<m_chnind;
194 
195  drawOneLine(samples, row, col, wise_type, chanIndx, xCoordScale*x, yCoordScale*y, w, h);
196  //glEndList();
197  }
198 }
199 void GLWidget_OnDisp::drawOneLine(float * samples, int row, int col, int wise_type, int chanIndx,
200  float x, float y, float w, float h)
201 {
202 
203  float wscale = w/col;
204  float hscale = 2;//h/10;
205 
206  glBegin(GL_LINE_STRIP);
207  if(wise_type==0){ //0 --column wise
208  for(int i=0;i<col;i++){
209  glVertex2f(i*wscale+x,hscale*(y-h/2+samples[i*row+chanIndx]));
210  }
211  }
212  else
213  { // 1 -- raw wise
214  for(int i=0;i<col;i++){
215  glVertex2f(i*wscale+x,hscale*(y-h/2+samples[i+chanIndx*col]));
216  }
217  }
218  glEnd();
219 }