File indexing completed on 2024-04-28 04:02:03
0001 /* 0002 This file is part of the KDE project "KBounce" 0003 0004 SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com> 0005 SPDX-FileCopyrightText: 2007 Tomasz Boczkowski <tboczkowski@onet.pl> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "renderer.h" 0011 #include "settings.h" 0012 #include "debug.h" 0013 0014 // KDEGames 0015 #include <KGameThemeProvider> 0016 0017 #include <QDir> 0018 #include <QRandomGenerator> 0019 0020 static KGameThemeProvider* provider() 0021 { 0022 KGameThemeProvider* prov = new KGameThemeProvider; 0023 prov->discoverThemes( 0024 QStringLiteral("themes"), //theme file location 0025 QStringLiteral("default") //default theme file name 0026 ); 0027 return prov; 0028 } 0029 0030 KBounceRenderer::KBounceRenderer() : KGameRenderer(provider()), m_backgroundSize( QSize( 0, 0 ) ), m_useRandomBackgrounds(false) 0031 { 0032 } 0033 0034 KBounceRenderer::~KBounceRenderer() 0035 { 0036 } 0037 0038 void KBounceRenderer::setCustomBackgroundPath(const QString& path) 0039 { 0040 m_useRandomBackgrounds = !path.isEmpty(); 0041 m_customBackgroundPath = path; 0042 m_cachedBackground = QPixmap(); 0043 } 0044 0045 void KBounceRenderer::setBackgroundSize( QSize size ) 0046 { 0047 if (size != m_backgroundSize ) 0048 { 0049 m_backgroundSize = size; 0050 if ( m_useRandomBackgrounds && !m_cachedBackground.isNull() ) 0051 { 0052 m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); 0053 } 0054 else 0055 { 0056 m_cachedBackground = QPixmap(); 0057 } 0058 } 0059 } 0060 0061 bool KBounceRenderer::loadNewBackgroundPixmap() 0062 { 0063 bool backgroundFound = false; 0064 if ( !m_customBackgroundPath.isEmpty() ) 0065 { 0066 m_randomBackground = getRandomBackgroundPixmap(m_customBackgroundPath); 0067 if ( !m_randomBackground.isNull() ) 0068 { 0069 m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); 0070 backgroundFound = true; 0071 } 0072 } 0073 return backgroundFound; 0074 } 0075 0076 0077 QPixmap KBounceRenderer::renderBackground() 0078 { 0079 if (m_cachedBackground.isNull() && !m_backgroundSize.isNull()) 0080 { 0081 //This is a dirty fix to the Qt's m_svgRenderer.render() method that 0082 //leaves a garbage-filled border of a pixmap 0083 qCDebug(KBOUNCE_LOG) << "Rendering the background. Size:" << m_backgroundSize; 0084 if ( m_useRandomBackgrounds && loadNewBackgroundPixmap() ) 0085 { 0086 return m_cachedBackground; 0087 } 0088 // If no valid background pixmap found use the original from theme ... 0089 m_cachedBackground = spritePixmap( QStringLiteral("background"), m_backgroundSize ); 0090 } 0091 return m_cachedBackground; 0092 } 0093 0094 QPixmap KBounceRenderer::getRandomBackgroundPixmap(const QString& path) 0095 { 0096 // list directory 0097 QDir dir( path, QStringLiteral("*.png *.jpg"), QDir::Name|QDir::IgnoreCase, QDir::Files ); 0098 if ( !dir.exists() ) { 0099 qCDebug(KBOUNCE_LOG) << "CustomBackground Directory not found"; 0100 return QPixmap(); 0101 } 0102 0103 if ( dir.count() > 1 ) 0104 { 0105 QString filename; 0106 // return random pixmap 0107 const uint pos = QRandomGenerator::global()->bounded(dir.count()); 0108 if ( pos < dir.count() ) 0109 { 0110 filename = dir.absoluteFilePath( dir[pos] ); 0111 } 0112 0113 if (!filename.isEmpty() && QFile(filename).exists()) 0114 { 0115 return QPixmap(filename); 0116 } 0117 else return QPixmap(); 0118 } 0119 else if ( dir.count() == 1 ) 0120 { 0121 return QPixmap( dir.absoluteFilePath(dir[0]) ); 0122 } 0123 else return QPixmap(); 0124 } 0125 0126 #include "moc_renderer.cpp"