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)
24 , m_display_ratio(4.0 / 3.0)
25 , m_backgroundColor(Qt::gray)
27 setAttribute(Qt::WA_PaintOnScreen);
28 setAttribute(Qt::WA_OpaquePaintEvent);
31 VideoGLWidget::~VideoGLWidget()
35 glDeleteTextures(1, &m_texture);
38 QSize VideoGLWidget::minimumSizeHint() const
43 QSize VideoGLWidget::sizeHint() const
45 return QSize(400, 300);
48 void VideoGLWidget::setImageAspectRatio(double ratio)
50 m_display_ratio = ratio;
51 resizeGL(width(), height());
54 void VideoGLWidget::initializeGL()
56 qglClearColor(m_backgroundColor);
57 glShadeModel(GL_FLAT);
58 glDisable(GL_DEPTH_TEST);
59 glDisable(GL_CULL_FACE);
60 glDisable(GL_LIGHTING);
63 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
66 void VideoGLWidget::resizeEvent(QResizeEvent* event)
68 resizeGL(event->size().width(),event->size().height());
71 void VideoGLWidget::resizeGL(int width, int height)
73 double this_aspect = (double) width / height;
75 // Special case optimisation to negate odd effect of sample aspect ratio
76 // not corresponding exactly with image resolution.
77 if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
81 // Use OpenGL to normalise sample aspect ratio
82 else if (height * m_display_ratio > width) {
84 h = width / m_display_ratio;
86 w = height * m_display_ratio;
92 glViewport(0, 0, width, height);
93 glMatrixMode(GL_PROJECTION);
95 gluOrtho2D(0, width, height, 0);
96 glMatrixMode(GL_MODELVIEW);
97 glClear(GL_COLOR_BUFFER_BIT);
100 void VideoGLWidget::activateMonitor()
103 glViewport(0, 0, width(), height());
104 glMatrixMode(GL_PROJECTION);
106 gluOrtho2D(0, width(), height(), 0);
107 glMatrixMode(GL_MODELVIEW);
108 glClear(GL_COLOR_BUFFER_BIT);
111 void VideoGLWidget::paintGL()
115 glClear(GL_COLOR_BUFFER_BIT);
117 glEnable(GL_TEXTURE_RECTANGLE_EXT);
121 glTexCoord2i(m_image_width, 0);
122 glVertex2i(x + w, y);
123 glTexCoord2i(m_image_width, m_image_height);
124 glVertex2i(x + w, y + h);
125 glTexCoord2i(0, m_image_height);
126 glVertex2i(x, y + h);
128 glDisable(GL_TEXTURE_RECTANGLE_EXT);
132 void VideoGLWidget::showImage(const QImage &image)
134 m_image_width = image.width();
135 m_image_height = image.height();
138 glDeleteTextures(1, &m_texture);
140 glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
141 glGenTextures(1, &m_texture);
142 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
143 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
144 glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
145 glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGB,
146 GL_UNSIGNED_BYTE, image.bits());
150 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
152 // TODO: disable screensaver?
153 Qt::WindowFlags flags = windowFlags();
154 if (!isFullScreen()) {
155 // Check if we ahave a multiple monitor setup
156 #if QT_VERSION >= 0x040600
157 int monitors = QApplication::desktop()->screenCount();
159 int monitors = QApplication::desktop()->numScreens();
163 // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
164 int currentScreen = QApplication::desktop()->screenNumber(this);
165 if (currentScreen < monitors - 1)
166 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
168 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
169 move(QPoint(screenres.x(), screenres.y()));
170 resize(screenres.width(), screenres.height());
173 m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
175 flags ^= Qt::SubWindow;
176 setWindowFlags(flags);
178 // This works around a bug with Compiz
179 // as the window must be visible before we can set the state
182 setWindowState(windowState() | Qt::WindowFullScreen); // set
184 setWindowState(windowState() | Qt::WindowFullScreen); // set
188 flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
189 flags |= m_baseFlags; //then we reset the flags (window and subwindow)
190 setWindowFlags(flags);
191 setWindowState(windowState() ^ Qt::WindowFullScreen); // reset
198 #include "videoglwidget.moc"