| 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 "PaletteHandler.h" |
| 18 |
|
| 19 |
#include "App.h" |
| 20 |
#include "core/support/Debug.h" |
| 21 |
|
| 22 |
#include <kglobal.h> |
| 23 |
|
| 24 |
|
| 25 |
namespace The { |
| 26 |
static PaletteHandler* s_PaletteHandler_instance = 0; |
| 27 |
|
| 28 |
PaletteHandler* paletteHandler() |
| 29 |
{ |
| 30 |
if( !s_PaletteHandler_instance ) |
| 31 |
s_PaletteHandler_instance = new PaletteHandler(); |
| 32 |
|
| 33 |
return s_PaletteHandler_instance; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
PaletteHandler::PaletteHandler( QObject* parent ) |
| 39 |
: QObject( parent ) |
| 40 |
{} |
| 41 |
|
| 42 |
|
| 43 |
PaletteHandler::~PaletteHandler() |
| 44 |
{ |
| 45 |
DEBUG_BLOCK |
| 46 |
|
| 47 |
The::s_PaletteHandler_instance = 0; |
| 48 |
} |
| 49 |
|
| 50 |
void |
| 51 |
PaletteHandler::setPalette( const QPalette & palette ) |
| 52 |
{ |
| 53 |
m_palette = palette; |
| 54 |
emit( newPalette( m_palette ) ); |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
PaletteHandler::updateItemView( QAbstractItemView * view ) |
| 59 |
{ |
| 60 |
QPalette p = m_palette; |
| 61 |
|
| 62 |
QColor c = p.color( QPalette::Base ); |
| 63 |
c.setAlpha( 0 ); |
| 64 |
p.setColor( QPalette::Base, c ); |
| 65 |
|
| 66 |
c = p.color( QPalette::AlternateBase ); |
| 67 |
c.setAlpha( 77 ); |
| 68 |
p.setColor( QPalette::AlternateBase, c ); |
| 69 |
|
| 70 |
view->setPalette( p ); |
| 71 |
} |
| 72 |
|
| 73 |
QPalette |
| 74 |
PaletteHandler::palette() |
| 75 |
{ |
| 76 |
return m_palette; |
| 77 |
} |
| 78 |
|
| 79 |
QColor |
| 80 |
PaletteHandler::highlightColor() |
| 81 |
{ |
| 82 |
QColor highlight = App::instance()->palette().highlight().color(); |
| 83 |
qreal saturation = highlight.saturationF(); |
| 84 |
saturation *= 0.5; |
| 85 |
highlight.setHsvF( highlight.hueF(), saturation, highlight.valueF(), highlight.alphaF() ); |
| 86 |
|
| 87 |
return highlight; |
| 88 |
} |
| 89 |
|
| 90 |
QColor |
| 91 |
PaletteHandler::highlightColor( qreal saturationPercent, qreal valuePercent ) |
| 92 |
{ |
| 93 |
QColor highlight = QColor( App::instance()->palette().highlight().color() ); |
| 94 |
qreal saturation = highlight.saturationF(); |
| 95 |
saturation *= saturationPercent; |
| 96 |
qreal value = highlight.valueF(); |
| 97 |
value *= valuePercent; |
| 98 |
if( value > 1.0 ) |
| 99 |
value = 1.0; |
| 100 |
highlight.setHsvF( highlight.hueF(), saturation, value, highlight.alphaF() ); |
| 101 |
|
| 102 |
return highlight; |
| 103 |
} |
| 104 |
|
| 105 |
QColor |
| 106 |
PaletteHandler::backgroundColor() |
| 107 |
{ |
| 108 |
QColor base = App::instance()->palette().base().color(); |
| 109 |
base.setHsvF( highlightColor().hueF(), base.saturationF(), base.valueF() ); |
| 110 |
return base; |
| 111 |
} |
| 112 |
|
| 113 |
QColor |
| 114 |
PaletteHandler::alternateBackgroundColor() |
| 115 |
{ |
| 116 |
const QColor alternate = App::instance()->palette().alternateBase().color(); |
| 117 |
const QColor window = App::instance()->palette().window().color(); |
| 118 |
const QColor base = backgroundColor(); |
| 119 |
|
| 120 |
const int alternateDist = abs( alternate.value() - base.value() ); |
| 121 |
const int windowDist = abs( window.value() - base.value() ); |
| 122 |
|
| 123 |
QColor result = alternateDist > windowDist ? alternate : window; |
| 124 |
result.setHsvF( highlightColor().hueF(), highlightColor().saturationF(), result.valueF() ); |
| 125 |
return result; |
| 126 |
} |
| 127 |
|
| 128 |
#include "PaletteHandler.moc" |