]> git.sesse.net Git - kdenlive/blob - src/abstractmonitor.cpp
Better to use nepomukcore
[kdenlive] / src / abstractmonitor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "abstractmonitor.h"
22 #include "kdenlivesettings.h"
23 #include "monitormanager.h"
24
25 #include <KDebug>
26
27 #include <QPaintEvent>
28 #include <QDesktopWidget>
29 #include <QVBoxLayout>
30
31
32 AbstractMonitor::AbstractMonitor(Kdenlive::MONITORID id, MonitorManager *manager, QWidget *parent): 
33     QWidget(parent),
34     videoSurface(NULL),
35     m_id(id),
36     m_monitorManager(manager)
37 {
38     videoBox = new VideoContainer(this);
39 }
40
41
42 AbstractMonitor::~AbstractMonitor()
43 {
44     delete videoSurface;
45 }
46
47 void AbstractMonitor::createVideoSurface()
48 {
49     QVBoxLayout *lay = new QVBoxLayout;
50     lay->setContentsMargins(0, 0, 0, 0);
51     videoSurface = new VideoSurface;
52     lay->addWidget(videoSurface);
53     videoBox->setLayout(lay);
54 }
55
56 bool AbstractMonitor::isActive() const
57 {
58     return m_monitorManager->isActive(m_id);
59 }
60
61 bool AbstractMonitor::slotActivateMonitor(bool forceRefresh)
62 {
63     return m_monitorManager->activateMonitor(m_id, forceRefresh);
64 }
65
66 VideoContainer::VideoContainer(AbstractMonitor* monitor, QWidget *parent) :
67     QFrame(parent)
68     , m_monitor(monitor)
69 {
70     setFrameShape(QFrame::NoFrame);
71     setFocusPolicy(Qt::ClickFocus);
72     //setEnabled(false);
73     setContentsMargins(0, 0, 0, 0);
74     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
75 }
76
77 // virtual
78 void VideoContainer::mouseReleaseEvent(QMouseEvent * event)
79 {
80     if (event->button() != Qt::RightButton) {
81         if (m_monitor->isActive()) {
82             m_monitor->slotPlay();
83         }
84     }
85 }
86
87 // virtual
88 void VideoContainer::keyPressEvent(QKeyEvent *event)
89 {
90     // Exit fullscreen with Esc key
91     if (event->key() == Qt::Key_Escape && isFullScreen()) {
92         switchFullScreen();
93         event->setAccepted(true);
94     } else event->setAccepted(false);
95 }
96
97 // virtual
98 void VideoContainer::wheelEvent(QWheelEvent * event)
99 {
100     m_monitor->slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
101     event->accept();
102 }
103
104 void VideoContainer::mouseDoubleClickEvent(QMouseEvent * event)
105 {
106     if (!KdenliveSettings::openglmonitors())
107         switchFullScreen();
108     event->accept();
109 }
110
111 void VideoContainer::switchFullScreen()
112 {
113     // TODO: disable screensaver?
114     Qt::WindowFlags flags = windowFlags();
115     if (!isFullScreen()) {
116         // Check if we ahave a multiple monitor setup
117         setUpdatesEnabled(false);
118 #if QT_VERSION >= 0x040600
119         int monitors = QApplication::desktop()->screenCount();
120 #else
121         int monitors = QApplication::desktop()->numScreens();
122 #endif
123         if (monitors > 1) {
124             QRect screenres;
125             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
126             int currentScreen = QApplication::desktop()->screenNumber(this);
127             if (currentScreen < monitors - 1)
128                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
129             else
130                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
131             move(QPoint(screenres.x(), screenres.y()));
132             resize(screenres.width(), screenres.height());
133         }
134
135         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
136         flags |= Qt::Window;
137         flags ^= Qt::SubWindow;
138         setWindowFlags(flags);
139 #ifdef Q_WS_X11
140         // This works around a bug with Compiz
141         // as the window must be visible before we can set the state
142         show();
143         raise();
144         setWindowState(windowState() | Qt::WindowFullScreen);   // set
145 #else
146         setWindowState(windowState() | Qt::WindowFullScreen);   // set
147         setUpdatesEnabled(true);
148         show();
149 #endif
150         setEnabled(true);
151     } else {
152         setUpdatesEnabled(false);
153         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
154         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
155         setWindowFlags(flags);
156         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
157         setUpdatesEnabled(true);
158         setEnabled(false);
159         show();
160     }
161 }
162
163 VideoSurface::VideoSurface(QWidget* parent) :
164     QWidget(parent)
165 {
166     // MonitorRefresh is used as container for the SDL display (it's window id is passed to SDL)
167     setAttribute(Qt::WA_PaintOnScreen);
168     setAttribute(Qt::WA_OpaquePaintEvent);
169     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
170     setAttribute(Qt::WA_NoSystemBackground);
171     //setUpdatesEnabled(false);
172 }
173
174 void VideoSurface::paintEvent(QPaintEvent *event)
175 {
176     Q_UNUSED(event);
177     //WARNING: This might trigger unnecessary refreshes from MLT's producer, but without this,
178     // as soon as monitor is covered by a popup menu or another window, image is corrupted.
179     emit refreshMonitor();
180 }
181
182
183 #include "abstractmonitor.moc"