chalk

kis_filters_listview.cpp
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#include "kis_filters_listview.h"
23
24#include <tqapplication.h>
25#include "tqtimer.h"
26#include "tqpainter.h"
27#include "tqpixmap.h"
28
29#include <tdeglobalsettings.h>
30
31#include "kis_types.h"
32#include "kis_paint_device.h"
33#include "kis_cursor.h"
34#include "kis_image.h"
35#include "kis_paint_layer.h"
36#include "kis_group_layer.h"
37#include "kis_filter.h"
38#include "kis_filter_strategy.h"
39#include "kis_thread_pool.h"
40
41// ------------------------------------------------
42
43KisFiltersThumbnailThread::KisFiltersThumbnailThread(TQIconView * parent, KisFiltersIconViewItem * iconItem, KisFilterConfiguration * config, KisFilter * filter, KisPaintDeviceSP dev, const TQRect & bounds, KisProfile * profile)
44 : m_parent(parent)
45 , m_iconItem(iconItem)
46 , m_config(config)
47 , m_filter(filter)
48 , m_dev(dev)
49 , m_bounds(bounds)
50 , m_profile(profile)
51{
52}
53
54KisFiltersThumbnailThread::~KisFiltersThumbnailThread()
55{
56 m_iconItem->resetThread();
57}
58
59void KisFiltersThumbnailThread::run()
60{
61 if (m_canceled) return;
62
63 KisPaintDeviceSP thumbPreview = new KisPaintDevice(*m_dev);
64 m_filter->disableProgress();
65 m_filter->process(thumbPreview, thumbPreview, m_config, m_bounds);
66
67 if (!m_canceled) {
68 m_pixmap = thumbPreview->convertToTQImage(m_profile);
69
70 tqApp->postEvent(m_parent, new KisThumbnailDoneEvent (m_iconItem, m_pixmap));
71
72 }
73}
74
75TQPixmap KisFiltersThumbnailThread::pixmap()
76{
77 return m_pixmap;
78}
79
80void KisFiltersThumbnailThread::cancel()
81{
82 m_canceled = true;
83 m_filter->cancel();
84
85}
86
87
88// ------------------------------------------------
89
90KisFiltersIconViewItem::KisFiltersIconViewItem(TQIconView * parent, const TQString & text, const TQPixmap & icon,
91 KisID id, KisFilter* filter, KisFilterConfiguration* filterConfig,
92 KisPaintDeviceSP thumb, const TQRect & bounds, KisProfile * profile)
93 : TQIconViewItem(parent, text, icon)
94 , m_id(id)
95 , m_filter(filter)
96 , m_filterconfig(filterConfig)
97{
98 m_thread = new KisFiltersThumbnailThread(parent, this, filterConfig, filter, thumb, bounds, profile);
99}
100
101KisFiltersIconViewItem::~KisFiltersIconViewItem()
102{
103 if (m_thread) m_thread->cancel();
104}
105
106
107// ------------------------------------------------
108
109KisFiltersListView::KisFiltersListView(TQWidget* parent, bool filterForAdjustmentLayers, const char* name)
110 : TDEIconView(parent, name)
111 , m_original(0)
112 , m_profile(0)
113 , m_filterForAdjustmentLayers(filterForAdjustmentLayers)
114{
115 init();
116}
117
118KisFiltersListView::KisFiltersListView(TQWidget * parent, const char * name, WFlags f, bool filterForAdjustmentLayers)
119 : TDEIconView(parent, name, f)
120 , m_original(0)
121 , m_profile(0)
122 , m_filterForAdjustmentLayers(filterForAdjustmentLayers)
123{
124 init();
125}
126
127KisFiltersListView::KisFiltersListView(KisLayerSP layer, TQWidget* parent, bool filterForAdjustmentLayers, const char * name)
128 : TDEIconView(parent, name)
129 , m_original(0)
130 , m_profile(0)
131 , m_filterForAdjustmentLayers(filterForAdjustmentLayers)
132{
133 KisPaintLayer* pl = dynamic_cast<KisPaintLayer*>(layer.data());
134 if(pl != 0)
135 {
136 m_original = pl->paintDevice();
137 buildPreview();
138 }
139 init();
140}
141
142KisFiltersListView::KisFiltersListView(KisPaintDeviceSP device, TQWidget* parent, bool filterForAdjustmentLayers, const char * name)
143 : TDEIconView(parent, name)
144 , m_original(device)
145 , m_profile(0)
146 , m_filterForAdjustmentLayers(filterForAdjustmentLayers)
147{
148 buildPreview();
149 init();
150}
151
152void KisFiltersListView::init()
153{
154 setCaption(i18n("Filters List"));
155 setItemsMovable(false);
156 setSelectionMode(TQIconView::Single);
157 setSizePolicy(TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Expanding ));
158 setMinimumWidth(160);
159
160}
161
162void KisFiltersListView::setLayer(KisLayerSP layer) {
163 KisPaintLayer* pl = dynamic_cast<KisPaintLayer*>(layer.data());
164 if(pl == 0)
165 return;
166 KisPaintDeviceSP npd = pl->paintDevice();
167 if(npd!= m_original)
168 {
169 m_original = npd;
170 buildPreview();
171 }
172}
173
174void KisFiltersListView::setCurrentFilter(KisID filter)
175{
176 setCurrentItem(findItem(filter.name()));
177}
178
179void KisFiltersListView::buildPreview()
180{
181 TQTime t;
182 if(m_original== 0)
183 return;
184
185 TQApplication::setOverrideCursor(KisCursor::waitCursor());
186 t.start();
187 m_thumb = m_original->createThumbnailDevice(150, 150);
188
189 t.start();
190 TQRect bounds = m_thumb->exactBounds();
191 TQPixmap pm(bounds.width(), bounds.height());
192 TQPainter gc(&pm);
193 gc.fillRect(0, 0, bounds.width(), bounds.height(), backgroundColor());
194 gc.end();
195
196 t.start();
197 KisIDList l = KisFilterRegistry::instance()->listKeys();
198 KisIDList::iterator it;
199 it = l.begin();
200 // Iterate over the list of filters
201 for (it = l.begin(); it != l.end(); ++it) {
202 KisFilterSP f = KisFilterRegistry::instance()->get(*it);
203 // Check if filter support the preview and work with the current colorspace
204 if (f->supportsPreview() && f->workWith( m_original->colorSpace() ) ) {
205
206 if (m_filterForAdjustmentLayers) {
207 kdDebug() << "We're filtering for adj layers, and this filter (" << f->id().name() << ") supports them: " << f->supportsAdjustmentLayers() << endl;
208 if(!f->supportsAdjustmentLayers()) continue;
209 }
210 std::list<KisFilterConfiguration*> configlist = f->listOfExamplesConfiguration(m_thumb);
211 // apply the filter for each of example of configuration
212 for(std::list<KisFilterConfiguration*>::iterator itc = configlist.begin();
213 itc != configlist.end();
214 itc++)
215 {
216 KisFiltersIconViewItem * icon = new KisFiltersIconViewItem( this, (*it).name(), pm, *it, f, *itc, m_thumb, bounds, m_profile );
217 //KisThreadPool::instance()->enqueue(icon->thread());
218 icon->thread()->runDirectly();
219 }
220 }
221 }
222 TQApplication::restoreOverrideCursor();
223}
224
225
226void KisFiltersListView::customEvent(TQCustomEvent * e)
227{
228 KisThumbnailDoneEvent * ev = dynamic_cast<KisThumbnailDoneEvent *>(e);
229 if (ev) {
230 TQPixmap * p = ev->m_iconItem->pixmap();
231 TQImage img = ev->m_image;
232 int x, y;
233
234 if (p->width() > img.width())
235 x = (p->width() - img.width()) / 2;
236 else
237 x = 0;
238 if (p->height() > img.height())
239 y = (p->height() - img.height()) / 2;
240 else
241 y = 0;
242
243 TQPainter gc(p);
244 gc.drawImage(TQPoint(x,y), img);
245 gc.end();
246
247 //ev->m_iconItem->setPixmap(TQPixmap(*p));
248 arrangeItemsInGrid();
249 }
250}
A KisFilterConfiguration is the serializable representation of the filter parameters.
Basic interface of a Chalk filter.
Definition kis_filter.h:46
Chalk has a large number of extensible resources.
Definition kis_id.h:57
A paint device contains the actual pixel data and offers methods to read and write pixels.
This layer is of a type that can be painted on.