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

0001 /*
0002     SPDX-FileCopyrightText: 2018 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "slidewidget.hpp"
0007 #include "assets/model/assetparametermodel.hpp"
0008 
0009 SlideWidget::SlideWidget(std::shared_ptr<AssetParameterModel> model, QModelIndex index, QWidget *parent)
0010     : AbstractParamWidget(std::move(model), index, parent)
0011 {
0012     // setup the comment
0013     setupUi(this);
0014     QString comment = m_model->data(m_index, AssetParameterModel::CommentRole).toString();
0015 
0016     slotRefresh();
0017     connect(end_up, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0018     connect(end_down, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0019     connect(end_left, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0020     connect(end_right, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0021     connect(end_center, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0022     connect(start_up, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0023     connect(start_down, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0024     connect(start_left, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0025     connect(start_right, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0026     connect(start_center, &QAbstractButton::clicked, this, &SlideWidget::updateValue);
0027     connect(start_transp, &QAbstractSlider::valueChanged, this, &SlideWidget::updateValue);
0028     connect(end_transp, &QAbstractSlider::valueChanged, this, &SlideWidget::updateValue);
0029 
0030     // Q_EMIT the signal of the base class when appropriate
0031     connect(this, &SlideWidget::modified, [this](const QString &val) { Q_EMIT valueChanged(m_index, val, true); });
0032 
0033     // setup comment
0034     setToolTip(comment);
0035 }
0036 
0037 void SlideWidget::slotShowComment(bool) {}
0038 
0039 void SlideWidget::slotRefresh()
0040 {
0041     QString value = m_model->data(m_index, AssetParameterModel::ValueRole).toString();
0042     QColor bg = QPalette().highlight().color();
0043     setStyleSheet(QStringLiteral("QPushButton:checked {background-color:rgb(%1,%2,%3);}").arg(bg.red()).arg(bg.green()).arg(bg.blue()));
0044     wipeInfo w = getWipeInfo(value);
0045     switch (w.start) {
0046     case UP:
0047         start_up->setChecked(true);
0048         break;
0049     case DOWN:
0050         start_down->setChecked(true);
0051         break;
0052     case RIGHT:
0053         start_right->setChecked(true);
0054         break;
0055     case LEFT:
0056         start_left->setChecked(true);
0057         break;
0058     default:
0059         start_center->setChecked(true);
0060         break;
0061     }
0062     switch (w.end) {
0063     case UP:
0064         end_up->setChecked(true);
0065         break;
0066     case DOWN:
0067         end_down->setChecked(true);
0068         break;
0069     case RIGHT:
0070         end_right->setChecked(true);
0071         break;
0072     case LEFT:
0073         end_left->setChecked(true);
0074         break;
0075     default:
0076         end_center->setChecked(true);
0077         break;
0078     }
0079     start_transp->setValue(w.startTransparency);
0080     end_transp->setValue(w.endTransparency);
0081 }
0082 
0083 void SlideWidget::updateValue()
0084 {
0085     wipeInfo info;
0086     if (start_left->isChecked()) {
0087         info.start = LEFT;
0088     } else if (start_right->isChecked()) {
0089         info.start = RIGHT;
0090     } else if (start_up->isChecked()) {
0091         info.start = UP;
0092     } else if (start_down->isChecked()) {
0093         info.start = DOWN;
0094     } else if (start_center->isChecked()) {
0095         info.start = CENTER;
0096     } else {
0097         info.start = LEFT;
0098     }
0099     info.startTransparency = start_transp->value();
0100 
0101     if (end_left->isChecked()) {
0102         info.end = LEFT;
0103     } else if (end_right->isChecked()) {
0104         info.end = RIGHT;
0105     } else if (end_up->isChecked()) {
0106         info.end = UP;
0107     } else if (end_down->isChecked()) {
0108         info.end = DOWN;
0109     } else if (end_center->isChecked()) {
0110         info.end = CENTER;
0111     } else {
0112         info.end = RIGHT;
0113     }
0114     info.endTransparency = end_transp->value();
0115     Q_EMIT modified(getWipeString(info));
0116 }
0117 
0118 SlideWidget::wipeInfo SlideWidget::getWipeInfo(QString value)
0119 {
0120     wipeInfo info;
0121     // Convert old geometry values that used a comma as separator
0122     if (value.contains(QLatin1Char(','))) {
0123         value.replace(',', '/');
0124     }
0125     QString start = value.section(QLatin1Char(';'), 0, 0).section(QLatin1Char('='), 1);
0126     QString end = value.section(QLatin1Char(';'), 1, 1).section(QLatin1Char('='), 1, 1);
0127     if (start.startsWith(QLatin1String("-100% 0"))) {
0128         info.start = LEFT;
0129     } else if (start.startsWith(QLatin1String("100% 0"))) {
0130         info.start = RIGHT;
0131     } else if (start.startsWith(QLatin1String("0% 100%"))) {
0132         info.start = DOWN;
0133     } else if (start.startsWith(QLatin1String("0% -100%"))) {
0134         info.start = UP;
0135     } else {
0136         info.start = CENTER;
0137     }
0138 
0139     if (start.split(QLatin1Char(' ')).count() == 5) {
0140         qDebug() << "== READING START TR: " << start.section(QLatin1Char(' '), -1);
0141         info.startTransparency = start.section(QLatin1Char(' '), -1).section(QLatin1Char('%'), 0, 0).toInt();
0142     } else {
0143         info.startTransparency = 100;
0144     }
0145 
0146     if (end.startsWith(QLatin1String("-100% 0"))) {
0147         info.end = LEFT;
0148     } else if (end.startsWith(QLatin1String("100% 0"))) {
0149         info.end = RIGHT;
0150     } else if (end.startsWith(QLatin1String("0% 100%"))) {
0151         info.end = DOWN;
0152     } else if (end.startsWith(QLatin1String("0% -100%"))) {
0153         info.end = UP;
0154     } else {
0155         info.end = CENTER;
0156     }
0157 
0158     if (end.split(QLatin1Char(' ')).count() == 5) {
0159         info.endTransparency = end.section(QLatin1Char(' '), -1).section(QLatin1Char('%'), 0, 0).toInt();
0160     } else {
0161         info.endTransparency = 100;
0162     }
0163 
0164     return info;
0165 }
0166 
0167 const QString SlideWidget::getWipeString(wipeInfo info)
0168 {
0169 
0170     QString start;
0171     QString end;
0172     switch (info.start) {
0173     case LEFT:
0174         start = QStringLiteral("-100% 0% 100% 100%");
0175         break;
0176     case RIGHT:
0177         start = QStringLiteral("100% 0% 100% 100%");
0178         break;
0179     case DOWN:
0180         start = QStringLiteral("0% 100% 100% 100%");
0181         break;
0182     case UP:
0183         start = QStringLiteral("0% -100% 100% 100%");
0184         break;
0185     default:
0186         start = QStringLiteral("0% 0% 100% 100%");
0187         break;
0188     }
0189     start.append(QString(" %1%").arg(info.startTransparency));
0190 
0191     switch (info.end) {
0192     case LEFT:
0193         end = QStringLiteral("-100% 0% 100% 100%");
0194         break;
0195     case RIGHT:
0196         end = QStringLiteral("100% 0% 100% 100%");
0197         break;
0198     case DOWN:
0199         end = QStringLiteral("0% 100% 100% 100%");
0200         break;
0201     case UP:
0202         end = QStringLiteral("0% -100% 100% 100%");
0203         break;
0204     default:
0205         end = QStringLiteral("0% 0% 100% 100%");
0206         break;
0207     }
0208     end.append(QString(" %1%").arg(info.endTransparency));
0209     return QString("0=%1;-1=%2").arg(start, end);
0210 }