File indexing completed on 2024-04-28 04:52:22

0001 /*
0002     SPDX-FileCopyrightText: 2010 Simon Andreas Eugster <simon.eu@gmail.com>
0003     This file is part of kdenlive. See www.kdenlive.org.
0004 
0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "rgbparade.h"
0009 #include "rgbparadegenerator.h"
0010 #include <QDebug>
0011 #include <QElapsedTimer>
0012 #include <QPainter>
0013 #include <QRect>
0014 
0015 #include "klocalizedstring.h"
0016 #include <KConfigGroup>
0017 #include <KSharedConfig>
0018 
0019 RGBParade::RGBParade(QWidget *parent)
0020     : AbstractGfxScopeWidget(true, parent)
0021 {
0022     m_ui = new Ui::RGBParade_UI();
0023     m_ui->setupUi(this);
0024 
0025     m_ui->paintMode->addItem(i18n("RGB"), QVariant(RGBParadeGenerator::PaintMode_RGB));
0026     m_ui->paintMode->addItem(i18n("White"), QVariant(RGBParadeGenerator::PaintMode_White));
0027 
0028     m_menu->addSeparator();
0029     m_aAxis = new QAction(i18n("Draw axis"), this);
0030     m_aAxis->setCheckable(true);
0031     m_menu->addAction(m_aAxis);
0032     connect(m_aAxis, &QAction::changed, this, &RGBParade::forceUpdateScope);
0033 
0034     m_aGradRef = new QAction(i18n("Gradient reference line"), this);
0035     m_aGradRef->setCheckable(true);
0036     m_menu->addAction(m_aGradRef);
0037     connect(m_aGradRef, &QAction::changed, this, &RGBParade::forceUpdateScope);
0038 
0039     connect(m_ui->paintMode, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &RGBParade::forceUpdateScope);
0040     connect(this, &RGBParade::signalMousePositionChanged, this, &RGBParade::forceUpdateHUD);
0041 
0042     m_rgbParadeGenerator = new RGBParadeGenerator();
0043     init();
0044 }
0045 
0046 RGBParade::~RGBParade()
0047 {
0048     writeConfig();
0049 
0050     delete m_ui;
0051     delete m_rgbParadeGenerator;
0052     delete m_aAxis;
0053     delete m_aGradRef;
0054 }
0055 
0056 void RGBParade::readConfig()
0057 {
0058     AbstractGfxScopeWidget::readConfig();
0059 
0060     KSharedConfigPtr config = KSharedConfig::openConfig();
0061     KConfigGroup scopeConfig(config, configName());
0062     m_ui->paintMode->setCurrentIndex(scopeConfig.readEntry("paintmode", 0));
0063     m_aAxis->setChecked(scopeConfig.readEntry("axis", false));
0064     m_aGradRef->setChecked(scopeConfig.readEntry("gradref", false));
0065 }
0066 
0067 void RGBParade::writeConfig()
0068 {
0069     KSharedConfigPtr config = KSharedConfig::openConfig();
0070     KConfigGroup scopeConfig(config, configName());
0071     scopeConfig.writeEntry("paintmode", m_ui->paintMode->currentIndex());
0072     scopeConfig.writeEntry("axis", m_aAxis->isChecked());
0073     scopeConfig.writeEntry("gradref", m_aGradRef->isChecked());
0074     scopeConfig.sync();
0075 }
0076 
0077 QString RGBParade::widgetName() const
0078 {
0079     return QStringLiteral("RGB Parade");
0080 }
0081 
0082 QRect RGBParade::scopeRect()
0083 {
0084     QPoint topleft(offset, m_ui->verticalSpacer->geometry().y() + 2 * offset);
0085     return QRect(topleft, QPoint(this->size().width() - offset, this->size().height() - offset));
0086 }
0087 
0088 QImage RGBParade::renderHUD(uint)
0089 {
0090     QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
0091     hud.fill(qRgba(0, 0, 0, 0));
0092 
0093     QPainter davinci;
0094     bool ok = davinci.begin(&hud);
0095     if (!ok) {
0096         qDebug() << "Could not initialise QPainter for RGB Parade HUD.";
0097         return hud;
0098     }
0099     davinci.setPen(penLight);
0100 
0101     int x = scopeRect().width() - 30;
0102 
0103     davinci.drawText(x, scopeRect().height() - RGBParadeGenerator::distBottom, QStringLiteral("0"));
0104     davinci.drawText(x, 10, QStringLiteral("255"));
0105 
0106     if (scopeRect().height() > 0 && m_mouseWithinWidget) {
0107 
0108         int y = m_mousePos.y() - scopeRect().y();
0109 
0110         // Draw a horizontal line through the current mouse position
0111         // and show the value of the waveform there
0112         davinci.drawLine(0, y, scopeRect().size().width() - RGBParadeGenerator::distRight, y);
0113 
0114         // Make the value stick to the line unless it is at the top/bottom of the scope
0115         const int top = 30;
0116         const int bottom = 20 + RGBParadeGenerator::distBottom;
0117 
0118         int valY = y + 5;
0119         if (valY < top) {
0120             valY = top;
0121         } else if (valY > scopeRect().height() - bottom) {
0122             valY = scopeRect().height() - bottom;
0123         }
0124 
0125         int val = 255 + int(255. * (1 - y) / (scopeRect().height() - RGBParadeGenerator::distBottom));
0126         if ((val >= 0) && (val <= 255)) {
0127             davinci.drawText(x, valY, QVariant(val).toString());
0128         }
0129     }
0130 
0131     Q_EMIT signalHUDRenderingFinished(1, 1);
0132     return hud;
0133 }
0134 
0135 QImage RGBParade::renderGfxScope(uint accelerationFactor, const QImage &qimage)
0136 {
0137     QElapsedTimer timer;
0138     timer.start();
0139 
0140     int paintmode = m_ui->paintMode->itemData(m_ui->paintMode->currentIndex()).toInt();
0141     QImage parade = m_rgbParadeGenerator->calculateRGBParade(m_scopeRect.size(), qimage, RGBParadeGenerator::PaintMode(paintmode), m_aAxis->isChecked(),
0142                                                              m_aGradRef->isChecked(), accelerationFactor);
0143     Q_EMIT signalScopeRenderingFinished(uint(timer.elapsed()), accelerationFactor);
0144     return parade;
0145 }
0146 
0147 QImage RGBParade::renderBackground(uint)
0148 {
0149     return QImage();
0150 }
0151 
0152 bool RGBParade::isHUDDependingOnInput() const
0153 {
0154     return false;
0155 }
0156 
0157 bool RGBParade::isScopeDependingOnInput() const
0158 {
0159     return true;
0160 }
0161 
0162 bool RGBParade::isBackgroundDependingOnInput() const
0163 {
0164     return false;
0165 }