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)
37 m_scopePalette = QPalette();
38 m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
39 m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
40 m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
41 m_scopePalette.setBrush(QPalette::Text, QBrush(light));
42 m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
43 m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
44 this->setPalette(m_scopePalette);
45 this->setAutoFillBackground(true);
47 m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
48 m_aAutoRefresh->setCheckable(true);
49 m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
50 m_aRealtime->setCheckable(true);
52 m_menu = new QMenu(this);
53 m_menu->setPalette(m_scopePalette);
54 m_menu->addAction(m_aAutoRefresh);
55 m_menu->addAction(m_aRealtime);
57 this->setContextMenuPolicy(Qt::CustomContextMenu);
59 if (m_projMonitor->isActive()) {
60 m_activeRender = m_projMonitor->render;
62 m_activeRender = m_clipMonitor->render;
65 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
66 connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
67 connect(this, SIGNAL(signalScopeRenderingFinished()), this, SLOT(slotScopeRenderingFinished()));
72 AbstractScopeWidget::~AbstractScopeWidget()
75 delete m_aAutoRefresh;
78 void AbstractScopeWidget::prodScopeThread()
80 if (m_semaphoreScope.tryAcquire(1)) {
81 Q_ASSERT(!m_threadScope.isRunning());
83 m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope);
84 qDebug() << "Scope thread started in " << widgetName();
87 qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
94 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
98 QWidget::mouseReleaseEvent(event);
101 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
103 // Update the dimension of the available rect for painting
104 m_scopeRect = scopeRect();
106 m_newHUDUpdates.fetchAndAddRelaxed(1);
107 m_newScopeUpdates.fetchAndAddRelaxed(1);
108 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
110 QWidget::resizeEvent(event);
114 void AbstractScopeWidget::paintEvent(QPaintEvent *)
116 QPainter davinci(this);
117 davinci.drawImage(scopeRect().topLeft(), m_imgBackground);
118 davinci.drawImage(scopeRect().topLeft(), m_imgScope);
119 davinci.drawImage(scopeRect().topLeft(), m_imgHUD);
120 davinci.fillRect(scopeRect(), QBrush(QColor(200, 100, 0, 16)));
123 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
125 m_menu->exec(this->mapToGlobal(pos));
131 void AbstractScopeWidget::slotHUDRenderingFinished()
136 void AbstractScopeWidget::slotScopeRenderingFinished()
138 qDebug() << "Scope rendering has finished, waiting for termination in " << widgetName();
139 m_threadScope.waitForFinished();
140 m_imgScope = m_threadScope.result();
141 m_semaphoreScope.release(1);
145 void AbstractScopeWidget::slotBackgroundRenderingFinished()
150 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
153 m_activeRender = m_clipMonitor->render;
154 disconnect(this, SLOT(slotRenderZoneUpdated()));
155 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
157 m_activeRender = m_projMonitor->render;
158 disconnect(this, SLOT(slotRenderZoneUpdated()));
159 connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
163 void AbstractScopeWidget::slotRenderZoneUpdated()
165 m_newHUDFrames.fetchAndAddRelaxed(1);
166 m_newScopeFrames.fetchAndAddRelaxed(1);
167 m_newBackgroundFrames.fetchAndAddRelaxed(1);
169 qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
170 << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
172 if (this->visibleRegion().isEmpty()) {
173 qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
175 if (m_aAutoRefresh->isChecked()) {
176 // TODO run the updater functions here.