summaryrefslogtreecommitdiffstats
path: root/kghostview/kgv_miniwidget.cpp
blob: 4a4dfc9305d8894d661171de249e969942eeb441 (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
/** 
 * Copyright (C) 1997-2003 the KGhostView authors. See file AUTHORS.
 * 	
 * 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 <math.h>

#include <tqlistbox.h>

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kinstance.h>
#include <tdeversion.h>
#include <tdelocale.h>
#include <tdemessagebox.h>

// KLineEditDlg is depricated as of 3.2. use KInputDialog instead
#if TDE_VERSION >= TDE_MAKE_VERSION(3,1,90)
  #include <kinputdialog.h>
#else
  #include <tqvalidator.h>
  #include <tqwidget.h>
  #include <klineeditdlg.h>
#endif

#include <config.h>
#include <stdlib.h>

#include "infodialog.h"
#include "marklist.h"
#include "kgvdocument.h"
#include "kgv_view.h"
#include "version.h"
#include "scrollbox.h"

#include "kgv_miniwidget.h"

#include "kpswidget.h"

using namespace KGV;

KGVMiniWidget::KGVMiniWidget( KGVPart* part, const char* name ) : 
    TQObject( part, name ),
    _document( 0 ),
    _part( part ),
    _psWidget( 0 ),
    _usePageLabels( true ),
    _visiblePage( -1 )
{
    TDELocale locale( "kghostview" );
    _fallBackPageMedia = pageSizeToString( 
              static_cast< TQPrinter::PageSize >( locale.pageSize() ) );
    _thumbnailService = new ThumbnailService( this );

    connect( this, TQT_SIGNAL( newPageShown( int ) ), 
             TQT_SLOT( updateStatusBarText( int ) ) );
}

void KGVMiniWidget::setDocument( KGVDocument* document )
{
    _document = document;
    if( _document )
	connect( _document, TQT_SIGNAL( completed() ), 
	         TQT_SLOT( slotDocumentOpened() ) );
}

TQString KGVMiniWidget::pageSizeToString( TQPrinter::PageSize pageSize )
{
    switch( pageSize )
    {
    case TQPrinter::A3:     return "A3";
    case TQPrinter::A4:     return "A4";
    case TQPrinter::A5:     return "A5";
    case TQPrinter::B4:     return "B4";
    case TQPrinter::Ledger: return "Ledger";
    case TQPrinter::Legal:  return "Legal";
    case TQPrinter::Letter: return "Letter";
    default:               return "Unknown";
    }
}

void KGVMiniWidget::reset()
{
    /*
    if( _psWidget )
	_psWidget->disableInterpreter();
    */

    // return to document defaults
    _options.reset();
    emit setStatusBarText( "" );
}

void KGVMiniWidget::setPSWidget( KPSWidget* psWidget )
{
    _psWidget = psWidget;
    // setMagnification( _magnification );
    connect( _psWidget, TQT_SIGNAL( newPageImage( TQPixmap ) ), 
             this, TQT_SLOT( sendPage() ) );
}

void KGVMiniWidget::goToPage()
{
#if TDE_VERSION >= TDE_MAKE_VERSION(3,1,90)
	int num;
	bool ok = false;
	num = KInputDialog::getInteger(i18n("Go to Page"), i18n("Page:"), 1,
		1, dsc()->page_count(), 1, 10, &ok, _part->widget());
	if (ok) goToPage( num-1 );
#else
    TQString num;
    bool b = false;
    num = KLineEditDlg::getText(i18n("Go to Page"), i18n("Page:"), TQString(), &b, _part->widget(), new TQIntValidator(1, dsc()->page_count(), this));
    if (b) goToPage( num.toInt() - 1 );
#endif
}

void KGVMiniWidget::info()
{
    if( !document()->isOpen() ) 
	return;

    InfoDialog* infoDialog = new InfoDialog( _part->widget(), "info", true );
    infoDialog->setup( _part->url().prettyURL(),
                        dsc()->dsc_title(), dsc()->dsc_date() );
    infoDialog->exec();
    delete infoDialog;
}

void KGVMiniWidget::goToPage( int page )
{
    if( _options.page() != page ) {
	_options.setPage( page );
	showPage( _options.page() );
    }
}

void KGVMiniWidget::zoomIn()
{
    if ( _options.zoomIn() ) showPage( _options.page() );
}

void KGVMiniWidget::zoomOut()
{
    if ( _options.zoomOut() ) showPage( _options.page() );
}

bool KGVMiniWidget::atMaxZoom() const
{
    return !_options.canZoomIn();
}

bool KGVMiniWidget::atMinZoom() const
{
    return !_options.canZoomOut();
}

void KGVMiniWidget::fitWidth( unsigned int width )
{
    if ( (orientation() == CDSC_LANDSCAPE) || (orientation() == CDSC_SEASCAPE) )
        setMagnification( ( (double)width / TQPaintDevice::x11AppDpiY()) /
                      ( (double)boundingBox().height() / 72) );
    else // default
        setMagnification( ( (double)width / TQPaintDevice::x11AppDpiX() ) /
                      ( (double)boundingBox().width() / 72) );
}

void KGVMiniWidget::fitHeight( unsigned int height )
{
    if ( (orientation() == CDSC_LANDSCAPE) || (orientation() == CDSC_SEASCAPE) )
        setMagnification( ( (double)height / TQPaintDevice::x11AppDpiY()) /
                      ( (double)boundingBox().width() / 72) );
    else  //default
        setMagnification( ( (double)height / TQPaintDevice::x11AppDpiY()) /
                      ( (double)boundingBox().height() / 72) );
}

void KGVMiniWidget::fitWidthHeight( unsigned int w, unsigned int h )
{
    double magnification = std::min<double>( 
	    ( ( double )h / TQPaintDevice::x11AppDpiY() ) /
	    ( ( double )boundingBox().height() / 72.0 ),
	    ( ( double )w / TQPaintDevice::x11AppDpiX() ) /
	    ( ( double )boundingBox().width() / 72.0 ) );
    setMagnification( magnification );
}

void KGVMiniWidget::firstPage()
{
    goToPage( 0 );
}

void KGVMiniWidget::lastPage()
{
    if ( !dsc() ) return;

    goToPage( dsc()->page_count() - 1 );
}

bool KGVMiniWidget::prevPage()
{
    if ( !dsc() ) return false;

    int new_page = 0;

    if( dsc()->isStructured() ) {
	new_page = _options.page() - 1;
	if( new_page < 0 )
	    return false;
    }

    goToPage( new_page );
    return true;
}

bool KGVMiniWidget::nextPage()
{
    if ( !dsc() ) return false;

    int new_page = 0;

    if( dsc()->isStructured() ) {
	new_page = _options.page() + 1;
	if( (unsigned int)new_page >= dsc()->page_count() )
	    return false;
    }

    goToPage( new_page );
    return true;
}


void KGVMiniWidget::redisplay ()
{
    if( !document()->psFile() )
	return;

    _psWidget->stopInterpreter();
    showPage( _options.page() );
}

void KGVMiniWidget::restoreOverrideOrientation()
{
    _options.restoreOverrideOrientation();
    showPage( _options.page() );
}

void KGVMiniWidget::setOverrideOrientation( CDSC_ORIENTATION_ENUM orientation )
{
    _options.setOverrideOrientation( orientation );
    showPage( _options.page() );
}

CDSC_ORIENTATION_ENUM KGVMiniWidget::orientation() const
{
    if( _options.overrideOrientation() != CDSC_ORIENT_UNKNOWN )
	return _options.overrideOrientation();
    else if( dsc()->page_orientation() != CDSC_ORIENT_UNKNOWN )
	return static_cast< CDSC_ORIENTATION_ENUM >( dsc()->page_orientation());
    else if( dsc()->bbox().get() != 0 
	  && dsc()->bbox()->width() > dsc()->bbox()->height() )
        return CDSC_LANDSCAPE;
    else
	return CDSC_PORTRAIT;
}

CDSC_ORIENTATION_ENUM KGVMiniWidget::orientation( int pagenumber ) const
{
    if ( !dsc() ||  unsigned( pagenumber ) >= dsc()->page_count() ) {
	return orientation();
    }
    if( _options.overrideOrientation() != CDSC_ORIENT_UNKNOWN ) { 
	return _options.overrideOrientation();
    }
	
    if( dsc()->page()[ pagenumber ].orientation != CDSC_ORIENT_UNKNOWN ) { 
	return static_cast< CDSC_ORIENTATION_ENUM >( dsc()->page()[ pagenumber ].orientation );
    }
    if( dsc()->page_orientation() != CDSC_ORIENT_UNKNOWN ) { 
	return static_cast< CDSC_ORIENTATION_ENUM >( dsc()->page_orientation());
    }
    if( !dsc()->epsf() ) {
	return CDSC_PORTRAIT;
    }
    if( dsc()->bbox().get() != 0
          && dsc()->bbox()->width() > dsc()->bbox()->height() ) {
	return CDSC_LANDSCAPE;
    }
	return CDSC_PORTRAIT;
}

void KGVMiniWidget::restoreOverridePageMedia()
{
    _options.restoreOverridePageMedia();
    redisplay();
	showPage( _options.page() );
}

void KGVMiniWidget::setOverridePageMedia( const TQString& mediaName )
{
    _options.setOverridePageMedia( mediaName );
	showPage( _options.page() );
}

TQString KGVMiniWidget::pageMedia() const
{
    if( !_options.overridePageMedia().isNull() )
	return _options.overridePageMedia();
    else if( dsc()->page_media() != 0 )
	return TQString( dsc()->page_media()->name );
    else if( dsc()->bbox().get() != 0 )
	return TQString( "BoundingBox" );
    else
	return _fallBackPageMedia;
}

TQString KGVMiniWidget::pageMedia( int pagenumber ) const
{
    kdDebug( 4500 ) << "KGVMiniWidget::pageMedia( " <<  pagenumber << " )" << endl;
    if ( !dsc() ) return pageMedia();
    if ( unsigned( pagenumber ) >= dsc()->page_count() ) return pageMedia();
    if( !_options.overridePageMedia().isNull() )
	return _options.overridePageMedia();
    else if( dsc()->page()[ pagenumber ].media != 0 )
	return TQString( dsc()->page()[ pagenumber ].media->name );
    else if( dsc()->page_media() != 0 )
	return TQString( dsc()->page_media()->name );
    else if( dsc()->bbox().get() != 0 )
	return TQString( "BoundingBox" );
    else
	return _fallBackPageMedia;
}

KDSCBBOX KGVMiniWidget::boundingBox() const
{
    TQString currentMedia = pageMedia();
    if( currentMedia == "BoundingBox" )
	return KDSCBBOX( *dsc()->bbox().get() );
    else {
	TQSize size = document()->computePageSize( currentMedia );
	return KDSCBBOX( 0, 0, size.width(), size.height() );
    }
}

KDSCBBOX KGVMiniWidget::boundingBox( int pageNo ) const
{
    TQString currentMedia = pageMedia( pageNo );
    if( currentMedia == "BoundingBox" )
	return KDSCBBOX( *dsc()->bbox().get() );
    else {
	TQSize size = document()->computePageSize( currentMedia );
	return KDSCBBOX( 0, 0, size.width(), size.height() );
    }
}

bool KGVMiniWidget::atFirstPage() const
{
    return ( _options.page() == 0 );
}

bool KGVMiniWidget::atLastPage() const
{
    return ( _options.page() == static_cast<int>( dsc()->page_count() ) - 1 );
}

void KGVMiniWidget::showPage( int pagenumber )
{
    if( !document()->isOpen() )
	return;

    kdDebug(4500) << "KGVMiniWidget::showPage( " << pagenumber << " )" << endl;

    static_cast<TQWidget*>( _psWidget->parent() )->show();

    _psWidget->setFileName(_document->fileName(), dsc()->isStructured() );
    _psWidget->clear();

    if( dsc()->isStructured() ) 
    {
	// Coerce page number to fall in range
	if( ( unsigned int)pagenumber >= dsc()->page_count() )
	    pagenumber = dsc()->page_count() - 1;
	if( pagenumber < 0 )
	    pagenumber = 0;

	_options.setPage( pagenumber );
	
	
        _psWidget->setOrientation( orientation( _options.page() ) );
	_psWidget->setBoundingBox( boundingBox( _options.page() ) );
	_psWidget->setMagnification( _options.magnification() );
   
	if( !_psWidget->isInterpreterRunning() )
	{
	    // Start interpreter, send preamble and send the current page.
	    if( _psWidget->startInterpreter() )
	    {
		_psWidget->sendPS( psFile(), dsc()->beginprolog(),
		                             dsc()->endprolog() );
		_psWidget->sendPS( psFile(), dsc()->beginsetup(),
		                             dsc()->endsetup() );
		_psWidget->sendPS( psFile(), dsc()->page()[ _options.page() ].begin,
		                             dsc()->page()[ _options.page() ].end );
		_visiblePage = _options.page();
	    }
	}
	else
	    sendPage();
    }
    else 
    {
	_psWidget->setOrientation( orientation() );
	_psWidget->setBoundingBox( boundingBox() );
	_psWidget->setMagnification( _options.magnification() );
	
	if( !_psWidget->isInterpreterRunning() ) 
	{
	    // This is not a structured document -- start interpreter
	    _psWidget->startInterpreter();
	    if( !dsc() )
		_psWidget->stopInterpreter();
	}
	else if( _psWidget->isInterpreterReady() )
	    _psWidget->nextPage();
	else 
	{
	    /*
	    KNotifyClient::userEvent
	      (i18n("KGhostview cannot load the document, \"%1\".\n"
		    "It appears to be broken.").arg( _fileName ),
	       KNotifyClient::Messagebox);
	    _psWidget->disableInterpreter();
	    _psFile=0;

	    //TODO: More to do to turn off display?
	    */
	    return;
	}
    }
    // Do this after ajusting pagenumber above
    _thumbnailService->cancelRequests( -1 , _part->scrollBox(), TQT_SLOT( setThumbnail( TQPixmap ) ) );
    _thumbnailService->delayedGetThumbnail( pagenumber, _part->scrollBox(), TQT_SLOT( setThumbnail( TQPixmap ) ), true );

    emit newPageShown( pagenumber );
}

void KGVMiniWidget::sendPage()
{
    // Send the page to the interpreter.
    if( !_psWidget->isInterpreterBusy() && _visiblePage != _options.page() )
    {
	// Interpreter ready - Fire off next page
	_psWidget->clear();
	_psWidget->nextPage();
	_psWidget->sendPS( psFile(), dsc()->page()[ _options.page() ].begin,
	                             dsc()->page()[ _options.page() ].end );
	_visiblePage = _options.page();
    }
}

void KGVMiniWidget::updateStatusBarText( int pageNumber )
{
    if( !dsc() )
	return;

    if( dsc()->isStructured() ) 
    {
	TQString text;
	
	if( pageNumber == -1 )
	    text = i18n( "Page 1" );
	else
	    if( !_usePageLabels || document()->format() == KGVDocument::PDF )
		text = i18n( "Page %1 of %2" )
		       .arg( pageNumber + 1 )
		       .arg( dsc()->page_count() );
	    else
		text = i18n( "Page %1 (%2 of %3)" )
		       .arg( dsc()->page()[ _options.page() ].label )
		       .arg( pageNumber + 1 )
		       .arg( dsc()->page_count() );

	emit setStatusBarText( text );
    }
}

void KGVMiniWidget::buildTOC()
{
    if( !dsc() ) 
	return;

    // Build table of contents
    // Well, that's what it used to be called !!

    int last_page = 0;

    MarkList*  marklist = _part->markList();

    if( dsc()->isStructured() ) {
	if( _usePageLabels )
	    for( unsigned i = 0; i < dsc()->page_count(); ++i ) {
		unsigned j = i;
		if( dsc()->page_order() == CDSC_DESCEND )
		    j = ( dsc()->page_count() - 1 ) - i;
		last_page = atoi( dsc()->page()[j].label );
	    }

	// finally set marked list
	TQString s;
	for( unsigned i = 0; i < dsc()->page_count(); ++i ) {
	    const char * label = dsc()->page()[ i ].label;
	    TQString tip = TQString::fromLocal8Bit( label ? label : "" );

	    if( !_usePageLabels )
		s.setNum( i + 1 );
	    else
		s = tip;

	    marklist->insertItem( s, i, tip );
	}
    }
    else {
	marklist->insertItem( TQString::fromLatin1( "1" ), 0 );
    }
}

void KGVMiniWidget::setMagnification( double magnification )
{
    if ( magnification != _options.magnification() ) {
	_options.setMagnification( magnification );
	showPage( _options.page() );
    }
}

void KGVMiniWidget::enablePageLabels( bool b )
{
    if( _usePageLabels != b ) 
    {
	_usePageLabels = b;
	updateStatusBarText( _options.page() );
	buildTOC();
    }
}

void KGVMiniWidget::slotDocumentOpened()
{
    buildTOC();
    showPage( _options.page() );
}

void KGVMiniWidget::setDisplayOptions( const DisplayOptions& newOptions )
{
    _options = newOptions;
}

#include "kgv_miniwidget.moc"