~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kcontrol/style/menupreview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: menupreview.cpp,v 1.3 2003/02/18 22:14:45 mueller Exp $
 
3
 *
 
4
 * Menu Transparency Preview Widget
 
5
 * Copyright (C) 2002 Karol Szwed <gallium@kde.org>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public
 
9
 * License version 2 as published by the Free Software Foundation.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; see the file COPYING.  If not, write to
 
18
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 * Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "menupreview.h"
 
23
 
 
24
#include <qpainter.h>
 
25
#include <qimage.h>
 
26
 
 
27
#include <kpixmap.h>
 
28
#include <kpixmapeffect.h>
 
29
#include <klocale.h>
 
30
#include <kimageeffect.h>
 
31
#include <kiconloader.h>
 
32
 
 
33
 
 
34
MenuPreview::MenuPreview( QWidget* parent, int opacity, PreviewMode pvm )
 
35
        : QWidget( parent, 0, WStyle_Customize | WRepaintNoErase ),
 
36
        pixBackground(NULL), pixOverlay(NULL), pixBlended(NULL)
 
37
{
 
38
        setFixedSize(150, 150);
 
39
        setFocusPolicy( NoFocus );
 
40
 
 
41
        mode = pvm;
 
42
        if (opacity < 0)   opacity = 0;
 
43
        if (opacity > 100) opacity = 100;
 
44
        menuOpacity = opacity/100.0;
 
45
 
 
46
        pixBackground = new KPixmap();
 
47
        pixOverlay = new KPixmap();
 
48
        pixBlended = new KPixmap();
 
49
 
 
50
        createPixmaps();
 
51
        blendPixmaps();
 
52
}
 
53
 
 
54
MenuPreview::~MenuPreview()
 
55
{
 
56
        delete pixBackground;
 
57
        delete pixOverlay;
 
58
        delete pixBlended;
 
59
}
 
60
 
 
61
void MenuPreview::createPixmaps()
 
62
{
 
63
        int w = width()-2;
 
64
        int h = height()-2;
 
65
 
 
66
        if (pixBackground)
 
67
                pixBackground->resize( w, h );
 
68
        if (pixOverlay)
 
69
                pixOverlay->resize( w, h );     
 
70
        if (pixBlended)
 
71
                pixBlended->resize( w, h );
 
72
        
 
73
        QColorGroup cg = colorGroup();
 
74
        QColor c1 = cg.background();
 
75
        QColor c2 = cg.mid();
 
76
 
 
77
        if (pixBackground) {
 
78
                // Paint checkerboard
 
79
                QPainter p;
 
80
                p.begin(pixBackground);
 
81
                for(int x=0; x < pixBackground->width(); x+=5)
 
82
                        for(int y=0; y < pixBackground->height(); y+=5)
 
83
                                p.fillRect( x, y, 5, 5, 
 
84
                                                                 (x % 2) ? 
 
85
                                                                ((y % 2) ?  c2 : c1  ) :        // See the grid? ;-)
 
86
                                                                ((y % 2) ?  c1 : c2  ) );       
 
87
                KIconLoader* icl = KGlobal::iconLoader();
 
88
                QPixmap pix = icl->loadIcon("go", KIcon::Desktop, KIcon::SizeLarge, KIcon::ActiveState);
 
89
                p.drawPixmap( (width()-2-pix.width())/2, (height()-2-pix.height())/2, pix );
 
90
        }
 
91
 
 
92
        if (pixOverlay) {
 
93
                c1 = cg.button().light(110);
 
94
                c2 = cg.button().dark(110);
 
95
                KPixmapEffect::gradient( *pixOverlay, c1, c2, KPixmapEffect::VerticalGradient );
 
96
        }
 
97
}
 
98
 
 
99
void MenuPreview::blendPixmaps()
 
100
{
 
101
        // Rebuild pixmaps, and repaint
 
102
        if (pixBlended && pixBackground) 
 
103
        {
 
104
                if (mode == Blend && pixOverlay) {
 
105
                        QImage src = pixOverlay->convertToImage();
 
106
                        QImage dst = pixBackground->convertToImage();
 
107
                        KImageEffect::blend(src, dst, menuOpacity);
 
108
                        pixBlended->convertFromImage( dst );
 
109
                } else if (mode == Tint) {
 
110
                        QColor clr = colorGroup().button();
 
111
                        QImage dst = pixBackground->convertToImage();
 
112
                        KImageEffect::blend(clr, dst, menuOpacity);
 
113
                        pixBlended->convertFromImage( dst );
 
114
                }
 
115
        }
 
116
}
 
117
 
 
118
void MenuPreview::setOpacity( int opacity )
 
119
{
 
120
        if (opacity < 0 || opacity > 100)
 
121
                return;
 
122
 
 
123
        if ((int)(menuOpacity*100) != opacity) {
 
124
                menuOpacity = opacity/100.0;
 
125
                blendPixmaps();
 
126
                repaint( false );
 
127
        }
 
128
}
 
129
 
 
130
void MenuPreview::setPreviewMode( PreviewMode pvm )
 
131
{
 
132
        if (mode != pvm) {
 
133
                mode = pvm;
 
134
                blendPixmaps();
 
135
                repaint( false );
 
136
        }
 
137
}
 
138
 
 
139
void MenuPreview::paintEvent( QPaintEvent* /* pe */ )
 
140
{
 
141
        // Paint the frame and blended pixmap
 
142
        QColorGroup cg = colorGroup();
 
143
        int x2 = width()-1;
 
144
        int y2 = height()-1;
 
145
 
 
146
        QPainter p(this);
 
147
        p.setPen(cg.dark());
 
148
        p.drawLine(0, 0, x2, 0);
 
149
        p.drawLine(0, 0, 0, y2);
 
150
        p.setPen(cg.light());
 
151
        p.drawLine(1, y2, x2, y2);
 
152
        p.drawLine(x2, 1, x2, y2);
 
153
 
 
154
        if (mode == NoEffect)
 
155
                p.fillRect(1, 1, --x2, --y2, cg.button());
 
156
        else if (mode != NoEffect && pixBlended)
 
157
                p.drawPixmap(1, 1, *pixBlended, 0, 0, --x2, --y2);
 
158
 
 
159
        QRect r = rect();
 
160
        r.moveBy(6,3);
 
161
        p.setPen( cg.text() );
 
162
        p.drawText( r, AlignTop | AlignLeft, QString::number((int)(menuOpacity*100))+i18n("%") );
 
163
}
 
164
 
 
165
#include "menupreview.moc"
 
166
 
 
167
// vim: set noet ts=4:
 
168