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);
52 m_aRealtime->setEnabled(false);
54 m_menu = new QMenu(this);
55 m_menu->setPalette(m_scopePalette);
56 m_menu->addAction(m_aAutoRefresh);
57 m_menu->addAction(m_aRealtime);
59 this->setContextMenuPolicy(Qt::CustomContextMenu);
61 if (m_projMonitor->isActive()) {
62 m_activeRender = m_projMonitor->render;
64 m_activeRender = m_clipMonitor->render;
67 connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
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)));
77 AbstractScopeWidget::~AbstractScopeWidget()
80 delete m_aAutoRefresh;
83 void AbstractScopeWidget::prodHUDThread() {}
85 void AbstractScopeWidget::prodScopeThread()
87 if (m_semaphoreScope.tryAcquire(1)) {
88 Q_ASSERT(!m_threadScope.isRunning());
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();
96 qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
100 void AbstractScopeWidget::prodBackgroundThread() {}
105 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
109 prodBackgroundThread();
110 QWidget::mouseReleaseEvent(event);
113 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
115 // Update the dimension of the available rect for painting
116 m_scopeRect = scopeRect();
118 m_newHUDUpdates.fetchAndAddRelaxed(1);
119 m_newScopeUpdates.fetchAndAddRelaxed(1);
120 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
124 prodBackgroundThread();
126 QWidget::resizeEvent(event);
129 void AbstractScopeWidget::paintEvent(QPaintEvent *)
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
137 m_scopeRect = scopeRect();
138 initialDimensionUpdateDone = true;
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)));
148 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
150 m_menu->exec(this->mapToGlobal(pos));
156 void AbstractScopeWidget::slotHUDRenderingFinished(uint)
161 void AbstractScopeWidget::slotScopeRenderingFinished(uint)
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);
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;
175 void AbstractScopeWidget::slotBackgroundRenderingFinished(uint)
180 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
182 qDebug() << "Active monitor has changed in " << widgetName() << ". Is the clip monitor active now? " << isClipMonitor;
184 bool disconnected = m_activeRender->disconnect(this);
185 Q_ASSERT(disconnected);
187 m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
189 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
190 connect(m_activeRender, SIGNAL(rendererPositionBefore0()), this, SLOT(slotRenderZoneUpdated()));
192 // Update the scope for the new monitor.
195 prodBackgroundThread();
198 void AbstractScopeWidget::slotRenderZoneUpdated()
200 m_newHUDFrames.fetchAndAddRelaxed(1);
201 m_newScopeFrames.fetchAndAddRelaxed(1);
202 m_newBackgroundFrames.fetchAndAddRelaxed(1);
204 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
205 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
207 if (this->visibleRegion().isEmpty()) {
208 qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
210 if (m_aAutoRefresh->isChecked()) {
213 prodBackgroundThread();