summaryrefslogtreecommitdiffstats
path: root/krita/core/kis_painter.h
blob: fa8de088909091f665b848e06bf3e4df565cf9e6 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
 *  Copyright (c) 2004 Clarence Dang <dang@kde.org>
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef KIS_PAINTER_H_
#define KIS_PAINTER_H_

#include <kcommand.h>

#include "kis_color.h"
#include "kis_global.h"
#include "kis_types.h"
#include "kis_paint_device.h"
#include "kis_point.h"
#include "kis_filter.h"
#include "kis_progress_subject.h"
#include "kis_paintop.h"
#include "kis_color.h"

#include <koffice_export.h>

class QRect;
class KisTransaction;
class KisBrush;
class KisPattern;

/**
 * KisPainter contains the graphics primitives necessary to draw on a
 * KisPaintDevice. This is the same kind of abstraction as used in Qt
 * itself, where you have QPainter and QPaintDevice.
 *
 * However, KisPainter works on a tiled image and supports different
 * colour models, and that's a lot more complicated.
 *
 * KisPainter supports transactions that can group various paint operations
 * in one undoable step.
 *
 * For more complex operations, you might want to have a look at the subclasses
 * of KisPainter: KisConvolutionPainter, KisFillPainter and KisGradientPainter
 */
class KRITACORE_EXPORT KisPainter : public KisProgressSubject {
    typedef KisProgressSubject super;

public:
    /// Construct painter without a device
    KisPainter();
    /// Construct a painter, and begin painting on the device
    KisPainter(KisPaintDeviceSP device);
    virtual ~KisPainter();

private:
    // Implement KisProgressSubject
    virtual void cancel() { m_cancelRequested = true; }

public:
    /**
     * Start painting on the specified device. Not undoable.
     */
    void begin(KisPaintDeviceSP device);

    /**
     * Finish painting on the current device
     */
    KCommand *end();

    /// Begin an undoable paint operation
    void beginTransaction(const QString& customName = QString::null);

    /// Finish the undoable paint operation
    KCommand *endTransaction();

    /// begin a transaction with the given command
    void beginTransaction( KisTransaction* command);

    /// Return the current transcation
    KisTransaction  * transaction() { return m_transaction; }


    /// Returns the current paint device.
    KisPaintDeviceSP device() const { return m_device; }


    // -----------------------------------------------------------------
    //  Native paint methods that are undo/redo-able,
    // use the color strategies and the composite operations.

    /**
     * Blast the specified region from src onto the current paint device.
     */
    void bitBlt(Q_INT32 dx, Q_INT32 dy,
                const KisCompositeOp& op,
                KisPaintDeviceSP src,
                Q_INT32 sx, Q_INT32 sy,
                Q_INT32 sw, Q_INT32 sh)
    {
        bitBlt(dx, dy, op, src, OPACITY_OPAQUE, sx, sy, sw, sh);
    }

    /**
     * Overloaded version of the previous, differs in that it is possible to specify
     * a value for opacity
     */
    void bitBlt(Q_INT32 dx, Q_INT32 dy,
                const KisCompositeOp& op,
                KisPaintDeviceSP src,
                Q_UINT8 opacity,
                Q_INT32 sx, Q_INT32 sy,
                Q_INT32 sw, Q_INT32 sh);

    /**
     * A version of bitBlt that renders using an external mask, ignoring
     * the src device's own selection, if it has one.
     */
    void bltMask(Q_INT32 dx, Q_INT32 dy,
                      const KisCompositeOp &op,
                      KisPaintDeviceSP src,
                      KisPaintDeviceSP selMask,
                      Q_UINT8 opacity,
                      Q_INT32 sx, Q_INT32 sy,
                      Q_INT32 sw, Q_INT32 sh);

    /**
     * A version of bitBlt that renders using an external selection mask, ignoring
     * the src device's own selection, if it has one.
     */
    void bltSelection(Q_INT32 dx, Q_INT32 dy,
                      const KisCompositeOp &op,
                      KisPaintDeviceSP src,
                      KisSelectionSP selMask,
                      Q_UINT8 opacity,
                      Q_INT32 sx, Q_INT32 sy,
                      Q_INT32 sw, Q_INT32 sh);


    /**
     * A version of bitBlt that renders using the src device's selection mask, if it has one.
     */
    void bltSelection(Q_INT32 dx, Q_INT32 dy,
                      const KisCompositeOp &op,
                      KisPaintDeviceSP src,
                      Q_UINT8 opacity,
                      Q_INT32 sx, Q_INT32 sy,
                      Q_INT32 sw, Q_INT32 sh);


    /**
     * The methods below are 'higher' level than the above methods. They need brushes, colors
     * etc. set before they can be called. The methods do not directly tell the image to
     * update, but you can call dirtyRect() to get the rect that needs to be notified by your
     * painting code.
     *
     * Call will RESET the dirtyRect!
    */
    QRect dirtyRect();

    /**
     * Add the r to the current dirty rect, and return the dirtyRect after adding r to it.
     */
    QRect addDirtyRect(QRect r) { m_dirtyRect |= r; return m_dirtyRect; }



    /**
     * Paint a line that connects the dots in points
     */
    void paintPolyline(const QValueVector <KisPoint> &points,
                       int index = 0, int numPoints = -1);

    /**
     * Draw a line between pos1 and pos2 using the currently set brush and color.
     * If savedDist is less than zero, the brush is painted at pos1 before being
     * painted along the line using the spacing setting.
     * @return the drag distance, that is the remains of the distance between p1 and p2 not covered
     * because the currenlty set brush has a spacing greater than that distance.
     */
    double paintLine(const KisPoint &pos1,
             const double pressure1,
             const double xTilt1,
             const double yTilt1,
             const KisPoint &pos2,
             const double pressure2,
             const double xTilt2,
             const double yTilt2,
             const double savedDist = -1);

    /**
     * Draw a Bezier curve between pos1 and pos2 using control points 1 and 2.
     * If savedDist is less than zero, the brush is painted at pos1 before being
     * painted along the curve using the spacing setting.
     * @return the drag distance, that is the remains of the distance between p1 and p2 not covered
     * because the currenlty set brush has a spacing greater than that distance.
     */
    double paintBezierCurve(const KisPoint &pos1,
                const double pressure1,
                const double xTilt1,
                const double yTilt1,
                const KisPoint &control1,
                const KisPoint &control2,
                const KisPoint &pos2,
                const double pressure2,
                const double xTilt2,
                const double yTilt2,
                const double savedDist = -1);

    /**
     * Fill the given vector points with the points needed to draw the Bezier curve between
     * pos1 and pos2 using control points 1 and 2, excluding the final pos2.
     */
    void getBezierCurvePoints(const KisPoint &pos1,
                  const KisPoint &control1,
                  const KisPoint &control2,
                  const KisPoint &pos2,
                  vKisPoint& points);


    /**
     * Paint the rectangle with given begin and end points
     */
    void paintRect(const KisPoint &startPoint,
               const KisPoint &endPoint,
               const double pressure,
               const double xTilt,
               const double yTilt);


    /**
     * Paint the ellipse with given begin and end points
     */
    void paintEllipse(const KisPoint &startPoint,
                      const KisPoint &endPoint,
                      const double pressure,
                      const double /*xTilt*/,
                      const double /*yTilt*/);

    /**
     * Paint the polygon with the points given in points. It automatically closes the polygon
     * by drawing the line from the last point to the first.
     */
    void paintPolygon(const vKisPoint& points);

    /** Draw a spot at pos using the currently set paint op, brush and color */
    void paintAt(const KisPoint &pos,
             const double pressure,
             const double /*xTilt*/,
             const double /*yTilt*/);


    // ------------------------------------------------------------------------
    // Set the parameters for the higher level graphics primitives.

    /// Determines whether the brush spacing should vary when drawing
    /// lines with the pressure
    void setVaryBrushSpacingWithPressureWhenDrawingALine( bool varyBrushSpacingWithPressureWhenDrawingALine )
        { m_varyBrushSpacingWithPressureWhenDrawingALine = varyBrushSpacingWithPressureWhenDrawingALine; }
    bool varyBrushSpacingWithPressureWhenDrawingALine() {  return m_varyBrushSpacingWithPressureWhenDrawingALine;  }

    /// Set the current brush
    void setBrush(KisBrush* brush) { m_brush = brush; }
    /// Returns the currently set brush
    KisBrush * brush() const { return m_brush; }

    /// Set the current pattern
    void setPattern(KisPattern * pattern) { m_pattern = pattern; }
    /// Returns the currently set pattern
    KisPattern * pattern() const { return m_pattern; }

    /// Set the color that will be used to paint with
    void setPaintColor(const KisColor& color) { m_paintColor = color;}

    /// Returns the color that will be used to paint with
    KisColor paintColor() const { return m_paintColor; }

    /// Set the current background color
    void setBackgroundColor(const KisColor& color) {m_backgroundColor = color; }
    /// Returns the current background color
    KisColor backgroundColor() const { return m_backgroundColor; }

    /// Set the current fill color
    void setFillColor(const KisColor& color) { m_fillColor = color; }
    /// Returns the current fill color
    KisColor fillColor() const { return m_fillColor; }


    /// This enum contains the styles with which we can fill things like polygons and ellipses
    enum FillStyle {
        FillStyleNone,
        FillStyleForegroundColor,
        FillStyleBackgroundColor,
        FillStylePattern,
        FillStyleGradient,
        FillStyleStrokes
    };

    /// Set the current style with which to fill
    void setFillStyle(FillStyle fillStyle) { m_fillStyle = fillStyle; }
    /// Returns the current fill style
    FillStyle fillStyle() const { return m_fillStyle; }

    /// The style of the brush stroke around polygons and so
    enum StrokeStyle {
        StrokeStyleNone,
        StrokeStyleBrush
    };

    /// Set the current brush stroke style
    void setStrokeStyle(StrokeStyle strokeStyle) { m_strokeStyle = strokeStyle; }
    /// Returns the current brush stroke style
    StrokeStyle strokeStyle() const { return m_strokeStyle; }

    /// Set the opacity which is used in painting (like filling polygons)
    void setOpacity(Q_UINT8 opacity) { m_opacity = opacity; }
    /// Returns the opacity that is used in painting
    Q_UINT8 opacity() const { return m_opacity; }

    /**
     * Sets the current composite operation. Everything painted will be composited on
     * the destination layer with this composite op.
     **/
    void setCompositeOp(const KisCompositeOp& op) { m_compositeOp = op; }
    /// Returns the current composite operation
    KisCompositeOp compositeOp() const { return m_compositeOp; }

    /// Sets the current KisFilter, used by the paintops that support it (like KisFilterOp)
    void setFilter(KisFilterSP filter) { m_filter = filter; }
    /// Returns the current KisFilter
    KisFilterSP filter() { return m_filter; }

    /**
     * The offset for paint operations that use it (like KisDuplicateOp). It will use as source
     * the part of the layer that is at its paintedPosition - duplicateOffset
     */
    // TODO: this is an hack ! it must be fix, the following functions have nothing to do here
    void setDuplicateOffset(const KisPoint& offset) { m_duplicateOffset = offset; }
    /// Returns the offset for duplication
    KisPoint duplicateOffset(){ return m_duplicateOffset; }

    inline void setDuplicateHealing(bool v) { m_duplicateHealing = v; }
    inline bool duplicateHealing() { return m_duplicateHealing; }

    inline void setDuplicateHealingRadius(int r) { m_duplicateHealingRadius = r; }
    inline int duplicateHealingRadius() { return m_duplicateHealingRadius; }

    inline void setDuplicatePerspectiveCorrection(bool v) { m_duplicatePerspectiveCorrection = v; }
    inline bool duplicatePerspectiveCorrection() { return m_duplicatePerspectiveCorrection; }

    void setDuplicateStart(const KisPoint start) { m_duplicateStart = start;}
    KisPoint duplicateStart() { return m_duplicateStart;}

    /// Sets the current pressure for things that like to use this
    void setPressure(double pressure) { m_pressure = pressure; }
    /// Returns the current pressure
    double pressure() { return m_pressure; }

    /**
     * Set the current paint operation. This is used for all drawing functions.
     * The painter will DELETE the paint op itself!!
     * That means no that you should not delete it yourself (or put it on the stack)
     */
    void setPaintOp(KisPaintOp * paintOp) { delete m_paintOp; m_paintOp = paintOp; }
    /// Returns the current paint operation
    KisPaintOp * paintOp() const { return m_paintOp; }

    /// Set a current 'dab'. This usually is a paint device containing a rendered brush
    void setDab(KisPaintDeviceSP dab) { m_dab = dab; }
    /// Get the currently set dab
    KisPaintDeviceSP dab() const { return m_dab; }

    /// Is cancel Requested by the KisProgressSubject for this painter
    bool cancelRequested() const { return m_cancelRequested; }

protected:
    /// Initialize, set everything to '0' or defaults
    void init();
    KisPainter(const KisPainter&);
    KisPainter& operator=(const KisPainter&);

    /// Calculate the distance that point p is from the line made by connecting l0 and l1
    static double pointToLineDistance(const KisPoint& p, const KisPoint& l0, const KisPoint& l1);

    /// Fill the polygon defined by points with the fillStyle
    void fillPolygon(const vKisPoint& points, FillStyle fillStyle);

protected:
    KisPaintDeviceSP m_device;
    KisTransaction  *m_transaction;

    QRect m_dirtyRect;

    KisColor m_paintColor;
    KisColor m_backgroundColor;
    KisColor m_fillColor;
    FillStyle m_fillStyle;
    StrokeStyle m_strokeStyle;
    KisBrush *m_brush;
    KisPattern *m_pattern;
    KisPoint m_duplicateOffset;
    KisPoint m_duplicateStart;
    bool m_duplicateHealing;
    int m_duplicateHealingRadius;
    bool m_duplicatePerspectiveCorrection;
    Q_UINT8 m_opacity;
    KisCompositeOp m_compositeOp;
    KisFilterSP m_filter;
    KisPaintOp * m_paintOp;
    double m_pressure;
    bool m_cancelRequested;
    Q_INT32 m_pixelSize;
    KisColorSpace * m_colorSpace;
    KisProfile *  m_profile;
    KisPaintDeviceSP m_dab;
    bool m_varyBrushSpacingWithPressureWhenDrawingALine;

};


#endif // KIS_PAINTER_H_