]> git.sesse.net Git - kdenlive/blob - src/videoglwidget.cpp
fixed gl widget on mac (did not resize anymore)
[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::resizeEvent(QResizeEvent* event)
52 {
53     resizeGL(event->size().width(),event->size().height());
54 }
55 void VideoGLWidget::resizeGL(int width, int height)
56 {
57
58     double this_aspect = (double) width / height;
59
60     // Special case optimisation to negate odd effect of sample aspect ratio
61     // not corresponding exactly with image resolution.
62     if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
63         w = width;
64         h = height;
65     }
66     // Use OpenGL to normalise sample aspect ratio
67     else if (height * m_display_ratio > width) {
68         w = width;
69         h = width / m_display_ratio;
70     } else {
71         w = height * m_display_ratio;
72         h = height;
73     }
74     x = (width - w) / 2;
75     y = (height - h) / 2;
76
77     glViewport(0, 0, width, height);
78     glMatrixMode(GL_PROJECTION);
79     glLoadIdentity();
80     gluOrtho2D(0, width, height, 0);
81     glMatrixMode(GL_MODELVIEW);
82     glClear(GL_COLOR_BUFFER_BIT); // | GL_DEPTH_BUFFER_BIT // Depth is disabled, so shouldn'b be necessary to clear DEPTH_BUFFER
83 }
84
85 void VideoGLWidget::paintGL()
86 {
87     if (m_texture) {
88         glEnable(GL_TEXTURE_RECTANGLE_EXT);
89         glBegin(GL_QUADS);
90         glTexCoord2i(0, 0);
91         glVertex2i(x, y);
92         glTexCoord2i(m_image_width, 0);
93         glVertex2i(x + w, y);
94         glTexCoord2i(m_image_width, m_image_height);
95         glVertex2i(x + w, y + h);
96         glTexCoord2i(0, m_image_height);
97         glVertex2i(x, y + h);
98         glEnd();
99         glDisable(GL_TEXTURE_RECTANGLE_EXT);
100     }
101 }
102
103 void VideoGLWidget::showImage(QImage image)
104 {
105     m_image_width = image.width();
106     m_image_height = image.height();
107
108     makeCurrent();
109     if (m_texture)
110         glDeleteTextures(1, &m_texture);
111
112     glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
113     glGenTextures(1, &m_texture);
114     glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
115     glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
116     glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
117     glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGBA,
118                  GL_UNSIGNED_BYTE, image.bits());
119     updateGL();
120 }
121
122 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
123 {
124     // TODO: disable screensaver?
125     Qt::WindowFlags flags = windowFlags();
126     if (!isFullScreen()) {
127         // Check if we ahave a multiple monitor setup
128 #if QT_VERSION >= 0x040600
129         int monitors = QApplication::desktop()->screenCount();
130 #else
131         int monitors = QApplication::desktop()->numScreens();
132 #endif
133         if (monitors > 1) {
134             QRect screenres;
135             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
136             int currentScreen = QApplication::desktop()->screenNumber(this);
137             if (currentScreen < monitors - 1)
138                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
139             else
140                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
141             move(QPoint(screenres.x(), screenres.y()));
142             resize(screenres.width(), screenres.height());
143         }
144
145         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
146         flags |= Qt::Window;
147         flags ^= Qt::SubWindow;
148         setWindowFlags(flags);
149 #ifdef Q_WS_X11
150         // This works around a bug with Compiz
151         // as the window must be visible before we can set the state
152         show();
153         raise();
154         setWindowState(windowState() | Qt::WindowFullScreen);   // set
155 #else
156         setWindowState(windowState() | Qt::WindowFullScreen);   // set
157         show();
158 #endif
159     } else {
160         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
161         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
162         setWindowFlags(flags);
163         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
164         show();
165     }
166     event->accept();
167 }
168