File indexing completed on 2024-05-19 04:53:52

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003     Some code was borrowed from shotcut
0004     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "lumaliftgainparam.hpp"
0008 #include "assets/keyframes/model/keyframemodellist.hpp"
0009 #include "assets/model/assetparametermodel.hpp"
0010 #include "colorwheel.h"
0011 #include "utils/flowlayout.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 static const double LIFT_FACTOR = 1.0;
0016 static const double GAMMA_FACTOR = 2.0;
0017 static const double GAIN_FACTOR = 4.0;
0018 
0019 LumaLiftGainParam::LumaLiftGainParam(std::shared_ptr<AssetParameterModel> model, const QModelIndex &index, QWidget *parent)
0020     : QWidget(parent)
0021     , m_model(std::move(model))
0022     , m_index(index)
0023 {
0024     m_flowLayout = new FlowLayout(this, 10, 10, 4);
0025     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
0026     m_lift = new ColorWheel(QStringLiteral("lift"), i18n("Lift"), NegQColor::fromRgbF(0.5, 0.5, 0.5), this);
0027     m_lift->setFactorDefaultZero(LIFT_FACTOR, 0, 0);
0028     connect(m_lift, &ColorWheel::colorChange, this, &LumaLiftGainParam::liftChanged);
0029     m_gamma = new ColorWheel(QStringLiteral("gamma"), i18n("Gamma"), NegQColor::fromRgbF(1. / GAMMA_FACTOR, 1. / GAMMA_FACTOR, 1. / GAMMA_FACTOR), this);
0030     m_gamma->setFactorDefaultZero(GAMMA_FACTOR, 1, 0);
0031     connect(m_gamma, &ColorWheel::colorChange, this, &LumaLiftGainParam::gammaChanged);
0032     m_gain = new ColorWheel(QStringLiteral("gain"), i18n("Gain"), NegQColor::fromRgbF(1. / GAIN_FACTOR, 1. / GAIN_FACTOR, 1. / GAIN_FACTOR), this);
0033     m_gain->setFactorDefaultZero(GAIN_FACTOR, 1, 0);
0034     connect(m_gain, &ColorWheel::colorChange, this, &LumaLiftGainParam::gainChanged);
0035     QMap<QString, QModelIndex> indexes;
0036     for (int i = 0; i < m_model->rowCount(); ++i) {
0037         QModelIndex local_index = m_model->index(i, 0);
0038         QString name = m_model->data(local_index, AssetParameterModel::NameRole).toString();
0039         indexes.insert(name, local_index);
0040     }
0041 
0042     m_flowLayout->addWidget(m_lift);
0043     m_flowLayout->addWidget(m_gamma);
0044     m_flowLayout->addWidget(m_gain);
0045     setLayout(m_flowLayout);
0046     slotRefresh(0);
0047 
0048     connect(this, &LumaLiftGainParam::liftChanged, [this, indexes](const NegQColor &sourceColor, const NegQColor &color, bool createUndo) {
0049         QList<QModelIndex> ixes{indexes.value(QStringLiteral("lift_r")), indexes.value(QStringLiteral("lift_g")), indexes.value(QStringLiteral("lift_b"))};
0050         QStringList sourceValues{QString::number(sourceColor.redF() * 2 - 1, 'f'), QString::number(sourceColor.greenF() * 2 - 1, 'f'),
0051                                  QString::number(sourceColor.blueF() * 2 - 1, 'f')};
0052         QStringList values{QString::number(color.redF() * 2 - 1, 'f'), QString::number(color.greenF() * 2 - 1, 'f'),
0053                            QString::number(color.blueF() * 2 - 1, 'f')};
0054         Q_EMIT valuesChanged(ixes, sourceValues, values, createUndo);
0055     });
0056     connect(this, &LumaLiftGainParam::gammaChanged, [this, indexes](const NegQColor &sourceColor, const NegQColor &color, bool createUndo) {
0057         QList<QModelIndex> ixes{indexes.value(QStringLiteral("gamma_r")), indexes.value(QStringLiteral("gamma_g")), indexes.value(QStringLiteral("gamma_b"))};
0058         QStringList sourceValues{QString::number(sourceColor.redF() * GAMMA_FACTOR, 'f'), QString::number(sourceColor.greenF() * GAMMA_FACTOR, 'f'),
0059                                  QString::number(sourceColor.blueF() * GAMMA_FACTOR, 'f')};
0060         QStringList values{QString::number(color.redF() * GAMMA_FACTOR, 'f'), QString::number(color.greenF() * GAMMA_FACTOR, 'f'),
0061                            QString::number(color.blueF() * GAMMA_FACTOR, 'f')};
0062         Q_EMIT valuesChanged(ixes, sourceValues, values, createUndo);
0063     });
0064     connect(this, &LumaLiftGainParam::gainChanged, [this, indexes](const NegQColor &sourceColor, const NegQColor &color, bool createUndo) {
0065         QList<QModelIndex> ixes{indexes.value(QStringLiteral("gain_r")), indexes.value(QStringLiteral("gain_g")), indexes.value(QStringLiteral("gain_b"))};
0066         QStringList sourceValues{QString::number(sourceColor.redF() * GAIN_FACTOR, 'f'), QString::number(sourceColor.greenF() * GAIN_FACTOR, 'f'),
0067                                  QString::number(sourceColor.blueF() * GAIN_FACTOR, 'f')};
0068         QStringList values{QString::number(color.redF() * GAIN_FACTOR, 'f'), QString::number(color.greenF() * GAIN_FACTOR, 'f'),
0069                            QString::number(color.blueF() * GAIN_FACTOR, 'f')};
0070         Q_EMIT valuesChanged(ixes, sourceValues, values, createUndo);
0071     });
0072 }
0073 
0074 void LumaLiftGainParam::updateEffect(QDomElement &effect)
0075 {
0076     NegQColor lift = m_lift->color();
0077     NegQColor gamma = m_gamma->color();
0078     NegQColor gain = m_gain->color();
0079     QMap<QString, double> values;
0080     values.insert(QStringLiteral("lift_r"), lift.redF() * 2 - 1);
0081     values.insert(QStringLiteral("lift_g"), lift.greenF() * 2 - 1);
0082     values.insert(QStringLiteral("lift_b"), lift.blueF() * 2 - 1);
0083 
0084     values.insert(QStringLiteral("gamma_r"), gamma.redF() * GAMMA_FACTOR);
0085     values.insert(QStringLiteral("gamma_g"), gamma.greenF() * GAMMA_FACTOR);
0086     values.insert(QStringLiteral("gamma_b"), gamma.blueF() * GAMMA_FACTOR);
0087 
0088     values.insert(QStringLiteral("gain_r"), gain.redF() * GAIN_FACTOR);
0089     values.insert(QStringLiteral("gain_g"), gain.greenF() * GAIN_FACTOR);
0090     values.insert(QStringLiteral("gain_b"), gain.blueF() * GAIN_FACTOR);
0091 
0092     QDomNodeList namenode = effect.childNodes();
0093     for (int i = 0; i < namenode.count(); ++i) {
0094         QDomElement pa = namenode.item(i).toElement();
0095         if (pa.tagName() != QLatin1String("parameter")) {
0096             continue;
0097         }
0098         if (values.contains(pa.attribute(QStringLiteral("name")))) {
0099             pa.setAttribute(QStringLiteral("value"),
0100                             int(values.value(pa.attribute(QStringLiteral("name"))) * pa.attribute(QStringLiteral("factor"), QStringLiteral("1")).toDouble()));
0101         }
0102     }
0103 }
0104 
0105 void LumaLiftGainParam::resizeEvent(QResizeEvent *ev)
0106 {
0107     QWidget::resizeEvent(ev);
0108     if (height() != m_flowLayout->miniHeight()) {
0109         Q_EMIT updateHeight(m_flowLayout->miniHeight());
0110     }
0111 }
0112 
0113 int LumaLiftGainParam::miniHeight()
0114 {
0115     return m_flowLayout->miniHeight();
0116 }
0117 
0118 void LumaLiftGainParam::slotRefresh(int pos)
0119 {
0120     QMap<QString, double> values;
0121     for (int i = 0; i < m_model->rowCount(); ++i) {
0122         const QModelIndex local_index = m_model->index(i, 0);
0123         QString name = m_model->data(local_index, AssetParameterModel::NameRole).toString();
0124         double val = m_model->getKeyframeModel()->getInterpolatedValue(pos, local_index).toDouble();
0125         values.insert(name, val);
0126     }
0127     m_lift->setColor({(values.value(QStringLiteral("lift_r")) + 1) / 2., (values.value(QStringLiteral("lift_g")) + 1) / 2.,
0128                       (values.value(QStringLiteral("lift_b")) + 1) / 2.});
0129     m_gamma->setColor({values.value(QStringLiteral("gamma_r")), values.value(QStringLiteral("gamma_g")), values.value(QStringLiteral("gamma_b"))});
0130     m_gain->setColor({values.value(QStringLiteral("gain_r")), values.value(QStringLiteral("gain_g")), values.value(QStringLiteral("gain_b"))});
0131 }