summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_layer.h
blob: 2cff4bc99538f0619e5d6e01070ff1232914b92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 *  Copyright (c) 2002 Patrick Julien <[email protected]>
 *  Copyright (c) 2005 Casper Boemann <[email protected]>
 *
 *  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., 675 mass ave, cambridge, ma 02139, usa.
 */
#ifndef KIS_LAYER_H_
#define KIS_LAYER_H_

#include <tqobject.h>
#include "kis_types.h"
#include "kis_layer_visitor.h"
#include "kis_composite_op.h"
#include <koffice_export.h>

class KNamedCommand;
class TQPainter;
class KisUndoAdapter;
class KisGroupLayer;

/**
 * Abstract class that represents the concept of a Layer in Chalk. This is not related
 * to the paint devices: this is merely an abstraction of how layers can be stacked and
 * rendered differently.
 * Regarding the previous-, first-, next- and lastChild() calls, first means that it the layer
 * is at the top of the group in the layerlist, using next will iterate to the bottom to last,
 * whereas previous will go up to first again.
 **/
class KRITACORE_EXPORT KisLayer : public TQObject, public KShared
{
    Q_OBJECT
  TQ_OBJECT

public:
    KisLayer(KisImage *img, const TQString &name, TQ_UINT8 opacity);
    KisLayer(const KisLayer& rhs);
    virtual ~KisLayer();

    
    /**
     * Set the specified rect to clean
     */
    virtual void setClean(const TQRect & rect);
    
    /**
     * If the layer has been changed and not been composited yet, this returns true
     */
    virtual bool dirty();

    /**
     * Return true if the given rect intersects the dirty rect(s) of this layer
     */
    virtual bool dirty(const TQRect & rc);


    virtual TQRect dirtyRect() const;
    
    
    /**
     * Set the entire layer extent dirty; this percolates up to parent layers all the
     * way to the root layer.
     */
    virtual void setDirty(bool propagate = true);

    /**
     * Add the given rect to the set of dirty rects for this layer;
     * this percolates up to parent layers all the way to the root
     * layer.
     */
    virtual void setDirty(const TQRect & rect, bool propagate = true);
    
    /// Return a copy of this layer
    virtual KisLayerSP clone() const = 0;

    /// Returns the ID of the layer, which is guaranteed to be unique among all KisLayers.
    int id() const { return m_id; }

    /* Returns the index of the layer in its parent's list of child layers. Indices
     * increase from 0, which is the topmost layer in the list, to the bottommost.
     */
    virtual int index() const;

    /// Moves this layer to the specified index within its parent's list of child layers.
    virtual void setIndex(int index);

    /**
     * Returns the parent layer of a layer. This is 0 only for a root layer; otherwise
     * this will be an actual GroupLayer */
    virtual KisGroupLayerSP parent() const;

    /**
     * Returns the previous sibling of this layer in the parent's list. This is the layer
     * *above* this layer. 0 is returned if there is no parent, or if this child has no more
     * previous siblings (== firstChild())
     */
    virtual KisLayerSP prevSibling() const;

    /**
     * Returns the next sibling of this layer in the parent's list. This is the layer *below*
     * this layer. 0 is returned if there is no parent, or if this child has no more next 
     * siblings (== lastChild())
     */
    virtual KisLayerSP nextSibling() const;

    /**
     * Returns the sibling above this layer in its parent's list. 0 is returned if there is no parent,
     * or if this layer is the topmost layer in its group. This is the same as calling prevSibling().
     */
    KisLayerSP siblingAbove() const { return prevSibling(); }

    /**
     * Returns the sibling below this layer in its parent's list. 0 is returned if there is no parent,
     * or if this layer is the bottommost layer in its group.  This is the same as calling nextSibling().
     */
    KisLayerSP siblingBelow() const { return nextSibling(); }

    /// Returns how many direct child layers this layer has (not recursive).
    virtual uint childCount() const { return 0; }

    /// Returns the first child layer of this layer (if it supports that).
    virtual KisLayerSP firstChild() const { return 0; }

    /// Returns the last child layer of this layer (if it supports that).
    virtual KisLayerSP lastChild() const { return 0; }

    /// Recursively searches this layer and any child layers for a layer with the specified name.
    virtual KisLayerSP findLayer(const TQString& name) const;

    /// Recursively searches this layer and any child layers for a layer with the specified ID.
    virtual KisLayerSP findLayer(int id) const;

    enum { Visible = 1, Hidden = 2, Locked = 4, Unlocked = 8 };

    /// Returns the total number of layers in this layer, its child layers, and their child layers recursively, optionally ones with the specified properties Visible or Locked, which you can OR together.
    virtual int numLayers(int type = 0) const;

public:
    /// Called when the layer is made active
    virtual void activate() {};

    /// Called when another layer is made active
    virtual void deactivate() {};

public:
    virtual TQ_INT32 x() const = 0;
    virtual void setX(TQ_INT32) = 0;

    virtual TQ_INT32 y() const = 0;
    virtual void setY(TQ_INT32) = 0;

    virtual KNamedCommand *moveCommand(TQPoint oldPosition, TQPoint newPosition);

    /// Returns an approximation of where the bounds on actual data are in this layer
    virtual TQRect extent() const = 0;
    /// Returns the exact bounds of where the actual data resides in this layer
    virtual TQRect exactBounds() const = 0;

    virtual const bool visible() const;
    virtual void setVisible(bool v);
    KNamedCommand *setVisibleCommand(bool visiblel);

    TQ_UINT8 opacity() const;
    void setOpacity(TQ_UINT8 val);
    KNamedCommand *setOpacityCommand(TQ_UINT8 val);
    KNamedCommand *setOpacityCommand(TQ_UINT8 prevOpacity, TQ_UINT8 newOpacity);

    bool locked() const;
    void setLocked(bool l);
    KNamedCommand *setLockedCommand(bool locked);

    void notifyPropertyChanged();

    bool temporary() const;
    void setTemporary(bool t);

    virtual TQString name() const;
    virtual void setName(const TQString& name);

    KisCompositeOp compositeOp() { return m_compositeOp; }
    void setCompositeOp(const KisCompositeOp& compositeOp);
    KNamedCommand *setCompositeOpCommand(const KisCompositeOp& compositeOp);

    KisImage *image() const { return m_image; }
    virtual void setImage(KisImage *image) { m_image = image; }

    KisUndoAdapter *undoAdapter() const;

    /// paints a mask where the selection on this layer resides
    virtual void paintSelection(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h);
    virtual void paintSelection(TQImage &img, const TQRect& scaledImageRect, const TQSize& scaledImageSize, const TQSize& imageSize);

    /// paints where no data is on this layer. Useful when it is a transparent layer stacked on top of another one
    virtual void paintMaskInactiveLayers(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h);

    /// Returns a thumbnail in requested size. The TQImage may have transparent parts.
    /// May also return 0
    virtual TQImage createThumbnail(TQ_INT32 w, TQ_INT32 h);

    /// Accept the KisLayerVisitor (for the Visitor design pattern), should call the correct function on the KisLayerVisitor for this layer type
    virtual bool accept(KisLayerVisitor &) = 0;

private:
    friend class KisGroupLayer;

    bool matchesFlags(int flags) const;

    int m_id;
    int m_index;
    TQ_UINT8 m_opacity;
    bool m_locked;
    bool m_visible;
    bool m_temporary;

    // XXX: keep a list of dirty rects instead of always aggegrating them
    TQRect m_dirtyRect;
    TQString m_name;
    KisGroupLayerSP m_parent;
    KisImage *m_image;

    // Operation used to composite this layer with the layers _under_ this layer
    KisCompositeOp m_compositeOp;
};

// For classes that support indirect painting
class KRITACORE_EXPORT KisLayerSupportsIndirectPainting {
    // To simulate the indirect painting
    KisPaintDeviceSP m_temporaryTarget;
    KisCompositeOp m_compositeOp;
    TQ_UINT8 m_compositeOpacity;
public:
    // Indirect painting
    void setTemporaryTarget(KisPaintDeviceSP t);
    void setTemporaryCompositeOp(const KisCompositeOp& c);
    void setTemporaryOpacity(TQ_UINT8 o);
    KisPaintDeviceSP temporaryTarget();
    KisCompositeOp temporaryCompositeOp() const;
    TQ_UINT8 temporaryOpacity() const;

    // Or I could make KisLayer a virtual base of KisLayerSupportsIndirectPainting and so, but
    // I'm sure virtual diamond inheritance isn't as appreciated as this
    virtual KisLayer* layer() = 0;
};

#endif // KIS_LAYER_H_