1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
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 ***************************************************************************/
11 #include "qtconcurrentrun.h"
13 #include "abstractscopewidget.h"
23 const QColor light(250, 238, 226, 255);
24 const QColor dark ( 40, 40, 39, 255);
25 const QColor dark2( 25, 25, 23, 255);
27 AbstractScopeWidget::AbstractScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
29 m_projMonitor(projMonitor),
30 m_clipMonitor(clipMonitor),
34 m_semaphoreBackground(1),
35 initialDimensionUpdateDone(false)
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);
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);
53 m_menu = new QMenu(this);
54 m_menu->setPalette(m_scopePalette);
55 m_menu->addAction(m_aAutoRefresh);
56 m_menu->addAction(m_aRealtime);
58 this->setContextMenuPolicy(Qt::CustomContextMenu);
60 if (m_projMonitor->isActive()) {
61 m_activeRender = m_projMonitor->render;
63 m_activeRender = m_clipMonitor->render;
66 connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
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)));
76 AbstractScopeWidget::~AbstractScopeWidget()
79 delete m_aAutoRefresh;
82 void AbstractScopeWidget::prodHUDThread() {}
84 void AbstractScopeWidget::prodScopeThread()
86 if (m_semaphoreScope.tryAcquire(1)) {
87 Q_ASSERT(!m_threadScope.isRunning());
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();
95 qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
99 void AbstractScopeWidget::prodBackgroundThread() {}
104 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
108 prodBackgroundThread();
109 QWidget::mouseReleaseEvent(event);
112 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
114 // Update the dimension of the available rect for painting
115 m_scopeRect = scopeRect();
117 m_newHUDUpdates.fetchAndAddRelaxed(1);
118 m_newScopeUpdates.fetchAndAddRelaxed(1);
119 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
123 prodBackgroundThread();
125 QWidget::resizeEvent(event);
128 void AbstractScopeWidget::paintEvent(QPaintEvent *)
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
136 m_scopeRect = scopeRect();
137 initialDimensionUpdateDone = true;
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)));
147 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
149 m_menu->exec(this->mapToGlobal(pos));
155 void AbstractScopeWidget::slotHUDRenderingFinished(uint)
160 void AbstractScopeWidget::slotScopeRenderingFinished(uint)
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);
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;
174 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint)
179 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
181 qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << 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.";
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.";
193 // Update the scope for the new monitor.
196 prodBackgroundThread();
199 void AbstractScopeWidget::slotRenderZoneUpdated()
201 m_newHUDFrames.fetchAndAddRelaxed(1);
202 m_newScopeFrames.fetchAndAddRelaxed(1);
203 m_newBackgroundFrames.fetchAndAddRelaxed(1);
205 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
206 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
208 if (this->visibleRegion().isEmpty()) {
209 qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
211 if (m_aAutoRefresh->isChecked()) {
214 prodBackgroundThread();