]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.cpp
Display Jpeg exif data in clip properties metadata
[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     m_aRealtime->setEnabled(false);
53
54     m_menu = new QMenu(this);
55     m_menu->setPalette(m_scopePalette);
56     m_menu->addAction(m_aAutoRefresh);
57     m_menu->addAction(m_aRealtime);
58
59     this->setContextMenuPolicy(Qt::CustomContextMenu);
60
61     if (m_projMonitor->isActive()) {
62         m_activeRender = m_projMonitor->render;
63     } else {
64         m_activeRender = m_clipMonitor->render;
65     }
66
67     connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
68
69     connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
70     connect(this, SIGNAL(signalHUDRenderingFinished(uint)), this, SLOT(slotHUDRenderingFinished(uint)));
71     connect(this, SIGNAL(signalScopeRenderingFinished(uint)), this, SLOT(slotScopeRenderingFinished(uint)));
72     connect(this, SIGNAL(signalBackgroundRenderingFinished(uint)), this, SLOT(slotBackgroundRenderingFinished(uint)));
73
74
75 }
76
77 AbstractScopeWidget::~AbstractScopeWidget()
78 {
79     delete m_menu;
80     delete m_aAutoRefresh;
81 }
82
83 void AbstractScopeWidget::prodHUDThread() {}
84
85 void AbstractScopeWidget::prodScopeThread()
86 {
87     if (m_semaphoreScope.tryAcquire(1)) {
88         Q_ASSERT(!m_threadScope.isRunning());
89
90         m_newScopeFrames.fetchAndStoreRelaxed(0);
91         m_newScopeUpdates.fetchAndStoreRelaxed(0);
92         m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope);
93         qDebug() << "Scope thread started in " << widgetName();
94
95     } else {
96         qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
97     }
98 }
99
100 void AbstractScopeWidget::prodBackgroundThread() {}
101
102
103 ///// Events /////
104
105 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
106 {
107     prodHUDThread();
108     prodScopeThread();
109     prodBackgroundThread();
110     QWidget::mouseReleaseEvent(event);
111 }
112
113 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
114 {
115     // Update the dimension of the available rect for painting
116     m_scopeRect = scopeRect();
117
118     m_newHUDUpdates.fetchAndAddRelaxed(1);
119     m_newScopeUpdates.fetchAndAddRelaxed(1);
120     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
121
122     prodHUDThread();
123     prodScopeThread();
124     prodBackgroundThread();
125
126     QWidget::resizeEvent(event);
127 }
128
129 void AbstractScopeWidget::paintEvent(QPaintEvent *)
130 {
131 //    qDebug() << "Painting now on scope " << widgetName();
132     if (!initialDimensionUpdateDone) {
133         // This is a workaround.
134         // When updating the dimensions in the constructor, the size
135         // of the control items at the top are simply ignored! So do
136         // it here instead.
137         m_scopeRect = scopeRect();
138         initialDimensionUpdateDone = true;
139     }
140
141     QPainter davinci(this);
142     davinci.drawImage(scopeRect().topLeft(), m_imgBackground);
143     davinci.drawImage(scopeRect().topLeft(), m_imgScope);
144     davinci.drawImage(scopeRect().topLeft(), m_imgHUD);
145     davinci.fillRect(scopeRect(), QBrush(QColor(200, 100, 0, 16)));
146 }
147
148 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
149 {
150     m_menu->exec(this->mapToGlobal(pos));
151 }
152
153
154 ///// Slots /////
155
156 void AbstractScopeWidget::slotHUDRenderingFinished(uint)
157 {
158
159 }
160
161 void AbstractScopeWidget::slotScopeRenderingFinished(uint)
162 {
163     qDebug() << "Scope rendering has finished, waiting for termination in " << widgetName();
164     m_threadScope.waitForFinished();
165     m_imgScope = m_threadScope.result();
166     m_semaphoreScope.release(1);
167     this->update();
168
169     if ( (m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
170         qDebug() << "Trying to start a new thread for " << widgetName()
171                 << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
172     }
173 }
174
175 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint)
176 {
177
178 }
179
180 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
181 {
182     qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
183
184     bool disconnected = m_activeRender->disconnect(this);
185     Q_ASSERT(disconnected);
186
187     m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
188
189     connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
190     connect(m_activeRender, SIGNAL(rendererPositionBefore0()), this, SLOT(slotRenderZoneUpdated()));
191
192     // Update the scope for the new monitor.
193     prodHUDThread();
194     prodScopeThread();
195     prodBackgroundThread();
196 }
197
198 void AbstractScopeWidget::slotRenderZoneUpdated()
199 {
200     m_newHUDFrames.fetchAndAddRelaxed(1);
201     m_newScopeFrames.fetchAndAddRelaxed(1);
202     m_newBackgroundFrames.fetchAndAddRelaxed(1);
203
204     qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
205             << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
206
207     if (this->visibleRegion().isEmpty()) {
208         qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
209     } else {
210         if (m_aAutoRefresh->isChecked()) {
211             prodHUDThread();
212             prodScopeThread();
213             prodBackgroundThread();
214         }
215     }
216 }