1 /*
2  * Copyright (C) 2016 Boudewijn Rempt <boud@valdyas.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef ColorModelPerLayerCHECK_H
21 #define ColorModelPerLayerCHECK_H
22 
23 #include "KisExportCheckRegistry.h"
24 #include <KoID.h>
25 #include <klocalizedstring.h>
26 #include <kis_image.h>
27 #include <KoColorSpace.h>
28 #include <KoColorModelStandardIds.h>
29 #include <kis_layer.h>
30 #include <kis_node_visitor.h>
31 #include "kis_node.h"
32 #include "kis_paint_layer.h"
33 #include "kis_group_layer.h"
34 #include "kis_adjustment_layer.h"
35 #include "kis_external_layer_iface.h"
36 #include "kis_clone_layer.h"
37 #include "generator/kis_generator_layer.h"
38 
39 class KisColorModelCheckVisitor : public KisNodeVisitor
40 {
41 public:
42 
43     using KisNodeVisitor::visit;
44 
KisColorModelCheckVisitor(KoID colorModelID,KoID colorDepthID)45     KisColorModelCheckVisitor (KoID colorModelID, KoID colorDepthID)
46         : m_count(0)
47         , m_colorModelID(colorModelID)
48         , m_colorDepthID(colorDepthID)
49     {
50     }
51 
count()52     quint32 count() {
53         return m_count;
54     }
55 
visit(KisNode * node)56     bool visit(KisNode* node) override {
57         return check(node);
58     }
59 
visit(KisPaintLayer * layer)60     bool visit(KisPaintLayer *layer) override {
61         return check(layer);
62     }
63 
visit(KisGroupLayer * layer)64     bool visit(KisGroupLayer *layer) override {
65         return check(layer);
66     }
67 
68 
visit(KisAdjustmentLayer * layer)69     bool visit(KisAdjustmentLayer *layer) override {
70         return check(layer);
71     }
72 
visit(KisExternalLayer * layer)73     bool visit(KisExternalLayer *layer) override {
74         return check(layer);
75     }
76 
visit(KisCloneLayer * layer)77     bool visit(KisCloneLayer *layer) override {
78         return check(layer);
79     }
80 
visit(KisGeneratorLayer * layer)81     bool visit(KisGeneratorLayer * layer) override {
82         return check(layer);
83     }
84 
visit(KisFilterMask *)85     bool visit(KisFilterMask *) override {return true;}
86 
visit(KisTransformMask *)87     bool visit(KisTransformMask *) override {return true;}
88 
visit(KisTransparencyMask *)89     bool visit(KisTransparencyMask *) override {return true;}
90 
visit(KisSelectionMask *)91     bool visit(KisSelectionMask *) override {return true;}
92 
visit(KisColorizeMask *)93     bool visit(KisColorizeMask *) override {return true;}
94 
95 
96 private:
check(KisNode * node)97     bool check(KisNode * node)
98     {
99         KisLayer *layer = dynamic_cast<KisLayer*>(node);
100         if (layer) {
101             const KoColorSpace * cs = layer->colorSpace();
102             if (cs->colorModelId() == m_colorModelID && cs->colorDepthId() == m_colorDepthID) {
103                 m_count++;
104             }
105         }
106         visitAll(node);
107         return true;
108     }
109 
110     quint32 m_count;
111     const KoID m_colorModelID;
112     const KoID m_colorDepthID;
113 
114 };
115 
116 class ColorModelPerLayerCheck : public KisExportCheckBase
117 {
118 public:
119 
120     ColorModelPerLayerCheck(const KoID &colorModelID, const KoID &colorDepthID, const QString &id, Level level, const QString &customWarning = QString())
KisExportCheckBase(id,level,customWarning,true)121         : KisExportCheckBase(id, level, customWarning, true)
122         , m_ColorModelID(colorModelID)
123         , m_colorDepthID(colorDepthID)
124     {
125         Q_ASSERT(!colorModelID.name().isEmpty());
126         Q_ASSERT(!colorDepthID.name().isEmpty());
127 
128         if (customWarning.isEmpty()) {
129             m_warning = i18nc("image conversion warning", "Your image contains layers with the color model <b>%1</b> and channel depth <b>%2</b> which cannot be saved to this format. The layers will be converted."
130             , m_ColorModelID.name()
131             ,m_colorDepthID.name());
132         }
133     }
134 
checkNeeded(KisImageSP image)135     bool checkNeeded(KisImageSP image) const override
136     {
137         KisColorModelCheckVisitor v(m_ColorModelID, m_colorDepthID);
138         image->rootLayer()->accept(v);
139         return (v.count() > 0);
140     }
141 
check(KisImageSP)142     Level check(KisImageSP /*image*/) const override
143     {
144         return m_level;
145     }
146 
147     const KoID m_ColorModelID;
148     const KoID m_colorDepthID;
149 };
150 
151 class ColorModelPerLayerCheckFactory : public KisExportCheckFactory
152 {
153 public:
154 
ColorModelPerLayerCheckFactory(const KoID & ColorModelID,const KoID & colorDepthId)155     ColorModelPerLayerCheckFactory(const KoID &ColorModelID, const KoID &colorDepthId)
156         : m_colorModelID(ColorModelID)
157         , m_colorDepthID(colorDepthId)
158     {
159     }
160 
~ColorModelPerLayerCheckFactory()161     ~ColorModelPerLayerCheckFactory() override {}
162 
create(KisExportCheckBase::Level level,const QString & customWarning)163     KisExportCheckBase *create(KisExportCheckBase::Level level, const QString &customWarning) override
164     {
165         return new ColorModelPerLayerCheck(m_colorModelID, m_colorDepthID, id(), level, customWarning);
166     }
167 
id()168     QString id() const override {
169         return "ColorModelPerLayerCheck/" + m_colorModelID.id() + "/" + m_colorDepthID.id();
170     }
171 
172     const KoID m_colorModelID;
173     const KoID m_colorDepthID;
174 };
175 
176 #endif // ColorModelPerLayerCHECK_H
177