MNE-CPP  beta 0.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
glwidget.cpp
1 
2 #include <QtWidgets>
3 #include <QtOpenGL>
4 #include <QDebug>
5 #include <qmath.h>
6 
7 #include "glwidget.h"
8 
9 #define LINES_MORE 1
10 #define BOXES_MORE 2
11 
12 GLWidget::GLWidget(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 
18  NumLines = 0;
19  NumBoxes = 0;
20  Onset = 0;
21  xCoordScale = 1;
22  yCoordScale = 1;
23  VScale = 10;
24  xl = -1000.0;
25  xr = 1000.0;
26  yt = 1000.0;
27  yd = -1000.0;
28 
29  NeedBox = false;
30  NeedLabel = false;
31  DragMode = false;
32 }
33 
34 GLWidget::~GLWidget()
35 {
36 }
37 
38 QSize GLWidget::minimumSizeHint() const
39 {
40  return QSize(0, 0);
41 }
42 
43 QSize GLWidget::sizeHint() const
44 {
45  //return QSize(400, 400);
46  //qDebug()<<"GL:Width="<<this->width();
47  //qDebug()<<"GL:Height="<<this->height();
48  return QSize(this->width(), this->height());
49 
50 }
51 
52 void GLWidget::initializeGL()
53 {
54  //qglClearColor(qtPurple.dark());
55  glClearColor(1.0f, 1.0f, 1.0f, 0.0);
56  glLoadIdentity();
57 }
58 
59 void GLWidget::paintGL()
60 {
61  glClear(GL_COLOR_BUFFER_BIT);
62 
63  glPushMatrix(); /* GL_MODELVIEW is default */
64  glScalef(1.0, 1.0, 1.0);
65 
66  glColor3f(0.0f,0.0,0.0);
67  glCallList(LINES_MORE);
68 
69  if (NeedBox){
70  glColor3f(1.0f,0.0,0.0);
71  glCallList(BOXES_MORE);
72  }
73  glPopMatrix();
74 
75  if (NeedLabel){
76  DrawLabels();
77  }
78 }
79 
80 void GLWidget::DrawLabels()
81 {
82  for (int i = 0; i< mlabels.size(); i++)
83  {
84  renderText(xCoordScale*mx[i],yCoordScale*my[i],0.0,mlabels[i]);
85  }
86 }
87 /*
88 void GLWidget::paintEvent(QPaintEvent *)
89 {
90  QPainter painter;
91  painter.begin(this);
92  painter.setRenderHint(QPainter::Antialiasing);
93  helper->paint(&painter);
94  painter.end();
95 }
96 */
97 void GLWidget::SetBoxMode(bool boxmode)
98 {
99  NeedBox = boxmode;
100 }
101 
102 void GLWidget::SetLabelMode(bool labelmode)
103 {
104  NeedLabel = labelmode;
105 }
106 void GLWidget::SetLabels(QList <QString> labels,QVector <float> x,QVector <float> y)
107 {
108  mlabels = labels;
109  mx = x;
110  my = y;
111 }
112 
113 void GLWidget::SetOnset(float ons)
114 {
115  Onset = ons;
116 }
117 
118 void GLWidget::SetXYScales(float xScale, float yScale)
119 {
120  xCoordScale = xScale;
121  yCoordScale = yScale;
122 }
123 
124 void GLWidget::SetVScale(float v)
125 {
126  VScale = v;
127 }
128 
129 void GLWidget::drawBoxes(QVector <float> x, QVector <float> y, float w, float h)
130 {
131  if(x.size()!= y.size()) {NumBoxes = 0; qDebug()<<"Error: box coordinates";}
132 
133  NumBoxes = x.size();
134  qDebug()<<"NumBoxes"<<NumBoxes;
135  if (NumBoxes > 0)
136  {
137  glNewList(BOXES_MORE, GL_COMPILE);
138  for (int chanIndx=0; chanIndx < NumBoxes; chanIndx++)
139  {
140  //qDebug()<<"x,y"<<CoordScale*x[chanIndx]<<CoordScale*y[chanIndx];
141  glBegin(GL_LINE_STRIP);
142  glColor3f(1.0f,0.0,0.0);
143  glVertex2f(xCoordScale*x[chanIndx], yCoordScale*y[chanIndx]); //left top point
144  glVertex2f(xCoordScale*x[chanIndx]+w,yCoordScale*y[chanIndx]); //right top
145  glVertex2f(xCoordScale*x[chanIndx]+w,yCoordScale*y[chanIndx]-h); //right bottom
146  glVertex2f(xCoordScale*x[chanIndx], yCoordScale*y[chanIndx]-h); //left bottom
147  glVertex2f(xCoordScale*x[chanIndx], yCoordScale*y[chanIndx]);
148  glEnd();
149 
150  // horizontal line
151  glBegin(GL_LINE_STRIP);
152  glColor3f(1.0f,0.0,0.0);
153  glVertex2f(xCoordScale*x[chanIndx], yCoordScale*y[chanIndx]-h/2); //left middle
154  glVertex2f(xCoordScale*x[chanIndx]+w,yCoordScale*y[chanIndx]-h/2); //right middle
155  glEnd();
156 
157  //Onset line
158  glBegin(GL_LINE_STRIP);
159  glColor3f(1.0f,0.0,0.0);
160  glVertex2f(xCoordScale*x[chanIndx]+Onset, yCoordScale*y[chanIndx]); //left middle
161  glVertex2f(xCoordScale*x[chanIndx]+Onset,yCoordScale*y[chanIndx]-h); //right middle
162  glEnd();
163  }
164  glLineWidth(4);
165  glBegin(GL_LINE_STRIP);
166  glColor3f(1.0f,0.0,0.0);//xl, xr, yd, yt,
167  glVertex2f(xr,yt); //right top
168  glVertex2f(xr,yt-h); //right bottom
169  glEnd();
170  glLineWidth(1);
171  qDebug()<<"scale mark"<<xl<<w<<yd<<h;
172  glEndList();
173  }
174 }
175 void GLWidget::drawLines(float * samples, int row, int col, int wise_type,
176  QVector <float> x, QVector <float> y, float w, float h)
177 {
178  NumLines = row;
179  //drawOneLine(samples, row, col, wise_type, 0, 0.0, 0.0);
180  //qDebug()<<"col"<<col;
181  //qDebug()<<"row"<<row<<"x size"<<x.size()<<"y size"<<y.size();
182  if((x.size()!=row) && (y.size()!=row)) NumLines = 0;
183  if (NumLines>0){
184  glNewList(LINES_MORE, GL_COMPILE);
185  for (int chanIndx=0; chanIndx < row; chanIndx++)
186  drawOneLine(samples, row, col, wise_type, chanIndx, xCoordScale*x[chanIndx], yCoordScale*y[chanIndx], w, h);// 0.0,chanIndx*10.0);//
187 
188  glEndList();
189  }
190 }
191 
192 void GLWidget::drawOneLine(float * samples, int row, int col, int wise_type, int chanIndx,
193  float x, float y, float w, float h)
194 {
195  // qDebug()<<"drawlines"<<samples[0*row+chanIndx]<<samples[1*row+chanIndx];
196  // qDebug()<<"drawlines1"<<samples[0+chanIndx*col]<<samples[1+chanIndx*col];
197 
198  float wscale = w/col;
199  float hscale = h/VScale;
200  glBegin(GL_LINE_STRIP);
201  if(wise_type==0){ //0 --column wise
202  for(int i=0;i<col;i++){
203  glVertex2f(i*wscale+x,y-h/2+hscale*samples[i*row+chanIndx]);
204  }
205  }
206  else
207  { // 1 -- raw wise
208  for(int i=0;i<col;i++){
209  glVertex2f(i*wscale+x,y-h/2+hscale*samples[i+chanIndx*col]);
210  }
211  }
212  glEnd();
213 }
214 
215 void GLWidget::resizeGL(int width, int height)
216 {
217  glViewport( 0, 0, (GLint)width, (GLint)height );
218  glMatrixMode( GL_PROJECTION );
219  glLoadIdentity();
220  glOrtho( xl, xr, yd, yt, -1.0, 1.0 );
221  glMatrixMode( GL_MODELVIEW );
222 }
223 
224 void GLWidget::SetGLView(float xleft, float xright, float ydown, float ytop)
225 {
226  xl = xleft;
227  xr = xright;
228  yd = ydown;
229  yt = ytop;
230 
231  // reset port view
232  glMatrixMode( GL_PROJECTION );
233  glLoadIdentity();
234  glOrtho( xl, xr, yd, yt, -1.0, 1.0 );
235  glMatrixMode( GL_MODELVIEW );
236 }
237 
238 void GLWidget::mousePressEvent(QMouseEvent *event)
239 {
240  startPos = event->pos();
241  qDebug()<<startPos;
242  DragMode = false;
243 }
244 
245 void GLWidget::mouseMoveEvent(QMouseEvent *event)
246 {
247  MovePos = event->pos();
248  qDebug()<<MovePos;
249  DragMode = true;
250 }
251 void GLWidget::mouseReleaseEvent(QMouseEvent *event)
252 {
253  lastPos = event->pos();
254  qDebug()<<lastPos;
255 
256  if (DragMode){
257  // coordinates transform from 0,0 to center [Screen Coordinates]
258  QPoint CenterPos(this->width()/2,this->height()/2);
259 
260  startPos = startPos - CenterPos;
261  lastPos = lastPos - CenterPos;
262 
263  QPoint NewStartPos(((xr-xl)/this->width())*startPos.x()+(xr+xl)/2, (yt-yd)*startPos.y()/this->height()+(yt+yd)/2);
264 
265  QPoint NewLastPos(((xr-xl)/this->width())*lastPos.x()+(xr+xl)/2, (yt-yd)*lastPos.y()/this->height()+(yt+yd)/2);
266 
267  float xsl = NewStartPos.x();
268  float xsr = NewLastPos.x();
269  float yst = NewStartPos.y();
270  float ysd = NewLastPos.y();
271 
272  if (xsl > xsr) {float t = xsr; xsr = xsl; xsl = t;}
273  if (ysd > yst) {float t = yst; yst = ysd; ysd = t;}
274 
275  qDebug()<<"Width"<<this->width();
276  qDebug()<<xsl<<xsr<<ysd<<yst;
277 
278  SetGLView(xsl, xsr, ysd, yst);
279  updateGL();
280  }
281  DragMode = false;
282 }