summaryrefslogtreecommitdiffstats
path: root/kview/kview.cpp
blob: 44d792dfbdedb972ca6625dbe0a4c79b3b5f464a (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
/*  This file is part of the KDE project
    Copyright (C) 2001-2003 Matthias Kretz <[email protected]>

    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 "kview.h"
#include "kimageviewer/viewer.h"
#include "kimageviewer/canvas.h"

#include <ksettings/dialog.h>
#include <ksettings/dispatcher.h>
#include <kurl.h>
#include <klibloader.h>
#include <kmessagebox.h>
#include <kparts/componentfactory.h>
#include <kparts/plugin.h>
#include <kparts/part.h>
#include <kapplication.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kimageio.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kwinmodule.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <kdebug.h>
#include <kedittoolbar.h>
#include <kstdaccel.h>
#include <kprogress.h>

#include <tqimage.h>
#include <tqsize.h>
#include <tqscrollbar.h>
#include <tqstyle.h>
#include <tqclipboard.h>
#include <tqdragobject.h>
#include <tqvaluelist.h>
#include <tqtimer.h>
#include <tqdockarea.h>

#include <assert.h>
#include <kplugininfo.h>

KView::KView()
    : KParts::MainWindow( 0, "KView" )
	, m_pViewer( 0 )
	, m_pCanvas( 0 )
	, m_pWinModule( new KWinModule( this, KWinModule::INFO_DESKTOP ) )
	, m_bImageSizeChangedBlocked( false )
	, m_bFullscreen( false )
{
	KParts::ReadWritePart * part = KParts::ComponentFactory::createPartInstanceFromLibrary<KParts::ReadWritePart>(
			"libkviewviewer", this, "KViewViewer Widget", this, "KImageViewer Part" );
	if( part )
	{
		m_pViewer = static_cast<KImageViewer::Viewer *>( part );
		if( m_pViewer )
			m_pCanvas = m_pViewer->canvas();
	}

	kdDebug( 4600 ) << "m_pViewer = " << m_pViewer << endl;

	if( m_pCanvas )
	{
		setupActions( part );

		setCentralWidget( part->widget() );

		setStandardToolBarMenuEnabled( true );

		connect( part->widget(), TQT_SIGNAL( imageSizeChanged( const TQSize & ) ),
				TQT_SLOT( imageSizeChanged( const TQSize & ) ) );
		connect( part->widget(), TQT_SIGNAL( selectionChanged( const TQRect & ) ),
				TQT_SLOT( selectionChanged( const TQRect & ) ) );
		connect( part->widget(), TQT_SIGNAL( contextPress( const TQPoint & ) ),
				TQT_SLOT( contextPress( const TQPoint & ) ) );

		connect( TQApplication::clipboard(), TQT_SIGNAL( dataChanged() ),
				TQT_SLOT( clipboardDataChanged() ) );

		connect( m_pViewer, TQT_SIGNAL( started( KIO::Job * ) ),
				this, TQT_SLOT( jobStarted( KIO::Job * ) ) );
		connect( m_pViewer, TQT_SIGNAL( completed() ),
				this, TQT_SLOT( jobCompleted() ) );
		connect( m_pViewer, TQT_SIGNAL( completed( bool ) ),
				this, TQT_SLOT( jobCompleted( bool ) ) );
		connect( m_pViewer, TQT_SIGNAL( canceled( const TQString & ) ),
				this, TQT_SLOT( jobCanceled( const TQString & ) ) );
		connect( m_pViewer, TQT_SIGNAL( imageOpened( const KURL & ) ),
				m_paRecent, TQT_SLOT( addURL( const KURL & ) ) );

		connect( m_pCanvas->widget(), TQT_SIGNAL( cursorPos( const TQPoint & ) ), TQT_SLOT( cursorPos( const TQPoint & ) ) );

		m_paRecent->loadEntries( KGlobal::config() );
		if (!initialGeometrySet())
			resize(500, 350);
		readSettings();
		m_pViewer->widget()->installEventFilter( this );

		// reload configuration when it's changed by the conf dlg
		KSettings::Dispatcher::self()->registerInstance( instance(), this, TQT_SLOT( readSettings() ) );

		setPluginLoadingMode( LoadPluginsIfEnabled );
		createGUI( part );

		// create status bar (hidden by default)
		statusBar()->insertItem( "", STATUSBAR_SPEED_ID, 0, true );
		statusBar()->setItemFixed( STATUSBAR_SPEED_ID,
				8 + fontMetrics().width( i18n( "%1/s" ).arg( KIO::convertSize( 999000 ) ) ) );
		statusBar()->insertItem( "", STATUSBAR_CURSOR_ID, 0, true );
		statusBar()->setItemFixed( STATUSBAR_CURSOR_ID, 8 + fontMetrics().width( "8888, 8888" ) );
		statusBar()->insertItem( "", STATUSBAR_SIZE_ID, 0, true );
		statusBar()->setItemFixed( STATUSBAR_SIZE_ID, 8 + fontMetrics().width( "8888 x 8888" ) );

		statusBar()->insertItem( TQString::null, STATUSBAR_SELECTION_ID );

		m_pProgressBar = new KProgress( statusBar() );
		m_pProgressBar->setFixedSize( 140, fontMetrics().height() );
		statusBar()->addWidget( m_pProgressBar, 0, true );
		m_pProgressBar->hide();

		setAutoSaveSettings();
		m_paShowMenubar->setChecked( ! menuBar()->isHidden() );

		// show progress info dialog if the statusbar is hidden
		m_pViewer->setProgressInfoEnabled( statusBar()->isHidden() );

		// set small minimum size
		setMinimumSize( 0, 0 );
	}
	else
	{
		KMessageBox::error( this, i18n( "An error occurred while loading the KViewViewer KPart. Check your installation." ) );
		TQTimer::singleShot( 0, kapp, TQT_SLOT( quit() ) );
	}
}

KView::~KView()
{
	saveSettings( KGlobal::config() );
	KGlobal::config()->sync();
}

void KView::load( const KURL & url )
{
	if( m_pViewer )
	{
		m_pViewer->openURL( url );
		if( url.isLocalFile() )
		{
			// XXX: this code is what
			//KRecentDirs::add( TQString::fromLatin1( ":load_image" ), url.directory() );
			// would do:
			TQString directory = url.directory();
			TQString key = TQString::fromLatin1( "load_image" );
			KConfig * config = KGlobal::config();

			config->setGroup( TQString::fromLatin1( "Recent Dirs" ) );
			TQStringList result = config->readPathListEntry( key );
			// make sure the dir is first in history
			result.remove( directory );
			result.prepend( directory );
			while( result.count() > 3 )
				result.remove( result.fromLast() );
			config->writePathEntry( key, result );
			config->sync();
		}
	}
}

void KView::loadFromStdin()
{
	if( m_pViewer )
	{
		TQFile file;
		file.open( IO_ReadOnly, stdin );
		TQImage image( file.readAll() );
		file.close();
		m_pViewer->newImage( image );
	}
}

TQSize KView::sizeForCentralWidgetSize( TQSize size )
{
	// add size of the dockareas
	kdDebug( 4600 ) << "sizeForCentralWidgetSize " << size << endl;
	size.rheight() += topDock()->height() + bottomDock()->height();
	size.rwidth()  += leftDock()->width() + rightDock()->width() - 2;
	kdDebug( 4600 ) << "added Dockareas:         " << size << endl;
	KStatusBar * sb = statusBar();
	size.rheight() += sb->isHidden() ? 0 : sb->height();
	kdDebug( 4600 ) << "added Statusbar:         " << size << endl;
	KMenuBar * mb = menuBar();
	if( ! mb->isHidden() )
	{
		size.rheight() += mb->heightForWidth( width() );
		if( style().styleHint( TQStyle::SH_MainWindow_SpaceBelowMenuBar, this ) )
			size.rheight() += dockWindowsMovable() ? 1 : 2;
	}
	kdDebug( 4600 ) << "added Menubar:           " << size << endl;
	return size;
}

bool KView::queryClose()
{
	return m_pViewer->closeURL();
}

void KView::saveProperties( KConfig * /*config*/ )
{
	// save session data:
	// What URL is currently open
	// somehow the plugins have to get a chance to store their data
}

void KView::readProperties( KConfig * /*config*/ )
{
	// read session data
}

void KView::saveSettings( KConfig * config )
{
	// write settings to config/kviewrc
	kdDebug( 4600 ) << k_funcinfo << endl;
	m_paRecent->saveEntries( config );
}

void KView::readSettings() // KConfig * config )
{
	// read settings from config/kviewrc
	kdDebug( 4600 ) << k_funcinfo << endl;
	KConfigGroup cfgGroup( KGlobal::config(), "KView General" );
	m_nResizeMode = cfgGroup.readNumEntry( "Resize Mode", 2 );
	kdDebug( 4600 ) << "m_nResizeMode = " << m_nResizeMode << endl;
	loadPlugins();
}

bool KView::eventFilter( TQObject * obj, TQEvent * ev )
{
	if( obj == m_pViewer->widget() && ev->type() == TQEvent::Resize )
	{
		if( m_nResizeMode == ResizeImage )
			handleResize();
	}
	return KParts::MainWindow::eventFilter( obj, ev );
}

void KView::imageSizeChanged( const TQSize & /*size*/ )
{
	TQSize size = m_pCanvas->imageSize();
	statusBar()->changeItem( TQString( "%1 x %2" ).arg( size.width() ).arg( size.height() ), STATUSBAR_SIZE_ID );
	handleResize();
}

void KView::selectionChanged( const TQRect & rect )
{
	kdDebug( 4600 ) << k_funcinfo << rect << endl;
	if( rect.isNull() )
		statusBar()->changeItem( TQString::null, STATUSBAR_SELECTION_ID );
	else
		statusBar()->changeItem( TQString( "%1, %2 - %3 x %4" ).arg( rect.x() ).arg( rect.y() ).arg( rect.width() ).arg( rect.height() ), STATUSBAR_SELECTION_ID );
	action( "crop" )->setEnabled( ! rect.isNull() );
}

void KView::contextPress( const TQPoint & point )
{
	TQPopupMenu * pop = ( TQPopupMenu* )factory()->container( "popupmenu", this );
	pop->popup( point );
}

void KView::slotOpenFile()
{
	KURL url = KFileDialog::getImageOpenURL( ":load_image", this );
	load( url );
}

void KView::slotOpenRecent( const KURL & url )
{
	load( url );
}

void KView::slotClose()
{
	if( m_pViewer->closeURL() )
		m_pCanvas->clear();
}

void KView::slotCopy()
{
	QClipboard *cb = TQApplication::clipboard();
	cb->setSelectionMode( false );

	TQRect selectarea = m_pCanvas->selection();
	if( selectarea.isEmpty() )
	{
		kdDebug( 4600 ) << k_funcinfo << " copy whole image" << endl;
		cb->setImage( *m_pCanvas->image() );
	}
	else
	{
		kdDebug( 4600 ) << k_funcinfo << " copy selected area of image" << endl;
		cb->setImage( m_pCanvas->image()->copy( selectarea ) );
	}
}

void KView::slotPaste()
{
	// Get TQImage from clipboard and create a new image.
	QClipboard *cb = TQApplication::clipboard();
    TQImage img = cb->image();
	if( ! img.isNull() )
		m_pViewer->newImage( img );
}

void KView::slotCrop()
{
	TQRect selectarea = m_pCanvas->selection();
	kdDebug( 4600 ) << "Crop following area: " << selectarea.x() << ", " << selectarea.y() << ", " << selectarea.width() << ", " << selectarea.height() << endl;

	if( selectarea.isNull() )
		return;
	const TQImage * origimg = m_pCanvas->image();
	if( origimg == 0 )
		return;

	m_pCanvas->setImage( origimg->copy( selectarea ) );
	m_pViewer->setModified( true );
}

void KView::slotUpdateFullScreen( bool set )
{
	m_bFullscreen = set;
	if( set )
	{ // switch to FullScreen mode
		saveMainWindowSettings( KGlobal::config(), "nonFullScreen MainWindow" );
		showFullScreen();
		applyMainWindowSettings( KGlobal::config(), "FullScreen MainWindow" );
		m_paShowMenubar->setChecked( ! menuBar()->isHidden() );
	}
	else
	{ // leave FullScreen mode
		saveMainWindowSettings( KGlobal::config(), "FullScreen MainWindow" );
		showNormal();
		applyMainWindowSettings( KGlobal::config(), "nonFullScreen MainWindow" );
		m_paShowMenubar->setChecked( ! menuBar()->isHidden() );
		handleResize();
	}
}

void KView::slotToggleMenubar()
{
	if( menuBar()->isVisible() )
		menuBar()->hide();
	else
		menuBar()->show();
	handleResize();
}

void KView::slotPreferences()
{
	// construct KCD for KView app
	static KSettings::Dialog * dlg = 0;
	if( ! dlg )
	{
		dlg = new KSettings::Dialog( this );
		//dlg = new KConfigureDialog( KConfigureDialog::Configurable, this );
		//dlg->addPluginInfos( KPluginInfo::fromKPartsInstanceName( instance()->instanceName(), KGlobal::config(), "KParts Plugins" ) );
	}
	dlg->show();
}

void KView::slotConfigureToolbars()
{
	saveMainWindowSettings( KGlobal::config(), "MainWindow" );
	KEditToolbar dlg( factory() );
	connect( &dlg, TQT_SIGNAL( newToolbarConfig() ), TQT_SLOT( slotNewToolbarConfig() ) );
	dlg.exec();
}

void KView::slotNewToolbarConfig()
{
	applyMainWindowSettings( KGlobal::config(), "MainWindow" );
}

void KView::reloadConfig()
{
	readSettings(); //KGlobal::config() );
}

void KView::enableAction( const char * name, bool b )
{
	KAction * a = actionCollection()->action( name );
	if( a )
		a->setEnabled( b );
	else
		kdWarning( 4600 ) << k_funcinfo << " unknown action" << endl;
}

void KView::clipboardDataChanged()
{
	QClipboard * cb = TQApplication::clipboard();
	cb->setSelectionMode( false );
	bool hasImage = TQImageDrag::canDecode( cb->data( QClipboard::Clipboard ) );
	m_paPaste->setEnabled( hasImage );
}

void KView::jobStarted( KIO::Job * job )
{
	if( job )
	{
		connect( job, TQT_SIGNAL( percent( KIO::Job *, unsigned long ) ), this, TQT_SLOT( loadingProgress( KIO::Job *, unsigned long ) ) );
		connect( job, TQT_SIGNAL( speed(   KIO::Job *, unsigned long ) ), this, TQT_SLOT( speedProgress(   KIO::Job *, unsigned long ) ) );
		//connect( job, TQT_SIGNAL( infoMessage( KIO::Job *, const TQString & ) ), this, TQT_SLOT() );
		loadingProgress( job, 0 );
		speedProgress( job, 0 );
	}
}

void KView::jobCompleted()
{
	jobCompleted( false );
}

void KView::jobCompleted( bool /*hasPending*/ )
{
	loadingProgress( 0, 101 );
	statusBar()->changeItem( "", STATUSBAR_SPEED_ID );
}

void KView::jobCanceled( const TQString & errorMsg )
{
	statusBar()->message( errorMsg );
	jobCompleted();
}

void KView::loadingProgress( KIO::Job *, unsigned long percent )
{
	if( percent > 100 )
	{
		m_pProgressBar->hide();
		return;
	}

	if( ! m_pProgressBar->isVisible() )
		m_pProgressBar->show();

	m_pProgressBar->setValue( percent );
}

void KView::speedProgress( KIO::Job *, unsigned long bytesPerSecond )
{
	TQString sizeStr;

	if( bytesPerSecond > 0 )
		sizeStr = i18n( "%1/s" ).arg( KIO::convertSize( bytesPerSecond ) );
	else
		sizeStr = i18n( "Stalled" );

	statusBar()->changeItem( sizeStr, STATUSBAR_SPEED_ID );
}

void KView::slotSetStatusBarText( const TQString & msg )
{
	kdDebug( 4600 ) << k_funcinfo << endl;
	statusBar()->message( msg );
	if( statusBar()->isHidden() )
		KMessageBox::information( this, msg );
}

void KView::cursorPos( const TQPoint & pos )
{
	statusBar()->changeItem( TQString( "%1, %2" ).arg( pos.x() ).arg( pos.y() ), STATUSBAR_CURSOR_ID );
}

void KView::setupActions( TQObject * partobject )
{
	// File
	KStdAction::open( this, TQT_SLOT( slotOpenFile() ), actionCollection() );
	m_paRecent = KStdAction::openRecent( this, TQT_SLOT( slotOpenRecent( const KURL & ) ), actionCollection() );
	KAction * aClose = KStdAction::close( this, TQT_SLOT( slotClose() ), actionCollection() );
	aClose->setEnabled( false );
	connect( m_pViewer->widget(), TQT_SIGNAL( hasImage( bool ) ), aClose, TQT_SLOT( setEnabled( bool ) ) );

	TQObject * extension = partobject->child( 0, "KParts::BrowserExtension", false );
	if( extension )
	{
		TQStrList slotNames = extension->metaObject()->slotNames();
		if( slotNames.contains( "print()" ) )
			KStdAction::print( extension, TQT_SLOT( print() ), actionCollection(), "print" );
		if( slotNames.contains( "del()" ) )
			( void )new KAction( i18n( "&Delete" ), "editdelete", SHIFT+Key_Delete,
								 extension, TQT_SLOT( del() ), actionCollection(), "del" );
		connect( extension, TQT_SIGNAL( enableAction( const char *, bool ) ), TQT_SLOT( enableAction( const char *, bool ) ) );
	}
	KStdAction::quit( this, TQT_SLOT( close() ), actionCollection() );

	// Edit
	KAction * aCopy = KStdAction::copy( this, TQT_SLOT( slotCopy() ), actionCollection() );
	aCopy->setEnabled( false );
	connect( m_pViewer->widget(), TQT_SIGNAL( hasImage( bool ) ), aCopy, TQT_SLOT( setEnabled( bool ) ) );
	m_paPaste = KStdAction::paste( this, TQT_SLOT( slotPaste() ), actionCollection() );
	clipboardDataChanged(); //enable or disable paste
	KAction * aCrop = new KAction( i18n( "Cr&op" ), Key_C, this, TQT_SLOT( slotCrop() ), actionCollection(), "crop" );
	aCrop->setEnabled( false );

	KAction * aReload = new KAction( i18n( "&Reload" ), "reload", KStdAccel::shortcut( KStdAccel::Reload ), partobject,
			TQT_SLOT( reload() ), actionCollection(), "reload" );
	aReload->setEnabled( false );
	connect( m_pViewer->widget(), TQT_SIGNAL( hasImage( bool ) ), aReload, TQT_SLOT( setEnabled( bool ) ) );

	// Settings
	m_paShowMenubar = KStdAction::showMenubar( this, TQT_SLOT( slotToggleMenubar() ), actionCollection() );
	createStandardStatusBarAction();
	m_paShowStatusBar = ::qt_cast<KToggleAction*>( action( "options_show_statusbar" ) );
	if( m_paShowStatusBar )
		connect( m_paShowStatusBar, TQT_SIGNAL( toggled( bool ) ), TQT_SLOT( statusbarToggled( bool ) ) );
	m_paShowFullScreen = KStdAction::fullScreen( 0, 0, actionCollection(), this );
	connect( m_paShowFullScreen, TQT_SIGNAL( toggled( bool )), this, TQT_SLOT( slotUpdateFullScreen( bool )));
	KStdAction::preferences( this, TQT_SLOT( slotPreferences() ), actionCollection() );
	KStdAction::keyBindings(guiFactory(), TQT_SLOT(configureShortcuts()), 
actionCollection());
	KStdAction::configureToolbars( this, TQT_SLOT( slotConfigureToolbars() ), actionCollection() );
}

void KView::handleResize()
{
	if( m_bImageSizeChangedBlocked )
		return;
	m_bImageSizeChangedBlocked = true;
	setUpdatesEnabled( false );
	switch( m_nResizeMode )
	{
		case ResizeWindow:
			fitWindowToImage();
			fitWindowToImage();
			break;
		case ResizeImage:
			m_pCanvas->boundImageTo( m_pViewer->widget()->size() );
			break;
		case BestFit:
			TQSize imageSize = m_pCanvas->imageSize();
			if( imageSize.isEmpty() )
				return;

			// Compare the image size and the maximum available space in the
			// display canvas i.e. will the image fit without resizing ?
			TQSize maxCanvas = maxCanvasSize();
			if( ( maxCanvas.height() >= imageSize.height() )
					&& ( maxCanvas.width() >= imageSize.width() ) )
			{
				//Image can be displayed at full size
				m_pCanvas->setZoom( 1.0 );
			}
			else
			{
				// Image is too big for the available canvas.
				m_pCanvas->boundImageTo( maxCanvas );
			}
			// We assume the displayed image size has changed and we must resize
			// the window around it (using the code for ResizeWindow).
			fitWindowToImage();
			fitWindowToImage();
			break;
	}
	setUpdatesEnabled( true );
	m_bImageSizeChangedBlocked = false;
}

void KView::fitWindowToImage()
{
	if( m_bFullscreen ) // don't do anything in fullscreen mode
		return;

	bool centeredOrig = m_pCanvas->centered();
	m_pCanvas->setCentered( false );

	TQSize imagesize = m_pCanvas->currentSize();
	if( imagesize.isEmpty() )
		return;

	TQSize winsize = sizeForCentralWidgetSize( imagesize );
	TQRect workarea = m_pWinModule->workArea();

	TQScrollBar * sb = new TQScrollBar( Qt::Horizontal, this );
	int scrollbarwidth = sb->height();
	delete sb;

	if( winsize.width() > workarea.width() )
	{
		winsize.setWidth( workarea.width() );
		winsize.rheight() += scrollbarwidth;
		if( winsize.height() > workarea.height() )
			winsize.setHeight( workarea.height() );
	}
	else if( winsize.height() > workarea.height() )
	{
		winsize.setHeight( workarea.height() );
		winsize.rwidth() += scrollbarwidth;
		if( winsize.width() > workarea.width() )
			winsize.setWidth( workarea.width() );
	}

	TQRect winrect( geometry() );
	winrect.setSize( winsize );

	int xdiff = winrect.x() + winrect.width()  - workarea.x() - workarea.width();
	int ydiff = winrect.y() + winrect.height() - workarea.y() - workarea.height();

	if( xdiff > 0 )
	{
		winrect.rLeft() -= xdiff;
		winrect.rRight() -= xdiff;
	}
	if( ydiff > 0 )
	{
		winrect.rTop() -= ydiff;
		winrect.rBottom() -= ydiff;
	}

	setGeometry( winrect );

	m_pCanvas->setCentered( centeredOrig );
}

TQSize KView::barSize( int mainwinwidth, BarSizeFrom from )
{
	int height = 0;
	int width = 0;
	if( toolBar()->isVisibleTo( this ) )
	{
		switch( toolBar()->barPos() )
		{
			case KToolBar::Top:
			case KToolBar::Bottom:
				height += toolBar()->height();
				break;
			case KToolBar::Left:
			case KToolBar::Right:
				width += toolBar()->width();
				break;
			case KToolBar::Flat:
				height += kapp->style().pixelMetric( TQStyle::PM_DockWindowHandleExtent );
				break;
			case KToolBar::Floating:
				break;
			case KToolBar::Unmanaged:
				break;
		}
	}
	if( menuBar()->isVisibleTo( this ) && ( ! menuBar()->isTopLevelMenu() ) )
		height += menuBar()->heightForWidth( mainwinwidth + ( ( from == FromImageSize ) ? width : 0 ) );
	if( statusBar()->isVisibleTo( this ) )
		height += statusBar()->height();

	return TQSize( width, height );
}

TQSize KView::maxCanvasSize()
{
	TQSize workarea = m_pWinModule->workArea().size();
	TQSize framesize = frameSize() - size();
	TQSize maxcanvassize = workarea - framesize - barSize( workarea.width() - framesize.width(), FromWidgetSize );
	kdDebug( 4600 ) << "maxcanvassize = " << maxcanvassize.width() << "x" << maxcanvassize.height() << endl;
	return maxcanvassize;
}


void KView::loadPlugins()
{
	createGUI( 0 );
	createShellGUI( false );
	createGUI( m_pViewer );
}

void KView::statusbarToggled( bool sbvisible )
{
	kdDebug( 4600 ) << k_funcinfo << sbvisible << endl;
	m_pViewer->setProgressInfoEnabled( !sbvisible );
}

// vim:sw=4:ts=4

#include "kview.moc"