File indexing completed on 2024-05-12 04:52:54

0001 /*
0002     SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle
0003     This file is part of Kdenlive. See www.kdenlive.org.
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "multiswitchparamwidget.hpp"
0009 #include "assets/model/assetparametermodel.hpp"
0010 
0011 MultiSwitchParamWidget::MultiSwitchParamWidget(std::shared_ptr<AssetParameterModel> model, QModelIndex index, QWidget *parent)
0012     : AbstractParamWidget(std::move(model), index, parent)
0013 {
0014     setupUi(this);
0015 
0016     // setup the comment
0017     QString comment = m_model->data(m_index, AssetParameterModel::CommentRole).toString();
0018     setToolTip(comment);
0019     m_labelComment->setText(comment);
0020     m_widgetComment->setHidden(true);
0021 
0022     // setup the name
0023     m_labelName->setText(m_model->data(m_index, Qt::DisplayRole).toString());
0024     setMinimumHeight(m_labelName->sizeHint().height());
0025 
0026     // set check state
0027     slotRefresh();
0028 
0029     // Q_EMIT the signal of the base class when appropriate
0030     connect(this->m_checkBox, &QCheckBox::stateChanged, this, [this](int state) {
0031         QString value;
0032         if (state == Qt::Checked) {
0033             value = m_model->data(m_index, AssetParameterModel::MaxRole).toString();
0034             if (value.contains(QLatin1String("0="))) {
0035                 value.replace(QLatin1String("0="), QLatin1String("00:00:00.000="));
0036             }
0037             if (value.contains(QLatin1String("-1="))) {
0038                 // Replace -1 with out position
0039                 int out = m_model->data(m_index, AssetParameterModel::OutRole).toInt() - m_model->data(m_index, AssetParameterModel::InRole).toInt();
0040                 value.replace(QLatin1String("-1="), QString("%1=").arg(m_model->framesToTime(out)));
0041             }
0042         } else {
0043             value = m_model->data(m_index, AssetParameterModel::MinRole).toString();
0044             if (value.contains(QLatin1String("0="))) {
0045                 value.replace(QLatin1String("0="), QLatin1String("00:00:00.000="));
0046             }
0047             if (value.contains(QLatin1String("-1="))) {
0048                 // Replace -1 with out position
0049                 int out = m_model->data(m_index, AssetParameterModel::OutRole).toInt() - m_model->data(m_index, AssetParameterModel::InRole).toInt();
0050                 value.replace(QLatin1String("-1="), QString("%1=").arg(m_model->framesToTime(out)));
0051             }
0052         }
0053         Q_EMIT valueChanged(m_index, value, true);
0054     });
0055 }
0056 
0057 void MultiSwitchParamWidget::slotShowComment(bool show)
0058 {
0059     if (!m_labelComment->text().isEmpty()) {
0060         m_widgetComment->setVisible(show);
0061     }
0062 }
0063 
0064 void MultiSwitchParamWidget::slotRefresh()
0065 {
0066     const QSignalBlocker bk(m_checkBox);
0067     QString max = m_model->data(m_index, AssetParameterModel::MaxRole).toString();
0068     const QString value = m_model->data(m_index, AssetParameterModel::ValueRole).toString();
0069     bool convertToTime = false;
0070     if (value.contains(QLatin1Char(':'))) {
0071         convertToTime = true;
0072     }
0073     if (max.contains(QLatin1String("0=")) && convertToTime) {
0074         max.replace(QLatin1String("0="), QLatin1String("00:00:00.000="));
0075     }
0076     if (max.contains(QLatin1String("-1=")) && !value.contains(QLatin1String("-1="))) {
0077         // Replace -1 with out position
0078         int out = m_model->data(m_index, AssetParameterModel::OutRole).toInt() - m_model->data(m_index, AssetParameterModel::InRole).toInt();
0079         qDebug() << "=== REPLACING WITH MAX OUT: " << out;
0080         if (convertToTime) {
0081             max.replace(QLatin1String("-1="), QString("%1=").arg(m_model->framesToTime(out)));
0082         } else {
0083             max.replace(QLatin1String("-1="), QString("%1=").arg(out));
0084         }
0085     }
0086     qDebug() << "=== GOT FILTER IN ROLE: " << m_model->data(m_index, AssetParameterModel::InRole).toInt()
0087              << " / OUT: " << m_model->data(m_index, AssetParameterModel::OutRole).toInt();
0088     qDebug() << "==== COMPARING MULTISWITCH: " << value << " = " << max;
0089     m_checkBox->setChecked(value == max);
0090 }