1 /***************************************************************************
2 * Copyright (C) 1996-2000 the kicker authors. *
3 * Copyright (C) 2005 Mark Kretschmann <markey@web.de> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "prettypopupmenu.h"
23 #include <KApplication>
25 #include <KConfigGroup>
26 #include <KIconEffect>
27 #include <KStandardDirs>
30 #include <QPaintEvent>
32 #include <QResizeEvent>
34 #include <QStyleOptionFrame>
37 QImage
PrettyPopupMenu::s_sidePixmap
;
38 QColor
PrettyPopupMenu::s_sidePixmapColor
;
40 ////////////////////////////////////////////////////////////////////////////////
42 ////////////////////////////////////////////////////////////////////////////////
44 PrettyPopupMenu::PrettyPopupMenu( QWidget
* parent
)
47 // Must be initialized so that we know the size on first invocation
48 if ( s_sidePixmap
.isNull() )
53 ////////////////////////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////////////////////////
58 PrettyPopupMenu::generateSidePixmap()
60 const QColor newColor
= calcPixmapColor();
62 if ( newColor
!= s_sidePixmapColor
) {
63 s_sidePixmapColor
= newColor
;
64 s_sidePixmap
.load( KStandardDirs::locate( "data","amarok/images/menu_sidepixmap.png" ) );
65 KIconEffect::colorize( s_sidePixmap
, newColor
, 1.0 );
70 PrettyPopupMenu::sideImageRect() const
72 return QStyle::visualRect( layoutDirection(), rect(), QRect( frameWidth(), frameWidth(),
73 s_sidePixmap
.width(), height() - 2*frameWidth() ) );
77 PrettyPopupMenu::calcPixmapColor()
79 KConfigGroup cg
= KGlobal::config()->group("WM");
80 QColor color
= QApplication::palette().active().highlight();
81 // QColor activeTitle = QApplication::palette().active().background();
82 // QColor inactiveTitle = QApplication::palette().inactive().background();
83 QColor activeTitle
= cg
.readEntry("activeBackground", color
);
84 QColor inactiveTitle
= cg
.readEntry("inactiveBackground", color
);
86 // figure out which color is most suitable for recoloring to
87 int h1
, s1
, v1
, h2
, s2
, v2
, h3
, s3
, v3
;
88 activeTitle
.hsv(&h1
, &s1
, &v1
);
89 inactiveTitle
.hsv(&h2
, &s2
, &v2
);
90 QApplication::palette().active().background().hsv(&h3
, &s3
, &v3
);
92 if ( (qAbs(h1
-h3
)+qAbs(s1
-s3
)+qAbs(v1
-v3
) < qAbs(h2
-h3
)+qAbs(s2
-s3
)+qAbs(v2
-v3
)) &&
93 ((qAbs(h1
-h3
)+qAbs(s1
-s3
)+qAbs(v1
-v3
) < 32) || (s1
< 32)) && (s2
> s1
))
94 color
= inactiveTitle
;
98 // limit max/min brightness
100 color
.rgb(&r
, &g
, &b
);
101 int gray
= qGray(r
, g
, b
);
103 r
= (r
- (gray
- 180) < 0 ? 0 : r
- (gray
- 180));
104 g
= (g
- (gray
- 180) < 0 ? 0 : g
- (gray
- 180));
105 b
= (b
- (gray
- 180) < 0 ? 0 : b
- (gray
- 180));
106 } else if (gray
< 76) {
107 r
= (r
+ (76 - gray
) > 255 ? 255 : r
+ (76 - gray
));
108 g
= (g
+ (76 - gray
) > 255 ? 255 : g
+ (76 - gray
));
109 b
= (b
+ (76 - gray
) > 255 ? 255 : b
+ (76 - gray
));
111 color
.setRgb(r
, g
, b
);
117 PrettyPopupMenu::setMinimumSize(const QSize
& s
)
119 KMenu::setMinimumSize(s
.width() + s_sidePixmap
.width(), s
.height());
123 PrettyPopupMenu::setMaximumSize(const QSize
& s
)
125 KMenu::setMaximumSize(s
.width() + s_sidePixmap
.width(), s
.height());
129 PrettyPopupMenu::setMinimumSize(int w
, int h
)
131 KMenu::setMinimumSize(w
+ s_sidePixmap
.width(), h
);
135 PrettyPopupMenu::setMaximumSize(int w
, int h
)
137 KMenu::setMaximumSize(w
+ s_sidePixmap
.width(), h
);
140 void PrettyPopupMenu::resizeEvent(QResizeEvent
* e
)
142 KMenu::resizeEvent( e
);
145 setFrameRect( QStyle::visualRect( QRect( s_sidePixmap
.width(), 0,
146 width() - s_sidePixmap
.width(), height() ), this ) );
150 //Workaround Qt3.3.x sizing bug, by ensuring we're always wide enough.
151 void PrettyPopupMenu::resize( int width
, int height
)
153 width
= qMax(width
, maximumSize().width());
154 KMenu::resize(width
, height
);
158 PrettyPopupMenu::paintEvent( QPaintEvent
* e
)
160 generateSidePixmap();
163 p
.setClipRegion(e
->region());
165 QStyleOptionFrame frOpt
;
167 frOpt
.lineWidth
= frameWidth();
168 frOpt
.midLineWidth
= 0;
169 style()->drawPrimitive( QStyle::PE_FrameMenu
, &frOpt
, &p
, this);
171 QRect r
= sideImageRect();
172 r
.setBottom( r
.bottom() - s_sidePixmap
.height() );
173 if ( r
.intersects( e
->rect() ) )
175 QRect drawRect
= r
.intersect( e
->rect() ).intersect( sideImageRect() );
176 QRect pixRect
= drawRect
;
177 pixRect
.moveBy( -r
.left(), -r
.top() );
178 p
.drawImage( drawRect
.topLeft(), s_sidePixmap
, pixRect
);
181 KMenu::paintEvent( e
);
183 p
.setClipRegion( e
->region() );
187 #include "prettypopupmenu.moc"