]> git.sesse.net Git - kdenlive/blob - src/abstractscopewidget.cpp
Abstract scope class AbstractScopeWidget added.
[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
36 {
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);
46
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);
51
52     m_menu = new QMenu(this);
53     m_menu->setPalette(m_scopePalette);
54     m_menu->addAction(m_aAutoRefresh);
55     m_menu->addAction(m_aRealtime);
56
57     this->setContextMenuPolicy(Qt::CustomContextMenu);
58
59     if (m_projMonitor->isActive()) {
60         m_activeRender = m_projMonitor->render;
61     } else {
62         m_activeRender = m_clipMonitor->render;
63     }
64
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()));
68
69
70 }
71
72 AbstractScopeWidget::~AbstractScopeWidget()
73 {
74     delete m_menu;
75     delete m_aAutoRefresh;
76 }
77
78 void AbstractScopeWidget::prodScopeThread()
79 {
80     if (m_semaphoreScope.tryAcquire(1)) {
81         Q_ASSERT(!m_threadScope.isRunning());
82
83         m_threadScope = QtConcurrent::run(this, &AbstractScopeWidget::renderScope);
84         qDebug() << "Scope thread started in " << widgetName();
85
86     } else {
87         qDebug() << "Scope semaphore locked, not prodding in " << widgetName() << ". Thread running: " << m_threadScope.isRunning();
88     }
89 }
90
91
92 ///// Events /////
93
94 void AbstractScopeWidget::mouseReleaseEvent(QMouseEvent *event)
95 {
96     // TODO Render again
97     prodScopeThread();
98     QWidget::mouseReleaseEvent(event);
99 }
100
101 void AbstractScopeWidget::resizeEvent(QResizeEvent *event)
102 {
103     // Update the dimension of the available rect for painting
104     m_scopeRect = scopeRect();
105
106     m_newHUDUpdates.fetchAndAddRelaxed(1);
107     m_newScopeUpdates.fetchAndAddRelaxed(1);
108     m_newBackgroundUpdates.fetchAndAddRelaxed(1);
109
110     QWidget::resizeEvent(event);
111     // TODO Calculation
112 }
113
114 void AbstractScopeWidget::paintEvent(QPaintEvent *)
115 {
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)));
121 }
122
123 void AbstractScopeWidget::customContextMenuRequested(const QPoint &pos)
124 {
125     m_menu->exec(this->mapToGlobal(pos));
126 }
127
128
129 ///// Slots /////
130
131 void AbstractScopeWidget::slotHUDRenderingFinished()
132 {
133
134 }
135
136 void AbstractScopeWidget::slotScopeRenderingFinished()
137 {
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);
142     this->update();
143 }
144
145 void AbstractScopeWidget::slotBackgroundRenderingFinished()
146 {
147
148 }
149
150 void AbstractScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
151 {
152     if (isClipMonitor) {
153         m_activeRender = m_clipMonitor->render;
154         disconnect(this, SLOT(slotRenderZoneUpdated()));
155         connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
156     } else {
157         m_activeRender = m_projMonitor->render;
158         disconnect(this, SLOT(slotRenderZoneUpdated()));
159         connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
160     }
161 }
162
163 void AbstractScopeWidget::slotRenderZoneUpdated()
164 {
165     m_newHUDFrames.fetchAndAddRelaxed(1);
166     m_newScopeFrames.fetchAndAddRelaxed(1);
167     m_newBackgroundFrames.fetchAndAddRelaxed(1);
168
169     qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
170             << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
171
172     if (this->visibleRegion().isEmpty()) {
173         qDebug() << "Scope of widget " << widgetName() << " is not at the top, not rendering.";
174     } else {
175         if (m_aAutoRefresh->isChecked()) {
176             // TODO run the updater functions here.
177         }
178     }
179 }