summaryrefslogtreecommitdiffstats
path: root/src/kchmviewwindow_qtextbrowser.cpp
blob: f66dc88276312167481627e7c49a76ecddab4b98 (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
/***************************************************************************
 *   Copyright (C) 2004-2007 by Georgy Yunaev, gyunaev@ulduzsoft.com       *
 *   Please do not use email address above for bug reports; see            *
 *   the README file                                                       *
 *                                                                         *
 *   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 <tqprinter.h>
#include <tqpainter.h>
#include <tqsimplerichtext.h>
#include <tqpaintdevicemetrics.h>

#include "kde-qt.h"
#include "kchmmainwindow.h"
#include "kchmviewwindow_qtextbrowser.h"

/*
 * If defined, all the data viewed is kept in source factory. It increases the response time
 * when a user opens the page he has already seen, in cost of everything which has been opened 
 * is stored in memory, increasing memory usage.
 *
 * If not defined, on any page change the source factory cleans up, saving the memory, but 
 * increasing the page loading time in case the page has the same images, or the page is opened
 * second time.
 */
#define KEEP_ALL_OPENED_DATA_IN_SOURCE_FACTORY

KCHMViewWindow_QTextBrowser::KCHMViewWindow_QTextBrowser( TQTabWidget * parent )
	: TQTextBrowser ( parent ), KCHMViewWindow ( parent )
{
	m_zoomfactor = 0;
	m_sourcefactory = 0;
	tqinvalidate();
	
	setTextFormat ( TQt::RichText );
	connect( this, TQT_SIGNAL( linkClicked (const TQString &) ), this, TQT_SLOT( slotLinkClicked(const TQString &) ) );
}


KCHMViewWindow_QTextBrowser::~KCHMViewWindow_QTextBrowser()
{
	delete m_sourcefactory;
}

bool KCHMViewWindow_QTextBrowser::openPage (const TQString& url)
{
	// If we're using a memory saving scheme, we destroy MimeSourceFactory (including all the stored data)
	// when opening a new page. It saves some memory, but spends more time while looking for already loaded
	// images and HTML pages
#if !defined (KEEP_ALL_OPENED_DATA_IN_SOURCE_FACTORY)
	delete m_sourcefactory;
	m_sourcefactory = new KCHMSourceFactory;
	setMimeSourceFactory (m_sourcefactory);
#endif	

	setSource (url);
	return true;
}

void KCHMViewWindow_QTextBrowser::setSource ( const TQString & name )
{
	if ( m_allowSourceChange )
	{
		// Do URI decoding, qtextbrowser does stupid job.
		TQString fixedname = decodeUrl( name );
		TQTextBrowser::setSource (fixedname);
	}
	else
		m_allowSourceChange = true;
}

void KCHMViewWindow_QTextBrowser::setZoomFactor( int zoom )
{
	m_zoomfactor = zoom;
	
	if ( zoom < 0 )
		TQTextBrowser::zoomOut( -zoom );
	else if ( zoom > 0 )
		TQTextBrowser::zoomIn( zoom);
}

void KCHMViewWindow_QTextBrowser::tqinvalidate( )
{
	delete m_sourcefactory;
	m_sourcefactory = new KCHMSourceFactory (this);
	setMimeSourceFactory (m_sourcefactory);
	m_zoomfactor = 0;
	m_allowSourceChange = true;
	m_searchLastIndex = 0;
	m_searchLastParagraph = 0;
	m_searchText = TQString();
	reload();
	
	KCHMViewWindow::tqinvalidate( );
}

int KCHMViewWindow_QTextBrowser::getScrollbarPosition( )
{
	return contentsY ();
}

void KCHMViewWindow_QTextBrowser::setScrollbarPosition( int pos )
{
	setContentsPos (0, pos);
}

void KCHMViewWindow_QTextBrowser::addZoomFactor( int value )
{
	setZoomFactor( value);
}

void KCHMViewWindow_QTextBrowser::slotLinkClicked( const TQString & newlink )
{
	emit signalLinkClicked (newlink, m_allowSourceChange);
}


bool KCHMViewWindow_QTextBrowser::printCurrentPage( )
{
#if !defined (TQT_NO_PRINTER)
    TQPrinter printer( TQPrinter::HighResolution );
    printer.setFullPage(TRUE);
	
	if ( printer.setup( this ) )
	{
		TQPainter p( &printer );
		
		if( !p.isActive() ) // starting printing failed
			return false;
		
		TQPaintDeviceMetrics metrics(p.device());
		int dpiy = metrics.logicalDpiY();
		int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
		TQRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
		TQSimpleRichText richText( text(),
								  TQFont(),
								  context(),
								  styleSheet(),
								  mimeSourceFactory(),
								  body.height() );
		richText.setWidth( &p, body.width() );
		TQRect view( body );
		
		int page = 1;
		
		do
		{
			richText.draw( &p, body.left(), body.top(), view, tqcolorGroup() );
			view.moveBy( 0, body.height() );
			p.translate( 0 , -body.height() );
			p.drawText( view.right() - p.fontMetrics().width( TQString::number(page) ),
						view.bottom() + p.fontMetrics().ascent() + 5, TQString::number(page) );
			
			if ( view.top()  >= richText.height() )
				break;
			
			TQString msg = i18n( "Printing (page %1)...") .tqarg(page);
			::mainWindow->showInStatusBar( msg );
			
			printer.newPage();
			page++;
		}
		while (TRUE);
	
		::mainWindow->showInStatusBar( i18n( "Printing completed") );
		return true;
	}

	::mainWindow->showInStatusBar( i18n( "Printing aborted") );
	return false;

#else /* TQT_NO_PRINTER */

	TQMessageBox::warning( this, 
		i18n( "%1 - could not print") . arg(APP_NAME),
		i18n( "Could not print.\nYour TQt library has been compiled without printing support");
	return false;

#endif /* TQT_NO_PRINTER */
}


void KCHMViewWindow_QTextBrowser::searchWord( const TQString & word, bool forward, bool )
{
	if ( m_searchText == word )
	{
		if ( forward && (m_searchLastIndex || m_searchLastParagraph) )
			m_searchLastIndex += m_searchText.length();
	}
	else
	{
		m_searchLastParagraph = m_searchLastIndex = 0;
		m_searchText = word;
	}

	if ( find (m_searchText, false, false, forward, &m_searchLastParagraph, &m_searchLastIndex) )
		::mainWindow->showInStatusBar( i18n( "Search failed") );
}

void KCHMViewWindow_QTextBrowser::clipSelectAll( )
{
	selectAll (TRUE);
}

void KCHMViewWindow_QTextBrowser::clipCopy( )
{
	copy ();
}


// Shamelessly stolen from TQt
TQString KCHMViewWindow_QTextBrowser::decodeUrl( const TQString &input )
{
	TQString temp;

    int i = 0;
	int len = input.length();
	int a, b;
	TQChar c;
	while (i < len)
	{
		c = input[i];
		if (c == '%' && i + 2 < len)
		{
			a = input[++i];
			b = input[++i];

			if (a >= '0' && a <= '9') a -= '0';
			else if (a >= 'a' && a <= 'f') a = a - 'a' + 10;
			else if (a >= 'A' && a <= 'F') a = a - 'A' + 10;

			if (b >= '0' && b <= '9') b -= '0';
			else if (b >= 'a' && b <= 'f') b  = b - 'a' + 10;
			else if (b >= 'A' && b <= 'F') b  = b - 'A' + 10;

			temp.append( (TQChar)((a << 4) | b ) );
		}
		else
		{
			temp.append( c );
		}

		++i;
	}

    return temp;
}

TQPopupMenu * KCHMViewWindow_QTextBrowser::createPopupMenu( const TQPoint & pos )
{
	KTQPopupMenu * menu = getContextMenu( anchorAt( pos ), this );
	menu->exec( mapToGlobal( contentsToViewport( pos ) ) );
	return 0;
}

#include "kchmviewwindow_qtextbrowser.moc"