]> git.sesse.net Git - kdenlive/blob - src/waveform.cpp
RGB Parade changes:
[kdenlive] / src / waveform.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 <QMenu>
12 #include <QMouseEvent>
13 #include <QPainter>
14 #include <QPoint>
15
16 #include "renderer.h"
17 #include "waveform.h"
18 #include "waveformgenerator.h"
19
20
21 const QSize Waveform::m_textWidth(35,0);
22
23 Waveform::Waveform(Monitor *projMonitor, Monitor *clipMonitor, QWidget *parent) :
24     AbstractScopeWidget(projMonitor, clipMonitor, true, parent)
25 {
26     ui = new Ui::Waveform_UI();
27     ui->setupUi(this);
28
29     ui->paintMode->addItem(i18n("Yellow"), QVariant(WaveformGenerator::PaintMode_Yellow));
30     ui->paintMode->addItem(i18n("White"), QVariant(WaveformGenerator::PaintMode_White));
31     ui->paintMode->addItem(i18n("Green"), QVariant(WaveformGenerator::PaintMode_Green));
32
33
34     m_aRec601 = new QAction(i18n("Rec. 601"), this);
35     m_aRec601->setCheckable(true);
36     m_aRec709 = new QAction(i18n("Rec. 709"), this);
37     m_aRec709->setCheckable(true);
38     m_agRec = new QActionGroup(this);
39     m_agRec->addAction(m_aRec601);
40     m_agRec->addAction(m_aRec709);
41     m_menu->addSeparator()->setText(i18n("Luma mode"));
42     m_menu->addAction(m_aRec601);
43     m_menu->addAction(m_aRec709);
44
45
46     bool b = true;
47     b &= connect(ui->paintMode, SIGNAL(currentIndexChanged(int)), this, SLOT(forceUpdateScope()));
48     b &= connect(this, SIGNAL(signalMousePositionChanged()), this, SLOT(forceUpdateHUD()));
49     b &= connect(m_aRec601, SIGNAL(toggled(bool)), this, SLOT(forceUpdateScope()));
50     b &= connect(m_aRec709, SIGNAL(toggled(bool)), this, SLOT(forceUpdateScope()));
51     Q_ASSERT(b);
52
53     init();
54     m_waveformGenerator = new WaveformGenerator();
55 }
56
57 Waveform::~Waveform()
58 {
59     writeConfig();
60
61     delete m_waveformGenerator;
62     delete m_aRec601;
63     delete m_aRec709;
64     delete m_agRec;
65 }
66
67 void Waveform::readConfig()
68 {
69     AbstractScopeWidget::readConfig();
70
71     KSharedConfigPtr config = KGlobal::config();
72     KConfigGroup scopeConfig(config, configName());
73     ui->paintMode->setCurrentIndex(scopeConfig.readEntry("paintmode", 0));
74     m_aRec601->setChecked(scopeConfig.readEntry("rec601", false));
75     m_aRec709->setChecked(!m_aRec601->isChecked());
76 }
77
78 void Waveform::writeConfig()
79 {
80     KSharedConfigPtr config = KGlobal::config();
81     KConfigGroup scopeConfig(config, configName());
82     scopeConfig.writeEntry("paintmode", ui->paintMode->currentIndex());
83     scopeConfig.writeEntry("rec601", m_aRec601->isChecked());
84     scopeConfig.sync();
85 }
86
87
88 QRect Waveform::scopeRect()
89 {
90     // Distance from top/left/right
91     int offset = 6;
92
93     QPoint topleft(offset, ui->verticalSpacer->geometry().y()+offset);
94
95     return QRect(topleft, this->size() - QSize(offset+topleft.x(), offset+topleft.y()));
96 }
97
98
99 ///// Implemented methods /////
100
101 QString Waveform::widgetName() const { return QString("Waveform"); }
102 bool Waveform::isHUDDependingOnInput() const { return false; }
103 bool Waveform::isScopeDependingOnInput() const { return true; }
104 bool Waveform::isBackgroundDependingOnInput() const { return false; }
105
106 QImage Waveform::renderHUD(uint)
107 {
108     QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
109     hud.fill(qRgba(0,0,0,0));
110
111     QPainter davinci(&hud);
112     davinci.setPen(penLight);
113
114     int x = scopeRect().width()-m_textWidth.width()+3;
115     int y = m_mousePos.y() - scopeRect().y();
116
117     if (scopeRect().height() > 0 && m_mouseWithinWidget) {
118         // Draw a horizontal line through the current mouse position
119         // and show the value of the waveform there
120         davinci.drawLine(0, y, scopeRect().size().width()-m_textWidth.width(), y);
121
122         int val = 255*(1-(float)y/scopeRect().height());
123         davinci.drawText(x, scopeRect().height()/2, QVariant(val).toString());
124     }
125     davinci.drawText(x, scopeRect().height(), "0");
126     davinci.drawText(x, 10, "255");
127
128     emit signalHUDRenderingFinished(0, 1);
129     return hud;
130 }
131
132 QImage Waveform::renderScope(uint accelFactor, QImage qimage)
133 {
134     QTime start = QTime::currentTime();
135     start.start();
136
137     int paintmode = ui->paintMode->itemData(ui->paintMode->currentIndex()).toInt();
138     WaveformGenerator::Rec rec = m_aRec601->isChecked() ? WaveformGenerator::Rec_601 : WaveformGenerator::Rec_709;
139     QImage wave = m_waveformGenerator->calculateWaveform(scopeRect().size() - m_textWidth, qimage, (WaveformGenerator::PaintMode) paintmode,
140                                                          true, rec, accelFactor);
141
142     emit signalScopeRenderingFinished(start.elapsed(), 1);
143     return wave;
144 }
145
146 QImage Waveform::renderBackground(uint)
147 {
148     emit signalBackgroundRenderingFinished(0, 1);
149     return QImage();
150 }