File indexing completed on 2024-06-16 04:16:38

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "kis_wdg_convert_height_to_normal_map.h"
0007 #include <filter/kis_filter_configuration.h>
0008 #include <QComboBox>
0009 #include <klocalizedstring.h>
0010 #include <KoChannelInfo.h>
0011 #include <KisGlobalResourcesInterface.h>
0012 
0013 KisWdgConvertHeightToNormalMap::KisWdgConvertHeightToNormalMap(QWidget *parent, const KoColorSpace *cs)
0014     : KisConfigWidget(parent)
0015     , ui(new Ui_WidgetConvertHeightToNormalMap)
0016     , m_cs(cs)
0017 
0018 {
0019     if (cs->channelCount() < 3) {
0020         QVBoxLayout *layout = new QVBoxLayout(this);
0021         layout->addWidget(new QLabel(i18n("Height to Normal Map does not work on this colorspace.")));
0022         return;
0023     }
0024 
0025 
0026     ui->setupUi(this);
0027     m_types << "prewitt"<< "sobol"<< "simple";
0028     m_types_translatable << i18n("Prewitt") << i18n("Sobel") << i18n("Simple");
0029     QStringList swizzle;
0030     swizzle<< "X+" << "X-" << "Y+" << "Y-" << "Z+" << "Z-";
0031 
0032     ui->cmbType->addItems(m_types_translatable);
0033     ui->cmbRed->addItems(swizzle);
0034     ui->cmbGreen->addItems(swizzle);
0035     ui->cmbBlue->addItems(swizzle);
0036 
0037     const QList<KoChannelInfo*> channels = m_cs->channels();
0038 
0039     for (int c = 0; c < (int)m_cs->channelCount(); c++) {
0040         ui->cmbChannel->addItem(channels.at(c)->name());
0041     }
0042 
0043     ui->btnAspect->setKeepAspectRatio(false);
0044     ui->sldHorizontalRadius->setRange(1.0, 100.0, 2);
0045     ui->sldHorizontalRadius->setSingleStep(0.01);
0046     ui->sldHorizontalRadius->setPrefix(i18n("Horizontal Radius:"));
0047     connect(ui->sldHorizontalRadius, SIGNAL(valueChanged(qreal)), this, SLOT(horizontalRadiusChanged(qreal)));
0048 
0049     ui->sldVerticalRadius->setRange(1.0, 100.0, 2);
0050     ui->sldVerticalRadius->setSingleStep(0.01);
0051     ui->sldVerticalRadius->setPrefix(i18n("Vertical Radius:"));
0052     connect(ui->sldVerticalRadius, SIGNAL(valueChanged(qreal)), this, SLOT(verticalRadiusChanged(qreal)));
0053 
0054     connect(ui->sldHorizontalRadius, SIGNAL(valueChanged(qreal)), this, SIGNAL(sigConfigurationItemChanged()));
0055     connect(ui->sldVerticalRadius, SIGNAL(valueChanged(qreal)), this, SIGNAL(sigConfigurationItemChanged()));
0056     connect(ui->btnAspect, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(aspectLockChanged(bool)));
0057     connect(ui->cmbType, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sigConfigurationItemChanged()));
0058     connect(ui->cmbChannel, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sigConfigurationItemChanged()));
0059     connect(ui->cmbRed, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sigConfigurationItemChanged()));
0060     connect(ui->cmbGreen, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sigConfigurationItemChanged()));
0061     connect(ui->cmbBlue, SIGNAL(currentIndexChanged(int)), this, SIGNAL(sigConfigurationItemChanged()));
0062 
0063 }
0064 
0065 KisWdgConvertHeightToNormalMap::~KisWdgConvertHeightToNormalMap()
0066 {
0067     delete ui;
0068 }
0069 
0070 KisPropertiesConfigurationSP KisWdgConvertHeightToNormalMap::configuration() const
0071 {
0072     KisFilterConfigurationSP config = new KisFilterConfiguration("height to normal", 1, KisGlobalResourcesInterface::instance());
0073     if (m_cs->channelCount() < 3) return config;
0074 
0075     config->setProperty("horizRadius", ui->sldHorizontalRadius->value());
0076     config->setProperty("vertRadius", ui->sldVerticalRadius->value());
0077     config->setProperty("type", m_types.at(ui->cmbType->currentIndex()));
0078     config->setProperty("lockAspect", ui->btnAspect->keepAspectRatio());
0079     config->setProperty("channelToConvert", ui->cmbChannel->currentIndex());
0080     config->setProperty("redSwizzle", ui->cmbRed->currentIndex());
0081     config->setProperty("greenSwizzle", ui->cmbGreen->currentIndex());
0082     config->setProperty("blueSwizzle", ui->cmbBlue->currentIndex());
0083 
0084     return config;
0085 }
0086 
0087 void KisWdgConvertHeightToNormalMap::setConfiguration(const KisPropertiesConfigurationSP config)
0088 {
0089     if (m_cs->channelCount() < 3) return;
0090 
0091     ui->sldHorizontalRadius->setValue(config->getFloat("horizRadius", 1.0));
0092     ui->sldVerticalRadius->setValue(config->getFloat("vertRadius", 1.0));
0093     int index = 0;
0094     if (m_types.contains(config->getString("type", "prewitt"))){
0095         index = m_types.indexOf(config->getString("type", "sobol"));
0096     }
0097     ui->cmbType->setCurrentIndex(index);
0098     ui->cmbChannel->setCurrentIndex(config->getInt("channelToConvert", 0));
0099     ui->btnAspect->setKeepAspectRatio(config->getBool("lockAspect", false));
0100     ui->cmbRed->setCurrentIndex(config->getInt("redSwizzle", xPlus));
0101     ui->cmbGreen->setCurrentIndex(config->getInt("greenSwizzle", yPlus));
0102     ui->cmbBlue->setCurrentIndex(config->getInt("blueSwizzle", zPlus));
0103 }
0104 
0105 void KisWdgConvertHeightToNormalMap::horizontalRadiusChanged(qreal r)
0106 {
0107     ui->sldHorizontalRadius->blockSignals(true);
0108     ui->sldHorizontalRadius->setValue(r);
0109     ui->sldHorizontalRadius->blockSignals(false);
0110 
0111     if (ui->btnAspect->keepAspectRatio()) {
0112         ui->sldVerticalRadius->blockSignals(true);
0113         ui->sldVerticalRadius->setValue(r);
0114         ui->sldVerticalRadius->blockSignals(false);
0115     }
0116 }
0117 
0118 void KisWdgConvertHeightToNormalMap::verticalRadiusChanged(qreal r)
0119 {
0120     ui->sldVerticalRadius->blockSignals(true);
0121     ui->sldVerticalRadius->setValue(r);
0122     ui->sldVerticalRadius->blockSignals(false);
0123 
0124     if (ui->btnAspect->keepAspectRatio()) {
0125         ui->sldHorizontalRadius->blockSignals(true);
0126         ui->sldHorizontalRadius->setValue(r);
0127         ui->sldHorizontalRadius->blockSignals(false);
0128     }
0129 }
0130 
0131 void KisWdgConvertHeightToNormalMap::aspectLockChanged(bool v)
0132 {
0133     if (v) {
0134         ui->sldVerticalRadius->setValue( ui->sldHorizontalRadius->value() );
0135     }
0136 }