summaryrefslogtreecommitdiffstats
path: root/kview/kviewcanvas/kimagecanvas.cpp
blob: 67d8fd9a437a2f056d826f3630222b632ed9f5bd (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*  This file is part of the KDE project
    Copyright (C) 2001-2002 Matthias Kretz <kretz@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    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 "kimagecanvas.h"
#include "kimageholder.h"
#include "version.h"
#include "config/defaults.h"

#include <tqcolor.h>
#include <tqimage.h>
#include <tqapplication.h>
#include <tqwmatrix.h>
#include <tqtimer.h>

#include <kpixmap.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <ksettings/dispatcher.h>
#include <kconfig.h>

#define KIMAGECANVAS_WIPESIZE 5

const int MOUSECURSORHIDETIME = 3000;

//extern bool qt_use_xrender;

typedef KGenericFactory<KImageCanvas> KImageCanvasFactory;
K_EXPORT_COMPONENT_FACTORY( libkviewcanvas,
		KImageCanvasFactory( "kviewcanvas" ) )

KImageCanvas::KImageCanvas( TQWidget * parent, const char * name, const TQStringList & )
	: TQScrollView( parent, name, WResizeNoErase | WStaticContents )
	, m_client( 0 )
	, m_oldClient( 0 )
	, m_image( 0 )
	, m_imageTransformed( 0 )
	, m_pixmap( 0 )
	, m_pTimer( new TQTimer( this, "KImageCanvas/Timer" ) )
	, m_maxsize( Defaults::maxSize )
	, m_minsize( Defaults::minSize )
	, m_currentsize( 0, 0 )
	, m_zoom( 1.0 )
	, m_fastscale( ! Defaults::smoothScaling )
	, m_keepaspectratio( Defaults::keepAspectRatio )
	, m_bImageChanged( false )
	, m_bSizeChanged( false )
	, m_bNeedNewPixmap( false )
	, m_bCentered( Defaults::centerImage )
	, m_bImageUpdateScheduled( false )
	, m_bNewImage( false )
	, m_iBlendTimerId( 0 )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	setFrameStyle( TQFrame::NoFrame );
	setResizePolicy( TQScrollView::Manual );
	setMinimumSize( 0, 0 );
	setBgColor( Defaults::bgColor );

	connect( this, TQT_SIGNAL( imageChanged() ), this, TQT_SLOT( slotImageChanged() ) );
	connect( m_pTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( hideCursor() ) );

	KSettings::Dispatcher::self()->registerInstance(
			KImageCanvasFactory::instance(), TQT_TQOBJECT(this),
			TQT_SLOT( loadSettings() ) );

	viewport()->setFocusProxy( this );
	clear();

	TQWidget::setMouseTracking( true );
	viewport()->setMouseTracking( true );
	m_cursor.setShape( Qt::CrossCursor );
	viewport()->setCursor( m_cursor );
	m_pTimer->start( MOUSECURSORHIDETIME, true );

	loadSettings();
}

KImageCanvas::~KImageCanvas()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	delete m_image; m_image = 0;
	delete m_pixmap; m_pixmap = 0;
}

void KImageCanvas::setBgColor( const TQColor & color )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	viewport()->setPaletteBackgroundColor( color );
	if( m_client )
		m_client->setPaletteBackgroundColor( color );
}

const TQColor & KImageCanvas::bgColor() const
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	return viewport()->paletteBackgroundColor();
}

int KImageCanvas::imageDepth() const
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_image )
		return 0;

	return m_image->depth();
}

TQSize KImageCanvas::imageSize() const
{
	//kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_image )
		return TQSize( 0, 0 );

	return m_matrix.isIdentity() ? m_image->size() : m_matrix.mapRect( TQRect( TQPoint(), m_image->size() ) ).size();
}

TQSize KImageCanvas::currentSize() const
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_image )
		return TQSize( 0, 0 );

	return m_currentsize;
}

const TQImage * KImageCanvas::image() const
{
	if( m_imageTransformed )
		return m_imageTransformed;
	return m_image;
}

TQRect KImageCanvas::selection() const
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_client )
		return m_selection;
	else
		return TQRect();
}

void KImageCanvas::setCentered( bool centered )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_bCentered != centered )
	{
		m_bCentered = centered;
		center();
	}
}

void KImageCanvas::setImage( const TQImage & newimage )
{
	bool emitHasImage = m_image ? false : true;
	m_matrix.reset();
	matrixChanged();
	delete m_image;
	m_image = new TQImage( newimage );
	m_bNewImage = true;
	// don't emit the signal here - call the slot directly
	slotImageChanged();

	sizeFromZoom( m_zoom );
	updateImage();
	if( emitHasImage && m_image )
		emit hasImage( true );
}

void KImageCanvas::setImage( const TQImage & newimage, const TQSize & size )
{
	kdDebug( 4620 ) << k_funcinfo << size << endl;
	bool emitHasImage = m_image ? false : true;
	m_matrix.reset();
	matrixChanged();
	delete m_image;
	m_image = new TQImage( newimage );
	m_bNewImage = true;
	// don't emit the signal here - call the slot directly
	slotImageChanged();

	resizeImage( size );
	updateImage();
	if( emitHasImage && m_image )
		emit hasImage( true );
}

void KImageCanvas::setZoom( double zoom )
{
	kdDebug( 4620 ) << k_funcinfo << zoom << endl;
	if( m_image == 0 )
		return;

	if( zoom > 0.0 && m_zoom != zoom )
	{
		m_zoom = zoom;
		sizeFromZoom( m_zoom );
		emit zoomChanged( m_zoom );
		updateImage();
	}
}

void KImageCanvas::boundImageTo( const TQSize & size )
{
	bool keepAspectRatio = m_keepaspectratio;
	m_keepaspectratio = true;
	resizeImage( size );
	m_keepaspectratio = keepAspectRatio;
}

void KImageCanvas::setMaximumImageSize( const TQSize & maxsize )
{
	kdDebug( 4620 ) << k_funcinfo << maxsize << endl;
	if( ( ! m_minsize.isEmpty() ) &&
			( maxsize.width() < m_minsize.width() || maxsize.height() < m_minsize.height() ) )
	{
		kdWarning( 4620 ) << "the new maximum image size is smaller than the minimum size" << endl;
		return;
	}

	m_maxsize = maxsize;

	resizeImage( m_currentsize );
}

void KImageCanvas::setMinimumImageSize( const TQSize & minsize )
{
	kdDebug( 4620 ) << k_funcinfo << minsize << endl;
	if( ( ! m_maxsize.isEmpty() ) &&
			( minsize.width() > m_maxsize.width() || minsize.height() > m_maxsize.height() ) )
	{
		kdWarning( 4620 ) << "the new minimum image size is greater than the maximum size" << endl;
		return;
	}

	m_minsize = minsize;

	resizeImage( m_currentsize );
}

void KImageCanvas::resizeImage( const TQSize & newsize )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_image == 0 )
		return;

	TQSize size = newsize;

	// check that it fits into min and max sizes
	checkBounds( size );

	// calculate the new zoom factor
	zoomFromSize( size );

	if( size != m_currentsize )
	{
		m_currentsize = size;
		sizeChanged();

		updateImage();
	}
}

void KImageCanvas::hideScrollbars( bool hidden )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( hidden )
	{
		setVScrollBarMode( AlwaysOff );
		setHScrollBarMode( AlwaysOff );
	}
	else
	{
		setVScrollBarMode( Auto );
		setHScrollBarMode( Auto );
	}
}

void KImageCanvas::setKeepAspectRatio( bool aspect )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_keepaspectratio = aspect;
}

unsigned int KImageCanvas::numOfBlendEffects() const
{
	return Defaults::numOfBlendEffects;
}

TQString KImageCanvas::blendEffectDescription( unsigned int idx ) const
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	switch( idx )
	{
		case NoBlending:
			kdWarning( 4620 ) << k_funcinfo << " shouldn't be called with an index of 0 - That's always not really defined\n";
			return i18n( Defaults::blendEffectDescription[ 0 ] );
		case AlphaBlend:
			return i18n( Defaults::blendEffectDescription[ 5 ] );
		case WipeFromLeft:
			return i18n( Defaults::blendEffectDescription[ 1 ] );
		case WipeFromRight:
			return i18n( Defaults::blendEffectDescription[ 2 ] );
		case WipeFromTop:
			return i18n( Defaults::blendEffectDescription[ 3 ] );
		case WipeFromBottom:
			return i18n( Defaults::blendEffectDescription[ 4 ] );
	}
	kdError( 4620 ) << "Effect description for effect with index " << idx << " doesn't exist\n";
	return TQString();
}

bool KImageCanvas::eventFilter( TQObject * obj, TQEvent * ev )
{
	if( ( TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_client) || TQT_BASE_OBJECT(obj) == TQT_BASE_OBJECT(m_oldClient) ) && ev->type() == TQEvent::MouseMove )
		mouseMoveEvent( TQT_TQMOUSEEVENT( ev ) );
	return TQScrollView::eventFilter( obj, ev );
}

void KImageCanvas::setFastScale( bool fastscale )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_fastscale = fastscale;
	if( m_fastscale )
	{
		// wo do scaling with a matrix now, so the m_imageTransformed isn't needed anymore
		delete m_imageTransformed;
		m_imageTransformed = 0;
	}
	else
	{
		matrixChanged(); // set the flag to dirty so that a new m_imageTransformed will be created
						 // else we very relyably get a crash
	}
	updateImage();
}

void KImageCanvas::clear()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	bool emitHasImage = m_image ? true : false;
	delete m_image;
	m_image = 0;
	m_currentsize -= m_currentsize; //zero size
	if( m_client )
		m_client->clear();
	if( emitHasImage && ! m_image )
		emit hasImage( false );
}

void KImageCanvas::flipHorizontal( bool change )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_image == 0 )
		return;

	if( change )
	{
		TQWMatrix matrix( 1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F );
		*m_image = m_image->xForm( matrix );
		emit imageChanged();
	}
	else
	{
		m_matrix.scale( 1.0, -1.0 );
		matrixChanged();
	}
	// size didn't change
	updateImage();
}

void KImageCanvas::flipVertical( bool change )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_image == 0 )
		return;

	if( change )
	{
		TQWMatrix matrix( -1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F );
		*m_image = m_image->xForm( matrix );
		emit imageChanged();
	}
	else
	{
		m_matrix.scale( -1.0, 1.0 );
		matrixChanged();
	}
	// size didn't change
	updateImage();
}

void KImageCanvas::rotate( double a, bool change )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_image == 0 )
		return;

	if( change )
	{
		TQWMatrix matrix;
		matrix.rotate( a );
		*m_image = m_image->xForm( matrix );
		emit imageChanged();
	}
	else
	{
		m_matrix.rotate( a );
		matrixChanged();
	}
	//adjust m_currentsize
	sizeFromZoom( m_zoom );
	updateImage();
}

void KImageCanvas::checkBounds( TQSize & newsize )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_keepaspectratio )
	{
		// check that the new size has the same aspect ratio the original image had
		TQSize origsize = imageSize();
		double x1 = double( origsize.height() ) / double( newsize.height() );
		double x2 = double( origsize.width() ) / double( newsize.width() );
		if( ( newsize * x1 != origsize ) || ( newsize * x2 != origsize ) )
		{
			// not OK
			kdDebug( 4620 ) << "checkBounds: the aspect ratio wasn't kept changing from " << newsize << endl;
			// the user want's that the aspect ratio doesn't change. The
			// question is: make it larger or smaller?
			// we make it smaller (we depend on that in boundImageTo)
			newsize = origsize / KMAX( x1, x2 );
			kdDebug( 4620 ) << "checkBounds: to " << newsize << endl;
		}
	}
	if( ( ! m_maxsize.isEmpty() ) &&
			( newsize.width() > m_maxsize.width() || newsize.height() > m_maxsize.height() ) )
	{
		kdDebug( 4620 ) << "checkBounds: the new size is bigger than the max size" << endl;
		if( m_keepaspectratio )
		{
			double x1 = double( m_maxsize.height() ) / double( newsize.height() );
			double x2 = double( m_maxsize.width() ) / double( newsize.width() );
			double x = KMIN( x1, x2 );//( x1 > x2 ) ? x2 : x1;
			newsize *= x;
		}
		else
			newsize = newsize.boundedTo( m_maxsize );
	}
	if( ( ! m_minsize.isEmpty() ) &&
			( newsize.width() < m_minsize.width() || newsize.height() < m_minsize.height() ) )
	{
		kdDebug( 4620 ) << "checkBounds: the new size is smaller than the min size" << endl;
		if( m_keepaspectratio )
		{
			double x1 = double( m_minsize.height() ) / double( newsize.height() );
			double x2 = double( m_minsize.width() ) / double( newsize.width() );
			double x = KMAX( x1, x2 );//( x1 > x2 ) ? x1 : x2;
			newsize *= x;
		}
		else
			newsize = newsize.expandedTo( m_minsize );
	}
	// if it still won't fit we have a problem: we can't keep the aspect ratio or we have
	// to violate the min/max settings
	if( ( ! m_maxsize.isEmpty() ) &&
			( newsize.width() > m_maxsize.width() || newsize.height() > m_maxsize.height() ) )
	{
		kdDebug( 4620 ) << "checkBounds: Sorry, I can't keep the aspect ratio." << endl;
		newsize = newsize.boundedTo( m_maxsize );
	}
}

void KImageCanvas::zoomFromSize( const TQSize & newsize )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_image )
		return;

	TQSize originalsize = imageSize();
	double widthzoom = double( newsize.width() ) / double( originalsize.width() );
	double heightzoom = double( newsize.height() ) / double( originalsize.height() );
	double zoom = ( widthzoom + heightzoom ) / 2;
	if( zoom != m_zoom )
	{
		m_zoom = zoom;
		emit zoomChanged( m_zoom );
	}
}

void KImageCanvas::sizeFromZoom( double zoom )
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_image )
		return;

	TQSize newsize = zoom * imageSize();
	kdDebug( 4620 ) << "change size from " << imageSize() << " to " << newsize << endl;
	resizeImage( newsize );
}

void KImageCanvas::updateImage()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( ! m_bImageUpdateScheduled )
		TQTimer::singleShot( 0, this, TQT_SLOT( slotUpdateImage() ) );
	m_bImageUpdateScheduled = true;
}

void KImageCanvas::slotUpdateImage()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_bImageUpdateScheduled = false;
	if( m_image == 0 )
		return;

	//only update if something was changed
	if( m_bImageChanged || m_bSizeChanged || m_bMatrixChanged )
	{
		kdDebug( 4620 ) << "actually updating the image now" << endl;
		TQApplication::setOverrideCursor( WaitCursor );
		if( m_bNewImage || ! m_client )
		{
			finishNewClient();
			m_oldClient = m_client;
			m_client = createNewClient();
		}
		m_client->setImage( pixmap() );

		if( m_bSizeChanged || m_bNewImage )
		{
			TQSize sh = m_client->sizeHint();
			if( ! sh.isValid() )
				sh = TQSize( 0, 0 );
			m_client->resize( sh );
			resizeContents( sh.width(), sh.height() );
			center();
		}
		TQRect drawRect = m_client->drawRect();
		switch( m_iBlendEffect )
		{
			case NoBlending:
				break;
			case AlphaBlend:
				break;
			case WipeFromLeft:
				drawRect.setRight( KIMAGECANVAS_WIPESIZE + contentsX() );
				m_client->setDrawRect( drawRect );
				break;
			case WipeFromRight:
				drawRect.rLeft() += KMIN( drawRect.width() - KIMAGECANVAS_WIPESIZE, contentsX() + visibleWidth() );
				m_client->setDrawRect( drawRect );
				break;
			case WipeFromTop:
				drawRect.setBottom( KIMAGECANVAS_WIPESIZE + contentsY() );
				m_client->setDrawRect( drawRect );
				break;
			case WipeFromBottom:
				drawRect.setTop( KMIN( drawRect.height() - KIMAGECANVAS_WIPESIZE, contentsY() + visibleHeight() ) );
				m_client->setDrawRect( drawRect );
				break;
		}
		m_client->update();
		m_iBlendTimerId = startTimer( 5 );
		TQApplication::restoreOverrideCursor();
	}

	m_bNewImage = false;
	m_bImageChanged = false;
	m_bSizeChanged = false;
	m_bMatrixChanged = false;
}

void KImageCanvas::mouseMoveEvent( TQMouseEvent * )
{
	if( m_cursor.tqshape() == TQt::BlankCursor )
	{
		m_cursor.setShape( Qt::CrossCursor );
		viewport()->setCursor( m_cursor );
		if( m_client )
			m_client->setCursor( m_cursor );
	}
	m_pTimer->start( MOUSECURSORHIDETIME, true );
}


void KImageCanvas::resizeEvent( TQResizeEvent * ev )
{
	kdDebug( 4620 ) << "KImageCanvas resized to " << ev->size() << endl;
	TQScrollView::resizeEvent( ev );
	center();
}

void KImageCanvas::contentsMousePressEvent( TQMouseEvent * ev )
{
	if ( ev->button() == Qt::RightButton )
		emit contextPress( ev->globalPos() );
	TQScrollView::contentsMousePressEvent( ev );
}

void KImageCanvas::contentsWheelEvent( TQWheelEvent * ev )
{
	//kdDebug( 4620 ) << k_funcinfo << endl;
    // Ctrl+Wheelmouse changes the zoom.
    // Wheelmouse scrolls around
    if ( ev->state() & ControlButton )
    {
		int delta = ev->delta() / 120;
		double zoom = m_zoom;
		// make zoom a value of 1/16, 1/15, 1/14, .. , 1/2, 1, 2, 3, .. , 15, 16
		bool done = false;
		for( int i = 15; i > 0; --i )
		{
			if( zoom <= ( 1.0 / i ) )
			{
				if( zoom < ( 1.0 / ( i + 0.5 ) ) )
					zoom = ( 1.0 / ( i + 1 ) );
				else
					zoom = ( 1.0 / i );
				done = true;
				// zoom = 1/16, 1/15, .. , 1/2, 1
				double x = 1.0 / zoom - delta;
				if( x == 0 )
					zoom = 2.0;
				else
					zoom = 1.0 / x;
				break;
			}
		}
		if( ! done )
			for( int i = 2; i < 17; ++i )
			{
				if( zoom < (double)i )
				{
					if( zoom < ( i - 0.5 ) )
						zoom = i - 1.0;
					else
						zoom = (double)i;
					done = true;
					// zoom = 1, 2, .., 15, 16
					zoom = zoom + delta;
					if( zoom < 0.9 )
						zoom = 0.5;
					break;
				}
			}
		if( ! done )
		{
			zoom = 16.0;
			zoom = zoom + delta;
			if( zoom > 16.0 )
				zoom = 16.0;
		}
		kdDebug( 4620 ) << "Mousewheel: oldzoom = " << m_zoom << " newzoom = " << zoom << endl;
		ev->accept();
		bool oldscale = fastScale();
		setFastScale( true );
		setZoom( zoom );
		setFastScale( oldscale );
    }
    else
        TQScrollView::contentsWheelEvent( ev );
}

void KImageCanvas::keyPressEvent( TQKeyEvent * ev )
{
	//kdDebug( 4620 ) << k_funcinfo << endl;
	switch( ev->key() )
	{
		case Key_Down:
			ev->accept();
			verticalScrollBar()->addLine();
			break;
		case Key_Up:
			ev->accept();
			verticalScrollBar()->subtractLine();
			break;
		case Key_Left:
			ev->accept();
			horizontalScrollBar()->subtractLine();
			break;
		case Key_Right:
			ev->accept();
			horizontalScrollBar()->addLine();
			break;
		case Key_PageUp:
			ev->accept();
			verticalScrollBar()->subtractPage();
			break;
		case Key_PageDown:
			ev->accept();
			verticalScrollBar()->addPage();
			break;
		default:
			ev->ignore();
			break;
	}
}

void KImageCanvas::timerEvent( TQTimerEvent * ev )
{
	if( ev->timerId() == m_iBlendTimerId )
	{
		TQRect drawRect = m_client->drawRect();
		switch( m_iBlendEffect )
		{
			case NoBlending:
				finishNewClient();
				break;
			case AlphaBlend:
				finishNewClient();
				//if( qt_use_xrender )
				//{
				//}
				//else
				//{
					//kdWarning( 4620 ) << "no XRender" << endl;
					//finishNewClient();
				//}
				break;
			case WipeFromLeft:
				drawRect.rRight() += KIMAGECANVAS_WIPESIZE;
				m_client->setDrawRect( drawRect );
				m_client->update( drawRect.right() - KIMAGECANVAS_WIPESIZE, 0, KIMAGECANVAS_WIPESIZE, m_client->height() );
				if( drawRect.right() >= contentsX() + visibleWidth() )
					finishNewClient();
				break;
			case WipeFromRight:
				drawRect.rLeft() -= KIMAGECANVAS_WIPESIZE;
				m_client->setDrawRect( drawRect );
				m_client->update( drawRect.left(), 0, KIMAGECANVAS_WIPESIZE, m_client->height() );
				if( drawRect.left() <= contentsX() )
					finishNewClient();
				break;
			case WipeFromTop:
				drawRect.rBottom() += KIMAGECANVAS_WIPESIZE;
				m_client->setDrawRect( drawRect );
				m_client->update( 0, drawRect.bottom() - KIMAGECANVAS_WIPESIZE, m_client->width(), KIMAGECANVAS_WIPESIZE );
				if( drawRect.bottom() >= contentsY() + visibleHeight() )
					finishNewClient();
				break;
			case WipeFromBottom:
				drawRect.rTop() -= KIMAGECANVAS_WIPESIZE;
				m_client->setDrawRect( drawRect );
				m_client->update( 0, drawRect.top(), m_client->width(), KIMAGECANVAS_WIPESIZE );
				if( drawRect.top() <= contentsY() )
					finishNewClient();
				break;
			default:
				kdFatal( 4620 ) << "unknown Blend Effect" << endl;
				break;
		}
	}
	else
		killTimer( ev->timerId() );
}

void KImageCanvas::hideCursor()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_cursor.setShape( Qt::BlankCursor );
	viewport()->setCursor( m_cursor );
	if( m_client )
		m_client->setCursor( m_cursor );
}

const KPixmap KImageCanvas::pixmap()
{
	kdDebug( 4620 ) << k_funcinfo << ( m_bNeedNewPixmap ? "convert from Image" : "use old copy" ) << endl;
	// create a new Pixmap in m_pixmap if needed
	if( m_bNeedNewPixmap )
	{
		// only do it again if requested
		m_bNeedNewPixmap = false;
		// ok, the old one may go now
		delete m_pixmap;

		// if smoothscaling is wanted and the transformation matrix or the image
		// itself changed...
		if( ! m_fastscale && ( m_bMatrixChanged || m_bImageChanged ) )
		{
			delete m_imageTransformed;
			// we create a new image transformed by the matrix
			m_imageTransformed = new TQImage( m_matrix.isIdentity() ? *m_image : m_image->xForm( m_matrix ) );
			kdDebug( 4620 ) << "Size of m_image: " << m_image->size() << endl;
			kdDebug( 4620 ) << "Size of m_imageTransformed: " << m_imageTransformed->size() << endl;
		}
		// smoothScale or fast scaling via m_matrix
		m_pixmap = new KPixmap();
		m_pixmap->convertFromImage( m_fastscale ? *m_image : m_imageTransformed->smoothScale( m_currentsize ), KPixmap::ColorOnly );
	}
	if( m_fastscale )
	{
		// fast scaling is needed so we need to scale now
		TQWMatrix matrix( m_matrix );
		matrix.scale( m_zoom, m_zoom );
		if( ! matrix.isIdentity() )
			return m_pixmap->xForm( matrix );
	}
	return *m_pixmap;
}

void KImageCanvas::slotImageChanged()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_bImageChanged = true;
	m_bNeedNewPixmap = true;
}

void KImageCanvas::loadSettings()
{
	KConfigGroup cfgGroup( KImageCanvasFactory::instance()->config(),
			"Settings" );
	setFastScale( ! cfgGroup.readBoolEntry( "Smooth Scaling", ! fastScale() ) );
	setKeepAspectRatio( cfgGroup.readBoolEntry( "Keep Aspect Ratio",
				keepAspectRatio() ) );
	setCentered( cfgGroup.readBoolEntry( "Center Image", centered() ) );

	setBgColor( cfgGroup.readColorEntry( "Background Color", &bgColor() ) );

	setMinimumImageSize( TQSize( cfgGroup.readNumEntry( "Minimum Width",
					minimumImageSize().width() ), cfgGroup.readNumEntry(
					"Minimum Height", minimumImageSize().height() ) ) );
	setMaximumImageSize( TQSize( cfgGroup.readNumEntry( "Maximum Width",
					maximumImageSize().width() ), cfgGroup.readNumEntry(
					"Maximum Height", maximumImageSize().height() ) ) );

	KConfigGroup blendConfig( KImageCanvasFactory::instance()->config(),
			"Blend Effects" );
	/* TODO
	m_vEffects.clear();
	for( unsigned int i = 1; i <= numOfBlendEffects(); ++i )
	{
		if( blendConfig.readBoolEntry( TQString::number( i ), false ) )
			m_vEffects.push_back( i );
	}
	// and now tell the canvas what blend effect to use
	switchBlendEffect();
	*/
}

void KImageCanvas::selected( const TQRect & rect )
{
	//kdDebug( 4620 ) << k_funcinfo << rect << endl;
	m_selection = rect;
	if( ! m_selection.isNull() )
	{
		m_selection.setTop( int( ( m_selection.top() + 0.5 ) / m_zoom ) );
		m_selection.setLeft( int( ( m_selection.left() + 0.5 ) / m_zoom ) );
		m_selection.setRight( int( ( m_selection.right() + 0.5 ) / m_zoom ) );
		m_selection.setBottom( int( ( m_selection.bottom() + 0.5 ) / m_zoom ) );
	}
	//kdDebug( 4620 ) << "m_selection = " << m_selection << endl;
	emit selectionChanged( m_selection );
}

void KImageCanvas::mapCursorPos( const TQPoint & pos )
{
	TQPoint mapped( static_cast<int>( ( pos.x() + 0.5 ) / m_zoom ), static_cast<int>( ( pos.y() + 0.5 ) / m_zoom ) );
	//kdDebug( 4620 ) << k_funcinfo << pos << " -> " << mapped << endl;
	emit cursorPos( mapped );
}

void KImageCanvas::sizeChanged()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_bSizeChanged = true;
	if( ! m_fastscale )
		m_bNeedNewPixmap = true;
	emit imageSizeChanged( m_currentsize );
}

void KImageCanvas::matrixChanged()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	m_bMatrixChanged = true;
	m_bNeedNewPixmap = true;
}

void KImageCanvas::center()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	if( m_bCentered && m_client )
	{
		int x = 0;
		int y = 0;

		int scrollbarwidth = ( height() >= m_currentsize.height() ) ? 0 : verticalScrollBar()->width();
		int scrollbarheight = ( width() - scrollbarwidth >= m_currentsize.width() ) ? 0 : horizontalScrollBar()->height();
		scrollbarwidth = ( height() - scrollbarheight >= m_currentsize.height() ) ? 0 : verticalScrollBar()->width();

		int availheight = height() - scrollbarheight;
		int availwidth = width() - scrollbarwidth;

		if( availwidth > m_currentsize.width() )
			x = ( availwidth - m_currentsize.width() ) / 2;
		if( availheight > m_currentsize.height() )
			y = ( availheight - m_currentsize.height() ) / 2;

		kdDebug( 4620 ) << "center with left top at " << x << ", " << y << endl;
		moveChild( m_client, x, y );
	}
}

void KImageCanvas::finishNewClient()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	killTimer( m_iBlendTimerId );
	if( m_client )
		m_client->setDrawRect( m_client->rect() );
	delete m_oldClient;
	m_oldClient = 0;
	emit showingImageDone();
}

KImageHolder * KImageCanvas::createNewClient()
{
	kdDebug( 4620 ) << k_funcinfo << endl;
	KImageHolder * client = new KImageHolder( viewport() );
	client->setMinimumSize( 0, 0 );
	client->setMouseTracking( true );
	client->installEventFilter( this );
	setFocusProxy( client );
	client->setFocusPolicy( TQ_StrongFocus );
	client->setFocus();

	addChild( client, 0, 0 );

	connect( client, TQT_SIGNAL( contextPress( const TQPoint& ) ), TQT_SIGNAL( contextPress( const TQPoint& ) ) );
	connect( client, TQT_SIGNAL( cursorPos( const TQPoint & ) ), TQT_SLOT( mapCursorPos( const TQPoint & ) ) );
	connect( client, TQT_SIGNAL( selected( const TQRect & ) ), TQT_SLOT( selected( const TQRect & ) ) );
	connect( client, TQT_SIGNAL( wannaScroll( int, int ) ), TQT_SLOT( scrollBy( int, int ) ) );

	return client;
}

#include "kimagecanvas.moc"

// vim:sw=4:ts=4