1 /*
2  * This file is part of Krita
3  *
4  * Copyright (c) 2018 Jouni Pentikainen <joupent@gmail.com>
5  * Copyright (c) 2020 L. E. Segovia <amy@amyspark.me>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
22 #ifndef _KIS_MULTICHANNEL_FILTER_BASE_H_
23 #define _KIS_MULTICHANNEL_FILTER_BASE_H_
24 
25 #include <QPair>
26 #include <QList>
27 
28 #include <filter/kis_color_transformation_filter.h>
29 #include <filter/kis_color_transformation_configuration.h>
30 #include <kis_config_widget.h>
31 #include <kis_paint_device.h>
32 #include "ui_wdg_perchannel.h"
33 
34 #include "virtual_channel_info.h"
35 
36 /**
37  * Base class for filters which use curves to operate on multiple channels.
38  */
39 class KisMultiChannelFilter : public KisColorTransformationFilter
40 {
41 public:
42     bool needsTransparentPixels(const KisFilterConfigurationSP config, const KoColorSpace *cs) const override;
43 
44     /**
45      * Get a list of adjustable channels for the color space.
46      * If maxChannels is non-negative, the number of channels is capped to the number. This is useful configurations
47      * from older documents (created in versions which supported fewer channels).
48      */
49     static QVector<VirtualChannelInfo> getVirtualChannels(const KoColorSpace *cs, int maxChannels = -1);
50     static int findChannel(const QVector<VirtualChannelInfo> &virtualChannels, const VirtualChannelInfo::Type &channelType);
51 
52 protected:
53     KisMultiChannelFilter(const KoID &id, const QString &entry);
54 };
55 
56 /**
57  * Base class for configurations of KisMultiChannelFilter subclasses
58  */
59 class KisMultiChannelFilterConfiguration : public KisColorTransformationConfiguration
60 {
61 public:
62     KisMultiChannelFilterConfiguration(int channelCount, const QString & name, qint32 version);
63     ~KisMultiChannelFilterConfiguration() override;
64 
65     using KisFilterConfiguration::fromXML;
66     using KisFilterConfiguration::toXML;
67     using KisFilterConfiguration::fromLegacyXML;
68 
69     void fromLegacyXML(const QDomElement& root) override;
70 
71     void fromXML(const QDomElement& e) override;
72     void toXML(QDomDocument& doc, QDomElement& root) const override;
73 
74     void setCurves(QList<KisCubicCurve> &curves) override;
75     bool isCompatible(const KisPaintDeviceSP) const override;
76 
77     const QVector<QVector<quint16> >& transfers() const;
78     const QList<KisCubicCurve>& curves() const override;
79 
80     virtual bool compareTo(const KisPropertiesConfiguration* rhs) const override;
81 
82 protected:
83     int m_channelCount;
84     QList<KisCubicCurve> m_curves;
85     QVector<QVector<quint16>> m_transfers;
86 
87     void init();
88     void updateTransfers();
89 
90     virtual KisCubicCurve getDefaultCurve() = 0;
91 };
92 
93 class WdgPerChannel : public QWidget, public Ui::WdgPerChannel
94 {
95     Q_OBJECT
96 
97 public:
WdgPerChannel(QWidget * parent)98     WdgPerChannel(QWidget *parent) : QWidget(parent) {
99         setupUi(this);
100     }
101 };
102 
103 /**
104  * Base class for configuration widgets of KisMultiChannelFilter subclasses
105  */
106 class KisMultiChannelConfigWidget : public KisConfigWidget
107 {
108     Q_OBJECT
109 
110 public:
111     KisMultiChannelConfigWidget(QWidget * parent, KisPaintDeviceSP dev, Qt::WindowFlags f = Qt::WindowFlags());
112     ~KisMultiChannelConfigWidget() override;
113 
114     void setConfiguration(const KisPropertiesConfigurationSP config) override;
115 
116 protected Q_SLOTS:
117     void logHistView();
118     void resetCurve();
119     void slotChannelSelected(int index);
120 
121 protected:
122     void init();
123     void resetCurves();
124     void setActiveChannel(int ch);
125 
126     virtual void updateChannelControls() = 0;
127     virtual KisPropertiesConfigurationSP getDefaultConfiguration() = 0;
128 
129     inline QPixmap getHistogram();
130     inline QPixmap createGradient(Qt::Orientation orient /*, int invert (not used now) */);
131 
132     QVector<VirtualChannelInfo> m_virtualChannels;
133     int m_activeVChannel = 0;
134     mutable QList<KisCubicCurve> m_curves;
135 
136     KisPaintDeviceSP m_dev;
137     WdgPerChannel * m_page;
138     KisHistogram *m_histogram;
139 };
140 
141 #endif
142