Molecule Viewer
 All Classes Functions Variables Enumerations Pages
widget.cpp
1 #include "widget.h"
2 
3 
4 Widget::Widget(QWidget *parent) : QGLWidget(parent) {
5  _initialized = false;
6 }
7 
8 
9 Widget::Widget(QWidget *parent, QGLFormat const &pformat): QGLWidget(pformat, parent) {
10  _initialized = false;
11 }
12 
13 void Widget::setMainApp(AppMain * mainapp) {
14  _mainapp = mainapp;
15  initializeGL();
16 }
17 
19 {
20  if (_initialized)
21  return;
22 
23  makeCurrent();
24 
25  glewExperimental = GL_TRUE;
26  GLenum err = glewInit();
27  if (GLEW_OK != err) {
28  /* Problem: glewInit failed, something is seriously wrong. */
29  qDebug() << reinterpret_cast<const char*>(glewGetErrorString(err));
30  //_errorMessage = reinterpret_cast<const char*>(glewGetErrorString(err));
31  //return false;
32  }
33 
34  glEnable(GL_FRAMEBUFFER_SRGB);
35  glEnable(GL_DEPTH_TEST);
36  glBlendFunc(GL_ONE, GL_ONE);
37  //glBlendEquation(GL_FUNC_ADD);
38  glEnable(GL_TEXTURE_2D);
39 
40  int width=size().width();
41  SetWindowWidth(width);
42 
43  int height=size().height();
44  SetWindowHeight(height);
45 
46  resize (width,height);
47  int occlusionQuality=0;
48  SetOcclusionQuality(occlusionQuality);
49 
50  _mainapp->PrepareScene();
51 
52  _initialized = true;
53 
54 }
55 
57 {
58  _mainapp->DrawScene();
59 }
60 
61 
62 void Widget::resizeGL(int w, int h)
63 {
64 
65  SetWindowHeight(h);
66  SetWindowWidth(w);
67  _mainapp->UpdateWindowSize();
68  updateGL();
69 
70 }
71 
72 
74 {
75 }
76 
77 
78 
79 void Widget::wheelEvent(QWheelEvent *event) {
80 
81  _mainapp->ZoomInOut(-event->delta()/15 );
82  updateGL();
83 
84 }
85 
86 void Widget::mouseMoveEvent(QMouseEvent *event) {
87 
88  int x_new = event->x();
89  int y_new = event->y();
90 
91 
92  if (_mouseLeftPressed)
93  _mainapp->RotateMolecule(0.4*(x_new-_mousePosX),0.4*(y_new-_mousePosY));
94  else if (_mouseRightPressed)
95  _mainapp->MoveLight(0.5*(x_new-_mousePosX),0.5*(y_new-_mousePosY));
96  else if(_mouseMiddlePressed)
97  _mainapp->MoveCamera(-0.2*(x_new-_mousePosX),0.2*(y_new-_mousePosY));
98 
99 
100  _mousePosX = x_new;
101  _mousePosY = y_new;
102 
103  updateGL();
104 
105 }
106 
107 
108 void Widget::mousePressEvent(QMouseEvent *event) {
109 
110 
111  int x_new = event->x();
112  int y_new = event->y();
113  _mousePosX = x_new;
114  _mousePosY = y_new;
115 
116  if (event->button() == Qt::LeftButton)
117  _mouseLeftPressed = true;
118  if (event->button() == Qt::RightButton)
119  _mouseRightPressed = true;
120  if (event->button() == Qt::MiddleButton)
121  _mouseMiddlePressed = true;
122 
123 }
124 void Widget::mouseReleaseEvent( QMouseEvent *event) {
125 
126  if (event->button() == Qt::LeftButton)
127  _mouseLeftPressed = false;
128  if (event->button() == Qt::RightButton)
129  _mouseRightPressed = false;
130  if (event->button() == Qt::MiddleButton)
131  _mouseMiddlePressed = false;
132 
133 }