Changeset View
Changeset View
Standalone View
Standalone View
renderer.cpp
Show All 33 Lines | 33 | { | |||
---|---|---|---|---|---|
34 | KgThemeProvider* prov = new KgThemeProvider; | 34 | KgThemeProvider* prov = new KgThemeProvider; | ||
35 | prov->discoverThemes( | 35 | prov->discoverThemes( | ||
36 | "appdata", QStringLiteral("themes"), //theme file location | 36 | "appdata", QStringLiteral("themes"), //theme file location | ||
37 | QStringLiteral("default") //default theme file name | 37 | QStringLiteral("default") //default theme file name | ||
38 | ); | 38 | ); | ||
39 | return prov; | 39 | return prov; | ||
40 | } | 40 | } | ||
41 | 41 | | |||
42 | /** | ||||
43 | * Constructor. | ||||
44 | * @param fileName path to SVG containing game graphics | ||||
yurchor: This line does not belong here. | |||||
45 | */ | ||||
42 | KBounceRenderer::KBounceRenderer() : KGameRenderer(provider()), m_backgroundSize( QSize( 0, 0 ) ), m_useRandomBackgrounds(false) | 46 | KBounceRenderer::KBounceRenderer() : KGameRenderer(provider()), m_backgroundSize( QSize( 0, 0 ) ), m_useRandomBackgrounds(false) | ||
43 | { | 47 | { | ||
44 | } | 48 | } | ||
45 | 49 | | |||
50 | /** | ||||
51 | * Destructor. | ||||
52 | */ | ||||
46 | KBounceRenderer::~KBounceRenderer() | 53 | KBounceRenderer::~KBounceRenderer() | ||
47 | { | 54 | { | ||
48 | } | 55 | } | ||
49 | 56 | | |||
57 | /** | ||||
58 | * Set s the path were custom background pictures are located. | ||||
59 | * @see render.h | ||||
60 | */ | ||||
50 | void KBounceRenderer::setCustomBackgroundPath(const QString& path) | 61 | void KBounceRenderer::setCustomBackgroundPath(const QString& path) | ||
51 | { | 62 | { | ||
52 | m_useRandomBackgrounds = !path.isEmpty(); | 63 | m_useRandomBackgrounds = !path.isEmpty(); | ||
53 | m_customBackgroundPath = path; | 64 | m_customBackgroundPath = path; | ||
54 | m_cachedBackground = QPixmap(); | 65 | m_cachedBackground = QPixmap(); | ||
55 | } | 66 | } | ||
56 | 67 | | |||
68 | /** | ||||
69 | * Sets Background size and invalidates background cache | ||||
70 | * @see render.h | ||||
71 | */ | ||||
57 | void KBounceRenderer::setBackgroundSize( const QSize& size ) | 72 | void KBounceRenderer::setBackgroundSize( const QSize& size ) | ||
58 | { | 73 | { | ||
59 | if (size != m_backgroundSize ) | 74 | if (size != m_backgroundSize) | ||
60 | { | 75 | { | ||
61 | m_backgroundSize = size; | 76 | m_backgroundSize = size; | ||
62 | if ( m_useRandomBackgrounds && !m_cachedBackground.isNull() ) | 77 | if (m_useRandomBackgrounds && !m_cachedBackground.isNull()) | ||
63 | { | | |||
64 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); | 78 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); | ||
65 | } | | |||
66 | else | 79 | else | ||
67 | { | | |||
68 | m_cachedBackground = QPixmap(); | 80 | m_cachedBackground = QPixmap(); | ||
69 | } | 81 | } | ||
70 | } | 82 | } | ||
71 | } | | |||
72 | 83 | | |||
84 | /** | ||||
85 | * Load a new background image | ||||
86 | * @see render.h | ||||
87 | */ | ||||
73 | bool KBounceRenderer::loadNewBackgroundPixmap() | 88 | bool KBounceRenderer::loadNewBackgroundPixmap() | ||
74 | { | 89 | { | ||
75 | bool backgroundFound = false; | 90 | bool backgroundFound = false; | ||
76 | if ( !m_customBackgroundPath.isEmpty() ) | 91 | if ( !m_customBackgroundPath.isEmpty() ) | ||
77 | { | 92 | { | ||
78 | m_randomBackground = getRandomBackgroundPixmap(m_customBackgroundPath); | 93 | m_randomBackground = getRandomBackgroundPixmap(m_customBackgroundPath); | ||
79 | if ( !m_randomBackground.isNull() ) | 94 | if ( !m_randomBackground.isNull() ) | ||
80 | { | 95 | { | ||
81 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); | 96 | m_cachedBackground = m_randomBackground.scaled(m_backgroundSize,Qt::IgnoreAspectRatio); | ||
82 | backgroundFound = true; | 97 | backgroundFound = true; | ||
83 | } | 98 | } | ||
84 | } | 99 | } | ||
85 | return backgroundFound; | 100 | return backgroundFound; | ||
86 | } | 101 | } | ||
87 | 102 | | |||
88 | 103 | /** | |||
104 | * Renders background to QPixmap of size set by setBachgroundSize | ||||
105 | * Background pixmap is cached (setBackgroundSize() invalidates the cache) | ||||
106 | * @see render.h | ||||
107 | */ | ||||
89 | QPixmap KBounceRenderer::renderBackground() | 108 | QPixmap KBounceRenderer::renderBackground() | ||
90 | { | 109 | { | ||
91 | if (m_cachedBackground.isNull() && !m_backgroundSize.isNull()) | 110 | if (m_cachedBackground.isNull() && !m_backgroundSize.isNull()) | ||
92 | { | 111 | { | ||
93 | //This is a dirty fix to the qt's m_svgRenderer.render() method that | 112 | //This is a dirty fix to the qt's m_svgRenderer.render() method that | ||
94 | //leaves an garbage-filled border of a pixmap | 113 | //leaves an garbage-filled border of a pixmap | ||
95 | qCDebug(KBOUNCE_LOG) << "Rendering the background. Size:" << m_backgroundSize; | 114 | qCDebug(KBOUNCE_LOG) << "Rendering the background. Size:" << m_backgroundSize; | ||
96 | if ( m_useRandomBackgrounds && loadNewBackgroundPixmap() ) | 115 | if ( m_useRandomBackgrounds && loadNewBackgroundPixmap() ) | ||
97 | { | | |||
98 | return m_cachedBackground; | 116 | return m_cachedBackground; | ||
99 | } | 117 | | ||
100 | // If no valid backgound pixmap found use the original from theme ... | 118 | // If no valid backgound pixmap found use the original from theme ... | ||
101 | m_cachedBackground = spritePixmap( QStringLiteral("background"), m_backgroundSize ); | 119 | m_cachedBackground = spritePixmap( QStringLiteral("background"), m_backgroundSize ); | ||
102 | } | 120 | } | ||
103 | return m_cachedBackground; | 121 | return m_cachedBackground; | ||
104 | } | 122 | } | ||
105 | 123 | | |||
124 | /** | ||||
125 | * Returns a random pixmap from the custom background path. | ||||
126 | * If no picture is located in this path the pixmap is null. | ||||
127 | * @see render.h | ||||
128 | */ | ||||
106 | QPixmap KBounceRenderer::getRandomBackgroundPixmap(const QString& path) | 129 | QPixmap KBounceRenderer::getRandomBackgroundPixmap(const QString& path) | ||
107 | { | 130 | { | ||
108 | // list directory | 131 | // list directory | ||
109 | QDir dir( path, QStringLiteral("*.png *.jpg"), QDir::Name|QDir::IgnoreCase, QDir::Files ); | 132 | QDir dir( path, QStringLiteral("*.png *.jpg"), QDir::Name|QDir::IgnoreCase, QDir::Files ); | ||
110 | if ( !dir.exists() ) { | 133 | if ( !dir.exists() ) { | ||
111 | qCDebug(KBOUNCE_LOG) << "CustomBackground Directory not found" << endl; | 134 | qCDebug(KBOUNCE_LOG) << "CustomBackground Directory not found" << endl; | ||
112 | return QPixmap(); | 135 | return QPixmap(); | ||
113 | } | 136 | } | ||
114 | 137 | | |||
115 | if ( dir.count() > 1 ) | 138 | if ( dir.count() > 1 ) | ||
116 | { | 139 | { | ||
117 | QString filename; | 140 | QString filename; | ||
118 | // return random pixmap | 141 | // return random pixmap | ||
119 | uint pos = KRandom::random() % dir.count(); | 142 | uint pos = KRandom::random() % dir.count(); | ||
120 | if ( pos < dir.count() ) | 143 | if ( pos < dir.count() ) | ||
121 | { | | |||
122 | filename = dir.absoluteFilePath( dir[pos] ); | 144 | filename = dir.absoluteFilePath( dir[pos] ); | ||
123 | } | | |||
124 | 145 | | |||
125 | if (!filename.isEmpty() && QFile(filename).exists()) | 146 | if (!filename.isEmpty() && QFile(filename).exists()) | ||
126 | { | | |||
127 | return QPixmap(filename); | 147 | return QPixmap(filename); | ||
148 | else | ||||
149 | return QPixmap(); | ||||
128 | } | 150 | } | ||
129 | else return QPixmap(); | 151 | else | ||
130 | } | 152 | if ( dir.count() == 1 ) | ||
131 | else if ( dir.count() == 1 ) | | |||
132 | { | | |||
133 | return QPixmap( dir.absoluteFilePath(dir[0]) ); | 153 | return QPixmap( dir.absoluteFilePath(dir[0]) ); | ||
134 | } | 154 | else | ||
135 | else return QPixmap(); | 155 | return QPixmap(); | ||
136 | } | 156 | } |
This line does not belong here.