1 | /* |
2 | * Copyright (C) 2006 Dmitry Suzdalev <dimsuz@gmail.com> |
3 | * Copyright (C) 2007 Tomasz Boczkowski <tboczkowski@onet.pl> |
4 | * |
5 | * This file is part of the KDE project "KBounce" |
6 | * |
7 | * KBounce is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Library General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2 of the License, or (at your option) any later version. |
11 | * |
12 | * KBounce is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Library General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public |
18 | * License along with KBounce; if not, write to the Free |
19 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | * Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "renderer.h" |
24 | #include "settings.h" |
25 | |
26 | #include <kdebug.h> |
27 | |
28 | #include <QtGui/QApplication> |
29 | #include <QtGui/QPainter> |
30 | #include <QtGui/QPalette> |
31 | #include <QDir> |
32 | #include <krandom.h> |
33 | #include <KgThemeProvider> |
34 | |
35 | static KgThemeProvider* provider() |
36 | { |
37 | KgThemeProvider* prov = new KgThemeProvider; |
38 | prov->discoverThemes( |
39 | "appdata" , QLatin1String("themes" ), //theme file location |
40 | QLatin1String("default" ) //default theme file name |
41 | ); |
42 | return prov; |
43 | } |
44 | |
45 | KBounceRenderer::KBounceRenderer() : KGameRenderer(provider()), m_backgroundSize( QSize( 0, 0 ) ) ,m_useRandomBackgrounds(false) |
46 | { |
47 | } |
48 | |
49 | KBounceRenderer::~KBounceRenderer() |
50 | { |
51 | } |
52 | |
53 | void KBounceRenderer::setCustomBackgroundPath(const QString& path) |
54 | { |
55 | m_useRandomBackgrounds = !path.isEmpty(); |
56 | m_customBackgroundPath = path; |
57 | m_cachedBackground = QPixmap(); |
58 | } |
59 | |
60 | void KBounceRenderer::setBackgroundSize( const QSize& size ) |
61 | { |
62 | if (size != m_backgroundSize ) |
63 | { |
64 | m_backgroundSize = size; |
65 | if ( m_useRandomBackgrounds && !m_cachedBackground.isNull() ) |
66 | { |
67 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); |
68 | } |
69 | else |
70 | { |
71 | m_cachedBackground = QPixmap(); |
72 | } |
73 | } |
74 | } |
75 | |
76 | bool KBounceRenderer::loadNewBackgroundPixmap() |
77 | { |
78 | bool backgroundFound = false; |
79 | if ( !m_customBackgroundPath.isEmpty() ) |
80 | { |
81 | m_randomBackground = getRandomBackgroundPixmap(m_customBackgroundPath); |
82 | if ( !m_randomBackground.isNull() ) |
83 | { |
84 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); |
85 | backgroundFound = true; |
86 | } |
87 | } |
88 | return backgroundFound; |
89 | } |
90 | |
91 | |
92 | QPixmap KBounceRenderer::renderBackground() |
93 | { |
94 | if (m_cachedBackground.isNull() && !m_backgroundSize.isNull()) |
95 | { |
96 | //This is a dirty fix to the qt's m_svgRenderer.render() method that |
97 | //leaves an garbage-filled border of a pixmap |
98 | kDebug() << "Rendering the background. Size:" << m_backgroundSize; |
99 | if ( m_useRandomBackgrounds && loadNewBackgroundPixmap() ) |
100 | { |
101 | return m_cachedBackground; |
102 | } |
103 | // If no valid backgound pixmap found use the original from theme ... |
104 | m_cachedBackground = spritePixmap( "background" , m_backgroundSize ); |
105 | } |
106 | return m_cachedBackground; |
107 | } |
108 | |
109 | QPixmap KBounceRenderer::getRandomBackgroundPixmap(const QString& path) |
110 | { |
111 | // list directory |
112 | QDir dir( path, "*.png *.jpg" , QDir::Name|QDir::IgnoreCase, QDir::Files ); |
113 | if ( !dir.exists() ) { |
114 | kDebug() << "CustomBackground Directory not found" << endl; |
115 | return QPixmap(); |
116 | } |
117 | |
118 | if ( dir.count() > 1 ) |
119 | { |
120 | QString filename; |
121 | // return random pixmap |
122 | uint pos = KRandom::random() % dir.count(); |
123 | if ( pos < dir.count() ) |
124 | { |
125 | filename = dir.absoluteFilePath( dir[pos] ); |
126 | } |
127 | |
128 | if (!filename.isEmpty() && QFile(filename).exists()) |
129 | { |
130 | return QPixmap(filename); |
131 | } |
132 | else return QPixmap(); |
133 | } |
134 | else if ( dir.count() == 1 ) |
135 | { |
136 | return QPixmap( dir.absoluteFilePath(dir[0]) ); |
137 | } |
138 | else return QPixmap(); |
139 | } |
140 | |
141 | |