File indexing completed on 2024-05-19 04:29:04

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2008 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "kis_paintop_option.h"
0008 
0009 
0010 #include <QWidget>
0011 
0012 #include <klocalizedstring.h>
0013 
0014 #include <KisResourcesInterface.h>
0015 #include <KoCanvasResourcesInterface.h>
0016 
0017 #include <lager/constant.hpp>
0018 #include <lager/state.hpp>
0019 #include <kis_paintop_lod_limitations.h>
0020 
0021 struct KisPaintOpOption::Private
0022 {
0023 public:
0024     lager::state<bool, lager::automatic_tag> checkedFallback;
0025     lager::cursor<bool> checkedCursor;
0026     lager::reader<bool> externallyEnabledReader;
0027     lager::reader<bool> pageEnabledReader;
0028     QString label;
0029     KisPaintOpOption::PaintopCategory category;
0030 
0031     /// the configuration page is owned by the higher level
0032     /// QStackWidget, so we shouldn't care about that
0033     QWidget *configurationPage {nullptr};
0034 
0035     bool updatesBlocked {false};
0036     bool isWritingSettings {false};
0037 
0038     KisResourcesInterfaceSP resourcesInterface;
0039     KoCanvasResourcesInterfaceSP canvasResourcesInterface;
0040 };
0041 
0042 KisPaintOpOption::KisPaintOpOption(const QString &label, PaintopCategory category, bool checked)
0043     : m_checkable(true)
0044     , m_d(new Private())
0045 
0046 {
0047     m_d->label = label;
0048     m_d->checkedFallback.set(checked);
0049     m_d->checkedCursor = m_d->checkedFallback;
0050     m_d->externallyEnabledReader = lager::make_constant(true);
0051     m_d->pageEnabledReader = m_d->checkedCursor;
0052     m_d->category = category;
0053     m_d->pageEnabledReader.bind(std::bind(&KisPaintOpOption::slotEnablePageWidget, this, std::placeholders::_1));
0054     m_d->checkedCursor.bind(std::bind(&KisPaintOpOption::sigCheckedChanged, this, std::placeholders::_1));
0055     m_d->externallyEnabledReader.bind(std::bind(&KisPaintOpOption::sigEnabledChanged, this, std::placeholders::_1));
0056 
0057 }
0058 
0059 KisPaintOpOption::KisPaintOpOption(const QString &label, KisPaintOpOption::PaintopCategory category,
0060                                    lager::cursor<bool> checkedCursor)
0061     : KisPaintOpOption(label, category, checkedCursor, lager::make_constant(true))
0062 {
0063 
0064 }
0065 
0066 KisPaintOpOption::KisPaintOpOption(const QString &label, KisPaintOpOption::PaintopCategory category,
0067                                    lager::cursor<bool> checkedCursor,
0068                                    lager::reader<bool> externallyEnabledLink)
0069     : m_checkable(true)
0070     , m_d(new Private())
0071 {
0072     m_d->label = label;
0073     m_d->checkedCursor = checkedCursor;
0074     m_d->externallyEnabledReader = externallyEnabledLink;
0075     m_d->pageEnabledReader = lager::with(m_d->checkedCursor, m_d->externallyEnabledReader).map(std::logical_and{});
0076     m_d->category = category;
0077     m_d->pageEnabledReader.bind(std::bind(&KisPaintOpOption::slotEnablePageWidget, this, std::placeholders::_1));
0078     m_d->checkedCursor.bind(std::bind(&KisPaintOpOption::sigCheckedChanged, this, std::placeholders::_1));
0079     m_d->externallyEnabledReader.bind(std::bind(&KisPaintOpOption::sigEnabledChanged, this, std::placeholders::_1));
0080 }
0081 
0082 KisPaintOpOption::~KisPaintOpOption()
0083 {
0084     delete m_d;
0085 }
0086 
0087 void KisPaintOpOption::emitSettingChanged()
0088 {
0089     KIS_ASSERT_RECOVER_RETURN(!m_d->isWritingSettings);
0090 
0091     if (!m_d->updatesBlocked) {
0092         emit sigSettingChanged();
0093     }
0094 }
0095 
0096 void KisPaintOpOption::emitCheckedChanged(bool checked)
0097 {
0098     KIS_ASSERT_RECOVER_RETURN(!m_d->isWritingSettings);
0099 
0100     if (!m_d->updatesBlocked) {
0101         emit sigCheckedChanged(checked);
0102     }
0103 }
0104 
0105 void KisPaintOpOption::emitEnabledChanged(bool enabled)
0106 {
0107     KIS_ASSERT_RECOVER_RETURN(!m_d->isWritingSettings);
0108 
0109     if (!m_d->updatesBlocked) {
0110         emit sigEnabledChanged(enabled);
0111     }
0112 }
0113 
0114 void KisPaintOpOption::startReadOptionSetting(const KisPropertiesConfigurationSP setting)
0115 {
0116     m_d->updatesBlocked = true;
0117     readOptionSetting(setting);
0118     m_d->updatesBlocked = false;
0119 }
0120 
0121 void KisPaintOpOption::startWriteOptionSetting(KisPropertiesConfigurationSP setting) const
0122 {
0123     m_d->isWritingSettings = true;
0124     writeOptionSetting(setting);
0125     m_d->isWritingSettings = false;
0126 }
0127 
0128 void KisPaintOpOption::lodLimitations(KisPaintopLodLimitations *l) const
0129 {
0130     Q_UNUSED(l);
0131 }
0132 
0133 KisPaintOpOption::OptionalLodLimitationsReader KisPaintOpOption::effectiveLodLimitations() const
0134 {
0135     OptionalLodLimitationsReader reader = lodLimitationsReader();
0136 
0137     if (reader) {
0138         reader = lager::with(m_d->pageEnabledReader, *reader)
0139             .map([](bool enabled, const KisPaintopLodLimitations &l) {
0140                 return enabled ? l : KisPaintopLodLimitations();
0141             });
0142     }
0143 
0144     return reader;
0145 }
0146 
0147 KisPaintOpOption::OptionalLodLimitationsReader KisPaintOpOption::lodLimitationsReader() const
0148 {
0149     // no limitations by default
0150     return std::nullopt;
0151 }
0152 
0153 KisPaintOpOption::PaintopCategory KisPaintOpOption::category() const
0154 {
0155     return m_d->category;
0156 }
0157 
0158 bool KisPaintOpOption::isChecked() const
0159 {
0160     return m_d->checkedCursor.get();
0161 }
0162 
0163 bool KisPaintOpOption::isCheckable() const {
0164     return m_checkable;
0165 }
0166 
0167 void KisPaintOpOption::setChecked(bool checked)
0168 {
0169     m_d->checkedCursor.set(checked);
0170 
0171     emitSettingChanged();
0172 }
0173 
0174 bool KisPaintOpOption::isEnabled() const
0175 {
0176     return m_d->externallyEnabledReader.get();
0177 }
0178 
0179 void KisPaintOpOption::setImage(KisImageWSP image)
0180 {
0181     Q_UNUSED(image);
0182 }
0183 
0184 void KisPaintOpOption::setNode(KisNodeWSP node)
0185 {
0186     Q_UNUSED(node);
0187 }
0188 
0189 void KisPaintOpOption::setResourcesInterface(KisResourcesInterfaceSP resourcesInterface)
0190 {
0191     m_d->resourcesInterface = resourcesInterface;
0192 }
0193 
0194 void KisPaintOpOption::setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP canvasResourcesInterface)
0195 {
0196     m_d->canvasResourcesInterface = canvasResourcesInterface;
0197 }
0198 
0199 KisResourcesInterfaceSP KisPaintOpOption::resourcesInterface() const
0200 {
0201     return m_d->resourcesInterface;
0202 }
0203 
0204 KoCanvasResourcesInterfaceSP KisPaintOpOption::canvasResourcesInterface() const
0205 {
0206     return m_d->canvasResourcesInterface;
0207 }
0208 
0209 void KisPaintOpOption::setConfigurationPage(QWidget * page)
0210 {
0211     if (m_d->configurationPage && !m_d->checkedCursor.get()) {
0212         m_d->configurationPage->setEnabled(true);
0213     }
0214 
0215     m_d->configurationPage = page;
0216 
0217     if (m_d->configurationPage) {
0218         m_d->configurationPage->setEnabled(m_d->checkedCursor.get());
0219     }
0220 }
0221 
0222 QWidget* KisPaintOpOption::configurationPage() const
0223 {
0224     return m_d->configurationPage;
0225 }
0226 
0227 void KisPaintOpOption::slotEnablePageWidget(bool value)
0228 {
0229     if (m_d->configurationPage) {
0230         m_d->configurationPage->setEnabled(value);
0231     }
0232 }
0233 
0234 void KisPaintOpOption::setLocked(bool value)
0235 {
0236     m_locked = value;
0237 }
0238 
0239 bool KisPaintOpOption::isLocked ()const
0240 {
0241     return m_locked;
0242 }
0243 
0244 QString KisPaintOpOption::label() const
0245 {
0246     return m_d->label;
0247 }