]> git.sesse.net Git - kdenlive/blob - src/videoglwidget.cpp
e3b0c7506ad071a9b72cd25cb5561dd394cad292
[kdenlive] / src / videoglwidget.cpp
1
2 #include <QtGui>
3 #include <QtOpenGL>
4 #include "videoglwidget.h"
5
6 #ifndef GL_TEXTURE_RECTANGLE_EXT
7 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
8 #endif
9
10 VideoGLWidget::VideoGLWidget(QWidget *parent)
11     : QGLWidget(parent)
12     , m_image_width(0)
13     , m_image_height(0)
14     , m_texture(0)
15     , m_display_ratio(4.0 / 3.0)
16     , m_backgroundColor(Qt::gray)
17 {
18     setAttribute(Qt::WA_PaintOnScreen);
19     setAttribute(Qt::WA_OpaquePaintEvent);
20 }
21
22 VideoGLWidget::~VideoGLWidget()
23 {
24     makeCurrent();
25     if (m_texture)
26         glDeleteTextures(1, &m_texture);
27 }
28
29 QSize VideoGLWidget::minimumSizeHint() const
30 {
31     return QSize(40, 30);
32 }
33
34 QSize VideoGLWidget::sizeHint() const
35 {
36     return QSize(400, 300);
37 }
38
39 void VideoGLWidget::initializeGL()
40 {
41     qglClearColor(m_backgroundColor);
42     glShadeModel(GL_FLAT);
43     glDisable(GL_DEPTH_TEST);
44     glDisable(GL_CULL_FACE);
45     glDisable(GL_LIGHTING);
46     glDisable(GL_DITHER);
47     glDisable(GL_BLEND);
48     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
49 }
50
51 void VideoGLWidget::resizeGL(int width, int height)
52 {
53     double this_aspect = (double) width / height;
54
55     // Special case optimisation to negate odd effect of sample aspect ratio
56     // not corresponding exactly with image resolution.
57     if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
58         w = width;
59         h = height;
60     }
61     // Use OpenGL to normalise sample aspect ratio
62     else if (height * m_display_ratio > width) {
63         w = width;
64         h = width / m_display_ratio;
65     } else {
66         w = height * m_display_ratio;
67         h = height;
68     }
69     x = (width - w) / 2;
70     y = (height - h) / 2;
71
72     glViewport(0, 0, width, height);
73     glMatrixMode(GL_PROJECTION);
74     glLoadIdentity();
75     gluOrtho2D(0, width, height, 0);
76     glMatrixMode(GL_MODELVIEW);
77     glClear(GL_COLOR_BUFFER_BIT); // | GL_DEPTH_BUFFER_BIT // Depth is disabled, so shouldn'b be necessary to clear DEPTH_BUFFER
78 }
79
80 void VideoGLWidget::paintGL()
81 {
82     if (m_texture) {
83         glEnable(GL_TEXTURE_RECTANGLE_EXT);
84         glBegin(GL_QUADS);
85         glTexCoord2i(0, 0);
86         glVertex2i(x, y);
87         glTexCoord2i(m_image_width, 0);
88         glVertex2i(x + w, y);
89         glTexCoord2i(m_image_width, m_image_height);
90         glVertex2i(x + w, y + h);
91         glTexCoord2i(0, m_image_height);
92         glVertex2i(x, y + h);
93         glEnd();
94         glDisable(GL_TEXTURE_RECTANGLE_EXT);
95     }
96 }
97
98 void VideoGLWidget::showImage(QImage image)
99 {
100     m_image_width = image.width();
101     m_image_height = image.height();
102
103     makeCurrent();
104     if (m_texture)
105         glDeleteTextures(1, &m_texture);
106
107     glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
108     glGenTextures(1, &m_texture);
109     glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
110     glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
111     glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112     glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGBA,
113                  GL_UNSIGNED_BYTE, image.bits());
114     updateGL();
115 }
116
117 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
118 {
119     // TODO: disable screensaver?
120     Qt::WindowFlags flags = windowFlags();
121     if (!isFullScreen()) {
122         // Check if we ahave a multiple monitor setup
123         int monitors = QApplication::desktop()->screenCount();
124         if (monitors > 1) {
125             QRect screenres;
126             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
127             int currentScreen = QApplication::desktop()->screenNumber(this);
128             if (currentScreen < monitors - 1)
129                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
130             else
131                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
132             move(QPoint(screenres.x(), screenres.y()));
133             resize(screenres.width(), screenres.height());
134         }
135
136         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
137         flags |= Qt::Window;
138         flags ^= Qt::SubWindow;
139         setWindowFlags(flags);
140 #ifdef Q_WS_X11
141         // This works around a bug with Compiz
142         // as the window must be visible before we can set the state
143         show();
144         raise();
145         setWindowState(windowState() | Qt::WindowFullScreen);   // set
146 #else
147         setWindowState(windowState() | Qt::WindowFullScreen);   // set
148         show();
149 #endif
150     } else {
151         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
152         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
153         setWindowFlags(flags);
154         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
155         show();
156     }
157     event->accept();
158 }
159