summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/tools/defaulttools/kis_tool_brush.cc
blob: 7c83a36b0bd0b63f6d76c80457eca3c2f9a3ba15 (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
/*
 *  kis_tool_brush.cc - part of Chalk
 *
 *  Copyright (c) 2003-2004 Boudewijn Rempt <boud@valdyas.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.
 */
#include <tqevent.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqwidget.h>
#include <tqtimer.h>
#include <tqpushbutton.h>
#include <tqpainter.h>
#include <tqrect.h>
#include <tqcheckbox.h>

#include <kdebug.h>
#include <tdeaction.h>
#include <kcommand.h>
#include <tdelocale.h>

#include "kis_config.h"
#include "kis_brush.h"
#include "kis_paintop.h"
#include "kis_paintop_registry.h"
#include "kis_cmb_composite.h"
#include "kis_cursor.h"
#include "kis_painter.h"
#include "kis_tool_brush.h"
#include "kis_canvas_subject.h"
#include "kis_boundary.h"
#include "kis_move_event.h"
#include "kis_canvas.h"
#include "kis_layer.h"

KisToolBrush::KisToolBrush()
        : super(i18n("Brush"))
{
    setName("tool_brush");
    setCursor(KisCursor::load("tool_freehand_cursor.png", 5, 5));
    m_rate = 100; // Conveniently hardcoded for now
    m_timer = new TQTimer(this);
    TQ_CHECK_PTR(m_timer);

    connect(m_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(timeoutPaint()));

}

KisToolBrush::~KisToolBrush()
{
    delete m_timer;
    m_timer = 0;
}

void KisToolBrush::timeoutPaint()
{
    if (currentImage() && painter()) {
        painter()->paintAt(m_prevPos, m_prevPressure, m_prevXTilt, m_prevYTilt);
        currentImage()->activeLayer()->setDirty(painter()->dirtyRect());
    }
}


void KisToolBrush::update(KisCanvasSubject *subject)
{
    super::update(subject);
}

void KisToolBrush::initPaint(KisEvent *e)
{
    super::initPaint(e);

    if (!m_painter) {
        kdWarning() << "Didn't create a painter! Something is wrong!\n";
        return;
    }
    KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp(m_subject->currentPaintop(), m_subject->currentPaintopSettings(), m_painter);
    if (!op) return;
    
    m_subject->canvasController()->kiscanvas()->update(); // remove the outline

    painter()->setPaintOp(op); // And now the painter owns the op and will destroy it.

    if (op->incremental()) {
        m_timer->start( m_rate );
    }
}


void KisToolBrush::endPaint()
{
    m_timer->stop();
    super::endPaint();
}


void KisToolBrush::setup(TDEActionCollection *collection)
{

    m_action = static_cast<TDERadioAction *>(collection->action(name()));

    if (m_action == 0) {
        m_action = new TDERadioAction(i18n("&Brush"),
                                    "tool_freehand", TQt::Key_B, this,
                                    TQT_SLOT(activate()), collection,
                                    name());
        m_action->setToolTip(i18n("Draw freehand"));
        m_action->setExclusiveGroup("tools");
        m_ownAction = true;
    }
}

void KisToolBrush::move(KisMoveEvent *e) {
    KisToolFreehand::move(e);
    KisConfig cfg;
    if (m_mode != PAINT && cfg.cursorStyle() == CURSOR_STYLE_OUTLINE)
        paintOutline(e->pos());
}

void KisToolBrush::leave(TQEvent */*e*/) {
    m_subject->canvasController()->kiscanvas()->update(); // remove the outline
}


void KisToolBrush::slotSetPaintingMode( int mode )
{
    if (mode == TQButton::On) {
        // Direct painting
        m_paintIncremental = true;
    }
    else {
        m_paintIncremental = false;
    }
}


TQWidget* KisToolBrush::createOptionWidget(TQWidget* parent)
{
    TQWidget *widget = super::createOptionWidget(parent);
    m_chkDirect = new TQCheckBox(i18n("Paint direct"), widget, "chkDirect");
    m_chkDirect->setChecked(true);
    connect(m_chkDirect, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(slotSetPaintingMode(int)));
    
    m_optionLayout = new TQGridLayout(NULL, 3, 2, 0, 6);
    TQ_CHECK_PTR(m_optionLayout);

    super::addOptionWidgetLayout(m_optionLayout);
    m_optionLayout->addWidget(m_chkDirect, 0, 0);

    return widget;
}

#include "kis_tool_brush.moc"