]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.cpp
Abstract Scope Widget extended.
[kdenlive] / src / abstractscopewidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #include "qtconcurrentrun.h"
12
13 #include "abstractscopewidget.h"
14 #include "renderer.h"
15 #include "monitor.h"
16
17 #include <QFuture>
18 #include <QColor>
19 #include <QDebug>
20 #include <QMenu>
21 #include <QPainter>
22
23 const QColor light(250, 238, 226, 255);
24 const QColor dark ( 40,  40,  39, 255);
25 const QColor dark2( 25,  25,  23, 255);
26
27 AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
28     QWidget(parent),
29     m_projMonitor(projMonitor),
30     m_clipMonitor(clipMonitor),
31     offset(5),
32     m_semaphoreHUD(1),
33     m_semaphoreScope(1),
34     m_semaphoreBackground(1),
35     initialDimensionUpdateDone(false)
36
37 {
38     m_scopePalette = QPalette();
39     m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
40     m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
41     m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
42     m_scopePalette.setBrush(QPalette::Text, QBrush(light));
43     m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
44     m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
45     this->setPalette(m_scopePalette);
46     this->setAutoFillBackground(true);
47
48     m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
49     m_aAutoRefresh->setCheckable(true);
50     m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
51     m_aRealtime->setCheckable(true);
52
53     m_menu = new QMenu(this);
54     m_menu->setPalette(m_scopePalette);
55     m_menu->addAction(m_aAutoRefresh);
56     m_menu->addAction(m_aRealtime);
57
58     this->setContextMenuPolicy(Qt::CustomContextMenu);
59
60     if (m_projMonitor->isActive()) {
61         m_activeRender = m_projMonitor->render;
62     } else {
63         m_activeRender = m_clipMonitor->render;
64     }
65
66     connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
67
68     connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
69     connect(this, SIGNAL(signalHUDRenderingFinished(uint)), this, SLOT(slotHUDRenderingFinished(uint)));
70     connect(this, SIGNAL(signalScopeRenderingFinished(uint)), this, SLOT(slotScopeRenderingFinished(uint)));
71     connect(this, SIGNAL(signalBackgroundRenderingFinished(uint)), this, SLOT(slotBackgroundRenderingFinished(uint)));
72
73
74 }
75
76 AbstractScopeWidget::~AbstractScopeWidget()
77 {
78     delete m_menu;
79     delete m_aAutoRefresh;
80 }
81
82 void AbstractScopeWidget::prodHUDThread() {}
83
84 void AbstractScopeWidget::prodScopeThread()
85 {
86     if (m_semaphoreScope.tryAcquire(1)) {
87         Q_ASSERT(!m_threadScope.isRunning());
88
89         m_newScopeFrames.fetchAndStoreRelaxed(0);
90         m_newScopeUpdates.fetchAndStoreRelaxed(0);
91         m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope);
92         qDebug() << "Scope thread started in " << widgetName();
93
94     } else {
95         qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
96     }
97 }
98
99 void AbstractScopeWidget::prodBackgroundThread() {}
100
101
102 ///// Events /////
103
104 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
105 {
106     prodHUDThread();
107     prodScopeThread();
108     prodBackgroundThread();
109     QWidget::mouseReleaseEvent(event);
110 }
111
112 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
113 {
114     // Update the dimension of the available rect for painting
115     m_scopeRect = scopeRect();
116
117     m_newHUDUpdates.fetchAndAddRelaxed(1);
118     m_newScopeUpdates.fetchAndAddRelaxed(1);
119     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
120
121     prodHUDThread();
122     prodScopeThread();
123     prodBackgroundThread();
124
125     QWidget::resizeEvent(event);
126 }
127
128 void AbstractScopeWidget::paintEvent(QPaintEvent *)
129 {
130 //    qDebug() << "Painting now on scope " << widgetName();
131     if (!initialDimensionUpdateDone) {
132         // This is a workaround.
133         // When updating the dimensions in the constructor, the size
134         // of the control items at the top are simply ignored! So do
135         // it here instead.
136         m_scopeRect = scopeRect();
137         initialDimensionUpdateDone = true;
138     }
139
140     QPainter davinci(this);
141     davinci.drawImage(scopeRect().topLeft(), m_imgBackground);
142     davinci.drawImage(scopeRect().topLeft(), m_imgScope);
143     davinci.drawImage(scopeRect().topLeft(), m_imgHUD);
144     davinci.fillRect(scopeRect(), QBrush(QColor(200, 100, 0, 16)));
145 }
146
147 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
148 {
149     m_menu->exec(this->mapToGlobal(pos));
150 }
151
152
153 ///// Slots /////
154
155 void AbstractScopeWidget::slotHUDRenderingFinished(uint)
156 {
157
158 }
159
160 void AbstractScopeWidget::slotScopeRenderingFinished(uint)
161 {
162     qDebug() << "Scope rendering has finished, waiting for termination in " << widgetName();
163     m_threadScope.waitForFinished();
164     m_imgScope = m_threadScope.result();
165     m_semaphoreScope.release(1);
166     this->update();
167
168     if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
169         qDebug() << "Trying to start a new thread for " << widgetName()
170                 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
171     }
172 }
173
174 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint)
175 {
176
177 }
178
179 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
180 {
181     qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
182     if (isClipMonitor) {
183         m_activeRender = m_clipMonitor->render;
184         disconnect(this, SLOT(slotRenderZoneUpdated()));
185         connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
186         qDebug() << "Connected to clip monitor.";
187     } else {
188         m_activeRender = m_projMonitor->render;
189         disconnect(this, SLOT(slotRenderZoneUpdated()));
190         connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
191         qDebug() << "Connected to project monitor.";
192     }
193     // Update the scope for the new monitor.
194     prodHUDThread();
195     prodScopeThread();
196     prodBackgroundThread();
197 }
198
199 void AbstractScopeWidget::slotRenderZoneUpdated()
200 {
201     m_newHUDFrames.fetchAndAddRelaxed(1);
202     m_newScopeFrames.fetchAndAddRelaxed(1);
203     m_newBackgroundFrames.fetchAndAddRelaxed(1);
204
205     qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
206             << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
207
208     if (this->visibleRegion().isEmpty()) {
209         qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
210     } else {
211         if (m_aAutoRefresh->isChecked()) {
212             prodHUDThread();
213             prodScopeThread();
214             prodBackgroundThread();
215         }
216     }
217 }