chalk

kis_adjustment_layer.cpp
1/*
2 * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
3 * Copyright (c) 2005 Casper Boemann <cbr@boemann.dk>
4 *
5 * this program is free software; you can redistribute it and/or modify
6 * it under the terms of the gnu general public license as published by
7 * the free software foundation; either version 2 of the license, or
8 * (at your option) any later version.
9 *
10 * this program is distributed in the hope that it will be useful,
11 * but without any warranty; without even the implied warranty of
12 * merchantability or fitness for a particular purpose. see the
13 * gnu general public license for more details.
14 *
15 * you should have received a copy of the gnu general public license
16 * along with this program; if not, write to the free software
17 * foundation, inc., 675 mass ave, cambridge, ma 02139, usa.
18 */
19
20#include <kdebug.h>
21#include <tqimage.h>
22
23#include "kis_debug_areas.h"
24#include "kis_group_layer.h"
25#include "kis_image.h"
26#include "kis_layer.h"
27#include "kis_adjustment_layer.h"
28#include "kis_painter.h"
29#include "kis_undo_adapter.h"
30#include "kis_selection.h"
31#include "kis_fill_painter.h"
32
33KisAdjustmentLayer::KisAdjustmentLayer(KisImageSP img, const TQString &name, KisFilterConfiguration * kfc, KisSelectionSP selection)
34 : KisLayer (img, name, OPACITY_OPAQUE)
35{
36 m_filterConfig = kfc;
37 setSelection( selection );
38 m_cachedPaintDev = new KisPaintDevice( img->colorSpace(), name.latin1());
39 m_showSelection = true;
40 Q_ASSERT(m_cachedPaintDev);
41 connect(img, TQ_SIGNAL(sigSelectionChanged(KisImageSP)),
42 this, TQ_SLOT(slotSelectionChanged(KisImageSP)));
43}
44
46 : KisLayer(rhs), KisLayerSupportsIndirectPainting(rhs)
47{
48 m_filterConfig = new KisFilterConfiguration(*rhs.m_filterConfig);
49 if (rhs.m_selection) {
50 m_selection = new KisSelection( *rhs.m_selection.data() );
51 m_selection->setParentLayer(this);
52 m_selection->setInterestedInDirtyness(true);
53 connect(rhs.image(), TQ_SIGNAL(sigSelectionChanged(KisImageSP)),
54 this, TQ_SLOT(slotSelectionChanged(KisImageSP)));
55 }
56 m_cachedPaintDev = new KisPaintDevice( *rhs.m_cachedPaintDev.data() );
57 m_showSelection = false;
58}
59
60
61KisAdjustmentLayer::~KisAdjustmentLayer()
62{
63 delete m_filterConfig;
64}
65
66
67KisLayerSP KisAdjustmentLayer::clone() const
68{
69 return new KisAdjustmentLayer(*this);
70}
71
72
73void KisAdjustmentLayer::resetCache()
74{
75 m_cachedPaintDev = new KisPaintDevice(image()->colorSpace(), name().latin1());
76}
77
78KisFilterConfiguration * KisAdjustmentLayer::filter()
79{
80 Q_ASSERT(m_filterConfig);
81 return m_filterConfig;
82}
83
84
85void KisAdjustmentLayer::setFilter(KisFilterConfiguration * filterConfig)
86{
87 Q_ASSERT(filterConfig);
88 m_filterConfig = filterConfig;
89}
90
91
92KisSelectionSP KisAdjustmentLayer::selection()
93{
94 return m_selection;
95}
96
97void KisAdjustmentLayer::setSelection(KisSelectionSP selection)
98{
99 m_selection = new KisSelection();
100 KisFillPainter gc(m_selection.data());
101 KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
102
103 if (selection) {
104 gc.bitBlt(0, 0, COMPOSITE_COPY, selection.data(),
105 0, 0, image()->bounds().width(), image()->bounds().height());
106 } else {
107 gc.fillRect(image()->bounds(), KisColor(TQt::white, cs), MAX_SELECTED);
108 }
109
110 gc.end();
111
112 m_selection->setParentLayer(this);
113 m_selection->setInterestedInDirtyness(true);
114}
115
117{
118 KisFillPainter gc(m_selection.data());
119 KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
120
121 TQRect bounds = extent();
122 bounds |= image()->bounds();
123 gc.fillRect(bounds, KisColor(TQt::white, cs), MIN_SELECTED);
124 gc.end();
125}
126
127
128TQ_INT32 KisAdjustmentLayer::x() const
129{
130 if (m_selection)
131 return m_selection->getX();
132 else
133 return 0;
134}
135
136void KisAdjustmentLayer::setX(TQ_INT32 x)
137{
138 if (m_selection) {
139 m_selection->setX(x);
140 resetCache();
141 }
142
143}
144
145TQ_INT32 KisAdjustmentLayer::y() const
146{
147 if (m_selection)
148 return m_selection->getY();
149 else
150 return 0;
151}
152
153void KisAdjustmentLayer::setY(TQ_INT32 y)
154{
155 if (m_selection) {
156 m_selection->setY(y);
157 resetCache();
158 }
159}
160
162{
163 if (m_selection)
164 return m_selection->selectedRect();
165 else if (image())
166 return image()->bounds();
167 else
168 return TQRect();
169}
170
172{
173 if (m_selection)
174 return m_selection->selectedRect();
175 else if (image())
176 return image()->bounds();
177 else
178 return TQRect();
179}
180
181bool KisAdjustmentLayer::accept(KisLayerVisitor & v)
182{
183 return v.visit( this );
184}
185
186void KisAdjustmentLayer::paintSelection(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h)
187{
188 if (showSelection() && selection())
189 selection()->paintSelection(img, x, y, w, h);
190}
191
192void KisAdjustmentLayer::paintSelection(TQImage &img, const TQRect& scaledImageRect, const TQSize& scaledImageSize, const TQSize& imageSize)
193{
194 if (showSelection() && selection())
195 selection()->paintSelection(img, scaledImageRect, scaledImageSize, imageSize);
196}
197
198TQImage KisAdjustmentLayer::createThumbnail(TQ_INT32 w, TQ_INT32 h)
199{
200 if (!selection())
201 return TQImage();
202
203 int srcw, srch;
204 if( image() )
205 {
206 srcw = image()->width();
207 srch = image()->height();
208 }
209 else
210 {
211 const TQRect e = extent();
212 srcw = e.width();
213 srch = e.height();
214 }
215
216 if (w > srcw)
217 {
218 w = srcw;
219 h = TQ_INT32(double(srcw) / w * h);
220 }
221 if (h > srch)
222 {
223 h = srch;
224 w = TQ_INT32(double(srch) / h * w);
225 }
226
227 if (srcw > srch)
228 h = TQ_INT32(double(srch) / srcw * w);
229 else if (srch > srcw)
230 w = TQ_INT32(double(srcw) / srch * h);
231
232 TQColor c;
233 TQ_UINT8 opacity;
234 TQImage img(w,h,32);
235
236 for (TQ_INT32 y=0; y < h; ++y) {
237 TQ_INT32 iY = (y * srch ) / h;
238 for (TQ_INT32 x=0; x < w; ++x) {
239 TQ_INT32 iX = (x * srcw ) / w;
240 m_selection->pixel(iX, iY, &c, &opacity);
241 img.setPixel(x, y, tqRgb(opacity, opacity, opacity));
242 }
243 }
244
245 return img;
246}
247
248void KisAdjustmentLayer::slotSelectionChanged(KisImageSP image) {
249 image->setModified();
250}
251
252#include "kis_adjustment_layer.moc"
Class that contains a KisFilter and optionally a KisSelection.
virtual TQImage createThumbnail(TQ_INT32 w, TQ_INT32 h)
Returns a thumbnail in requested size.
void clearSelection()
Clears the selection (doesn't call any of the update or dirty methods).
virtual KisLayerSP clone() const
Return a copy of this layer.
virtual TQRect exactBounds() const
Returns the exact bounds of where the actual data resides in this layer.
virtual TQRect extent() const
Returns an approximation of where the bounds on actual data are in this layer.
virtual bool accept(KisLayerVisitor &)
Accept the KisLayerVisitor (for the Visitor design pattern), should call the correct function on the ...
KisAdjustmentLayer(KisImageSP img, const TQString &name, KisFilterConfiguration *kfc, KisSelectionSP selection)
Create a new adjustment layer with the given configuration and selection.
void setSelection(KisSelectionSP selection)
Set the selction of this adjustment layer to a copy of selection.
virtual void paintSelection(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h)
paints a mask where the selection on this layer resides
A colorspace is the definition of a certain color model in Chalk.
A KisColor describes a color in a certain colorspace.
Definition kis_color.h:33
This painter can be used to fill paint devices in different ways.
void fillRect(TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h, const KisColor &c)
Fill a rectangle with a certain color.
A KisFilterConfiguration is the serializable representation of the filter parameters.
Abstract class that represents the concept of a Layer in Chalk.
Definition kis_layer.h:42
A paint device contains the actual pixel data and offers methods to read and write pixels.
KCommand * end()
Finish painting on the current device.
void bitBlt(TQ_INT32 dx, TQ_INT32 dy, const KisCompositeOp &op, KisPaintDeviceSP src, TQ_INT32 sx, TQ_INT32 sy, TQ_INT32 sw, TQ_INT32 sh)
Blast the specified region from src onto the current paint device.
KisSelection contains a byte-map representation of a layer, where the value of a byte signifies wheth...