make MemoryQueryMaker use new matcher classes instead of old private ones
[amarok.git] / src / prettypopupmenu.cpp
blob10667ee5f8e3e8241f901d334246bc6307fffaa4
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 <KIconEffect>
26 #include <KStandardDirs>
28 #include <QPainter>
29 #include <QPaintEvent>
30 #include <QPixmap>
31 #include <QResizeEvent>
32 #include <QStyle>
33 #include <QStyleOptionFrame>
36 QImage PrettyPopupMenu::s_sidePixmap;
37 QColor PrettyPopupMenu::s_sidePixmapColor;
39 ////////////////////////////////////////////////////////////////////////////////
40 // public
41 ////////////////////////////////////////////////////////////////////////////////
43 PrettyPopupMenu::PrettyPopupMenu( QWidget* parent )
44 : KMenu( parent )
46 // Must be initialized so that we know the size on first invocation
47 if ( s_sidePixmap.isNull() )
48 generateSidePixmap();
52 ////////////////////////////////////////////////////////////////////////////////
53 // private
54 ////////////////////////////////////////////////////////////////////////////////
56 void
57 PrettyPopupMenu::generateSidePixmap()
59 const QColor newColor = calcPixmapColor();
61 if ( newColor != s_sidePixmapColor ) {
62 s_sidePixmapColor = newColor;
63 s_sidePixmap.load( KStandardDirs::locate( "data","amarok/images/menu_sidepixmap.png" ) );
64 KIconEffect::colorize( s_sidePixmap, newColor, 1.0 );
68 QRect
69 PrettyPopupMenu::sideImageRect() const
71 return QStyle::visualRect( layoutDirection(), rect(), QRect( frameWidth(), frameWidth(),
72 s_sidePixmap.width(), height() - 2*frameWidth() ) );
75 QColor
76 PrettyPopupMenu::calcPixmapColor()
78 KConfigGroup cg = KGlobal::config()->group("WM");
79 QColor color = QApplication::palette().active().highlight();
80 // QColor activeTitle = QApplication::palette().active().background();
81 // QColor inactiveTitle = QApplication::palette().inactive().background();
82 QColor activeTitle = cg.readEntry("activeBackground", color);
83 QColor inactiveTitle = cg.readEntry("inactiveBackground", color);
85 // figure out which color is most suitable for recoloring to
86 int h1, s1, v1, h2, s2, v2, h3, s3, v3;
87 activeTitle.hsv(&h1, &s1, &v1);
88 inactiveTitle.hsv(&h2, &s2, &v2);
89 QApplication::palette().active().background().hsv(&h3, &s3, &v3);
91 if ( (qAbs(h1-h3)+qAbs(s1-s3)+qAbs(v1-v3) < qAbs(h2-h3)+qAbs(s2-s3)+qAbs(v2-v3)) &&
92 ((qAbs(h1-h3)+qAbs(s1-s3)+qAbs(v1-v3) < 32) || (s1 < 32)) && (s2 > s1))
93 color = inactiveTitle;
94 else
95 color = activeTitle;
97 // limit max/min brightness
98 int r, g, b;
99 color.rgb(&r, &g, &b);
100 int gray = qGray(r, g, b);
101 if (gray > 180) {
102 r = (r - (gray - 180) < 0 ? 0 : r - (gray - 180));
103 g = (g - (gray - 180) < 0 ? 0 : g - (gray - 180));
104 b = (b - (gray - 180) < 0 ? 0 : b - (gray - 180));
105 } else if (gray < 76) {
106 r = (r + (76 - gray) > 255 ? 255 : r + (76 - gray));
107 g = (g + (76 - gray) > 255 ? 255 : g + (76 - gray));
108 b = (b + (76 - gray) > 255 ? 255 : b + (76 - gray));
110 color.setRgb(r, g, b);
112 return color;
115 void
116 PrettyPopupMenu::setMinimumSize(const QSize & s)
118 KMenu::setMinimumSize(s.width() + s_sidePixmap.width(), s.height());
121 void
122 PrettyPopupMenu::setMaximumSize(const QSize & s)
124 KMenu::setMaximumSize(s.width() + s_sidePixmap.width(), s.height());
127 void
128 PrettyPopupMenu::setMinimumSize(int w, int h)
130 KMenu::setMinimumSize(w + s_sidePixmap.width(), h);
133 void
134 PrettyPopupMenu::setMaximumSize(int w, int h)
136 KMenu::setMaximumSize(w + s_sidePixmap.width(), h);
139 void PrettyPopupMenu::resizeEvent(QResizeEvent * e)
141 KMenu::resizeEvent( e );
143 #if 0
144 setFrameRect( QStyle::visualRect( QRect( s_sidePixmap.width(), 0,
145 width() - s_sidePixmap.width(), height() ), this ) );
146 #endif
149 //Workaround Qt3.3.x sizing bug, by ensuring we're always wide enough.
150 void PrettyPopupMenu::resize( int width, int height )
152 width = qMax(width, maximumSize().width());
153 KMenu::resize(width, height);
156 void
157 PrettyPopupMenu::paintEvent( QPaintEvent* e )
159 generateSidePixmap();
161 QPainter p( this );
162 p.setClipRegion(e->region());
164 QStyleOptionFrame frOpt;
165 frOpt.init(this);
166 frOpt.lineWidth = frameWidth();
167 frOpt.midLineWidth = 0;
168 style()->drawPrimitive( QStyle::PE_FrameMenu, &frOpt, &p, this);
170 QRect r = sideImageRect();
171 r.setBottom( r.bottom() - s_sidePixmap.height() );
172 if ( r.intersects( e->rect() ) )
174 QRect drawRect = r.intersect( e->rect() ).intersect( sideImageRect() );
175 QRect pixRect = drawRect;
176 pixRect.moveBy( -r.left(), -r.top() );
177 p.drawImage( drawRect.topLeft(), s_sidePixmap, pixRect );
180 KMenu::paintEvent( e );
182 p.setClipRegion( e->region() );
186 #include "prettypopupmenu.moc"