File indexing completed on 2024-04-21 04:47:52
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #define DEBUG_PREFIX "PaletteHandler" 0018 0019 #include "PaletteHandler.h" 0020 0021 #include <QAbstractItemView> 0022 #include <QPainter> 0023 0024 0025 namespace The { 0026 static PaletteHandler* s_PaletteHandler_instance = nullptr; 0027 0028 PaletteHandler* paletteHandler() 0029 { 0030 if( !s_PaletteHandler_instance ) 0031 s_PaletteHandler_instance = new PaletteHandler(); 0032 0033 return s_PaletteHandler_instance; 0034 } 0035 } 0036 0037 0038 PaletteHandler::PaletteHandler( QObject* parent ) 0039 : QObject( parent ) 0040 {} 0041 0042 0043 PaletteHandler::~PaletteHandler() 0044 { 0045 The::s_PaletteHandler_instance = nullptr; 0046 } 0047 0048 void 0049 PaletteHandler::setPalette( const QPalette & palette ) 0050 { 0051 m_palette = palette; 0052 Q_EMIT( newPalette( m_palette ) ); 0053 } 0054 0055 void 0056 PaletteHandler::updateItemView( QAbstractItemView * view ) 0057 { 0058 QPalette p = m_palette; 0059 QColor c; 0060 0061 // Widgets with keyboard focus become slightly transparent 0062 c = p.color( QPalette::Active, QPalette::AlternateBase ); 0063 c.setAlpha( 95 ); 0064 p.setColor( QPalette::Active, QPalette::AlternateBase, c ); 0065 0066 // For widgets that don't have keyboard focus reduce the opacity further 0067 c = p.color( QPalette::Inactive, QPalette::AlternateBase ); 0068 c.setAlpha( 75 ); 0069 p.setColor( QPalette::Inactive, QPalette::AlternateBase, c ); 0070 0071 // Base color is used during the expand/shrink animation. We set it 0072 // to transparent so that it won't interfere with our custom colors. 0073 p.setColor( QPalette::Active, QPalette::Base, Qt::transparent ); 0074 p.setColor( QPalette::Inactive, QPalette::Base, Qt::transparent ); 0075 0076 view->setPalette( p ); 0077 0078 if ( QWidget *vp = view->viewport() ) 0079 { 0080 // don't paint background - do NOT use Qt::transparent etc. 0081 vp->setAutoFillBackground( false ); 0082 vp->setBackgroundRole( QPalette::Window ); 0083 vp->setForegroundRole( QPalette::WindowText ); 0084 // erase custom viewport palettes, shall be "transparent" 0085 vp->setPalette(QPalette()); 0086 } 0087 } 0088 0089 QColor 0090 PaletteHandler::foregroundColor( const QPainter *p, bool selected ) 0091 { 0092 QPalette pal; 0093 QPalette::ColorRole fg = QPalette::WindowText; 0094 if ( p->device() && p->device()->devType() == QInternal::Widget) 0095 { 0096 QWidget *w = static_cast<QWidget*>( p->device() ); 0097 fg = w->foregroundRole(); 0098 pal = w->palette(); 0099 } 0100 else 0101 pal = palette(); 0102 0103 if( !selected ) 0104 return pal.color( QPalette::Active, fg ); 0105 0106 return pal.color( QPalette::Active, QPalette::HighlightedText ); 0107 } 0108 0109 QPalette 0110 PaletteHandler::palette() const 0111 { 0112 return m_palette; 0113 } 0114 0115 QColor 0116 PaletteHandler::highlightColor( qreal saturationPercent, qreal valuePercent ) 0117 { 0118 QColor highlight = The::paletteHandler()->palette().color( QPalette::Active, QPalette::Highlight ); 0119 qreal saturation = highlight.saturationF(); 0120 saturation *= saturationPercent; 0121 qreal value = highlight.valueF(); 0122 value *= valuePercent; 0123 if( value > 1.0 ) 0124 value = 1.0; 0125 highlight.setHsvF( highlight.hueF(), saturation, value, highlight.alphaF() ); 0126 0127 return highlight; 0128 } 0129 0130 QColor 0131 PaletteHandler::backgroundColor() 0132 { 0133 QColor base = The::paletteHandler()->palette().color( QPalette::Active, QPalette::Base ); 0134 base.setHsvF( highlightColor().hueF(), base.saturationF(), base.valueF() ); 0135 return base; 0136 } 0137 0138 QColor 0139 PaletteHandler::alternateBackgroundColor() 0140 { 0141 const QColor alternate = The::paletteHandler()->palette().color( QPalette::Active, QPalette::AlternateBase ); 0142 const QColor window = The::paletteHandler()->palette().color( QPalette::Active, QPalette::Window ); 0143 const QColor base = backgroundColor(); 0144 0145 const int alternateDist = abs( alternate.value() - base.value() ); 0146 const int windowDist = abs( window.value() - base.value() ); 0147 0148 QColor result = alternateDist > windowDist ? alternate : window; 0149 result.setHsvF( highlightColor().hueF(), highlightColor().saturationF(), result.valueF() ); 0150 return result; 0151 } 0152