]> git.sesse.net Git - kdenlive/blob - src/videoglwidget.cpp
Fix indent
[kdenlive] / src / videoglwidget.cpp
1
2 #include <QtGui>
3 #include <QtOpenGL>
4 #ifdef Q_WS_MAC
5 #include <OpenGL/glu.h>
6 #else
7 #include <GL/glu.h>
8 #endif
9 #include "videoglwidget.h"
10
11 #ifndef GL_TEXTURE_RECTANGLE_EXT
12 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
13 #endif
14
15 VideoGLWidget::VideoGLWidget(QWidget *parent)
16     : QGLWidget(parent)
17     , x(0)
18     , y(0)
19     , w(width())
20     , h(height())
21     , m_image_width(0)
22     , m_image_height(0)
23     , m_texture(0)
24     , m_display_ratio(4.0 / 3.0)
25     , m_backgroundColor(Qt::gray)
26 {  
27     setAttribute(Qt::WA_PaintOnScreen);
28     setAttribute(Qt::WA_OpaquePaintEvent);
29 }
30
31 VideoGLWidget::~VideoGLWidget()
32 {
33     makeCurrent();
34     if (m_texture)
35         glDeleteTextures(1, &m_texture);
36 }
37
38 QSize VideoGLWidget::minimumSizeHint() const
39 {
40     return QSize(40, 30);
41 }
42
43 QSize VideoGLWidget::sizeHint() const
44 {
45     return QSize(400, 300);
46 }
47
48 void VideoGLWidget::setImageAspectRatio(double ratio)
49 {
50     m_display_ratio = ratio;
51     resizeGL(width(), height());
52 }
53
54 void VideoGLWidget::initializeGL()
55 {
56     qglClearColor(m_backgroundColor);
57     glShadeModel(GL_FLAT);
58     glDisable(GL_DEPTH_TEST);
59     glDisable(GL_CULL_FACE);
60     glDisable(GL_LIGHTING);
61     glDisable(GL_DITHER);
62     glDisable(GL_BLEND);
63     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
64 }
65
66 void VideoGLWidget::resizeEvent(QResizeEvent* event)
67 {
68     resizeGL(event->size().width(),event->size().height());
69 }
70
71 void VideoGLWidget::resizeGL(int width, int height)
72 {
73     double this_aspect = (double) width / height;
74
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)) {
78         w = width;
79         h = height;
80     }
81     // Use OpenGL to normalise sample aspect ratio
82     else if (height * m_display_ratio > width) {
83         w = width;
84         h = width / m_display_ratio;
85     } else {
86         w = height * m_display_ratio;
87         h = height;
88     }
89     x = (width - w) / 2;
90     y = (height - h) / 2;
91
92     glViewport(0, 0, width, height);
93     glMatrixMode(GL_PROJECTION);
94     glLoadIdentity();
95     gluOrtho2D(0, width, height, 0);
96     glMatrixMode(GL_MODELVIEW);
97     glClear(GL_COLOR_BUFFER_BIT);
98 }
99
100 void VideoGLWidget::activateMonitor()
101 {
102     makeCurrent();
103     glViewport(0, 0, width(), height());
104     glMatrixMode(GL_PROJECTION);
105     glLoadIdentity();
106     gluOrtho2D(0, width(), height(), 0);
107     glMatrixMode(GL_MODELVIEW);
108     glClear(GL_COLOR_BUFFER_BIT);
109 }
110
111 void VideoGLWidget::paintGL()
112 {
113     if (m_texture) {
114 #ifdef Q_WS_MAC
115                 glClear(GL_COLOR_BUFFER_BIT);
116 #endif
117         glEnable(GL_TEXTURE_RECTANGLE_EXT);
118         glBegin(GL_QUADS);
119         glTexCoord2i(0, 0);
120         glVertex2i(x, y);
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);
127         glEnd();
128         glDisable(GL_TEXTURE_RECTANGLE_EXT);
129     }
130 }
131
132 void VideoGLWidget::showImage(const QImage &image)
133 {
134     m_image_width = image.width();
135     m_image_height = image.height();
136     makeCurrent();
137     if (m_texture)
138         glDeleteTextures(1, &m_texture);
139
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());
147     updateGL();
148 }
149
150 void VideoGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
151 {
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();
158 #else
159         int monitors = QApplication::desktop()->numScreens();
160 #endif
161         if (monitors > 1) {
162             QRect screenres;
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);
167             else
168                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
169             move(QPoint(screenres.x(), screenres.y()));
170             resize(screenres.width(), screenres.height());
171         }
172
173         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
174         flags |= Qt::Window;
175         flags ^= Qt::SubWindow;
176         setWindowFlags(flags);
177 #ifdef Q_WS_X11
178         // This works around a bug with Compiz
179         // as the window must be visible before we can set the state
180         show();
181         raise();
182         setWindowState(windowState() | Qt::WindowFullScreen);   // set
183 #else
184         setWindowState(windowState() | Qt::WindowFullScreen);   // set
185         show();
186 #endif
187     } else {
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
192         show();
193     }
194     event->accept();
195 }
196
197
198 #include "videoglwidget.moc"