File indexing completed on 2024-05-26 04:33:06

0001 /*
0002  * KDE. Krita Project.
0003  *
0004  * SPDX-FileCopyrightText: 2020 Deif Lou <ginoba@gmail.com>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include <generator/kis_generator.h>
0010 #include <generator/kis_generator_registry.h>
0011 #include <KoColor.h>
0012 #include <KisGlobalResourcesInterface.h>
0013 #include <filter/kis_filter_configuration.h>
0014 #include <kis_config_widget.h>
0015 #include <kis_signals_blocker.h>
0016 #include <KisSpinBoxI18nHelper.h>
0017 
0018 #include <QStringList>
0019 #include <QScrollBar>
0020 #include <QResizeEvent>
0021 
0022 #include "KisHalftoneConfigPageWidget.h"
0023 
0024 KisHalftoneConfigPageWidget::KisHalftoneConfigPageWidget(QWidget *parent, const KisPaintDeviceSP dev)
0025     : QWidget(parent)
0026     , m_paintDevice(dev)
0027     , m_generatorWidget(nullptr)
0028     , m_view(nullptr)
0029 {
0030     Q_ASSERT(m_paintDevice);
0031 
0032     ui()->setupUi(this);
0033 
0034     m_generatorIds = KisGeneratorRegistry::instance()->keys();
0035     m_generatorIds.sort();
0036 
0037     ui()->comboBoxGenerator->addItem(i18n("None"));
0038     for (const QString &generatorId : m_generatorIds) {
0039         KisGeneratorSP generator = KisGeneratorRegistry::instance()->get(generatorId);
0040         ui()->comboBoxGenerator->addItem(generator->name());
0041     }
0042 
0043     QVBoxLayout *generatorContainerLayout = new QVBoxLayout(ui()->widgetGeneratorContainer);
0044     generatorContainerLayout->setContentsMargins(0, 0, 0, 0);
0045 
0046     ui()->sliderHardness->setRange(0.0, 100.0, 2);
0047     ui()->sliderHardness->setSingleStep(1.0);
0048     KisSpinBoxI18nHelper::setText(ui()->sliderHardness,
0049                                   i18nc("{n} is the number value, % is the percent sign", "{n}%"));
0050 
0051     ui()->sliderForegroundOpacity->setRange(0, 100);
0052     KisSpinBoxI18nHelper::setText(ui()->sliderForegroundOpacity,
0053                                   i18nc("{n} is the number value, % is the percent sign", "Opacity: {n}%"));
0054     ui()->sliderBackgroundOpacity->setRange(0, 100);
0055     KisSpinBoxI18nHelper::setText(ui()->sliderBackgroundOpacity,
0056                                   i18nc("{n} is the number value, % is the percent sign", "Opacity: {n}%"));
0057 
0058     connect(ui()->comboBoxGenerator, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboBoxGenerator_currentIndexChanged(int)));
0059     connect(ui()->sliderHardness, SIGNAL(valueChanged(qreal)), this, SIGNAL(signal_configurationUpdated()));
0060     connect(ui()->checkBoxInvert, SIGNAL(toggled(bool)), this, SIGNAL(signal_configurationUpdated()));
0061     connect(ui()->buttonForegroundColor, SIGNAL(changed(const KoColor&)), this, SIGNAL(signal_configurationUpdated()));
0062     connect(ui()->sliderForegroundOpacity, SIGNAL(valueChanged(int)), this, SIGNAL(signal_configurationUpdated()));
0063     connect(ui()->buttonBackgroundColor, SIGNAL(changed(const KoColor&)), this, SIGNAL(signal_configurationUpdated()));
0064     connect(ui()->sliderBackgroundOpacity, SIGNAL(valueChanged(int)), this, SIGNAL(signal_configurationUpdated()));
0065 }
0066 
0067 KisHalftoneConfigPageWidget::~KisHalftoneConfigPageWidget()
0068 {}
0069 
0070 const Ui_HalftoneConfigPageWidget * KisHalftoneConfigPageWidget::ui() const
0071 {
0072     return &m_ui;
0073 }
0074 
0075 Ui_HalftoneConfigPageWidget * KisHalftoneConfigPageWidget::ui()
0076 {
0077     return &m_ui;
0078 }
0079 
0080 void KisHalftoneConfigPageWidget::showColors()
0081 {
0082     setColorsVisible(true);
0083 }
0084 
0085 void KisHalftoneConfigPageWidget::hideColors()
0086 {
0087     setColorsVisible(false);
0088 }
0089 
0090 void KisHalftoneConfigPageWidget::setColorsVisible(bool show)
0091 {
0092     ui()->labelForeground->setVisible(show);
0093     ui()->buttonForegroundColor->setVisible(show);
0094     ui()->sliderForegroundOpacity->setVisible(show);
0095     ui()->labelBackground->setVisible(show);
0096     ui()->buttonBackgroundColor->setVisible(show);
0097     ui()->sliderBackgroundOpacity->setVisible(show);
0098 }
0099 
0100 void KisHalftoneConfigPageWidget::setConfiguration(const KisHalftoneFilterConfigurationSP config, const QString & prefix)
0101 {
0102     {
0103         KisSignalsBlocker signalsBlocker(this, ui()->comboBoxGenerator);
0104 
0105         QString generatorId = config->generatorId(prefix);
0106         int generatorIndex = m_generatorIds.indexOf(generatorId);
0107         if (generatorIndex == -1) {
0108             ui()->comboBoxGenerator->setCurrentIndex(0);
0109             setGenerator("", nullptr);
0110         } else {
0111             ui()->comboBoxGenerator->setCurrentIndex(generatorIndex + 1);
0112             KisFilterConfigurationSP generatorConfiguration = config->generatorConfiguration(prefix);
0113             setGenerator(generatorId, generatorConfiguration);
0114         }
0115 
0116         ui()->sliderHardness->setValue(config->hardness(prefix));
0117         ui()->checkBoxInvert->setChecked(config->invert(prefix));
0118         ui()->buttonForegroundColor->setColor(config->foregroundColor(prefix));
0119         ui()->sliderForegroundOpacity->setValue(config->foregroundOpacity(prefix));
0120         ui()->buttonBackgroundColor->setColor(config->backgroundColor(prefix));
0121         ui()->sliderBackgroundOpacity->setValue(config->backgroundOpacity(prefix));
0122     }
0123     emit signal_configurationUpdated();
0124 }
0125 
0126 void KisHalftoneConfigPageWidget::configuration(KisHalftoneFilterConfigurationSP config, const QString & prefix) const
0127 {
0128     if (ui()->comboBoxGenerator->currentIndex() == 0) {
0129         config->setGeneratorId(prefix, "");
0130     } else {
0131         QString generatorId = m_generatorIds.at(ui()->comboBoxGenerator->currentIndex() - 1);
0132         config->setGeneratorId(prefix, generatorId);
0133         if (m_generatorWidget) {
0134             config->setGeneratorConfiguration(
0135                 prefix, dynamic_cast<KisFilterConfiguration*>(m_generatorWidget->configuration().data())
0136             );
0137         }
0138     }
0139 
0140     config->setHardness(prefix, ui()->sliderHardness->value());
0141     config->setInvert(prefix, ui()->checkBoxInvert->isChecked());
0142     config->setForegroundColor(prefix, ui()->buttonForegroundColor->color());
0143     config->setForegroundOpacity(prefix, ui()->sliderForegroundOpacity->value());
0144     config->setBackgroundColor(prefix, ui()->buttonBackgroundColor->color());
0145     config->setBackgroundOpacity(prefix, ui()->sliderBackgroundOpacity->value());
0146 }
0147 
0148 void KisHalftoneConfigPageWidget::setGenerator(const QString & generatorId, KisFilterConfigurationSP config)
0149 {
0150     if (m_generatorWidget && m_currentGeneratorId != generatorId) {
0151         ui()->widgetGeneratorContainer->layout()->removeWidget(m_generatorWidget);
0152         delete m_generatorWidget;
0153         m_generatorWidget = nullptr;
0154     }
0155 
0156     KisGeneratorSP generator = KisGeneratorRegistry::instance()->get(generatorId);
0157     if (generator && !m_generatorWidget) {
0158         KisConfigWidget *generatorWidget = generator->createConfigurationWidget(this, m_paintDevice, false);
0159         if (generatorWidget) {
0160             ui()->widgetGeneratorContainer->layout()->addWidget(generatorWidget);
0161 
0162             if (m_view) {
0163                 generatorWidget->setView(m_view);
0164             } else {
0165                 generatorWidget->setCanvasResourcesInterface(m_canvasResourcesInterface);
0166             }
0167 
0168             m_generatorWidget = generatorWidget;
0169             connect(generatorWidget, SIGNAL(sigConfigurationUpdated()), this, SIGNAL(signal_configurationUpdated()));
0170         }
0171     }
0172 
0173     m_currentGeneratorId = generatorId;
0174 
0175     if (m_generatorWidget) {
0176         if (config) {
0177             m_generatorWidget->setConfiguration(config);
0178         } else {
0179             KisFilterConfigurationSP generatorConfig =
0180                     generator->defaultConfiguration(KisGlobalResourcesInterface::instance());
0181             if (generatorId == "screentone") {
0182                 generatorConfig->setProperty("rotation", 45.0);
0183                 generatorConfig->setProperty("contrast", 50.0);
0184             }
0185             m_generatorWidget->setConfiguration(generatorConfig);
0186         }
0187     }
0188 }
0189 
0190 void KisHalftoneConfigPageWidget::setView(KisViewManager *view)
0191 {
0192     m_view = view;
0193 
0194     if (m_generatorWidget) {
0195         m_generatorWidget->setView(m_view);
0196     }
0197 }
0198 
0199 void KisHalftoneConfigPageWidget::setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
0200 {
0201     m_canvasResourcesInterface = canvasResourcesInterface;
0202 
0203     if (m_generatorWidget) {
0204         m_generatorWidget->setCanvasResourcesInterface(canvasResourcesInterface);
0205     }
0206 }
0207 
0208 void KisHalftoneConfigPageWidget::slot_comboBoxGenerator_currentIndexChanged(int index)
0209 {
0210     if (index < 0 || index > m_generatorIds.size()) {
0211         return;
0212     }
0213 
0214     if (index == 0) {
0215         setGenerator("", nullptr);
0216     } else {
0217         QString generatorId = m_generatorIds.at(index - 1);
0218         setGenerator(generatorId, nullptr);
0219     }
0220 
0221     emit signal_configurationUpdated();
0222 }