1 /****************************************************************************************
2  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "PopupDropperFactory.h"
18 
19 #include "MainWindow.h"
20 #include "PaletteHandler.h"
21 #include "SvgHandler.h"
22 #include "core/support/Debug.h"
23 #include "context/popupdropper/libpud/PopupDropperItem.h"
24 
25 #include <QAction>
26 
27 
28 namespace The
29 {
30     static PopupDropperFactory* s_PopupDropperFactory_instance = 0;
31 
popupDropperFactory()32     PopupDropperFactory* popupDropperFactory()
33     {
34         if( !s_PopupDropperFactory_instance )
35             s_PopupDropperFactory_instance = new PopupDropperFactory( The::mainWindow() );
36 
37         return s_PopupDropperFactory_instance;
38     }
39 }
40 
41 
PopupDropperFactory(QObject * parent)42 PopupDropperFactory::PopupDropperFactory( QObject* parent )
43     : QObject( parent )
44 {}
45 
46 
~PopupDropperFactory()47 PopupDropperFactory::~PopupDropperFactory()
48 {
49     DEBUG_BLOCK
50 }
51 
52 
createPopupDropper(QWidget * parent,bool ignoreEmptyParent)53 PopupDropper * PopupDropperFactory::createPopupDropper( QWidget * parent, bool ignoreEmptyParent )
54 {
55     DEBUG_BLOCK
56 
57     // Lazy loading of widgets not currently shown in layout means that parent could be zero
58     // if this happens, it pops up in its own window -- so detect this
59     // ignoreEmptyParent is for creating submenus, where you set the initial parent to zero
60     if( !parent && !ignoreEmptyParent )
61         return 0;
62 
63     PopupDropper* pd = new PopupDropper( parent );
64     if( !pd )
65         return 0;
66 
67     pd->setSvgRenderer( The::svgHandler()->getRenderer( "amarok/images/pud_items.svg" ) );
68     pd->setQuitOnDragLeave( false );
69     pd->setFadeInTime( 500 );
70     pd->setFadeOutTime( 300 );
71     //QColor origWindowColor( The::paletteHandler()->palette().color( QPalette::Window ) );
72     //QColor windowColor;
73     //windowColor.setRed( 255 - origWindowColor.red() );
74     //windowColor.setBlue( 255 - origWindowColor.blue() );
75     //windowColor.setGreen( 255 - origWindowColor.green() );
76     QColor windowColor( The::paletteHandler()->palette().color( QPalette::Base ) );
77     windowColor.setAlpha( 200 );
78     QColor textColor( The::paletteHandler()->palette().color( QPalette::Link ) );
79     QColor highlightedTextColor( The::paletteHandler()->palette().color( QPalette::Text ) );
80     QColor borderColor( The::paletteHandler()->palette().color( QPalette::Text ) );
81     QColor fillColor( borderColor );
82     fillColor.setAlpha( 48 );
83     pd->setColors( windowColor, textColor, highlightedTextColor, borderColor, fillColor );
84 
85     return pd;
86 }
87 
createItem(QAction * action)88 PopupDropperItem * PopupDropperFactory::createItem( QAction * action )
89 {
90     PopupDropperItem* pdi = new PopupDropperItem();
91     pdi->setAction( action );
92     QString text = pdi->text();
93     text.remove( QChar('&') );
94     pdi->setText( text );
95     adjustItem( pdi );
96     return pdi;
97 }
98 
adjustItem(PopupDropperItem * item)99 void PopupDropperFactory::adjustItem( PopupDropperItem *item )
100 {
101     if( !item )
102         return;
103     QFont font;
104     font.setPointSize( 16 );
105     font.setBold( true );
106     item->setFont( font );
107     item->setHoverMsecs( 800 );
108     QColor hoverIndicatorFillColor( The::paletteHandler()->palette().color( QPalette::Highlight ) );
109     hoverIndicatorFillColor.setAlpha( 96 );
110     QBrush brush = item->hoverIndicatorFillBrush();
111     brush.setColor( hoverIndicatorFillColor );
112     item->setHoverIndicatorFillBrush( brush );
113 
114     if( item->isSubmenuTrigger() )
115         item->setHoverIndicatorShowStyle( PopupDropperItem::OnHover );
116 }
117 
adjustItems(PopupDropper * pud)118 void PopupDropperFactory::adjustItems( PopupDropper* pud )
119 {
120     if( !pud )
121         return;
122     pud->forEachItem( adjustItemCallback );
123 }
124 
adjustItemCallback(void * pdi)125 void PopupDropperFactory::adjustItemCallback( void *pdi )
126 {
127     The::popupDropperFactory()->adjustItem( (PopupDropperItem*)pdi );
128 }
129 
130