SVN_SILENT made messages (.desktop file)
[kdegames.git] / ksame / renderer.cpp
blobb2b0f986eab02a9f5ca051e35d2b17f5dd3be582
1 /*******************************************************************
3 * Copyright (C) 2006 Dmitry Suzdalev <dimsuz@gmail.com>
4 * Copyright (C) 2006 Henrique Pinto <henrique.pinto@kdemail.net>
5 *
6 * This file is part of the KDE project
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
23 ********************************************************************/
24 #include "renderer.h"
26 #include <kdebug.h>
28 #include <QtGui/QPainter>
29 #include <QtCore/QtDebug>
31 KSame::Renderer::Renderer( const QString & fileName, const QSize& backgroundSize, const QSize& elementSize, QObject *parent )
32 : QObject( parent ), m_svgRenderer( fileName ), m_backgroundSize( backgroundSize ), m_elementSize( elementSize )
36 KSame::Renderer::~Renderer()
40 void KSame::Renderer::setElementSize( const QSize& size )
42 /* We check if the size is different than the current one,
43 * in order not to have to re-render images of the same size.
45 if ( size != m_elementSize )
47 // Invalidate the cache
48 m_elementCache.clear();
49 m_highlightedElementCache.clear();
50 // Set the new size
51 m_elementSize = size;
55 void KSame::Renderer::setBackgroundSize( const QSize& size )
57 /* We check if the size is different than the current one,
58 * in order not to have to re-render images of the same size.
60 if ( size != m_backgroundSize )
62 // Invalidate the cache
63 m_cachedBackground = QPixmap();
64 // Set the new size
65 m_backgroundSize = size;
69 QPixmap KSame::Renderer::renderElement( const QString& elementId )
71 // Check if the element is already in the cache
72 if ( !m_elementCache.contains( elementId ) )
74 // If it's not, render it and add it to the cache
75 kDebug() << "Rendering" << elementId << ". Size:" << m_elementSize;
76 /* Reason for QImage::Format_ARGB32_Premultiplied:
78 * "It depends what you need. If you need a pure argb32 format then you
79 * picked correctly. Premultiplied is going to be faster (on X11 even more
80 * so because on XRender we use premultiplied colors). So if you can
81 * (meaning if the underlying code can deal with premultplied alpha ) try
82 * to use premultiplied argb32, it's going to be quite a bit faster.
84 * Zack"
86 QImage baseImage( m_elementSize, QImage::Format_ARGB32_Premultiplied );
87 // Initialize the image. It contaings garbage by default
88 baseImage.fill( 0 );
89 // Render the SVG element onto the image
90 QPainter p( &baseImage );
91 m_svgRenderer.render( &p, elementId );
92 p.end();
93 // Convert the image to a pixmap
94 QPixmap renderedElement = QPixmap::fromImage( baseImage );
95 // Add it to the cache
96 m_elementCache[ elementId ] = renderedElement;
98 // Return the pixmap from the cache
99 return m_elementCache[ elementId ];
102 QPixmap KSame::Renderer::renderHighlightedElement( const QString& elementId )
104 if ( !m_highlightedElementCache.contains( elementId ) )
106 kDebug() << "Rendering highlighted" << elementId << ". Size:" << m_elementSize;
107 QPixmap highlightedPixmap = renderElement( elementId );
108 QPainter p( &highlightedPixmap );
109 m_svgRenderer.render( &p, "stone_highlighted" );
110 p.end();
111 m_highlightedElementCache[ elementId ] = highlightedPixmap;
113 return m_highlightedElementCache[ elementId ];
116 QPixmap KSame::Renderer::renderBackground()
118 // Is the background in cache?
119 if ( m_cachedBackground.isNull() )
121 // No, so let's render it
122 kDebug() << "Rendering the background. Size:" << m_backgroundSize;
123 m_cachedBackground = QPixmap( m_backgroundSize );
124 QPainter p( &m_cachedBackground );
125 m_svgRenderer.render( &p, "background" );
126 p.end();
128 // Return the background from the cache
129 return m_cachedBackground;
132 #include "renderer.moc"