1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
3 * 2014 by Jean-Nicolas Artaud (jeannicolasartaud@gmail.com) *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
24 #include <OpenGL/glu.h>
28 #include "widgets/videoglwidget.h"
30 #ifndef GL_TEXTURE_RECTANGLE_EXT
31 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
34 VideoGLWidget::VideoGLWidget(QWidget *parent, QGLWidget *share)
35 : QGLWidget(parent, share)
44 , m_display_ratio(4.0 / 3.0)
45 , m_backgroundColor(Qt::gray)
47 setAttribute(Qt::WA_PaintOnScreen);
48 setAttribute(Qt::WA_OpaquePaintEvent);
51 VideoGLWidget::~VideoGLWidget()
55 glDeleteTextures(1, &m_texture);
58 QSize VideoGLWidget::minimumSizeHint() const
63 QSize VideoGLWidget::sizeHint() const
65 return QSize(400, 300);
68 void VideoGLWidget::setImageAspectRatio(double ratio)
70 m_display_ratio = ratio;
71 resizeGL(width(), height());
74 void VideoGLWidget::initializeGL()
76 qglClearColor(m_backgroundColor);
77 glShadeModel(GL_FLAT);
78 glDisable(GL_DEPTH_TEST);
79 glDisable(GL_CULL_FACE);
80 glDisable(GL_LIGHTING);
83 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86 void VideoGLWidget::resizeEvent(QResizeEvent* event)
88 resizeGL(event->size().width(),event->size().height());
91 void VideoGLWidget::resizeGL(int width, int height)
93 double this_aspect = (double) width / height;
95 // Special case optimisation to negate odd effect of sample aspect ratio
96 // not corresponding exactly with image resolution.
97 if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
101 // Use OpenGL to normalise sample aspect ratio
102 else if (height * m_display_ratio > width) {
104 h = width / m_display_ratio;
106 w = height * m_display_ratio;
110 y = (height - h) / 2;
112 glViewport(0, 0, width, height);
113 glMatrixMode(GL_PROJECTION);
115 gluOrtho2D(0, width, height, 0);
116 glMatrixMode(GL_MODELVIEW);
117 glClear(GL_COLOR_BUFFER_BIT);
120 void VideoGLWidget::activateMonitor()
123 glViewport(0, 0, width(), height());
124 glMatrixMode(GL_PROJECTION);
126 gluOrtho2D(0, width(), height(), 0);
127 glMatrixMode(GL_MODELVIEW);
128 glClear(GL_COLOR_BUFFER_BIT);
131 void VideoGLWidget::paintGL()
135 glClear(GL_COLOR_BUFFER_BIT);
137 glEnable(GL_TEXTURE_RECTANGLE_EXT);
141 glTexCoord2i(m_image_width, 0);
142 glVertex2i(x + w, y);
143 glTexCoord2i(m_image_width, m_image_height);
144 glVertex2i(x + w, y + h);
145 glTexCoord2i(0, m_image_height);
146 glVertex2i(x, y + h);
148 glDisable(GL_TEXTURE_RECTANGLE_EXT);
150 if (m_other_texture) {
152 glClear(GL_COLOR_BUFFER_BIT);
154 glEnable(GL_TEXTURE_2D);
155 glBindTexture(GL_TEXTURE_2D, m_other_texture);
160 glVertex2i(x + w, y);
162 glVertex2i(x + w, y + h);
164 glVertex2i(x, y + h);
166 glDisable(GL_TEXTURE_2D);
170 void VideoGLWidget::showImage(const QImage &image)
172 m_image_width = image.width();
173 m_image_height = image.height();
176 glDeleteTextures(1, &m_texture);
179 glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
180 glGenTextures(1, &m_texture);
181 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
182 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
183 glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
184 glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGB,
185 GL_UNSIGNED_BYTE, image.bits());
189 void VideoGLWidget::showImage(GLuint texnum)
193 glDeleteTextures(1, &m_texture);
196 m_other_texture = texnum;
201 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
203 // TODO: disable screensaver?
204 Qt::WindowFlags flags = windowFlags();
205 if (!isFullScreen()) {
206 // Check if we ahave a multiple monitor setup
207 #if QT_VERSION >= 0x040600
208 int monitors = QApplication::desktop()->screenCount();
210 int monitors = QApplication::desktop()->numScreens();
214 // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
215 int currentScreen = QApplication::desktop()->screenNumber(this);
216 if (currentScreen < monitors - 1)
217 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
219 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
220 move(QPoint(screenres.x(), screenres.y()));
221 resize(screenres.width(), screenres.height());
224 m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
226 flags ^= Qt::SubWindow;
227 setWindowFlags(flags);
229 // This works around a bug with Compiz
230 // as the window must be visible before we can set the state
233 setWindowState(windowState() | Qt::WindowFullScreen); // set
235 setWindowState(windowState() | Qt::WindowFullScreen); // set
239 flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
240 flags |= m_baseFlags; //then we reset the flags (window and subwindow)
241 setWindowFlags(flags);
242 setWindowState(windowState() ^ Qt::WindowFullScreen); // reset
249 #include "videoglwidget.moc"