]> git.sesse.net Git - kdenlive/blob - src/abstractmonitor.cpp
Cleanup: remove duplicate monitor code, allow fullscreen for record monitor
[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
30
31 AbstractMonitor::AbstractMonitor(Kdenlive::MONITORID id, MonitorManager *manager, QWidget *parent): 
32     QWidget(parent),
33     m_id(id),
34     m_monitorManager(manager)
35 {}
36
37 bool AbstractMonitor::isActive() const
38 {
39     return m_monitorManager->isActive(m_id);
40 }
41
42 bool AbstractMonitor::slotActivateMonitor()
43 {
44     return m_monitorManager->activateMonitor(m_id);
45 }
46
47 VideoContainer::VideoContainer(AbstractMonitor* monitor, QWidget *parent) :
48     QFrame(parent)
49     , m_monitor(monitor)
50 {
51     setFrameShape(QFrame::NoFrame);
52     setFocusPolicy(Qt::ClickFocus);
53     setAttribute(Qt::WA_PaintOnScreen);
54     setAttribute(Qt::WA_OpaquePaintEvent);
55     //setEnabled(false);
56     setContentsMargins(0, 0, 0, 0);
57     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
58 }
59
60 // virtual
61 void VideoContainer::mouseReleaseEvent(QMouseEvent * event)
62 {
63     if (event->button() != Qt::RightButton) {
64         if (m_monitor->isActive()) {
65             m_monitor->slotPlay();
66         }
67     }
68 }
69
70 // virtual
71 void VideoContainer::keyPressEvent(QKeyEvent *event)
72 {
73     // Exit fullscreen with Esc key
74     if (event->key() == Qt::Key_Escape && isFullScreen()) {
75         switchFullScreen();
76         event->setAccepted(true);
77     } else event->setAccepted(false);
78 }
79
80 // virtual
81 void VideoContainer::wheelEvent(QWheelEvent * event)
82 {
83     m_monitor->slotMouseSeek(event->delta(), event->modifiers() == Qt::ControlModifier);
84     event->accept();
85 }
86
87 void VideoContainer::mouseDoubleClickEvent(QMouseEvent * event)
88 {
89     if (!KdenliveSettings::openglmonitors())
90         switchFullScreen();
91     event->accept();
92 }
93
94 void VideoContainer::switchFullScreen()
95 {
96     // TODO: disable screensaver?
97     m_monitor->pause();
98     Qt::WindowFlags flags = windowFlags();
99     if (!isFullScreen()) {
100         // Check if we ahave a multiple monitor setup
101         setUpdatesEnabled(false);
102 #if QT_VERSION >= 0x040600
103         int monitors = QApplication::desktop()->screenCount();
104 #else
105         int monitors = QApplication::desktop()->numScreens();
106 #endif
107         if (monitors > 1) {
108             QRect screenres;
109             // Move monitor widget to the second screen (one screen for Kdenlive, the other one for the Monitor widget
110             int currentScreen = QApplication::desktop()->screenNumber(this);
111             if (currentScreen < monitors - 1)
112                 screenres = QApplication::desktop()->screenGeometry(currentScreen + 1);
113             else
114                 screenres = QApplication::desktop()->screenGeometry(currentScreen - 1);
115             move(QPoint(screenres.x(), screenres.y()));
116             resize(screenres.width(), screenres.height());
117         }
118
119         m_baseFlags = flags & (Qt::Window | Qt::SubWindow);
120         flags |= Qt::Window;
121         flags ^= Qt::SubWindow;
122         setWindowFlags(flags);
123 #ifdef Q_WS_X11
124         // This works around a bug with Compiz
125         // as the window must be visible before we can set the state
126         show();
127         raise();
128         setWindowState(windowState() | Qt::WindowFullScreen);   // set
129 #else
130         setWindowState(windowState() | Qt::WindowFullScreen);   // set
131         setUpdatesEnabled(true);
132         show();
133 #endif
134         setEnabled(true);
135     } else {
136         setUpdatesEnabled(false);
137         flags ^= (Qt::Window | Qt::SubWindow); //clear the flags...
138         flags |= m_baseFlags; //then we reset the flags (window and subwindow)
139         setWindowFlags(flags);
140         setWindowState(windowState()  ^ Qt::WindowFullScreen);   // reset
141         setUpdatesEnabled(true);
142         setEnabled(false);
143         show();
144     }
145     m_monitor->unpause();
146 }