summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/tools/selectiontools/kis_tool_select_eraser.cc
blob: 7054f69cfbe99817e28cc3c92bdbd5812cc916dd (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
/*
 *  kis_tool_select_brush.cc - part of Chalk
 *
 *  Copyright (c) 2004 Boudewijn Rempt <[email protected]>
 *
 *  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 <kdebug.h>
#include <kaction.h>
#include <kcommand.h>
#include <klocale.h>

#include "kis_brush.h"
#include "kis_layer.h"
#include "kis_paintop.h"
#include "kis_paintop_registry.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_cmb_composite.h"
#include "kis_cursor.h"
#include "kis_doc.h"
#include "kis_move_event.h"
#include "kis_painter.h"
#include "kis_selection.h"
#include "kis_tool_select_eraser.h"
#include "kis_types.h"
#include "kis_view.h"
#include "kis_selection_options.h"

KisToolSelectEraser::KisToolSelectEraser()
        : super(i18n("SelectEraser"))
{
    setName("tool_select_eraser");
    setCursor(KisCursor::load("tool_eraser_selection_cursor.png", 5, 5));
    m_optWidget = 0;
    m_paintOnSelection = true;
}

KisToolSelectEraser::~KisToolSelectEraser()
{
}

void KisToolSelectEraser::activate()
{
    super::activate();

    if (!m_optWidget)
        return;

    m_optWidget->slotActivated();
}

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

    m_mode = PAINT;
    m_dragDist = 0;

    // Create painter
    KisPaintDeviceSP dev = m_currentImage->activeDevice();
    
    if (dev == 0) return;
    
    if (m_painter)
        delete m_painter;
    if(! dev->hasSelection())
    {
        dev->selection()->clear();
        dev->emitSelectionChanged();
    }
    KisSelectionSP selection = dev->selection();

    m_target = selection;
    m_painter = new KisPainter(selection.data());
    TQ_CHECK_PTR(m_painter);
    m_painter->beginTransaction(i18n("Selection Eraser"));
    m_painter->setPaintColor(KisColor(TQt::white, selection->colorSpace()));
    m_painter->setBrush(m_subject->currentBrush());
    m_painter->setOpacity(OPACITY_OPAQUE);
    m_painter->setCompositeOp(COMPOSITE_ERASE);
    KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp("eraser", 0, painter());
    painter()->setPaintOp(op); // And now the painter owns the op and will destroy it.

    // Set the cursor -- ideally. this should be a mask created from the brush,
    // now that X11 can handle colored cursors.
#if 0
    // Setting cursors has no effect until the tool is selected again; this
    // should be fixed.
    setCursor(KisCursor::eraserCursor());
#endif
}

void KisToolSelectEraser::endPaint() {
    super::endPaint();
    if (m_currentImage && m_currentImage->activeDevice())
        m_currentImage->activeDevice()->emitSelectionChanged();
}
void KisToolSelectEraser::setup(KActionCollection *collection)
{
    m_action = static_cast<KRadioAction *>(collection->action(name()));

    if (m_action == 0) {
        m_action = new KRadioAction(i18n("Selection &Eraser"),
                        "tool_eraser_selection", "Ctrl+Shift+E", this,
                        TQT_SLOT(activate()), collection,
                        name());
        TQ_CHECK_PTR(m_action);
        m_action->setToolTip(i18n("Erase parts of a selection"));
        m_action->setExclusiveGroup("tools");
        m_ownAction = true;
    }
}

TQWidget* KisToolSelectEraser::createOptionWidget(TQWidget* parent)
{
    Q_UNUSED(parent);
    // Commented out due to the fact that this doesn't actually work if you change the action
#if 0
    m_optWidget = new KisSelectionOptions(parent, m_subject);
    TQ_CHECK_PTR(m_optWidget);
    m_optWidget->setCaption(i18n("Selection Eraser"));

    TQVBoxLayout * l = dynamic_cast<TQVBoxLayout*>(m_optWidget->layout());
    l->addItem(new TQSpacerItem(1, 1, TQSizePolicy::Fixed, TQSizePolicy::Expanding));

    return m_optWidget;
#endif
    return 0;
}

TQWidget* KisToolSelectEraser::optionWidget()
{
    return m_optWidget;
}

#include "kis_tool_select_eraser.moc"