summaryrefslogtreecommitdiffstats
path: root/chalk/plugins/tools/tool_filter/kis_tool_filter.cc
blob: 07133d3bb5a357bbd065e68c91be7a66db5f92b7 (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
/*
 *  kis_tool_filter.cc - part of Chalk
 *
 *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
 *
 *  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 <tqbitmap.h>
#include <tqpainter.h>
#include <tqcombobox.h>
#include <tqlayout.h>
#include <tqlabel.h>

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

#include "kis_filter_config_widget.h"
#include "kis_tool_filter.h"
#include <kis_brush.h>
#include <kis_button_press_event.h>
#include <kis_button_release_event.h>
#include <kis_canvas_subject.h>
#include <kis_cmb_idlist.h>
#include <kis_cursor.h>
#include <kis_doc.h>
#include <kis_filter.h>
#include <kis_filterop.h>
#include <kis_id.h>
#include <kis_image.h>
#include <kis_layer.h>
#include <kis_move_event.h>
#include <kis_painter.h>
#include <kis_paintop.h>
#include <kis_paintop_registry.h>
#include <kis_vec.h>

KisToolFilter::KisToolFilter()
    : super(i18n("Filter Brush")), m_filterConfigurationWidget(0)
{
    setName("tool_filter");
    m_subject = 0;
    setCursor(KisCursor::load("tool_filter_cursor.png", 5, 5));
}

KisToolFilter::~KisToolFilter()
{
}

void KisToolFilter::setup(TDEActionCollection *collection)
{
    m_action = static_cast<TDERadioAction *>(collection->action(name()));

    if (m_action == 0) {
        m_action = new TDERadioAction(i18n("&Filter Brush"),
                        "tool_filter", 0, this,
                        TQT_SLOT(activate()), collection,
                        name());
        TQ_CHECK_PTR(m_action);
        m_action->setToolTip(i18n("Paint with filters"));
        m_action->setExclusiveGroup("tools");
        m_ownAction = true;
    }
}

void KisToolFilter::initPaint(KisEvent *e)
{
    // Some filters want to paint directly on the current state of
    // the canvas, others cannot handle that and need a temporary layer
    // so they can work on the old data before painting started.
    m_paintIncremental = m_filter->supportsIncrementalPainting();
    
    super::initPaint(e);
    KisPaintOp * op = KisPaintOpRegistry::instance()->paintOp("filter", 0, painter());
    op->setSource ( m_source );
    painter()->setPaintOp(op); // And now the painter owns the op and will destroy it.
    painter()->setFilter( m_filter );

    // XXX: Isn't there a better way to set the config? The filter config widget needs to
    // to go into the tool options widget, and just the data carried over to the filter.
    // I've got a bit of a problem with core classes having too much GUI about them.
    // BSAR.
    dynamic_cast<KisFilterOp *>(op)->setFilterConfiguration( m_filter->configuration( m_filterConfigurationWidget) );
}

TQWidget* KisToolFilter::createOptionWidget(TQWidget* parent)
{
    TQWidget *widget = super::createOptionWidget(parent);

    m_cbFilter = new KisCmbIDList(widget);
    TQ_CHECK_PTR(m_cbFilter);

    TQLabel* lbFilter = new TQLabel(i18n("Filter:"), widget);
    TQ_CHECK_PTR(lbFilter);

    // Check which filters support painting
    KisIDList l = KisFilterRegistry::instance()->listKeys();
    KisIDList l2;
    KisIDList::iterator it;
    for (it = l.begin(); it !=  l.end(); ++it) {
        KisFilterSP f = KisFilterRegistry::instance()->get(*it);
        if (f->supportsPainting()) {
            l2.push_back(*it);
        }
    }
    m_cbFilter ->setIDList( l2 );

    addOptionWidgetOption(m_cbFilter, lbFilter);

    m_optionLayout = new TQGridLayout(widget, 1, 1, 0, 6);
    TQ_CHECK_PTR(m_optionLayout);
    super::addOptionWidgetLayout(m_optionLayout);

    connect(m_cbFilter, TQT_SIGNAL(activated ( const KisID& )), this, TQT_SLOT( changeFilter( const KisID& ) ) );
    changeFilter( m_cbFilter->currentItem () );

    return widget;
}

void KisToolFilter::changeFilter( const KisID & id)
{
    m_filter =  KisFilterRegistry::instance()->get( id );
    Q_ASSERT(m_filter != 0);
    if( m_filterConfigurationWidget != 0 )
    {
        m_optionLayout->remove ( m_filterConfigurationWidget );
        delete m_filterConfigurationWidget;
    }

    m_source = m_currentImage->activeDevice();
    if (!m_source) return;

    m_filterConfigurationWidget = m_filter->createConfigurationWidget( optionWidget(), m_source );
    if( m_filterConfigurationWidget != 0 )
    {
        m_optionLayout->addMultiCellWidget ( m_filterConfigurationWidget, 2, 2, 0, 1 );
        m_filterConfigurationWidget->show();
    }
}

#include "kis_tool_filter.moc"