aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/src-glwidgets/glw-base.cpp
blob: 4065f70f6bdd7e1287946fb1e9df095b436bc362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===========================================
//  Lumina-desktop source code
//  Copyright (c) 2017, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "glw-base.h"
#include "glw-widget.h"
#include <QPaintEngine>
#include <QDebug>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLPaintDevice>

GLW_Base::GLW_Base(QWidget *parent, Qt::WindowFlags f) : QOpenGLWidget(parent,f){
  bg_color = QColor(Qt::black);
  mouse_over_child = 0;
  qDebug() << "Canvas supports threaded OpenGL:" <<  this->context()->supportsThreadedOpenGL();
  qDebug() << " - globally:" <<  QOpenGLContext::globalShareContext()->supportsThreadedOpenGL();
}

GLW_Base::~GLW_Base(){

}

QWidget * GLW_Base::mouseOverWidget(){
  return mouse_over_child;
}

// --- PUBLIC SLOTS ---
void GLW_Base::setBackgroundColor(QColor color){
  bg_color = color;
  this->update(); //repaint the entire widget (just in case you can see through the image)

}

void GLW_Base::setBackground(QRect geom, QImage img){
  QPainter P(&bg_img);
  P.drawImage(geom, img);
  this->update(geom);
}

void GLW_Base::repaintArea(QRect rect){
  this->update(rect);
}

void GLW_Base::setMouseOverWidget(QWidget *child){
  mouse_over_child = child;
}

// --- PROTECTED ---
void GLW_Base::mouseMoveEvent(QMouseEvent *ev){
  mouse_over_child = 0; //reset this flag - mouse is over the base right now
  QWidget::mouseMoveEvent(ev);
}

void GLW_Base::resizeEvent(QResizeEvent *ev){
  QOpenGLWidget::resizeEvent(ev);
  if(!bg_img.isNull()){
    bg_img = bg_img.scaled(ev->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  }
  emit BaseResized();
}

/*void GLW_Base::paintEvent(QPaintEvent *ev){
  QOpenGLWidget::paintEvent(ev);
}*/

void GLW_Base::paintGL(){

  //Setup the OpenGL stuff
  QOpenGLFunctions *f = this->context()->functions();
  f->glViewport(0, 0, width(), height());
  f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  GLfloat vertices[] = {
      0.0f, 0.707f,
      -0.5f, -0.5f,
      0.5f, -0.5f
  };

  f->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  f->glEnableVertexAttribArray(0);
  f->glDrawArrays(GL_TRIANGLES, 0, 3);
  f->glDisableVertexAttribArray(0);
  glDrawPixels(bg_img.width(), bg_img.height(), GL_RGBA, GL_UNSIGNED_BYTE, bg_img.bits());

  QRect rect = QRect(QPoint(0,0), this->size());
  //Prepare the image to be painted
  QImage img(this->size(), QImage::Format_RGBA8888);
  QPainter painter;
    painter.begin(&img);
    painter.fillRect(rect, bg_color);
  painter.end();

  //Now do any QPainter drawing
  /*QOpenGLPaintDevice device(rect.size());
  QStylePainter painter;
    painter.begin(&device, this);
  //qDebug() << "Paint Engine type:" << painter.paintEngine()->type();
  painter.setClipRegion(rect);
  //Fill in the background color first
  painter.fillRect(rect, bg_color);
  //Now paint any background image over that
  painter.drawImage(rect, bg_img, rect, Qt::AutoColor | Qt::PreferDither | Qt::NoOpaqueDetection);
  //Now find any children widgets and paint them if they are in that area
  QObjectList child = this->children(); //Note: This is returned in stacking order (lowest -> highest)
  for(int i=0; i<child.length(); i++){
    if( !child[i]->isWidgetType() ){ continue; } //not a widget
    GLW_Widget *glww = qobject_cast<GLW_Widget*>(child[i]);
    if(glww!=0){
      if(!rect.intersected(glww->widgetRect()).isNull()){
        glww->paintYourself(&painter, &rect);
        glww->paintChildren(&painter,&rect);
      }
    }
  }
  painter.end();
  update();*/
}
bgstack15