there is no moc file generated for this class
[kdegraphics.git] / kolourpaint / pixmapfx / kpPixmapFX_AbstractDraw.cpp
blob680a5543eb377e3ccddef5daf653ff628d844735
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_PIXMAP_FX 0
32 #include <kpPixmapFX.h>
34 #include <math.h>
36 #include <qapplication.h>
37 #include <qbitmap.h>
38 #include <qdatetime.h>
39 #include <qimage.h>
40 #include <qpainter.h>
41 #include <qpainterpath.h>
42 #include <qpixmap.h>
43 #include <qpoint.h>
44 #include <qpolygon.h>
45 #include <qrect.h>
47 #include <kconfig.h>
48 #include <kdebug.h>
49 #include <kglobal.h>
50 #include <klocale.h>
51 #include <kmessagebox.h>
53 #include <kpAbstractSelection.h>
54 #include <kpColor.h>
55 #include <kpDefs.h>
56 #include <kpTool.h>
57 #include <kconfiggroup.h>
60 // public static
61 QColor kpPixmapFX::draw_ToQColor (const kpColor &color, bool drawingOnRGBLayer)
63 if (drawingOnRGBLayer)
65 if (color.isOpaque ())
66 return color.toQColor ();
67 else // if (color.isTransparent())
69 // (arbitrary as image will be transparent here)
70 return Qt::black;
73 else
74 return color.maskColor ();
78 struct DrawPack
80 QPixmap *image;
81 void (*userDrawFunc) (QPainter * /*p*/,
82 bool /*drawingOnRGBLayer*/,
83 void * /*data*/);
84 void *userData;
87 static QRect DrawHelper (QPainter *rgbPainter, QPainter *maskPainter, void *data)
89 DrawPack *pack = static_cast <DrawPack *> (data);
91 if (rgbPainter)
92 pack->userDrawFunc (rgbPainter, true/*drawing on RGB*/, pack->userData);
94 if (maskPainter)
95 pack->userDrawFunc (maskPainter, false/*drawing on mask*/, pack->userData);
97 // Assume the whole image was clobbered.
98 return QRect (0, 0, pack->image->width (), pack->image->height ());
101 // public static
102 void kpPixmapFX::draw (QPixmap *image,
103 void (*drawFunc) (QPainter * /*p*/,
104 bool /*drawingOnRGBLayer*/,
105 void * /*data*/),
106 bool anyColorOpaque, bool anyColorTransparent,
107 void *data)
109 DrawPack pack;
110 pack.image = image;
111 pack.userDrawFunc = drawFunc;
112 pack.userData = data;
114 // Call below method.
115 kpPixmapFX::draw (image,
116 &::DrawHelper,
117 anyColorOpaque, anyColorTransparent,
118 &pack);
121 // public static
122 QRect kpPixmapFX::draw (QPixmap *image,
123 QRect (*drawFunc) (QPainter * /*rgbPainter*/, QPainter * /*maskPainter*/,
124 void * /*data*/),
125 bool anyColorOpaque, bool anyColorTransparent,
126 void *data)
128 #if DEBUG_KP_PIXMAP_FX
129 kDebug () << "kppixmapfx.cpp:Draw(image: rect=" << image->rect ()
130 << ",drawFunc=" << drawFunc
131 << ",anyColorOpaque=" << anyColorOpaque
132 << ",anyColorTransparent=" << anyColorTransparent
133 << ")" << endl;
134 #endif
136 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*image);
138 // Interesting note (in case we support depth 1 later):
139 // QBitmap's do not have masks but QBitmap::mask() returns itself.
140 Q_ASSERT (image->depth () > 1);
143 QBitmap mask = image->mask ();
145 #if DEBUG_KP_PIXMAP_FX
146 kDebug () << "\tDraw(): hasMask=" << !mask.isNull ();
147 #endif
149 QPainter rgbPainter, maskPainter;
151 // Draw on RGB layer?
152 if (anyColorOpaque)
154 #if DEBUG_KP_PIXMAP_FX
155 kDebug () << "\tDraw(): drawing on RGB";
156 #endif
157 // RGB draw is not allowed to touch mask.
158 image->setMask (QBitmap ());
160 rgbPainter.begin (image);
163 // Draw on mask layer?
164 if (anyColorTransparent ||
165 !mask.isNull ())
167 #if DEBUG_KP_PIXMAP_FX
168 kDebug () << "\tDraw(): drawing on transparent";
169 #endif
170 if (mask.isNull ())
171 mask = kpPixmapFX::getNonNullMask (*image);
173 maskPainter.begin (&mask);
177 if (!rgbPainter.isActive () && !maskPainter.isActive ())
179 // We did nothing.
180 return QRect ();
184 const QRect dirtyRect = (*drawFunc) (
185 rgbPainter.isActive () ? &rgbPainter : 0,
186 maskPainter.isActive () ? &maskPainter : 0,
187 data);
190 if (rgbPainter.isActive ())
191 rgbPainter.end ();
193 if (maskPainter.isActive ())
194 maskPainter.end ();
196 if (anyColorOpaque)
198 // A mask should not have been created - that's the job of the next step.
199 Q_ASSERT (!kpPixmapFX::hasMask (*image));
203 #if DEBUG_KP_PIXMAP_FX
204 kDebug () << "\tDraw(): setting mask " << !mask.isNull ();
205 #endif
207 // Set new mask.
208 image->setMask (mask);
211 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*image);
213 return dirtyRect;