File indexing completed on 2024-04-21 04:47:53
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org> * 0003 * Copyright (c) 2007 Mark Kretschmann <kretschmann@kde.org> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #include "SvgTinter.h" 0019 0020 #include "App.h" 0021 #include "core/support/Debug.h" 0022 0023 #include <QBuffer> 0024 #include <QFile> 0025 0026 #include <KCompressionDevice> 0027 0028 SvgTinter * SvgTinter::s_instance = nullptr; 0029 0030 SvgTinter::SvgTinter() 0031 : m_firstRun( true ) 0032 { 0033 init(); 0034 m_firstRun = false; 0035 } 0036 0037 0038 SvgTinter::~SvgTinter() 0039 {} 0040 0041 QByteArray 0042 SvgTinter::tint( const QString &filename ) 0043 { 0044 QFile file( filename ); 0045 if ( !file.open( QIODevice::ReadOnly ) ) 0046 { 0047 error() << "Unable to open file: " << filename; 0048 return QByteArray(); 0049 } 0050 0051 QByteArray svg_source( file.readAll() ); 0052 0053 // Copied from KSvgrenderer.cpp as we don't load it directly. 0054 if (!svg_source.startsWith("<?xml")) 0055 { 0056 QBuffer buf( &svg_source ); 0057 QIODevice *flt = new KCompressionDevice( &buf, false, KCompressionDevice::GZip ); 0058 if (!flt) 0059 return QByteArray(); 0060 if (!flt->open(QIODevice::ReadOnly)) 0061 { 0062 delete flt; 0063 return QByteArray(); 0064 } 0065 svg_source = flt->readAll(); 0066 delete flt; 0067 } 0068 0069 // QString svg_string( svg_source ); 0070 QHashIterator<QByteArray, QString> tintIter( m_tintMap ); 0071 while( tintIter.hasNext() ) 0072 { 0073 tintIter.next(); 0074 svg_source.replace( tintIter.key(), tintIter.value().toLocal8Bit() ); 0075 } 0076 return svg_source; 0077 } 0078 0079 void 0080 SvgTinter::init() 0081 { 0082 if ( m_lastPalette != pApp->palette() || m_firstRun ) { 0083 m_tintMap.insert( "#666765", pApp->palette().window().color().name() ); 0084 //insert a color for bright ( highlight color ) 0085 m_tintMap.insert( "#66ffff", pApp->palette().highlight().color().name() ); 0086 //a slightly lighter than window color: 0087 m_tintMap.insert( "#e8e8e8", blendColors( pApp->palette().window().color(), "#ffffff", 90 ).name() ); 0088 //a slightly darker than window color: 0089 m_tintMap.insert( "#565755", blendColors( pApp->palette().window().color(), "#000000", 90 ).name() ); 0090 0091 //list background: 0092 #ifdef Q_WS_MAC 0093 m_tintMap.insert( "#f0f0f0", blendColors( pApp->palette().window().color(), "#000000", 90 ).name() ); 0094 m_tintMap.insert( "#ffffff", blendColors( pApp->palette().window().color(), "#000000", 98 ).name() ); 0095 #else 0096 m_tintMap.insert( "#f0f0f0", pApp->palette().base().color().name() ); 0097 #endif 0098 0099 //alternate list background: 0100 m_tintMap.insert( "#e0e0e0", pApp->palette().alternateBase().color().name() ); 0101 0102 //highlight/window mix: 0103 m_tintMap.insert( "#123456", blendColors( pApp->palette().window().color(), pApp->palette().highlight().color().name(), 80 ).name() ); 0104 0105 //text color, useful for adding contrast 0106 m_tintMap.insert( "#010101", pApp->palette().text().color().name() ); 0107 0108 m_lastPalette = pApp->palette(); 0109 } 0110 } 0111 0112 QColor 0113 SvgTinter::blendColors( const QColor& color1, const QColor& color2, int percent ) 0114 { 0115 const float factor1 = ( float ) percent / 100; 0116 const float factor2 = ( 100 - ( float ) percent ) / 100; 0117 0118 const int r = static_cast<int>( color1.red() * factor1 + color2.red() * factor2 ); 0119 const int g = static_cast<int>( color1.green() * factor1 + color2.green() * factor2 ); 0120 const int b = static_cast<int>( color1.blue() * factor1 + color2.blue() * factor2 ); 0121 0122 QColor result; 0123 result.setRgb( r, g, b ); 0124 0125 return result; 0126 } 0127 0128 namespace The { 0129 SvgTinter* 0130 svgTinter() 0131 { 0132 if ( SvgTinter::s_instance == nullptr ) 0133 SvgTinter::s_instance = new SvgTinter(); 0134 0135 return SvgTinter::s_instance; 0136 } 0137 } 0138 0139 0140