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

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jean-Baptiste Mardelle
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "switchparamwidget.hpp"
0007 #include "assets/model/assetparametermodel.hpp"
0008 
0009 SwitchParamWidget::SwitchParamWidget(std::shared_ptr<AssetParameterModel> model, QModelIndex index, QWidget *parent)
0010     : AbstractParamWidget(std::move(model), index, parent)
0011 {
0012     setupUi(this);
0013 
0014     // setup the comment
0015     QString comment = m_model->data(m_index, AssetParameterModel::CommentRole).toString();
0016     setToolTip(comment);
0017     m_labelComment->setText(comment);
0018     m_widgetComment->setHidden(true);
0019 
0020     // setup the name
0021     m_labelName->setText(m_model->data(m_index, Qt::DisplayRole).toString());
0022     setMinimumHeight(m_labelName->sizeHint().height());
0023 
0024     // set check state
0025     slotRefresh();
0026 
0027     // Q_EMIT the signal of the base class when appropriate
0028     connect(this->m_checkBox, &QCheckBox::stateChanged, this, [this](int state) {
0029         Q_EMIT valueChanged(m_index,
0030                             (state == Qt::Checked ? m_model->data(m_index, AssetParameterModel::MaxRole).toString()
0031                                                   : m_model->data(m_index, AssetParameterModel::MinRole).toString()),
0032                             true);
0033     });
0034 }
0035 
0036 void SwitchParamWidget::slotShowComment(bool show)
0037 {
0038     if (!m_labelComment->text().isEmpty()) {
0039         m_widgetComment->setVisible(show);
0040     }
0041 }
0042 
0043 void SwitchParamWidget::slotRefresh()
0044 {
0045     const QSignalBlocker bk(m_checkBox);
0046     QString max = m_model->data(m_index, AssetParameterModel::MaxRole).toString();
0047     QString val = m_model->data(m_index, AssetParameterModel::ValueRole).toString();
0048     if (val == max) {
0049         m_checkBox->setChecked(true);
0050     } else {
0051         QString min = m_model->data(m_index, AssetParameterModel::MinRole).toString();
0052         if (val == min) {
0053             m_checkBox->setChecked(false);
0054             return;
0055         }
0056         if (val.contains(QLatin1Char(';'))) {
0057             // Value is not equal to min/max, might be caused by a frame > timecode replacement
0058             max = max.section(QLatin1Char(';'), 0, 0);
0059             val = val.section(QLatin1Char(';'), 0, 0);
0060             if (val.endsWith(max.section(QLatin1Char(' '), -1))) {
0061                 m_checkBox->setChecked(true);
0062             } else {
0063                 m_checkBox->setChecked(false);
0064             }
0065         }
0066     }
0067 }