some more work on collabsible albums. I think I will need to optimize the playlist...
[amarok.git] / src / prettypopupmenu.cpp
blobe5bc3c341d20f8ff8ab4c1302219910148629d62
1 /***************************************************************************
2 * Copyright (C) 1996-2000 the kicker authors. *
3 * Copyright (C) 2005 Mark Kretschmann <markey@web.de> *
4 * *
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. *
9 * *
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. *
14 * *
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>
24 #include <KConfig>
25 #include <KConfigGroup>
26 #include <KIconEffect>
27 #include <KStandardDirs>
29 #include <QPainter>
30 #include <QPaintEvent>
31 #include <QPixmap>
32 #include <QResizeEvent>
33 #include <QStyle>
34 #include <QStyleOptionFrame>
37 QImage PrettyPopupMenu::s_sidePixmap;
38 QColor PrettyPopupMenu::s_sidePixmapColor;
40 ////////////////////////////////////////////////////////////////////////////////
41 // public
42 ////////////////////////////////////////////////////////////////////////////////
44 PrettyPopupMenu::PrettyPopupMenu( QWidget* parent )
45 : KMenu( parent )
47 // Must be initialized so that we know the size on first invocation
48 if ( s_sidePixmap.isNull() )
49 generateSidePixmap();
53 ////////////////////////////////////////////////////////////////////////////////
54 // private
55 ////////////////////////////////////////////////////////////////////////////////
57 void
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 );
69 QRect
70 PrettyPopupMenu::sideImageRect() const
72 return QStyle::visualRect( layoutDirection(), rect(), QRect( frameWidth(), frameWidth(),
73 s_sidePixmap.width(), height() - 2*frameWidth() ) );
76 QColor
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;
95 else
96 color = activeTitle;
98 // limit max/min brightness
99 int r, g, b;
100 color.rgb(&r, &g, &b);
101 int gray = qGray(r, g, b);
102 if (gray > 180) {
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);
113 return color;
116 void
117 PrettyPopupMenu::setMinimumSize(const QSize & s)
119 KMenu::setMinimumSize(s.width() + s_sidePixmap.width(), s.height());
122 void
123 PrettyPopupMenu::setMaximumSize(const QSize & s)
125 KMenu::setMaximumSize(s.width() + s_sidePixmap.width(), s.height());
128 void
129 PrettyPopupMenu::setMinimumSize(int w, int h)
131 KMenu::setMinimumSize(w + s_sidePixmap.width(), h);
134 void
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 );
144 #if 0
145 setFrameRect( QStyle::visualRect( QRect( s_sidePixmap.width(), 0,
146 width() - s_sidePixmap.width(), height() ), this ) );
147 #endif
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);
157 void
158 PrettyPopupMenu::paintEvent( QPaintEvent* e )
160 generateSidePixmap();
162 QPainter p( this );
163 p.setClipRegion(e->region());
165 QStyleOptionFrame frOpt;
166 frOpt.init(this);
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"