5 #include <OpenGL/glu.h>
9 #include "videoglwidget.h"
11 #ifndef GL_TEXTURE_RECTANGLE_EXT
12 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
15 VideoGLWidget::VideoGLWidget(QWidget *parent)
20 , m_display_ratio(4.0 / 3.0)
21 , m_backgroundColor(Qt::gray)
23 setAttribute(Qt::WA_PaintOnScreen);
24 setAttribute(Qt::WA_OpaquePaintEvent);
27 VideoGLWidget::~VideoGLWidget()
31 glDeleteTextures(1, &m_texture);
34 QSize VideoGLWidget::minimumSizeHint() const
39 QSize VideoGLWidget::sizeHint() const
41 return QSize(400, 300);
44 void VideoGLWidget::setImageAspectRatio(double ratio)
46 m_display_ratio = ratio;
47 resizeGL(width(), height());
50 void VideoGLWidget::initializeGL()
52 qglClearColor(m_backgroundColor);
53 glShadeModel(GL_FLAT);
54 glDisable(GL_DEPTH_TEST);
55 glDisable(GL_CULL_FACE);
56 glDisable(GL_LIGHTING);
59 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
62 void VideoGLWidget::resizeEvent(QResizeEvent* event)
64 resizeGL(event->size().width(),event->size().height());
67 void VideoGLWidget::resizeGL(int width, int height)
69 double this_aspect = (double) width / height;
71 // Special case optimisation to negate odd effect of sample aspect ratio
72 // not corresponding exactly with image resolution.
73 if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
77 // Use OpenGL to normalise sample aspect ratio
78 else if (height * m_display_ratio > width) {
80 h = width / m_display_ratio;
82 w = height * m_display_ratio;
88 glViewport(0, 0, width, height);
89 glMatrixMode(GL_PROJECTION);
91 gluOrtho2D(0, width, height, 0);
92 glMatrixMode(GL_MODELVIEW);
93 glClear(GL_COLOR_BUFFER_BIT);
96 void VideoGLWidget::activateMonitor()
99 glViewport(0, 0, width(), height());
100 glMatrixMode(GL_PROJECTION);
102 gluOrtho2D(0, width(), height(), 0);
103 glMatrixMode(GL_MODELVIEW);
104 glClear(GL_COLOR_BUFFER_BIT);
107 void VideoGLWidget::paintGL()
111 glClear(GL_COLOR_BUFFER_BIT);
113 glEnable(GL_TEXTURE_RECTANGLE_EXT);
117 glTexCoord2i(m_image_width, 0);
118 glVertex2i(x + w, y);
119 glTexCoord2i(m_image_width, m_image_height);
120 glVertex2i(x + w, y + h);
121 glTexCoord2i(0, m_image_height);
122 glVertex2i(x, y + h);
124 glDisable(GL_TEXTURE_RECTANGLE_EXT);
128 void VideoGLWidget::showImage(QImage image)
130 m_image_width = image.width();
131 m_image_height = image.height();
134 glDeleteTextures(1, &m_texture);
136 glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
137 glGenTextures(1, &m_texture);
138 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
139 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140 glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
141 glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGBA,
142 GL_UNSIGNED_BYTE, image.bits());
146 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
148 // TODO: disable screensaver?
149 Qt::WindowFlags flags = windowFlags();
150 if (!isFullScreen()) {
151 // Check if we ahave a multiple monitor setup
152 #if QT_VERSION >= 0x040600
153 int monitors = QApplication::desktop()->screenCount();
155 int monitors = QApplication::desktop()->numScreens();
159 // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
160 int currentScreen = QApplication::desktop()->screenNumber(this);
161 if (currentScreen < monitors - 1)
162 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
164 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
165 move(QPoint(screenres.x(), screenres.y()));
166 resize(screenres.width(), screenres.height());
169 m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
171 flags ^= Qt::SubWindow;
172 setWindowFlags(flags);
174 // This works around a bug with Compiz
175 // as the window must be visible before we can set the state
178 setWindowState(windowState() | Qt::WindowFullScreen); // set
180 setWindowState(windowState() | Qt::WindowFullScreen); // set
184 flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
185 flags |= m_baseFlags; //then we reset the flags (window and subwindow)
186 setWindowFlags(flags);
187 setWindowState(windowState() ^ Qt::WindowFullScreen); // reset