summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_filter_manager.cpp
blob: 848f99b73e12e4338b922fe8bb154c7ddb886efa (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
 *  Copyright (c) 2007 Benjamin Schleimer <bensch128@yahoo.com>
 *
 *  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 "tqsignalmapper.h"
#include <tqlayout.h>
#include <tqframe.h>
#include <tqcursor.h>
#include <tqapplication.h>
#include <tdemessagebox.h>
#include <kguiitem.h>

#include <kis_cursor.h>
#include "tdeaction.h"

#include "kis_part_layer.h"
#include "kis_id.h"
#include "kis_view.h"
#include "kis_doc.h"
#include "kis_filter.h"
#include "kis_layer.h"
#include "kis_paint_device.h"
#include "kis_paint_layer.h"
#include "kis_filter_manager.h"
#include "kis_filter_config_widget.h"
#include "kis_previewwidget.h"
#include "kis_previewdialog.h"
#include "kis_filter_registry.h"
#include "kis_transaction.h"
#include "kis_undo_adapter.h"
#include "kis_previewdialog.h"
#include "kis_previewwidget.h"
#include "kis_painter.h"
#include "kis_selection.h"
#include "kis_id.h"
#include "kis_canvas_subject.h"
#include "kis_doc.h"
#include "kis_transaction.h"
#include <kis_progress_display_interface.h>

KisFilterManager::KisFilterManager(KisView * view, KisDoc * doc)
    : m_view(view),
    m_doc(doc)
{
    // XXX: Store & restore last filter & last filter configuration in session settings
    m_reapplyAction = 0;
    m_lastFilterConfig = 0;
    m_lastDialog = 0;
    m_lastFilter = 0;
    m_lastWidget = 0;

    m_filterMapper = new TQSignalMapper(this);

    connect(m_filterMapper, TQT_SIGNAL(mapped(int)), this, TQT_SLOT(slotApplyFilter(int)));

}

KisFilterManager::~KisFilterManager()
{
    //delete m_reapplyAction;
    //delete m_lastFilterConfig;
    //delete m_filterMapper;
}

void KisFilterManager::setup(TDEActionCollection * ac)
{
    KisFilter * f = 0;
    int i = 0;

    // Only create the submenu's we've actually got filters for.
    // XXX: Make this list extensible after 1.5

    TDEActionMenu * other = 0;
    TDEActionMenu * am = 0;

    m_filterList = KisFilterRegistry::instance()->listKeys();

    for ( KisIDList::Iterator it = m_filterList.begin(); it != m_filterList.end(); ++it ) {
        f = KisFilterRegistry::instance()->get(*it);
        if (!f) break;

        TQString s = f->menuCategory();
        if (s == "adjust" && !m_filterActionMenus.find("adjust")) {
            am = new TDEActionMenu(i18n("Adjust"), ac, "adjust_filters");
            m_filterActionMenus.insert("adjust", am);
        }

        else if (s == "artistic" && !m_filterActionMenus.find("artistic")) {
            am = new TDEActionMenu(i18n("Artistic"), ac, "artistic_filters");
            m_filterActionMenus.insert("artistic", am);
        }

        else if (s == "blur" && !m_filterActionMenus.find("blur")) {
            am = new TDEActionMenu(i18n("Blur"), ac, "blur_filters");
            m_filterActionMenus.insert("blur", am);
        }

        else if (s == "colors" && !m_filterActionMenus.find("colors")) {
            am = new TDEActionMenu(i18n("Colors"), ac, "color_filters");
            m_filterActionMenus.insert("colors", am);
        }

        else if (s == "decor" && !m_filterActionMenus.find("decor")) {
            am = new TDEActionMenu(i18n("Decor"), ac, "decor_filters");
            m_filterActionMenus.insert("decor", am);
        }

        else if (s == "edge" && !m_filterActionMenus.find("edge")) {
            am = new TDEActionMenu(i18n("Edge Detection"), ac, "edge_filters");
            m_filterActionMenus.insert("edge", am);
        }

        else if (s == "emboss" && !m_filterActionMenus.find("emboss")) {
            am = new TDEActionMenu(i18n("Emboss"), ac, "emboss_filters");
            m_filterActionMenus.insert("emboss", am);
        }

        else if (s == "enhance" && !m_filterActionMenus.find("enhance")) {
            am = new TDEActionMenu(i18n("Enhance"), ac, "enhance_filters");
            m_filterActionMenus.insert("enhance", am);
        }

        else if (s == "map" && !m_filterActionMenus.find("map")) {
            am = new TDEActionMenu(i18n("Map"), ac, "map_filters");
            m_filterActionMenus.insert("map", am);
        }

        else if (s == "nonphotorealistic" && !m_filterActionMenus.find("nonphotorealistic")) {
            am = new TDEActionMenu(i18n("Non-photorealistic"), ac, "nonphotorealistic_filters");
            m_filterActionMenus.insert("nonphotorealistic", am);
        }

        else if (s == "other" && !m_filterActionMenus.find("other")) {
            other = new TDEActionMenu(i18n("Other"), ac, "misc_filters");
            m_filterActionMenus.insert("other", other);
        }

    }

    m_reapplyAction = new TDEAction(i18n("Apply Filter Again"),
                "Ctrl+Shift+F",
                this, TQT_SLOT(slotApply()),
                ac, "filter_apply_again");

    m_reapplyAction->setEnabled(false);

    f = 0;
    i = 0;
    for ( KisIDList::Iterator it = m_filterList.begin(); it != m_filterList.end(); ++it ) {
        f = KisFilterRegistry::instance()->get(*it);

        if (!f) break;

        // Create action
        TDEAction * a = new TDEAction(f->menuEntry(), 0, m_filterMapper, TQT_SLOT(map()), ac,
                                  TQString("chalk_filter_%1").arg((*it) . id()).ascii());

        // Add action to the right submenu
        TDEActionMenu * m = m_filterActionMenus.find( f->menuCategory() );
        if (m) {
            m->insert(a);
        }
        else {
            if (!other) {
                other = new TDEActionMenu(i18n("Other"), ac, "misc_filters");
                m_filterActionMenus.insert("other", am);
            }
            other->insert(a);
        }

        // Add filter to list of filters for mapper
        m_filterMapper->setMapping( a, i );

        m_filterActions.append( a );
        ++i;
    }
}

void KisFilterManager::updateGUI()
{
    KisImageSP img = m_view->currentImg();
    if (!img) return;

    KisLayerSP layer = img->activeLayer();
    if (!layer) return;

    KisPartLayer * partLayer = dynamic_cast<KisPartLayer*>(layer.data());

    bool enable =  !(layer->locked() || !layer->visible() || partLayer);
    KisPaintLayerSP player = dynamic_cast<KisPaintLayer*>( layer.data());
    if(!player)
    {
        enable = false;
    }
    m_reapplyAction->setEnabled(m_lastFilterConfig);
    if (m_lastFilterConfig)
        m_reapplyAction->setText(i18n("Apply Filter Again") + ": "
            + KisFilterRegistry::instance()->get(m_lastFilterConfig->name())->id().name());
    else
        m_reapplyAction->setText(i18n("Apply Filter Again"));

    TDEAction * a;
    int i = 0;
    for (a = m_filterActions.first(); a; a = m_filterActions.next() , i++) {
        KisFilter* filter = KisFilterRegistry::instance()->get(m_filterList[i]);
        if(player && filter->workWith( player->paintDevice()->colorSpace()))
        {
            a->setEnabled(enable);
        } else {
            a->setEnabled(false);
        }
    }

}

void KisFilterManager::slotApply()
{
    apply();
}

bool KisFilterManager::apply()
{
    if (!m_lastFilter) return false;

    KisImageSP img = m_view->currentImg();
    if (!img) return false;

    KisPaintDeviceSP dev = img->activeDevice();
    if (!dev) return false;

    TQApplication::setOverrideCursor( KisCursor::waitCursor() );

    //Apply the filter
    m_lastFilterConfig = m_lastFilter->configuration(m_lastWidget);

    TQRect r1 = dev->extent();
    TQRect r2 = img->bounds();

    // Filters should work only on the visible part of an image.
    TQRect rect = r1.intersect(r2);

    if (dev->hasSelection()) {
        TQRect r3 = dev->selection()->selectedExactRect();
        rect = rect.intersect(r3);
    }

    m_lastFilter->enableProgress();

    m_view->progressDisplay()->setSubject(m_lastFilter, true, true);
    m_lastFilter->setProgressDisplay( m_view->progressDisplay());

    KisTransaction * cmd = 0;
    if (img->undo()) cmd = new KisTransaction(m_lastFilter->id().name(), dev);

    m_lastFilter->process(dev, dev, m_lastFilterConfig, rect);
    m_reapplyAction->setEnabled(m_lastFilterConfig);
    if (m_lastFilterConfig)
        m_reapplyAction->setText(i18n("Apply Filter Again") + ": "
            + KisFilterRegistry::instance()->get(m_lastFilterConfig->name())->id().name());

    else
        m_reapplyAction->setText(i18n("Apply Filter Again"));

    m_lastFilter->disableProgress();
    TQApplication::restoreOverrideCursor();
    

    if (m_lastFilter->cancelRequested()) {
        delete m_lastFilterConfig;
        if (cmd) {
            cmd->unexecute();
            delete cmd;
        }
        return false;

    } else {
        if (dev->parentLayer()) dev->parentLayer()->setDirty(rect);
        m_doc->setModified(true);
        if (img->undo() && cmd) img->undoAdapter()->addCommand(cmd);
        return true;
    }
}

void KisFilterManager::slotApplyFilter(int i)
{
    KisPreviewDialog * oldDialog = m_lastDialog;
    KisFilterConfiguration * oldConfig = m_lastFilterConfig;
    KisFilter * oldFilter = m_lastFilter;

    m_lastFilter = KisFilterRegistry::instance()->get(m_filterList[i]);

    if (!m_lastFilter) {
        m_lastFilter = oldFilter;
        return;
    }

    KisImageSP img = m_view->currentImg();
    if (!img) return;

    KisPaintDeviceSP dev = img->activeDevice();
    if (!dev) return;

    if (dev->colorSpace()->willDegrade(m_lastFilter->colorSpaceIndependence())) {
        // Warning bells!
        if (m_lastFilter->colorSpaceIndependence() == TO_LAB16) {
            if (KMessageBox::warningContinueCancel(m_view,
                                               i18n("The %1 filter will convert your %2 data to 16-bit L*a*b* and vice versa. ")
                                                       .arg(m_lastFilter->id().name())
                                                       .arg(dev->colorSpace()->id().name()),
                                               i18n("Filter Will Convert Your Layer Data"),
                                               KGuiItem(i18n("Continue")),
                                               "lab16degradation") != KMessageBox::Continue) return;

        }
        else if (m_lastFilter->colorSpaceIndependence() == TO_RGBA8) {
            if (KMessageBox::warningContinueCancel(m_view,
                                               i18n("The %1 filter will convert your %2 data to 8-bit RGBA and vice versa. ")
                                                       .arg(m_lastFilter->id().name())
                                                       .arg(dev->colorSpace()->id().name()),
                                               i18n("Filter Will Convert Your Layer Data"),
                                               KGuiItem(i18n("Continue")),
                                               "rgba8degradation") != KMessageBox::Continue) return;
        }
    }

    m_lastFilter->disableProgress();

    // Create the config dialog
    m_lastDialog = new KisPreviewDialog(m_view, m_lastFilter->id().name().ascii(), true, m_lastFilter->id().name());
    TQ_CHECK_PTR(m_lastDialog);
    m_lastWidget = m_lastFilter->createConfigurationWidget( (TQWidget*)m_lastDialog->container(), dev );

    bool accepted = true;

    if( m_lastWidget != 0)
    {
        connect(m_lastWidget, TQT_SIGNAL(sigPleaseUpdatePreview()), this, TQT_SLOT(slotConfigChanged()));

        m_lastDialog->previewWidget()->slotSetDevice( dev );

        connect(m_lastDialog->previewWidget(), TQT_SIGNAL(updated()), this, TQT_SLOT(refreshPreview()));

        TQGridLayout *widgetLayout = new TQGridLayout((TQWidget *)m_lastDialog->container(), 1, 1);

        widgetLayout->addWidget(m_lastWidget, 0 , 0);

        m_lastDialog->container()->setMinimumSize(m_lastWidget->minimumSize());

        refreshPreview();

        if(m_lastDialog->exec() == TQDialog::Rejected )
        {
	    accepted = false;
        }
    }

    if (!accepted || !apply()) {
	// Override the old configuration
        m_lastFilterConfig = oldConfig;
        m_lastDialog = oldDialog;
        m_lastFilter = oldFilter;
    } else {
        delete oldDialog;
        delete oldConfig;
    }

}

void KisFilterManager::slotConfigChanged()
{
    if( m_lastDialog == 0 )
        return;
    if(m_lastDialog->previewWidget()->getAutoUpdate())
    {
        refreshPreview();
    } else {
        m_lastDialog->previewWidget()->needUpdate();
    }
}


void KisFilterManager::refreshPreview( )
{
    if( m_lastDialog == 0 ) return;

    KisFilterConfiguration* config = m_lastFilter->configuration(m_lastWidget);

    // The preview widget is in charge of running the filter so it can optimize the performance
    m_lastDialog->previewWidget()->runFilter(m_lastFilter, config);
}


#include "kis_filter_manager.moc"