File indexing completed on 2024-12-22 04:15:20

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 <filter/kis_filter_configuration.h>
0010 #include <kis_filter_registry.h>
0011 #include <KisGlobalResourcesInterface.h>
0012 #include <KoChannelInfo.h>
0013 #include <KoColorModelStandardIds.h>
0014 #include <KoColorSpaceRegistry.h>
0015 #include <kis_signals_blocker.h>
0016 #include <KisViewManager.h>
0017 #include <kis_canvas_resource_provider.h>
0018 
0019 
0020 #include "KisHalftoneConfigWidget.h"
0021 #include "KisHalftoneConfigPageWidget.h"
0022 #include "KisHalftoneFilterConfiguration.h"
0023 
0024 KisHalftoneConfigWidget::KisHalftoneConfigWidget(QWidget *parent,  const KisPaintDeviceSP dev)
0025     : KisConfigWidget(parent)
0026     , m_paintDevice(dev)
0027     , m_intensityWidget(nullptr)
0028 {
0029     Q_ASSERT(m_paintDevice);
0030 
0031     m_channelsInfo = m_paintDevice->colorSpace()->channels();
0032     m_colorModelId = m_paintDevice->colorSpace()->colorModelId().id();
0033 
0034     m_ui.setupUi(this);
0035 
0036     const QString intensityString = i18nc("Brightness in HSI color model", "Intensity");
0037     const QString independentChannelsString = i18n("Independent Channels");
0038     const QString alphaString = KoColorSpaceRegistry::instance()->graya8()->channels().at(1)->name();
0039     const QString grayString = KoColorSpaceRegistry::instance()->graya8()->channels().at(0)->name();
0040 
0041     if (m_colorModelId == AlphaColorModelID.id()) {
0042         m_ui.comboBoxMode->addItem(alphaString);
0043     } else if (m_colorModelId == GrayColorModelID.id()) {
0044         m_ui.comboBoxMode->addItem(grayString);
0045     } else if (m_colorModelId == GrayAColorModelID.id()) {
0046         m_ui.comboBoxMode->addItem(grayString);
0047         m_ui.comboBoxMode->addItem(alphaString);
0048     } else {
0049         m_ui.comboBoxMode->addItem(intensityString);
0050         m_ui.comboBoxMode->addItem(independentChannelsString);
0051         m_ui.comboBoxMode->addItem(alphaString);
0052         m_intensityWidget = new KisHalftoneConfigPageWidget(this, m_paintDevice);
0053         m_intensityWidget->hide();
0054         connect(m_intensityWidget, SIGNAL(signal_configurationUpdated()), this, SIGNAL(sigConfigurationItemChanged()));
0055     }
0056 
0057     int alphaPos = static_cast<int>(m_paintDevice->colorSpace()->alphaPos());
0058     for (int i = 0; i < m_channelsInfo.size(); ++i)
0059     {
0060         KisHalftoneConfigPageWidget * w = new KisHalftoneConfigPageWidget(this, m_paintDevice);
0061         if (m_colorModelId != GrayColorModelID.id() && m_colorModelId != GrayAColorModelID.id()) {
0062             w->hideColors();
0063             if (i != alphaPos) {
0064                 w->setContentsMargins(10, 10, 10, 10);
0065             }
0066         } else {
0067             if (i == alphaPos) {
0068                 w->hideColors();
0069             }
0070         }
0071         w->hide();
0072         m_channelWidgets.append(w);
0073         connect(w, SIGNAL(signal_configurationUpdated()), this, SIGNAL(sigConfigurationItemChanged()));
0074     }
0075 
0076     connect(m_ui.comboBoxMode, SIGNAL(currentIndexChanged(int)), this, SLOT(slot_comboBoxMode_currentIndexChanged(int)));
0077 }
0078 
0079 KisHalftoneConfigWidget::~KisHalftoneConfigWidget()
0080 {}
0081 
0082 void KisHalftoneConfigWidget::setConfiguration(const KisPropertiesConfigurationSP config)
0083 {
0084     const KisHalftoneFilterConfigurationSP filterConfig =
0085         const_cast<KisHalftoneFilterConfiguration*>(
0086             dynamic_cast<const KisHalftoneFilterConfiguration*>(config.data())
0087         );
0088 
0089     {
0090         KisSignalsBlocker signalsBlocker(this);
0091 
0092         QString mode = filterConfig->mode();
0093 
0094         if (m_colorModelId == AlphaColorModelID.id()) {
0095             m_channelWidgets[0]->setConfiguration(filterConfig, "alpha_");
0096             m_ui.comboBoxMode->setCurrentIndex(0);
0097         } else if (m_colorModelId == GrayColorModelID.id()) {
0098             m_channelWidgets[0]->setConfiguration(filterConfig, "intensity_");
0099             m_ui.comboBoxMode->setCurrentIndex(0);
0100         } else if (m_colorModelId == GrayAColorModelID.id()) {
0101             m_channelWidgets[0]->setConfiguration(filterConfig, "intensity_");
0102             m_channelWidgets[1]->setConfiguration(filterConfig, "alpha_");
0103 
0104             if (mode == KisHalftoneFilterConfiguration::HalftoneMode_Alpha) {
0105                 m_ui.comboBoxMode->setCurrentIndex(1);
0106             } else {
0107                 m_ui.comboBoxMode->setCurrentIndex(0);
0108             }
0109         } else {
0110             int alphaPos = m_paintDevice->colorSpace()->alphaPos();
0111             m_intensityWidget->setConfiguration(filterConfig, "intensity_");
0112             for (int i = 0; i < m_channelWidgets.size(); ++i) {
0113                 if (i != alphaPos) {
0114                     m_channelWidgets[i]->setConfiguration(filterConfig, m_colorModelId + "_channel" + QString::number(i) + "_");
0115                 }
0116             }
0117             m_channelWidgets[alphaPos]->setConfiguration(filterConfig, "alpha_");
0118 
0119             if (mode == KisHalftoneFilterConfiguration::HalftoneMode_Alpha) {
0120                 m_ui.comboBoxMode->setCurrentIndex(2);
0121             } else if (mode == KisHalftoneFilterConfiguration::HalftoneMode_IndependentChannels) {
0122                 m_ui.comboBoxMode->setCurrentIndex(1);
0123             } else {
0124                 m_ui.comboBoxMode->setCurrentIndex(0);
0125             }
0126         }
0127 
0128         slot_comboBoxMode_currentIndexChanged(m_ui.comboBoxMode->currentIndex());
0129     }
0130     emit sigConfigurationItemChanged();
0131 }
0132 
0133 KisPropertiesConfigurationSP KisHalftoneConfigWidget::configuration() const
0134 {
0135     KisFilterSP filter = KisFilterRegistry::instance()->get("halftone");
0136     KisHalftoneFilterConfigurationSP filterConfig =
0137         dynamic_cast<KisHalftoneFilterConfiguration*>(
0138             filter->factoryConfiguration(KisGlobalResourcesInterface::instance()).data()
0139         );
0140 
0141     filterConfig->setColorModelId(m_colorModelId);
0142     
0143     if (m_colorModelId == AlphaColorModelID.id()) {
0144         filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Alpha);
0145         m_channelWidgets[0]->configuration(filterConfig, "alpha_");
0146     } else if (m_colorModelId == GrayColorModelID.id()) {
0147         filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Intensity);
0148         m_channelWidgets[0]->configuration(filterConfig, "intensity_");
0149     } else if (m_colorModelId == GrayAColorModelID.id()) {
0150         if (m_ui.comboBoxMode->currentIndex() == 0) {
0151             filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Intensity);
0152         } else {
0153             filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Alpha);
0154         }
0155         m_channelWidgets[0]->configuration(filterConfig, "intensity_");
0156         m_channelWidgets[1]->configuration(filterConfig, "alpha_");
0157     } else {
0158         int alphaPos = m_paintDevice->colorSpace()->alphaPos();
0159         if (m_ui.comboBoxMode->currentIndex() == 0) {
0160             filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Intensity);
0161         } else if (m_ui.comboBoxMode->currentIndex() == 1) {
0162             filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_IndependentChannels);
0163         } else {
0164             filterConfig->setMode(KisHalftoneFilterConfiguration::HalftoneMode_Alpha);
0165         }
0166         m_intensityWidget->configuration(filterConfig, "intensity_");
0167         for (int i = 0; i < m_channelWidgets.size(); ++i) {
0168             if (i != alphaPos) {
0169                 m_channelWidgets[i]->configuration(filterConfig, m_colorModelId + "_channel" + QString::number(i) + "_");
0170             }
0171         }
0172         m_channelWidgets[alphaPos]->configuration(filterConfig, "alpha_");
0173     }
0174 
0175     return filterConfig;
0176 }
0177 
0178 void KisHalftoneConfigWidget::setView(KisViewManager *view)
0179 {
0180     if (m_intensityWidget) {
0181         m_intensityWidget->setView(view);
0182     }
0183     for (KisHalftoneConfigPageWidget *configPageWidget : m_channelWidgets) {
0184         if (configPageWidget) {
0185             configPageWidget->setView(view);
0186         }
0187     }
0188 
0189     KoCanvasResourcesInterfaceSP canvasResourcesInterface = view ? view->canvasResourceProvider()->resourceManager()->canvasResourcesInterface() : nullptr;
0190     setCanvasResourcesInterface(canvasResourcesInterface);
0191 }
0192 
0193 void KisHalftoneConfigWidget::setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
0194 {
0195     if (m_intensityWidget) {
0196         m_intensityWidget->setCanvasResourcesInterface(canvasResourcesInterface);
0197     }
0198     for (KisHalftoneConfigPageWidget *configPageWidget : m_channelWidgets) {
0199         if (configPageWidget) {
0200             configPageWidget->setCanvasResourcesInterface(canvasResourcesInterface);
0201         }
0202     }
0203 }
0204 
0205 void KisHalftoneConfigWidget::slot_comboBoxMode_currentIndexChanged(int index)
0206 {
0207     while (m_ui.tabWidgetChannels->count()) {
0208         m_ui.tabWidgetChannels->removeTab(0);
0209     }
0210     
0211     if (m_colorModelId == AlphaColorModelID.id() || m_colorModelId == GrayColorModelID.id()) {
0212         m_ui.tabWidgetChannels->addTab(m_channelWidgets[0], m_channelsInfo.at(0)->name());
0213     } else if (m_colorModelId == GrayAColorModelID.id()) {
0214         if (index == 0) {
0215             m_ui.tabWidgetChannels->addTab(m_channelWidgets[0], m_channelsInfo.at(0)->name());
0216         } else {
0217             m_ui.tabWidgetChannels->addTab(m_channelWidgets[1], m_channelsInfo.at(1)->name());
0218         }
0219     } else {
0220         int alphaPos = m_paintDevice->colorSpace()->alphaPos();
0221         if (index == 0) {
0222             m_ui.tabWidgetChannels->addTab(m_intensityWidget, i18nc("Brightness in HSI color model", "Intensity"));
0223         } else if (index == 1) {
0224             for (int i = 0; i < m_channelsInfo.size(); ++i) {
0225                 if (i != alphaPos) {
0226                     m_ui.tabWidgetChannels->addTab(m_channelWidgets[i], m_channelsInfo.at(i)->name());
0227                 }
0228             }
0229         } else {
0230             m_ui.tabWidgetChannels->addTab(m_channelWidgets[alphaPos], m_channelsInfo.at(alphaPos)->name());
0231         }
0232     }
0233 
0234     emit sigConfigurationItemChanged();
0235 }