File: kpAbstractImageSelection.h

package info (click to toggle)
kolourpaint 4:18.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,544 kB
  • sloc: cpp: 37,589; xml: 327; sh: 10; makefile: 6
file content (266 lines) | stat: -rw-r--r-- 8,747 bytes parent folder | download
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
258
259
260
261
262
263
264
265
266

/*
   Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef kpAbstractImageSelection_H
#define kpAbstractImageSelection_H


#include "kpImageSelectionTransparency.h"
#include "layers/selections/kpAbstractSelection.h"
#include "imagelib/kpImage.h"


//
// An abstract selection with optional image content and background
// subtraction.  If there is image content, it is known as a "floating
// selection" that hovers above the document.  Otherwise, it is an
// image selection "border", that highlights pixels of the document, and
// may later be upgraded to a floating selection by giving it an image
// consisting of those pixels.
//
// The images passed to this class (known as "base images") should have all
// pixels, outside of the border, set to transparent.  However, nothing
// enforces this.  Pixels on, or inside, the border might be opaque or
// transparent, depending on the content.
//
// The "transparent image" is the base image with background subtraction
// (kpImageSelectionTransparency) applied.  This is automatically computed.
//
// The boundingRect() is the size of the border.  The base image must be of
// exactly the same size, except that the base image is allowed to be null
// (for a selection that only consists of a border).
//
// Instead of copying selections' images to the clipboard, we copy
// selections, to preserve the border across KolourPaint instances.
// Background subtraction is not copied to the clipboard so that the base
// image is affected by the background subtraction of the destination
// KolourPaint window.
//
class kpAbstractImageSelection : public kpAbstractSelection
{
Q_OBJECT

//
// Initialization
//

protected:
    // (Call these in subclass constructors)
    kpAbstractImageSelection (const kpImageSelectionTransparency &transparency =
        kpImageSelectionTransparency ());

    kpAbstractImageSelection (const QRect &rect,
        const kpImage &baseImage = kpImage (),
        const kpImageSelectionTransparency &transparency =
            kpImageSelectionTransparency ());

    kpAbstractImageSelection (const QRect &rect,
        const kpImageSelectionTransparency &transparency =
            kpImageSelectionTransparency ());

    // (Call this in subclass implementations of operator=)
    kpAbstractImageSelection &operator= (const kpAbstractImageSelection &rhs);

public:
    // (Covariant return-type specialization of superclass pure virtual method)
    kpAbstractImageSelection *clone () const override = 0;

    ~kpAbstractImageSelection () override;


//
// Marshalling
//

public:
    // You must override this if you have extra serializable fields.
    // Remember to call this base implementation before your code.
    bool readFromStream (QDataStream &stream) override;

    // You must override this if you have extra serializable fields.
    // Remember to call this base implementation before your code.
    void writeToStream (QDataStream &stream) const override;


//
// General Queries
//

public:
    QString name () const override;

    // You must override this, if you have extra fields that take a
    // non-constant amount of space, and add the size returned by this
    // implementation.
    kpCommandSize::SizeType size () const override;

    // Same as virtual size() (it even calls it) but subtracts the size of the
    // baseImage().
    //
    // kpCommand's store the kpImage's they are working on.  These images may
    // be from documents or selections.  In the case of a selection, the
    // selection's baseImage() is identical to that image, but takes no extra
    // space due to kpImage's copy-on-write.  This method fixes that
    // double-counting of baseImage()'s size.
    //
    // The size of the internal transparency() mask is still included
    // (see recalculateTransparencyMask()).
    //
    // sync: kpImage copy-on-write behavior
    //
    // TODO: Check all size() implementations are correct since we've
    //       started removing the old kpSelection::setPixmap(QPixmap())
    //       (now kpAbstractImageSelection::setBaseImage(kpImage()) or
    //        kpAbstractImageSelection::deleteContent()) space saving hack.
    kpCommandSize::SizeType sizeWithoutImage () const;


//
// Dimensions
//

public:
    int minimumWidth () const override;
    int minimumHeight () const override;


//
// Shape Mask
//
// These methods do not access any class instance fields.
//

public:
    // Returns the mask corresponding to the shape of the selection.
    //
    // If <nullForRectangular> is set, the method _may_ return a null
    // bitmap if the selection is rectangular.
    //
    // This base implementation calls calculatePoints() and ignores
    // <nullForRectangular>.
    //
    // You should override this if you can implement it more efficiently or
    // if you can honor <nullForRectangular>.
    //
    // Note: This must be consistent with the outputs of calculatePoints() and
    //       shapeRegion().
    //
    // TODO: Try to get rid of this method since it's slow.
    virtual QBitmap shapeBitmap (bool nullForRectangular = false) const;

    // Returns the region corresponding to the shape of the selection
    // e.g. elliptical region for an elliptical selection.
    //
    // Very slow.
    //
    // Note: This must be consistent with the outputs of calculatePoints() and
    //       shapeRegion().
    //
    // OPT: QRegion is probably incredibly slow - cache
    virtual QRegion shapeRegion () const = 0;

    // Returns the given <image> with the pixels outside of the selection's
    // shape set to transparent.
    //
    // Very slow.
    //
    // ASSUMPTION: The image has the same dimensions as the selection.
    kpImage givenImageMaskedByShape (const kpImage &image) const;


//
// Content - Base Image
//

public:
    // Returns whether there's a non-null base image.
    bool hasContent () const override;

    void deleteContent () override;

public:
    kpImage baseImage () const;
    void setBaseImage (const kpImage &baseImage);


//
// Background Subtraction
//

public:
    kpImageSelectionTransparency transparency () const;

    // Returns whether or not the selection changed due to setting the
    // transparency info.  If <checkTransparentPixmapChanged> is set,
    // it will try harder to return false (although the check is
    // expensive).
    bool setTransparency (const kpImageSelectionTransparency &transparency,
                          bool checkTransparentPixmapChanged = false);

private:
    // Updates the selection transparency (a.k.a. background subtraction) mask
    // so that transparentImage() will work.
    //
    // Called when the base image or selection transparency changes.
    void recalculateTransparencyMaskCache ();

public:
    // Returns baseImage() after applying kpImageSelectionTransparency
    kpImage transparentImage () const;


//
// Mutation - Effects
//

public:
    // Overwrites the base image with the selection's shape (e.g. ellipse)
    // filled in with <color>.  See shapeBitmap().
    void fill (const kpColor &color);

    virtual void flip (bool horiz, bool vert);


//
// Rendering
//

public:
    // (using transparent image)
    void paint (QImage *destPixmap, const QRect &docRect) const override;

    // (using base image)
    void paintWithBaseImage (QImage *destPixmap, const QRect &docRect) const;


private:
    struct kpAbstractImageSelectionPrivate * const d;
};


#endif  // kpAbstractImageSelection_H