summaryrefslogtreecommitdiffstats
path: root/kcontrol/style/menupreview.cpp
blob: 190f0517fbc7311970fd333b2d4999becc94c8aa (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
/*
 * Menu Transparency Preview Widget
 * Copyright (C) 2002 Karol Szwed <gallium@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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "menupreview.h"

#include <tqpainter.h>
#include <tqimage.h>

#include <kpixmap.h>
#include <kpixmapeffect.h>
#include <klocale.h>
#include <kimageeffect.h>
#include <kiconloader.h>


MenuPreview::MenuPreview( TQWidget* parent, int opacity, PreviewMode pvm )
	: TQWidget( parent, 0, (WFlags)(WStyle_Customize | WRepaintNoErase) ),
	pixBackground(NULL), pixOverlay(NULL), pixBlended(NULL)
{
	setFixedSize(150, 150);
	setFocusPolicy( TQ_NoFocus );

	mode = pvm;
	if (opacity < 0)   opacity = 0;
	if (opacity > 100) opacity = 100;
	menuOpacity = opacity/100.0;

	pixBackground = new KPixmap();
	pixOverlay = new KPixmap();
	pixBlended = new KPixmap();

	createPixmaps();
	blendPixmaps();
}

MenuPreview::~MenuPreview()
{
	delete pixBackground;
	delete pixOverlay;
	delete pixBlended;
}

void MenuPreview::createPixmaps()
{
	int w = width()-2;
	int h = height()-2;

	if (pixBackground)
		pixBackground->resize( w, h );
	if (pixOverlay)
		pixOverlay->resize( w, h );	
	if (pixBlended)
		pixBlended->resize( w, h );
	
	TQColorGroup cg = colorGroup();
	TQColor c1 = cg.background();
	TQColor c2 = cg.mid();

	if (pixBackground) {
		// Paint checkerboard
		TQPainter p;
		p.begin(pixBackground);
		for(int x=0; x < pixBackground->width(); x+=5)
			for(int y=0; y < pixBackground->height(); y+=5)
				p.fillRect( x, y, 5, 5, 
								 (x % 2) ? 
								((y % 2) ?  c2 : c1  ) : 	// See the grid? ;-)
								((y % 2) ?  c1 : c2  ) );	
		KIconLoader* icl = KGlobal::iconLoader();
		TQPixmap pix = icl->loadIcon("go", KIcon::Desktop, KIcon::SizeLarge, KIcon::ActiveState);
		p.drawPixmap( (width()-2-pix.width())/2, (height()-2-pix.height())/2, pix );
	}

	if (pixOverlay) {
		c1 = cg.button().light(110);
		c2 = cg.button().dark(110);
		KPixmapEffect::gradient( *pixOverlay, c1, c2, KPixmapEffect::VerticalGradient );
	}
}

void MenuPreview::blendPixmaps()
{
	// Rebuild pixmaps, and repaint
	if (pixBlended && pixBackground) 
	{
		if (mode == Blend && pixOverlay) {
			TQImage src = pixOverlay->convertToImage();
			TQImage dst = pixBackground->convertToImage();
			KImageEffect::blend(src, dst, menuOpacity);
			pixBlended->convertFromImage( dst );
		} else if (mode == Tint) {
			TQColor clr = colorGroup().button();
			TQImage dst = pixBackground->convertToImage();
			KImageEffect::blend(clr, dst, menuOpacity);
			pixBlended->convertFromImage( dst );
		}
	}
}

void MenuPreview::setOpacity( int opacity )
{
	if (opacity < 0 || opacity > 100)
		return;

	if ((int)(menuOpacity*100) != opacity) {
		menuOpacity = opacity/100.0;
		blendPixmaps();
		repaint( false );
	}
}

void MenuPreview::setPreviewMode( PreviewMode pvm )
{
	if (mode != pvm) {
		mode = pvm;
		blendPixmaps();
		repaint( false );
	}
}

void MenuPreview::paintEvent( TQPaintEvent* /* pe */ )
{
	// Paint the frame and blended pixmap
	TQColorGroup cg = colorGroup();
	int x2 = width()-1;
	int y2 = height()-1;

	TQPainter p(this);
	p.setPen(cg.dark());
	p.drawLine(0, 0, x2, 0);
	p.drawLine(0, 0, 0, y2);
	p.setPen(cg.light());
	p.drawLine(1, y2, x2, y2);
	p.drawLine(x2, 1, x2, y2);

	if (mode == NoEffect)
		p.fillRect(1, 1, --x2, --y2, cg.button());
	else if (mode != NoEffect && pixBlended)
		p.drawPixmap(1, 1, *pixBlended, 0, 0, --x2, --y2);

	TQRect r = rect();
	r.moveBy(6,3);
	p.setPen( cg.text() );
	p.drawText( r, AlignTop | AlignLeft, TQString::number((int)(menuOpacity*100))+i18n("%") );
}

#include "menupreview.moc"

// vim: set noet ts=4: