summaryrefslogtreecommitdiffstats
path: root/kview/kviewcanvas/kimageholder.cpp
blob: 1bf3df989bd56750faa2a0ea40418f0bc13c4db1 (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
/*  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.

*/

//$Id$

#include <assert.h>

#include <tqcolor.h>
#include <tqwidget.h>
#include <tqimage.h>
#include <tqpainter.h>
#include <tqpen.h>
#include <tqmovie.h>
#include <tqpixmap.h>

#include <kpixmap.h>
#include <kdebug.h>

#include "kimageholder.h"

KImageHolder::KImageHolder( TQWidget * tqparent, const char * name )
	: TQWidget( tqparent, name, TQt::WResizeNoErase | TQt::WRepaintNoErase )
	, m_selected( false )
	, m_bSelecting( false )
	, m_scrollTimerId( 0 )
	, m_xOffset( 0 )
	, m_yOffset( 0 )
	, m_pen( new TQPen( TQColor( 255, 255, 255 ), 0, DashLine ) )
	, m_pPixmap( 0 )
	, m_pDoubleBuffer( 0 )
	, m_pCheckboardPixmap( 0 )
{
	setBackgroundMode( TQWidget::NoBackground );
}

KImageHolder::~KImageHolder()
{
	delete m_pen;
	delete m_pPixmap;
	delete m_pDoubleBuffer;
	delete m_pCheckboardPixmap;
	m_pen = 0;
	m_pPixmap = 0;
	m_pDoubleBuffer = 0;
	m_pCheckboardPixmap = 0;
}

void KImageHolder::mousePressEvent( TQMouseEvent *ev )
{
	//kdDebug( 4620 ) << k_funcinfo << " ev->state() = " << ev->state() << endl;
	// if the right mouse button is pressed emit the contextPress signal
	if ( ev->button() == Qt::RightButton )
	{
		emit contextPress( mapToGlobal( ev->pos() ) );
		return;
	}

	if( m_pPixmap == 0 )
		return;

	if( ev->button() == Qt::LeftButton || ev->button() == Qt::MidButton )
	{
		m_scrollpos = ev->globalPos();
		m_selectionStartPoint = ev->pos();
	}
}

void KImageHolder::mouseMoveEvent( TQMouseEvent *ev )
{
	//FIXME: when scrolling the cursorpos shouldn't change
	if( TQT_TQRECT_OBJECT(this->rect()).contains( ev->pos(), false ) )
		emit cursorPos( ev->pos() );
	//kdDebug( 4620 ) << k_funcinfo << " ev->state() = " << ev->state() << endl;
	if( ev->state() & Qt::LeftButton || ev->state() & Qt::MidButton )
	{
		// scroll when a modifier and left button or the middle button is pressed
		if( ev->state() & AltButton || ev->state() & ControlButton || ev->state() & ShiftButton || ev->state() & Qt::MidButton )
		{
			TQPoint difference = m_scrollpos - ev->globalPos();
			emit wannaScroll( difference.x(), difference.y() );
		}
		else // create a selection
		{
			TQWidget * parentwidget = ( TQWidget* )tqparent();
			if( ! m_bSelecting )
			{
				m_bSelecting = true;
				if( m_selected ) {
					// remove old rubberband
					eraseSelect();
					m_selected = false;
				}

				m_selection.setLeft( m_selectionStartPoint.x() );
				m_selection.setRight( m_selectionStartPoint.x() );
				m_selection.setTop( m_selectionStartPoint.y() );
				m_selection.setBottom( m_selectionStartPoint.y() );
			}
			// currently called only on drag,
			// so assume a selection has been started
			bool erase = m_selected;

			if( !m_selected )
				m_selected = true;

			// Autoscrolling:
			// For every pixel the mouse leaves the image canvas the canvas
			// should scroll faster. The scrolling should be independent from
			// the mouse movement. Therefor I need a timer to do the scroll
			// movement:
			// e.g.: m_xOffset=-1 => scroll to the left 1 px per 50ms
			//                 -2 => scroll to the left 2 px per 50ms
			//                  2 => scroll to the right 2 px per 50ms
			//                 ...
			m_xOffset = mapTo( parentwidget, ev->pos() ).x();
			m_yOffset = mapTo( parentwidget, ev->pos() ).y();
			if( m_xOffset > 0 )
			{
				m_xOffset -= parentwidget->width();
				if( m_xOffset < 0 )
					m_xOffset = 0;
			}
			if( m_yOffset > 0 )
			{
				m_yOffset -= parentwidget->height();
				if( m_yOffset < 0 )
					m_yOffset = 0;
			}
			if( m_scrollTimerId != 0 && m_xOffset == 0 && m_yOffset == 0 )
			{
				killTimer( m_scrollTimerId );
				m_scrollTimerId = 0;
			}
			else if( m_scrollTimerId == 0 && ( m_xOffset != 0 || m_yOffset != 0 ) )
				m_scrollTimerId = startTimer( 50 );

			int r = ev->x() < width()  ? ( ev->x() < 0 ? 0 : ev->x() ) : width()  - 1;
			int b = ev->y() < height() ? ( ev->y() < 0 ? 0 : ev->y() ) : height() - 1;

			if( r != m_selection.right() || b != m_selection.bottom() )
			{
				if( erase )
					eraseSelect();

				m_selection.setRight( r );
				m_selection.setBottom( b );
				emit selected( m_selection.normalize() );

				TQPainter painter( this );
				drawSelect( painter );
			}
		}
		m_scrollpos = ev->globalPos();
		m_selectionStartPoint = ev->pos();
	}
}

void KImageHolder::mouseReleaseEvent( TQMouseEvent * ev )
{
	if( m_bSelecting )
	{
		m_xOffset = m_yOffset = 0;
		if( m_scrollTimerId != 0 )
		{
			killTimer( m_scrollTimerId );
			m_scrollTimerId = 0;
		}
	}
	if( ev->state() & Qt::LeftButton || ev->state() & Qt::MidButton )
		if( m_bSelecting )
			m_bSelecting = false;
		else
			clearSelection();
}

void KImageHolder::drawSelect( TQPainter & painter )
{
	painter.save();
	painter.setRasterOp( XorROP );
	painter.setPen( *m_pen );
	painter.drawRect( m_selection.normalize() );
	painter.restore();
}

void KImageHolder::eraseSelect()
{
	TQRegion r( m_selection.normalize() );
	TQRect inner = m_selection.normalize();
	inner.rLeft() += 1;
	inner.rTop() += 1;
	inner.rRight() -= 1;
	inner.rBottom() -= 1;
	r -= inner;

	TQMemArray<TQRect> rects = r.tqrects();

	if( m_pDoubleBuffer )
		for( unsigned int i = 0; i < rects.size(); ++i )
			bitBlt( this, rects[ i ].topLeft(), m_pDoubleBuffer, rects[ i ], CopyROP );
	else
		for( unsigned int i = 0; i < rects.size(); ++i )
			bitBlt( this, rects[ i ].topLeft(), m_pPixmap, rects[ i ], CopyROP );
}

void KImageHolder::clearSelection()
{
	if( m_selected )
	{
		eraseSelect();
		m_selected = false;
	}
	m_selection.setSize( TQSize( 0, 0 ) );
	emit selected( m_selection );
}

void KImageHolder::setImage( const KPixmap & pix )
{
	clearSelection();

	setPixmap( pix );
}

void KImageHolder::setImage( const TQImage & image )
{
	clearSelection();
	kdDebug( 4620 ) << "converting Image to Pixmap" << endl;
	KPixmap pix( image );

	setPixmap( pix );
}

void KImageHolder::setImage( const TQMovie & /*movie*/ )
{
	clearSelection();
	//setMovie( movie );
	kdWarning( 4620 ) << "setImage( TQMovie ) not implemented" << endl;
}

void KImageHolder::clear()
{
	delete m_pPixmap;
	m_pPixmap = 0;
	delete m_pDoubleBuffer;
	m_pDoubleBuffer = 0;
	hide();
	clearSelection();
}

TQRect KImageHolder::selection() const
{
	if( m_selected )
		return m_selection.normalize();
	else
		return TQRect();
}

TQSize KImageHolder::tqsizeHint() const
{
	if( m_pPixmap )
		return m_pPixmap->size();
	return TQSize( 0, 0 );
}

void KImageHolder::paintEvent( TQPaintEvent *ev )
{
	TQPainter painter( this );
	painter.setClipRegion( ev->region().intersect( m_drawRect ) );
	if( m_pPixmap )
	{
		if( m_pPixmap->tqmask() )
		{
			if( ! m_pDoubleBuffer )
			{
				m_pDoubleBuffer = new KPixmap( m_pPixmap->size() );
				TQPainter p( m_pDoubleBuffer );
				p.drawTiledPixmap( m_pDoubleBuffer->rect(), checkboardPixmap() );
				p.end();
				bitBlt( m_pDoubleBuffer, TQPoint( 0, 0 ), m_pPixmap, m_pPixmap->rect() );
			}
			painter.drawPixmap( 0, 0, *m_pDoubleBuffer );
		}
		else
			painter.drawPixmap( 0, 0, *m_pPixmap );
	}
	if( m_selected )
		drawSelect( painter );
}

void KImageHolder::timerEvent( TQTimerEvent * ev )
{
	if( ev->timerId() != m_scrollTimerId )
		return;

	emit wannaScroll( m_xOffset, m_yOffset );
}

void KImageHolder::setPixmap( const KPixmap & pixmap )
{
	kdDebug( 4620 ) << k_funcinfo << " " << pixmap.width() << 'x' << pixmap.height() << endl;
	delete m_pPixmap;
	delete m_pDoubleBuffer;
	m_pDoubleBuffer = 0;
	m_pPixmap = new KPixmap( pixmap );
	m_drawRect = m_pPixmap->rect();
	show();
}

const KPixmap & KImageHolder::checkboardPixmap()
{
	if( ! m_pCheckboardPixmap )
	{
		const char * xpm[] = {
			"32 32 2 1",
			"  c #666666",
			"X c #999999",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"                XXXXXXXXXXXXXXXX",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
			"XXXXXXXXXXXXXXXX                ",
		};
		m_pCheckboardPixmap = new KPixmap( xpm );
	}
	return *m_pCheckboardPixmap;
}

#include "kimageholder.moc"

// vim:sw=4:ts=4