summaryrefslogtreecommitdiffstats
path: root/kexi/widget/utils/kexigradientwidget.cpp
blob: f009bd435cc8f27a3adcbc31377bb661199d6d21 (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
/* This file is part of the KDE project
   Copyright (C) 2005 Christian Nitschkowski <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqapplication.h>
#include <tqbitmap.h>
#include <tqimage.h>
#include <tqobjectlist.h>
#include <tqpainter.h>
#include <tqstyle.h>

#include <kimageeffect.h>
#include <kpixmap.h>

#include "kexigradientwidget.h"

KexiGradientWidget::KexiGradientWidget( TQWidget *parent, const char *name, WFlags f )
	: TQWidget( parent, name, f ), p_displayMode( NoGradient ),
	p_gradientType( VerticalGradient ),
	p_color1( TQt::white ), p_color2( TQt::blue ), p_currentChild( 0 ),
	p_opacity( 0.5 ), p_cacheDirty( true )
{
	p_customBackgroundWidgets.setAutoDelete( false );
	p_knownWidgets.setAutoDelete( false );

	p_backgroundColor = TQWidget::paletteBackgroundColor();

	connect ( &p_rebuildDelayTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( setCacheDirty() ) );

	installEventFilter( this );
}

KexiGradientWidget::~KexiGradientWidget()
{
}

bool KexiGradientWidget::isValidChildWidget( TQObject* child ) {
	const TQWidget* wgt = dynamic_cast<TQWidget*>( child );

	if ( wgt == 0L )
		return false;

	if ( wgt->inherits( "TQScrollView" ) )
		return false;
	if ( wgt->inherits( "TQComboBox" ) )
		return false;
	if ( wgt->inherits( "TQLineEdit" ) )
		return false;
	if ( wgt->inherits( "KexiDBForm" ) )
		return false;

	return true;
}

void KexiGradientWidget::buildChildrenList( WidgetList& list, TQWidget* p ) {
	TQObjectList* objects = p->queryList( "TQWidget", 0, false, false );

	for ( TQObjectList::Iterator it = objects->begin(); it != objects->end(); ++it ) {
		if ( isValidChildWidget( ( *it ) ) == false )
			continue;
		list.append( dynamic_cast<TQWidget*>( ( *it ) ) );
		buildChildrenList( list, dynamic_cast<TQWidget*>( ( *it ) ) );
	}

	delete objects;
}

void KexiGradientWidget::rebuildCache( void ) {
	WidgetList childWidgetList;
	buildChildrenList( childWidgetList, this );

	/**
	Disable the effect and behave like a normal TQWidget.
	*/
	if ( p_displayMode == NoGradient ) {
//		if ( p_backgroundPixmap.isNull() ) {
			//unsetPalette();
		//} else {
			TQWidget::setPaletteBackgroundPixmap( p_backgroundPixmap );
		//}
		TQWidget::setPaletteBackgroundColor( p_backgroundColor );

		for ( WidgetList::Iterator it = childWidgetList.begin();
			it != childWidgetList.end(); ++it ) {

			if ( p_customBackgroundWidgets.contains( ( *it ) ) == false ) {
				( *it )->unsetPalette();
			}
		}
		/**
		The cache is now in a current state.
		*/
		p_cacheDirty = false;
		return;
	}

	KPixmap tempPixmap;
	TQImage gradientImage;
	TQImage bgImage;

	/**
	Draw the gradient
	*/
	gradientImage = KImageEffect::gradient( size(), p_color1, p_color2,
		(KImageEffect::GradientType)p_gradientType );

	/**
	Draw the widget-background in a pixmap and fade it with the gradient.
	*/
	if ( p_displayMode == FadedGradient ) {
		tempPixmap.resize( size() );
		TQPainter p( &tempPixmap, this );

		if ( p_backgroundPixmap.isNull() ) {
			/*
			Need to unset the palette, otherwise the old gradient
			will be used as a background, not the widget's default bg.
			*/
			unsetPalette();
			p.fillRect( 0, 0, width(), height(), palette().brush(
				isEnabled() ? TQPalette::Active : TQPalette::Disabled,
				TQColorGroup::Background ) );
		} else {
			p.drawTiledPixmap( 0, 0, width(), height(), p_backgroundPixmap );
		}

		p.end();

		bgImage = tempPixmap;

		KImageEffect::blend( gradientImage, bgImage, (float)p_opacity );

		tempPixmap.convertFromImage( bgImage );
	} else if ( p_displayMode == SimpleGradient ) {
	/**
	Use the gradient as the final background-pixmap
	if displaymode is set to SimpleGradient.
	*/
		tempPixmap.convertFromImage( gradientImage );
	}

	/**
	All children need to have our background set.
	*/
	KPixmap partPixmap;
	TQRect area;
	TQWidget* childWidget = 0;
	const TQPoint topLeft( 0, 0 );

	for ( WidgetList::Iterator it = childWidgetList.begin();
		it != childWidgetList.end(); ++it ) {

		childWidget = ( *it );

		/**
		Exclude widgets with a custom palette.
		*/
		if ( p_customBackgroundWidgets.contains( childWidget ) ) {
			continue;
		}

		partPixmap.resize( childWidget->size() );
		/**
		Get the part of the tempPixmap that is
		under the current child-widget.
		*/
		if ( childWidget->parent() == this ) {
			area = childWidget->geometry();
		} else {
			area.setTopLeft( childWidget->mapTo( this,
				childWidget->clipRegion().boundingRect().topLeft() ) );
			area.setSize( childWidget->size() );
		}
		bitBlt( &partPixmap, topLeft, &tempPixmap, area );

		p_currentChild = childWidget;
		childWidget->setPaletteBackgroundPixmap( partPixmap );
	}

	TQWidget::setPaletteBackgroundPixmap( tempPixmap );
	/**
	Unset the dirty-flag at the end of the method.
	TQWidget::setPaletteBackgroundPixmap() causes this
	to get set to true again, so set it to false
	right after setting the pixmap.
	*/
	p_cacheDirty = false;
}

void KexiGradientWidget::paintEvent( TQPaintEvent* e ) {
	/**
	Rebuild the background-pixmap if necessary.
	*/
	if ( p_cacheDirty == true ) {
		rebuildCache();
	}

	/**
	Draw the widget as usual
	*/
	TQWidget::paintEvent( e );
}

bool KexiGradientWidget::eventFilter( TQObject* object, TQEvent* event ) {
	TQWidget* child = dynamic_cast<TQWidget*>( object );

	/**
	Manage list of child-widgets.
	*/
	if ( object == this ) {
		if ( event->type() == TQEvent::ChildInserted ) {
			child = dynamic_cast<TQWidget*>( dynamic_cast<TQChildEvent*>( event )->child() );
			if ( isValidChildWidget( TQT_TQOBJECT(child) ) == false ) {
				return false;
			}
			/**
			Add the new child-widget to our list of known widgets.
			*/
			p_knownWidgets.append( child );
			/**
			... and install 'this' as the child's event-filter.
			*/
			child->installEventFilter( this );
		} else if ( event->type() == TQEvent::ChildRemoved ) {
			/**
			Remove the child-widget from the list of known widgets.
			*/
			p_knownWidgets.remove( dynamic_cast<TQWidget*>( dynamic_cast<TQChildEvent*>( event )->child() ) );
		}
		return false;
	}

	/**
	Manage custombackground-list.
	*/
	if ( event->type() == TQEvent::PaletteChange ) {
		/**
		p_currentChild will be == 0L, when the user
		sets it's palette manually.
		In this case, it has to be added to the customBackground-list.
		*/
		if ( p_currentChild == 0L && child != 0L ) {
			if ( p_customBackgroundWidgets.contains( child ) == false ) {
				p_customBackgroundWidgets.append( child );
				return false;
			}
		}
		/**
		Check if the widget whose PaletteChange-event we handle
		isn't the widget we set the background in rebuildCache().
		*/
		if ( child != p_currentChild && child != 0L ) {
			/**
			Add the new child to the list of widgets, we don't set
			the background ourselves if it isn't in the list.
			*/
			if ( p_customBackgroundWidgets.contains( child ) == false ) {
				if ( child->paletteBackgroundPixmap() != 0L ) {
					p_customBackgroundWidgets.append( child );
				}
			} else {
				/**
				If the palette is now the default-palette again,
				remove it from the "don't set background in rebuildCache()"-list
				and rebuild the cache, so it again will get the gradient background.
				*/
				if ( child->paletteBackgroundPixmap() == 0L ) {
					p_customBackgroundWidgets.remove( child );
					if ( p_displayMode != NoGradient ) {
						p_cacheDirty = true;
					}
				}
			}
		}
		p_currentChild = 0;
	}

	if ( event->type() == TQEvent::Move ) {
		if ( p_customBackgroundWidgets.contains( child ) == false ) {
			updateChildBackground( child );
		}
	}
	return false;
}

void KexiGradientWidget::updateChildBackground( TQWidget* childWidget )
{
	KPixmap partPixmap;
	KPixmap bgPixmap;
	TQRect area;
	const TQPoint topLeft( 0, 0 );

	bgPixmap = paletteBackgroundPixmap() ? (*paletteBackgroundPixmap()) : TQPixmap();
	if ( bgPixmap.isNull() )
		return;

	/**
	Exclude widgtes that don't have a parent.
	This happens when children are removed
	which are in the knownWidgets-list.
	*/
	if ( childWidget->parent() == 0L )
		return;

	/**
	Exclude widgets with a custom palette.
	*/
	if ( p_customBackgroundWidgets.contains( childWidget ) ) {
		return;
	}

	partPixmap.resize( childWidget->size() );
	/**
	Get the part of the tempPixmap that is
	under the current child-widget.
	*/
	if ( childWidget->parent() == this ) {
		area = childWidget->geometry();
	} else {
		area.setTopLeft( childWidget->mapTo( this,
			childWidget->clipRegion().boundingRect().topLeft() ) );
		area.setSize( childWidget->size() );
	}
	bitBlt( &partPixmap, topLeft, &bgPixmap, area );

	p_currentChild = childWidget;
	childWidget->setPaletteBackgroundPixmap( partPixmap );
}

void KexiGradientWidget::setPaletteBackgroundColor( const TQColor& color )
{
	p_backgroundColor = color;
	if ( p_displayMode == NoGradient ) {
		TQWidget::setPaletteBackgroundColor( p_backgroundColor );
	}
}

const TQColor& KexiGradientWidget::paletteBackgroundColor() const
{
	return p_backgroundColor;
}

#include "kexigradientwidget.moc"