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
409
|
/*
* kis_previewwidget.cpp - part of Chalk
*
* Copyright (c) 2001 John Califf <[email protected]>
* Copyright (c) 2004 Bart Coppens <[email protected]>
* Copyright (c) 2005 Cyrille Berger <[email protected]>
* Copyright (c) 2007 Ben Schleimer <[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 <tqcheckbox.h>
#include <tqradiobutton.h>
#include <tqpainter.h>
#include <tqpoint.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqapplication.h>
#include <tqcolor.h>
#include <tqgroupbox.h>
#include <tqcursor.h>
#include <tqtimer.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <kis_cursor.h>
#include <kis_colorspace.h>
#include <kis_colorspace_factory_registry.h>
#include <kis_config.h>
#include <kis_filter_strategy.h>
#include <kis_global.h>
#include <kis_image.h>
#include <kis_layer.h>
#include <kis_paint_layer.h>
#include <kis_group_layer.h>
#include <kis_meta_registry.h>
#include <kis_painter.h>
#include <kis_profile.h>
#include <kis_types.h>
#include <kis_undo_adapter.h>
#include <kis_label_progress.h>
#include <kis_selection.h>
#include <kis_transform_worker.h>
#include "kis_previewwidgetbase.h"
#include "kis_previewwidget.h"
#include "imageviewer.h"
static const int ZOOM_PAUSE = 100;
static const int FILTER_PAUSE = 500;
static const double ZOOM_FACTOR = 1.1;
KisPreviewWidget::KisPreviewWidget( TQWidget* parent, const char* name )
: PreviewWidgetBase( parent, name )
, m_autoupdate(true)
, m_previewIsDisplayed(true)
, m_scaledOriginal()
, m_dirtyOriginal(true)
, m_origDevice(new KisPaintDevice(KisMetaRegistry::instance()->csRegistry()->getRGB8(), "temp"))
, m_scaledPreview()
, m_dirtyPreview(true)
, m_previewDevice(new KisPaintDevice(KisMetaRegistry::instance()->csRegistry()->getRGB8(), "temp"))
, m_scaledImage(NULL)
, m_filterZoom(1.0)
, m_zoom(-1.0)
, m_profile(NULL)
, m_progress( 0 )
, m_zoomTimer(new TQTimer(this))
, m_filterTimer(new TQTimer(this))
, m_firstFilter(true)
, m_firstZoom(true)
{
btnZoomIn->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "zoom-in", TDEIcon::MainToolbar, 16 ));
connect(btnZoomIn, TQT_SIGNAL(clicked()), this, TQT_SLOT(zoomIn()));
btnZoomOut->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "zoom-out", TDEIcon::MainToolbar, 16 ));
connect(btnZoomOut, TQT_SIGNAL(clicked()), this, TQT_SLOT(zoomOut()));
btnUpdate->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "reload", TDEIcon::MainToolbar, 16 ));
connect(btnUpdate, TQT_SIGNAL(clicked()), this, TQT_SLOT(forceUpdate()));
connect(radioBtnPreview, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(setPreviewDisplayed(bool)));
connect(checkBoxAutoUpdate, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slotSetAutoUpdate(bool)));
btnZoomOneToOne->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "zoom-original", TDEIcon::MainToolbar, 16 ));
connect(btnZoomOneToOne, TQT_SIGNAL(clicked()), this, TQT_SLOT(zoomOneToOne()));
m_progress = new KisLabelProgress(frmProgress);
m_progress->setMaximumHeight(fontMetrics().height() );
TQVBoxLayout *vbox = new TQVBoxLayout( frmProgress );
vbox->addWidget(m_progress);
m_progress->hide();
connect(m_zoomTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateZoom()));
connect(m_filterTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(runFilterHelper()));
/* kToolBar1->insertLineSeparator();
kToolBar1->insertButton("reload",2, true, i18n("Update"));
connect(kToolBar1->getButton(2),TQT_SIGNAL(clicked()),this,TQT_SLOT(forceUpdate()));
kToolBar1->insertButton("",3, true, i18n("Auto Update"));
connect(kToolBar1->getButton(3),TQT_SIGNAL(clicked()),this,TQT_SLOT(toggleAutoUpdate()));
kToolBar1->insertButton("",4, true, i18n("Switch"));
connect(kToolBar1->getButton(4),TQT_SIGNAL(clicked()),this,TQT_SLOT(toggleImageDisplayed()));*/
// these currently don't yet work, reenable when they do work :) (TZ-12-2005)
// TODO reenable these
// kToolBar1->insertButton("",5, true, i18n("Popup Original and Preview"));
}
KisPreviewWidget::~KisPreviewWidget() { }
void KisPreviewWidget::forceUpdate()
{
if(m_previewIsDisplayed)
{
m_groupBox->setTitle(m_origDevice->name());
emit updated();
}
}
void KisPreviewWidget::slotSetDevice(KisPaintDeviceSP dev)
{
Q_ASSERT( dev );
if (!dev) return;
m_origDevice = dev;
m_previewDevice = dev;
m_filterZoom = 1.0;
KisConfig cfg;
TQString monitorProfileName = cfg.monitorProfile();
m_profile = KisMetaRegistry::instance()->csRegistry()->getProfileByName(monitorProfileName);
TQRect r = dev->exactBounds();
m_groupBox->setTitle(i18n("Preview: ") + dev->name());
m_previewIsDisplayed = true;
m_zoom = -1.0;
zoomChanged(double(m_preview->width()) / double(r.width()) );
}
void KisPreviewWidget::updateZoom()
{
TQApplication::setOverrideCursor(KisCursor::waitCursor());
if(m_previewIsDisplayed)
{
if(m_dirtyPreview)
{
TQSize r = m_previewDevice->extent().size();
int w = r.width(), h = r.height();
int sw = int(ceil(m_zoom * w / m_filterZoom));
int sh = int(ceil(m_zoom * h / m_filterZoom));
m_dirtyPreview = false;
m_scaledPreview = m_previewDevice->convertToTQImage(m_profile, 0, 0, w, h);
m_scaledPreview = m_scaledPreview.scale(sw,sh, TQ_ScaleMax); // Use scale instead of smoothScale for speed up
}
m_preview->setImage(m_scaledPreview);
} else
{
if(m_dirtyOriginal)
{
TQSize r = m_origDevice->extent().size();
int w = r.width(), h = r.height();
int sw = int(ceil(m_zoom * w));
int sh = int(ceil(m_zoom * h));
m_dirtyOriginal = false;
m_scaledOriginal = m_origDevice->convertToTQImage(m_profile, 0, 0, w, h);
m_scaledOriginal = m_scaledOriginal.scale(sw,sh, TQ_ScaleMax); // Use scale instead of smoothScale for speed up
}
m_preview->setImage(m_scaledOriginal);
}
TQApplication::restoreOverrideCursor();
}
void KisPreviewWidget::slotSetAutoUpdate(bool set) {
m_autoupdate = set;
}
void KisPreviewWidget::wheelEvent(TQWheelEvent * e)
{
if (e->delta() > 0) {
zoomIn();
} else {
zoomOut();
}
e->accept();
}
void KisPreviewWidget::setPreviewDisplayed(bool v)
{
if(v != m_previewIsDisplayed)
{
m_previewIsDisplayed = v;
if(m_previewIsDisplayed) {
m_groupBox->setTitle(i18n("Preview: ") + m_origDevice->name());
} else {
m_groupBox->setTitle(i18n("Original: ") + m_origDevice->name());
}
// Call directly without any pause because there is no scaling
updateZoom();
}
}
void KisPreviewWidget::needUpdate()
{
if(m_previewIsDisplayed)
m_groupBox->setTitle(i18n("Preview (needs update)"));
}
bool KisPreviewWidget::getAutoUpdate() const {
return m_autoupdate;
}
void KisPreviewWidget::zoomChanged(const double zoom)
{
// constrain the zoom
double tZoom = zoom;
if(zoom <= 1./8.) { tZoom = 1./8.; }
if(zoom > 8.) { tZoom = 8.; }
if(tZoom != m_zoom)
{
m_zoom = tZoom;
m_dirtyOriginal = true;
m_dirtyPreview = true;
if(m_firstZoom) {
m_firstZoom = false;
updateZoom();
} else {
m_zoomTimer->start(ZOOM_PAUSE, true);
}
}
}
void KisPreviewWidget::zoomIn() {
zoomChanged(m_zoom * ZOOM_FACTOR);
}
void KisPreviewWidget::zoomOut() {
zoomChanged(m_zoom / ZOOM_FACTOR);
}
void KisPreviewWidget::zoomOneToOne() {
zoomChanged(1.0);
}
static inline void cropDevice(KisPaintDevice * device, const double & zoom) {
TQRect r = device->exactBounds();
r.setX(int(zoom * r.x()) );
r.setY(int(zoom * r.y()) );
r.setWidth(int(zoom * r.width()) );
r.setHeight(int(zoom * r.height()) );
device->crop(r);
}
class MyCropVisitor : public KisLayerVisitor {
const double m_zoom;
public:
MyCropVisitor(const double & z) : m_zoom(z) { }
virtual ~MyCropVisitor() { }
virtual bool visit(KisPaintLayer *layer) {
KisPaintDeviceSP device = layer->paintDevice();
::cropDevice(device.data(), m_zoom);
// Make sure we have a tight fit for the selection
if(device->hasSelection()) {
::cropDevice(device->selection().data(), m_zoom);
}
return true;
}
virtual bool visit(KisGroupLayer *layer) {
for(KisLayerSP l = layer->firstChild(); l; l = l->nextSibling()) {
l->accept(*this);
}
return true;
}
virtual bool visit(KisPartLayer *) { return true; }
virtual bool visit(KisAdjustmentLayer *) { return true; }
};
void KisPreviewWidget::runFilter(KisFilter * filter, KisFilterConfiguration * config) {
if(!filter) return;
if(!config) return;
m_filter = filter;
m_config = config;
if(m_firstFilter) {
m_firstFilter = false;
runFilterHelper();
} else {
m_filterTimer->start(FILTER_PAUSE, true);
}
}
/**
* XXX: Fix the situations which m_origDevice is NOT associated with a image.
* If it comes from a adjustment layer or projection or thumbnail. Currently, nothing happens
*/
void KisPreviewWidget::runFilterHelper() {
m_filterZoom = m_zoom;
// Dont scale more then 1.0 so we don't waste time in preview widget for large scaling.
if(m_filterZoom > 1.0) {
m_filterZoom = 1.0;
}
KisPaintDeviceSP scaledDevice;
KisHermiteFilterStrategy strategy;
// Copy the image and scale
if (m_origDevice->image())
{
m_scaledImage = new KisImage(*m_origDevice->image());
if(!m_origDevice->parentLayer()) return;
TQString layerName = m_origDevice->parentLayer()->name();
KisPaintLayerSP pl = ::tqqt_cast<KisPaintLayer*>(m_scaledImage->findLayer(layerName));
if(!pl) return;
scaledDevice = pl->paintDevice();
KisSelectionSP select;
if(scaledDevice->hasSelection())
{
select = new KisSelection(*scaledDevice->selection());
scaledDevice->deselect();
}
// Scale
m_scaledImage->setUndoAdapter(NULL);
m_scaledImage->scale(m_filterZoom, m_filterZoom, NULL, &strategy);
// Scale the selection
if(select)
{
KisPaintDeviceSP t = select.data();
KisTransformWorker tw(t, m_filterZoom, m_filterZoom,
0.0, 0.0, 0.0, 0, 0, NULL, &strategy);
tw.run();
scaledDevice->setSelection(select);
select->setParentLayer(scaledDevice->parentLayer());
}
// Crop by the zoom value instead of cropping by rectangle. It gives better results
MyCropVisitor v(m_filterZoom);
m_scaledImage->rootLayer()->accept(v);
} else
{
scaledDevice = new KisPaintDevice(*m_origDevice);
KisSelectionSP select;
if(scaledDevice->hasSelection())
{
select = new KisSelection(*scaledDevice->selection());
scaledDevice->deselect();
}
KisTransformWorker tw(scaledDevice, m_filterZoom, m_filterZoom,
0.0, 0.0, 0.0, 0, 0, NULL, &strategy);
tw.run();
// Scale the selection
if(select)
{
KisPaintDeviceSP t = select.data();
KisTransformWorker tw(t, m_filterZoom, m_filterZoom,
0.0, 0.0, 0.0, 0, 0, NULL, &strategy);
tw.run();
scaledDevice->setSelection(select);
::cropDevice(select.data(), m_filterZoom);
}
::cropDevice(scaledDevice.data(), m_filterZoom);
}
m_previewDevice = new KisPaintDevice(*scaledDevice);
// Setup the progress display
m_filter->enableProgress();
m_progress->setSubject(m_filter, true, true);
m_filter->setProgressDisplay(m_progress);
m_filter->process(scaledDevice, m_previewDevice, m_config, scaledDevice->exactBounds());
m_filter->disableProgress();
m_dirtyPreview = true;
if(m_firstZoom) {
m_firstZoom = false;
updateZoom();
} else {
m_zoomTimer->start(ZOOM_PAUSE, true);
}
}
#include "kis_previewwidget.moc"
|