diff --git a/plugins/filters/gradientmap/gradientmap.cpp b/plugins/filters/gradientmap/gradientmap.cpp index db680493cb..983ccbfae4 100644 --- a/plugins/filters/gradientmap/gradientmap.cpp +++ b/plugins/filters/gradientmap/gradientmap.cpp @@ -1,100 +1,126 @@ /* * This file is part of the KDE project * * Copyright (c) 2016 Spencer Brown * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "QObject" #include "gradientmap.h" #include #include #include "krita_filter_gradient_map.h" #include "KoResourceServerProvider.h" #include "kis_config_widget.h" #include #include +#include +#include K_PLUGIN_FACTORY_WITH_JSON(KritaGradientMapFactory, "kritagradientmap.json", registerPlugin();) KritaGradientMapConfigWidget::KritaGradientMapConfigWidget(QWidget *parent, KisPaintDeviceSP dev, Qt::WFlags f) : KisConfigWidget(parent, f) { Q_UNUSED(dev) m_page = new WdgGradientMap(this); QHBoxLayout *l = new QHBoxLayout(this); Q_CHECK_PTR(l); l->addWidget(m_page); l->setContentsMargins(0, 0, 0, 0); - - connect(m_page->gradientchooser, SIGNAL(resourceSelected(KoResource*)), SIGNAL(sigConfigurationItemChanged())); + KoResourceServerProvider *serverProvider = KoResourceServerProvider::instance(); + QSharedPointer gradientResourceAdapter( + new KoResourceServerAdapter(serverProvider->gradientServer())); + m_gradientChangedCompressor = new KisSignalCompressor(100, KisSignalCompressor::FIRST_ACTIVE); + + m_gradientPopUp = new KoResourcePopupAction(gradientResourceAdapter, + m_page->btnGradientChooser); + m_activeGradient = KoStopGradient::fromQGradient(dynamic_cast(gradientResourceAdapter->resources().first())->toQGradient()); + m_page->gradientEditor->setGradient(m_activeGradient); + m_page->gradientEditor->setCompactMode(true); + m_page->gradientEditor->setEnabled(true); + m_page->btnGradientChooser->setDefaultAction(m_gradientPopUp); + m_page->btnGradientChooser->setPopupMode(QToolButton::InstantPopup); + connect(m_gradientPopUp, SIGNAL(resourceSelected(QSharedPointer)), this, SLOT(setAbstractGradientToEditor())); + connect(m_page->gradientEditor, SIGNAL(sigGradientChanged()), m_gradientChangedCompressor, SLOT(start())); + connect(m_gradientChangedCompressor, SIGNAL(timeout()), this, SIGNAL(sigConfigurationItemChanged())); } KritaGradientMapConfigWidget::~KritaGradientMapConfigWidget() { delete m_page; } -void KritaGradientMapConfigWidget::gradientResourceChanged(KoResource* gradient) +void KritaGradientMapConfigWidget::setAbstractGradientToEditor() { - Q_UNUSED(gradient) + QSharedPointer bg = + qSharedPointerDynamicCast( + m_gradientPopUp->currentBackground()); + m_activeGradient = KoStopGradient::fromQGradient(bg->gradient()); + m_page->gradientEditor->setGradient(m_activeGradient); + } KisPropertiesConfigurationSP KritaGradientMapConfigWidget::configuration() const { KisFilterConfigurationSP cfg = new KisFilterConfiguration("gradientmap", 2); - if (m_page && m_page->gradientchooser && m_page->gradientchooser->currentResource()) { - KoAbstractGradient *gradient = dynamic_cast(m_page->gradientchooser->currentResource()); - KoStopGradient *stopGradient = KoStopGradient::fromQGradient(gradient->toQGradient()); + if (m_activeGradient) { QDomDocument doc; QDomElement elt = doc.createElement("gradient"); - stopGradient->toXML(doc, elt); + m_activeGradient->toXML(doc, elt); doc.appendChild(elt); cfg->setProperty("gradientXML", doc.toString()); } return cfg; } //----------------------------- void KritaGradientMapConfigWidget::setConfiguration(const KisPropertiesConfigurationSP config) { - //m_page->gradientchooser->setCurrentResource(KoResourceServerProvider::instance()->gradientServer(false)->resourceByName(config->getString("gradientName"))); Q_ASSERT(config); - Q_UNUSED(config); + QDomDocument doc; + if (config->hasProperty("gradientXML")) { + doc.setContent(config->getString("gradientXML", "")); + qDebug()<getString("gradientXML", ""); + KoStopGradient gradient = KoStopGradient::fromXML(doc.firstChildElement()); + if (gradient.stops().size()>0) { + m_activeGradient->setStops(gradient.stops()); + } + } } void KritaGradientMapConfigWidget::setView(KisViewManager *view) { Q_UNUSED(view) } //------------------------------ KritaGradientMap::KritaGradientMap(QObject *parent, const QVariantList &) : QObject(parent) { KisFilterRegistry::instance()->add(KisFilterSP(new KritaFilterGradientMap())); } KritaGradientMap::~KritaGradientMap() { } //----------------------------- #include "gradientmap.moc" diff --git a/plugins/filters/gradientmap/gradientmap.h b/plugins/filters/gradientmap/gradientmap.h index b874933705..fa5fcbebdb 100644 --- a/plugins/filters/gradientmap/gradientmap.h +++ b/plugins/filters/gradientmap/gradientmap.h @@ -1,76 +1,83 @@ /* * This file is part of Krita * * Copyright (c) 2016 Spencer Brown * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #pragma once #include "QObject" #include "ui_wdg_gradientmap.h" #include "kis_properties_configuration.h" #include "filter/kis_color_transformation_configuration.h" #include "kis_config_widget.h" +#include +#include +#include class WdgGradientMap : public QWidget, public Ui::WdgGradientMap { Q_OBJECT public: WdgGradientMap(QWidget *parent) : QWidget(parent) { setupUi(this); } }; class KritaGradientMapFilterConfiguration : public KisColorTransformationConfiguration { public: KritaGradientMapFilterConfiguration(); ~KritaGradientMapFilterConfiguration() override; virtual void setGradient(const KoResource* gradient); virtual const KoResource* gradient() const; private: KoResource const* m_gradient; }; class KritaGradientMapConfigWidget : public KisConfigWidget { Q_OBJECT public: KritaGradientMapConfigWidget(QWidget *parent, KisPaintDeviceSP dev, Qt::WFlags f = 0); ~KritaGradientMapConfigWidget() override; KisPropertiesConfigurationSP configuration() const override; void setConfiguration(const KisPropertiesConfigurationSP config) override; - WdgGradientMap * m_page; + WdgGradientMap *m_page; + KoResourcePopupAction *m_gradientPopUp; + KisSignalCompressor *m_gradientChangedCompressor; + KoStopGradient *m_activeGradient; void setView(KisViewManager *view) override; - void gradientResourceChanged(KoResource *gradient); +private Q_SLOTS: + void setAbstractGradientToEditor(); }; class KritaGradientMap : public QObject { Q_OBJECT public: KritaGradientMap(QObject *parent, const QVariantList &); ~KritaGradientMap() override; }; diff --git a/plugins/filters/gradientmap/krita_filter_gradient_map.cpp b/plugins/filters/gradientmap/krita_filter_gradient_map.cpp index 808e264cb1..a1ae4bef6c 100644 --- a/plugins/filters/gradientmap/krita_filter_gradient_map.cpp +++ b/plugins/filters/gradientmap/krita_filter_gradient_map.cpp @@ -1,120 +1,112 @@ /* * This file is part of the KDE project * * Copyright (c) 2016 Spencer Brown * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "krita_filter_gradient_map.h" #include #include #include #include #include #include "kis_config_widget.h" #include #include #include #include #include #include "gradientmap.h" #include KritaFilterGradientMap::KritaFilterGradientMap() : KisFilter(id(), categoryMap(), i18n("&Gradient Map...")) { setColorSpaceIndependence(FULLY_INDEPENDENT); setShowConfigurationWidget(true); setSupportsLevelOfDetail(true); setSupportsPainting(true); setSupportsAdjustmentLayers(true); setSupportsThreading(true); } void KritaFilterGradientMap::processImpl(KisPaintDeviceSP device, const QRect& applyRect, const KisFilterConfigurationSP config, KoUpdater *progressUpdater) const { Q_ASSERT(!device.isNull()); if (progressUpdater) { progressUpdater->setRange(0, applyRect.height() * applyRect.width()); } QDomDocument doc; if (config->version()==1) { QDomElement elt = doc.createElement("gradient"); KoAbstractGradient *gradientAb = KoResourceServerProvider::instance()->gradientServer(false)->resourceByName(config->getString("gradientName")); if (!gradientAb) { qDebug() << "Could not find gradient" << config->getString("gradientName"); } gradientAb = KoResourceServerProvider::instance()->gradientServer(false)->resources().first(); KoStopGradient::fromQGradient(gradientAb->toQGradient())->toXML(doc, elt); doc.appendChild(elt); } else { doc.setContent(config->getString("gradientXML", "")); } KoStopGradient gradient = KoStopGradient::fromXML(doc.firstChildElement()); - KoColorSet *gradientCache = new KoColorSet(); - for (int i=0; i<256; i++) { - KoColor gc; - gradient.colorAt(gc, ((qreal)i/255.0)); - KoColorSetEntry col; - col.color = gc; - gradientCache->add(col); - } KoColor outColor(Qt::white, device->colorSpace()); KisSequentialIterator it(device, applyRect); int p = 0; quint8 grey; const int pixelSize = device->colorSpace()->pixelSize(); do { grey = device->colorSpace()->intensity8(it.oldRawData()); - outColor = gradientCache->getColorGlobal((quint32)grey).color; + gradient.colorAt(outColor,(qreal)grey/255); outColor.setOpacity(qMin(KoColor(it.oldRawData(), device->colorSpace()).opacityF(), outColor.opacityF())); outColor.convertTo(device->colorSpace()); memcpy(it.rawData(), outColor.data(), pixelSize); if (progressUpdater) progressUpdater->setValue(p++); } while (it.nextPixel()); } KisFilterConfigurationSP KritaFilterGradientMap::factoryConfiguration() const { KisFilterConfigurationSP config = new KisFilterConfiguration("gradientmap", 2); KoAbstractGradient *gradient = KoResourceServerProvider::instance()->gradientServer(false)->resources().first(); KoStopGradient stopGradient; stopGradient.fromQGradient(gradient->toQGradient()); QDomDocument doc; QDomElement elt = doc.createElement("gradient"); stopGradient.toXML(doc, elt); doc.appendChild(elt); config->setProperty("gradientXML", doc.toString()); return config; } KisConfigWidget * KritaFilterGradientMap::createConfigurationWidget(QWidget * parent, const KisPaintDeviceSP dev) const { return new KritaGradientMapConfigWidget(parent, dev); } diff --git a/plugins/filters/gradientmap/wdg_gradientmap.ui b/plugins/filters/gradientmap/wdg_gradientmap.ui index a660db5ff9..16bf69656e 100644 --- a/plugins/filters/gradientmap/wdg_gradientmap.ui +++ b/plugins/filters/gradientmap/wdg_gradientmap.ui @@ -1,39 +1,76 @@ WdgGradientMap 0 0 361 341 Gradient Map - + - - 200 - 200 + + 0 + 0 + + PushButton + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + + Qt::Vertical + + + + 20 + 40 + + + + - KisGradientChooser - QFrame -
kis_gradient_chooser.h
+ KoColorPopupButton + QToolButton +
KoColorPopupButton.h
+
+ + KisStopGradientEditor + QWidget +
kis_stopgradient_editor.h
1