summaryrefslogtreecommitdiffstats
path: root/kghostview/thumbnailservice.cpp
blob: 4d653cfad9c189149624bb39098e30f50d6739d7 (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
/** 
 * 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 "thumbnailservice.h"

#include "kgv_miniwidget.h"
#include "kpswidget.h"
#include "kgv_view.h"

#include <kdebug.h>
#include <qtimer.h>
#include <algorithm>
#include <cassert>
#include <cstring>

ThumbnailService::ThumbnailService( KGVMiniWidget* parent, const char* name ) :
	QObject( parent, name ),
	_mini( parent ),
	timer_( new QTimer( this ) ),
	_busy( false ),
	_enabled( false )
{
	_thumbnailDrawer = new KPSWidget( parent->_part->widget(), "thumbnail-drawer" );
	_thumbnailDrawer->readSettings();
	connect(  _thumbnailDrawer, SIGNAL(  newPageImage(  QPixmap ) ), SLOT( slotDone( QPixmap ) ) );
	connect( timer_, SIGNAL( timeout() ), SLOT( processOne() ) );
	_thumbnailDrawer->hide();
}

ThumbnailService::~ThumbnailService()
{
}

bool ThumbnailService::Request::operator < ( ThumbnailService::Request b ) const
{
	if ( urgent != b.urgent ) return urgent;
	if ( page != b.page ) return page < b.page;
	// below is just so that == can be in terms of "<"
	if ( receiver != b.receiver ) return std::less<QObject*>()( receiver, b.receiver );
	if ( slot != b.slot ) return std::strcmp( slot, b.slot ) < 0;
	return false;
}

void ThumbnailService::delayedGetThumbnail( const int page, QObject* rec, const char* slot, bool urgent )
{
	kdDebug( 4500 ) << "ThumbnailService::delayedGetThumbnail: request for page " << page << endl;
	pending.insert( Request( page, rec, slot, urgent ) );
	if ( !_busy ) {
		_busy = true;
		// The reason for the code below might not be obvious:
		// It is much nicer to have the the thumbnails appear from top to bottom.
		// However, when several are requested at once (or almost), then we cannot control the order
		// unless we delay a bit the first one
		if ( urgent ) processOne();
		else timer_->start( 150, true );
	}
}

void ThumbnailService::cancelRequests( const int page, QObject* rec, const char* slot )
{
	std::set<Request>::iterator first = pending.begin(), last = pending.end();
	while ( first != last ) {
		if ( ( page == -1 || page == first->page ) &&
				( !rec || rec == first->receiver ) &&
				( !slot || !strcmp( slot, first->slot ) ) ) {
			std::set<Request>::iterator next = first;
			++next;
			pending.erase( first );
			first = next;
		} else {
			++first;
		}
	}
}

void ThumbnailService::reset()
{
	kdDebug( 4500 ) << "ThumbnailService::reset()" << endl;
	timer_->stop();
	pending.clear();
	assert( _thumbnailDrawer );
	_thumbnailDrawer->stopInterpreter();
	_busy = false;
	_enabled = false;
}

void ThumbnailService::setEnabled( const bool e )
{
	kdDebug( 4500 ) << "ThumbnailService::setEnabled( " << ( e ? "true" : "false" ) << " )" << endl;
	_enabled = e;
	if ( _enabled && _busy ) processOne();
}

void ThumbnailService::slotDone( QPixmap pix )
{
	kdDebug( 4500 ) << "ThumbnailService::slotDone(): got page" << endl;
	pix.detach();
	emit relayPixmap( pix );
	processOne();
}


void ThumbnailService::processOne()
{
	kdDebug( 4500 ) << "ThumbnailService::processOne()" << endl;
	if ( !_enabled ) return;
	if ( !_mini || !_mini->dsc() || !_mini->dsc()->isStructured() ) {
		_busy = false;
		pending.clear();
		return;
	}
	assert( _thumbnailDrawer );
	if ( pending.empty() ) {
		_busy = false;
		return;
	}
	_busy = true;
	FILE* file = _mini->psFile();
	Request req = *pending.begin();
	kdDebug( 4500 ) << "ThumbnailService::processOne(): processing " << req.page << "(of " << pending.size() << " requests)" << endl;
	disconnect( SIGNAL( relayPixmap( QPixmap ) ) );
	while ( !pending.empty() && req.page == pending.begin()->page ) {
		req = *pending.begin();
		connect( this, SIGNAL( relayPixmap( QPixmap ) ), req.receiver, req.slot );
		pending.erase( pending.begin() );
	}
	_thumbnailDrawer->setOrientation( _mini->orientation( req.page ) );
	_thumbnailDrawer->setBoundingBox( _mini->boundingBox( req.page ) );
	_thumbnailDrawer->setMagnification(  0.2 );
	if ( !_thumbnailDrawer->isInterpreterRunning() ) {
		_thumbnailDrawer->setFileName( _mini->_document->fileName(), true );
		_thumbnailDrawer->startInterpreter();
		_thumbnailDrawer->sendPS(  file, _mini->dsc()->beginprolog(),
				_mini->dsc()->endprolog() );
		_thumbnailDrawer->sendPS(  file, _mini->dsc()->beginsetup(),
				_mini->dsc()->endsetup() );
	}
	else {
		_thumbnailDrawer->nextPage();
	}
	_thumbnailDrawer->sendPS(  file, _mini->dsc()->page()[ req.page ].begin,
			_mini->dsc()->page()[ req.page ].end );
}

#include "thumbnailservice.moc"