summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_tool_freehand.cc
blob: bb7dbc1e96705a3db2d8f1b2397029045d8413db (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
/*
 *  kis_tool_brush.cc - part of Chalk
 *
 *  Copyright (c) 2003-2004 Boudewijn Rempt <boud@valdyas.org>
 *  Copyright (c) 2004 Bart Coppens <kde@bartcoppens.be>
 *
 *  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.
 */
#include <tqevent.h>
#include <tqlabel.h>
#include <layout.h>
#include <tqwidget.h>
#include <tqrect.h>

#include <kdebug.h>
#include <kaction.h>
#include <kcommand.h>
#include <klocale.h>

#include "kis_canvas_subject.h"
#include "kis_undo_adapter.h"
#include "kis_selection.h"
#include "kis_painter.h"
#include "kis_fill_painter.h"
#include "kis_tool_freehand.h"
#include "kis_cursor.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_move_event.h"
#include "kis_layer.h"
#include "kis_group_layer.h"
#include "kis_paint_layer.h"
#include "kis_canvas.h"
#include "kis_canvas_painter.h"
#include "kis_boundary_painter.h"
#include "kis_brush.h"

KisToolFreehand::KisToolFreehand(TQString transactionText)
        : super(transactionText),
        m_dragDist ( 0 ),
        m_transactionText(transactionText),
        m_mode( HOVER )
{
    m_painter = 0;
    m_currentImage = 0;
    m_tempLayer = 0;
    m_paintIncremental = true;
    m_paintOnSelection = false;
    m_paintedOutline = false;
}

KisToolFreehand::~KisToolFreehand()
{
}

void KisToolFreehand::update(KisCanvasSubject *subject)
{
    super::update(subject);
    m_currentImage = m_subject->currentImg();
}

void KisToolFreehand::buttonPress(KisButtonPressEvent *e)
{
    if (!m_subject) return;

    if (!m_subject->currentBrush()) return;

    if (!m_currentImage || !m_currentImage->activeDevice()) return;

    if (e->button() == Qt::LeftButton) {

        m_currentImage->activeDevice()->lock( true );
        kdDebug() << ">>>>>>>>>>>>>>>>>>>Locking paint device\n";

        // People complain that they can't start brush strokes outside of the image boundaries.
        // This makes sense, especially when combined with BUG:132759, so commenting out the
        // next line makes sense.
        //if (!m_currentImage->bounds().contains(e->pos().floorTQPoint())) return;

        initPaint(e);
        paintAt(e->pos(), e->pressure(), e->xTilt(), e->yTilt());

        m_prevPos = e->pos();
        m_prevPressure = e->pressure();
        m_prevXTilt = e->xTilt();
        m_prevYTilt = e->yTilt();

        TQRect r = m_painter->dirtyRect();
        if ( r.isValid() ) {
            m_dirtyRect = r;

            r = TQRect(r.left()-1, r.top()-1, r.width()+2, r.height()+2); //needed to update selectionvisualization
            if (!m_paintOnSelection) {
                    m_currentImage->activeLayer()->setDirty(r);
            }
            else {
                m_target->setDirty(r);
                // Just update the canvas. XXX: After 1.5, find a better way to make sure tools don't set dirty what they didn't touch.
                m_subject->canvasController()->updateCanvas( r );
            }
        }
    }
}

void KisToolFreehand::buttonRelease(KisButtonReleaseEvent* e)
{
    if (e->button() == Qt::LeftButton && m_mode == PAINT) {
        endPaint();
        m_currentImage->activeDevice()->lock( false );
        kdDebug() << ">>>>>>>>>>>>>>>>>>>UNLocking paint device\n";

    }
    KisToolPaint::buttonRelease(e);
}

void KisToolFreehand::move(KisMoveEvent *e)
{
    if (m_mode == PAINT) {

        paintLine(m_prevPos, m_prevPressure, m_prevXTilt, m_prevYTilt, e->pos(), e->pressure(), e->xTilt(), e->yTilt());

        m_prevPos = e->pos();
        m_prevPressure = e->pressure();
        m_prevXTilt = e->xTilt();
        m_prevYTilt = e->yTilt();

        TQRect r = m_painter->dirtyRect();

        if (r.isValid()) {
            m_dirtyRect |= r;

            if (!m_paintOnSelection) {
                    m_currentImage->activeLayer()->setDirty(r);
            }
            else {
                // Just update the canvas
                r = TQRect(r.left()-1, r.top()-1, r.width()+2, r.height()+2); //needed to update selectionvisualization
                m_target->setDirty(r);
                m_subject->canvasController()->updateCanvas( r );
            }
        }
    }
}

void KisToolFreehand::initPaint(KisEvent *)
{
    if (!m_currentImage || !m_currentImage->activeDevice()) return;

    m_mode = PAINT;
    m_dragDist = 0;

    // Create painter
    KisPaintDeviceSP device;
    if (m_currentImage && (device = m_currentImage->activeDevice())) {

        if (m_painter)
            delete m_painter;

        if (!m_paintIncremental) {
            if (m_currentImage->undo())
                m_currentImage->undoAdapter()->beginMacro(m_transactionText);

            KisLayerSupportsIndirectPainting* layer;
            if ((layer = dynamic_cast<KisLayerSupportsIndirectPainting*>(
                    m_currentImage->activeLayer().data()))) {

                // Hack for the painting of single-layered layers using indirect painting,
                // because the group layer would not have a correctly synched cache (
                // because of an optimization that would happen, having this layer as
                // projection).
                KisLayer* l = layer->layer();
                KisPaintLayer* pl = dynamic_cast<KisPaintLayer*>(l);
                if (l->parent() && (l->parent()->parent() == 0)
                    && (l->parent()->childCount() == 1)
                    && l->parent()->paintLayerInducesProjectionOptimization(pl)) {
                    // If there's a mask, device could've been the mask. The induce function
                    // should catch this, but better safe than sorry
                    l->parent()->resetProjection(pl->paintDevice());
                }

                m_target = new KisPaintDevice(m_currentImage->activeLayer(),
                                              device->colorSpace());
                layer->setTemporaryTarget(m_target);
                layer->setTemporaryCompositeOp(m_compositeOp);
                layer->setTemporaryOpacity(m_opacity);

                if (device->hasSelection())
                    m_target->setSelection(device->selection());
            }
        } else {
            m_target = device;
        }
        if(m_target->hasSelection()) m_target->selection()->startCachingExactRect();
        m_painter = new KisPainter( m_target );
        Q_CHECK_PTR(m_painter);
        m_source = device;
        if (currentImage()->undo()) m_painter->beginTransaction(m_transactionText);
    }

    m_painter->setPaintColor(m_subject->fgColor());
    m_painter->setBackgroundColor(m_subject->bgColor());
    m_painter->setBrush(m_subject->currentBrush());


    // if you're drawing on a temporary layer, the layer already sets this
    if (m_paintIncremental) {
        m_painter->setCompositeOp(m_compositeOp);
        m_painter->setOpacity(m_opacity);
    } else {
        m_painter->setCompositeOp(COMPOSITE_ALPHA_DARKEN);
        m_painter->setOpacity( OPACITY_OPAQUE );

    }

/*    kdDebug() << "target: " << m_target << "( " << m_target->name() << " )"
            << " source: " << m_source << "( " << m_source->name() << " )"
            << ", incremental " << m_paintIncremental
            << ", paint on selection: " << m_paintOnSelection
            << ", active device has selection: " << device->hasSelection()
            << ", target has selection: " << m_target->hasSelection()
            << endl;
*/
}

void KisToolFreehand::endPaint()
{
    m_mode = HOVER;
    if (m_currentImage) {

        if (m_painter) {
            // If painting in mouse release, make sure painter
            // is destructed or end()ed
            if (!m_paintIncremental) {
                if (m_currentImage->undo())
                    m_painter->endTransaction();
                KisPainter painter( m_source );
                painter.setCompositeOp(m_compositeOp);
                if (m_currentImage->undo())
                    painter.beginTransaction(m_transactionText);
                painter.bitBlt(m_dirtyRect.x(), m_dirtyRect.y(), m_compositeOp, m_target,
                               m_opacity,
                               m_dirtyRect.x(), m_dirtyRect.y(),
                               m_dirtyRect.width(), m_dirtyRect.height());

                KisLayerSupportsIndirectPainting* layer =
                    dynamic_cast<KisLayerSupportsIndirectPainting*>(m_source->parentLayer());
                layer->setTemporaryTarget(0);
                m_source->parentLayer()->setDirty(m_dirtyRect);

                if (m_currentImage->undo()) {
                    m_currentImage->undoAdapter()->addCommand(painter.endTransaction());
                    m_currentImage->undoAdapter()->endMacro();
                }
            } else {
                if (m_currentImage->undo())
                    m_currentImage->undoAdapter()->addCommand(m_painter->endTransaction());
            }
        }
        delete m_painter;
        m_painter = 0;
        notifyModified();
        if(m_target->hasSelection()) m_target->selection()->stopCachingExactRect();
    }
}

void KisToolFreehand::paintAt(const KisPoint &pos,
               const double pressure,
               const double xTilt,
               const double yTilt)
{
    painter()->paintAt(pos, pressure, xTilt, yTilt);
}

void KisToolFreehand::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)
{
    m_dragDist = painter()->paintLine(pos1, pressure1, xtilt1, ytilt1, pos2, pressure2, xtilt2, ytilt2, m_dragDist);
}


KisImageSP KisToolFreehand::currentImage()
{
    return m_currentImage;
}


void KisToolFreehand::paintOutline(const KisPoint& point) {
    if (!m_subject) {
        return;
    }

    KisCanvasController *controller = m_subject->canvasController();

    if (currentImage() && !currentImage()->bounds().contains(point.floorTQPoint())) {
        if (m_paintedOutline) {
            controller->kiscanvas()->update();
            m_paintedOutline = false;
        }
        return;
    }

    KisCanvas *canvas = controller->kiscanvas();
    canvas->repaint();

    KisBrush *brush = m_subject->currentBrush();
    // There may not be a brush present, and we shouldn't crash in that case
    if (brush) {
        KisCanvasPainter gc(canvas);
        TQPen pen(TQt::SolidLine);

        KisPoint hotSpot = brush->hotSpot();

        gc.setRasterOp(TQt::NotROP);
        gc.setPen(pen);
        gc.setViewport(0, 0, static_cast<TQ_INT32>(canvas->width() * m_subject->zoomFactor()),
                       static_cast<TQ_INT32>(canvas->height() * m_subject->zoomFactor()));
        gc.translate((- controller->horzValue()) / m_subject->zoomFactor(),
                        (- controller->vertValue()) / m_subject->zoomFactor());

        KisPoint topLeft = point - hotSpot;

        if (m_subject->currentPaintop().id() == "pen") {
            // Pen paints on whole pixels only.
            topLeft = topLeft.roundTQPoint();
        }

        gc.translate(topLeft.x(), topLeft.y());

        KisBoundaryPainter::paint(brush->boundary(), gc);
        m_paintedOutline = true;
    }
}


#include "kis_tool_freehand.moc"