summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/tools/defaulttools/kis_tool_fill.cc
blob: 1e2b82c5f50275e3ab1f627fc2cde004379195bd (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
/*
 *  kis_tool_fill.cc - part of Krayon
 *
 *  Copyright (c) 2000 John Califf <jcaliff@compuzone.net>
 *  Copyright (c) 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 <kaction.h>
#include <kdebug.h>
#include <klocale.h>
#include <kcommand.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqcheckbox.h>

#include <tqcolor.h>

#include "knuminput.h"

#include "kis_layer.h"
#include "kis_cursor.h"
#include "kis_painter.h"
#include "kis_tool_brush.h"
#include "kis_cmb_composite.h"
#include "kis_tool_fill.h"
#include "kis_colorspace.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_move_event.h"
#include "kis_pattern.h"
#include "kis_fill_painter.h"
#include "kis_progress_display_interface.h"
#include "kis_undo_adapter.h"
#include "kis_canvas_subject.h"
#include "kis_selection.h"

KisToolFill::KisToolFill()
    : super(i18n("Fill")), m_wasPressed(false)
{
    setName("tool_fill");
    m_subject = 0;
    m_oldColor = 0;
    m_threshold = 15;
    m_usePattern = false;
    m_unmerged = false;
    m_fillOnlySelection = false;

    setCursor(KisCursor::load("tool_fill_cursor.png", 6, 6));
}

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

    super::update(m_subject);
}

KisToolFill::~KisToolFill()
{
}

bool KisToolFill::flood(int startX, int startY)
{
    KisPaintDeviceSP device = m_currentImage->activeDevice();
    if (!device) return false;

    if (m_fillOnlySelection) {
        TQRect rc = device->selection()->selectedRect();
        KisPaintDeviceSP filled = new KisPaintDevice(device->colorSpace(),  "filled");
        KisFillPainter painter(filled);
        if (m_usePattern)
            painter.fillRect(rc.x(), rc.y(), rc.width(), rc.height(),
                             m_subject->currentPattern());
        else
            painter.fillRect(rc.x(), rc.y(), rc.width(), rc.height(),
                             m_subject->fgColor(), m_opacity);
        painter.end();
        KisPainter painter2(device);
        if (m_currentImage->undo()) painter2.beginTransaction(i18n("Fill"));
        painter2.bltSelection(rc.x(), rc.y() , m_compositeOp, filled, m_opacity,
                              rc.x(), rc.y(), rc.width(), rc.height());

        device->setDirty(filled->extent());
        notifyModified();

        if (m_currentImage->undo()) {
            m_currentImage->undoAdapter()->addCommand(painter2.endTransaction());
        }
        return true;
    }

    KisFillPainter painter(device);
    if (m_currentImage->undo()) painter.beginTransaction(i18n("Flood Fill"));
    painter.setPaintColor(m_subject->fgColor());
    painter.setOpacity(m_opacity);
    painter.setFillThreshold(m_threshold);
    painter.setCompositeOp(m_compositeOp);
    painter.setPattern(m_subject->currentPattern());
    painter.setSampleMerged(!m_unmerged);
    painter.setCareForSelection(true);

    KisProgressDisplayInterface *progress = m_subject->progressDisplay();
    if (progress) {
        progress->setSubject(&painter, true, true);
    }

    if (m_usePattern)
        painter.fillPattern(startX, startY);
    else
        painter.fillColor(startX, startY);

    device->setDirty(painter.dirtyRect());
    notifyModified();

    if (m_currentImage->undo()) {
        m_currentImage->undoAdapter()->addCommand(painter.endTransaction());
    }

    return true;
}

void KisToolFill::buttonPress(KisButtonPressEvent *e)
{
    m_startPos = e->pos();
    m_wasPressed = true;
}

void KisToolFill::buttonRelease(KisButtonReleaseEvent *e)
{
    if (!m_subject) return;
    if (!m_currentImage || !m_currentImage->activeDevice()) return;
    if (e->button() != Qt::LeftButton) return;
    if(!m_wasPressed) return;
    m_wasPressed = false;
    int x, y;
    x = m_startPos.floorX();
    y = m_startPos.floorY();
    if (!m_currentImage->bounds().tqcontains(x, y)) {
        return;
    }
    flood(x, y);
    notifyModified();
}

TQWidget* KisToolFill::createOptionWidget(TQWidget* tqparent)
{
    TQWidget *widget = super::createOptionWidget(tqparent);

    m_lbThreshold = new TQLabel(i18n("Threshold: "), widget);
    m_slThreshold = new KIntNumInput( widget, "int_widget");
    m_slThreshold->setRange( 1, 100);
    m_slThreshold->setSteps( 3, 3);
    m_slThreshold->setValue(m_threshold);
    connect(m_slThreshold, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(slotSetThreshold(int)));

    m_checkUsePattern = new TQCheckBox(i18n("Use pattern"), widget);
    m_checkUsePattern->setChecked(m_usePattern);
    connect(m_checkUsePattern, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotSetUsePattern(bool)));

    m_checkSampleMerged = new TQCheckBox(i18n("Limit to current layer"), widget);
    m_checkSampleMerged->setChecked(m_unmerged);
    connect(m_checkSampleMerged, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotSetSampleMerged(bool)));

    m_checkFillSelection = new TQCheckBox(i18n("Fill entire selection"), widget);
    m_checkFillSelection->setChecked(m_fillOnlySelection);
    connect(m_checkFillSelection, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotSetFillSelection(bool)));

    addOptionWidgetOption(m_slThreshold, m_lbThreshold);

    addOptionWidgetOption(m_checkFillSelection);
    addOptionWidgetOption(m_checkSampleMerged);
    addOptionWidgetOption(m_checkUsePattern);

    return widget;
}

void KisToolFill::slotSetThreshold(int threshold)
{
    m_threshold = threshold;
}

void KisToolFill::slotSetUsePattern(bool state)
{
    m_usePattern = state;
}

void KisToolFill::slotSetSampleMerged(bool state)
{
    m_unmerged = state;
}

void KisToolFill::slotSetFillSelection(bool state)
{
    m_fillOnlySelection = state;
    m_slThreshold->setEnabled(!state);
    m_checkSampleMerged->setEnabled(!state);
}

void KisToolFill::setup(KActionCollection *collection)
{
    m_action = static_cast<KRadioAction *>(collection->action(name()));

    if (m_action == 0) {
        m_action = new KRadioAction(i18n("&Fill"),
                        "tool_color_fill",
                        TQt::Key_F,
                        this,
                        TQT_SLOT(activate()),
                        collection,
                        name());
        m_action->setToolTip(i18n("Contiguous fill"));
        m_action->setExclusiveGroup("tools");
        m_ownAction = true;
    }
}

#include "kis_tool_fill.moc"