1 | /*************************************************************************** |
2 | * KBlocks, a falling blocks game for KDE * |
3 | * Copyright (C) 2010 Mauricio Piacentini <mauricio@tabuleiro.com> * |
4 | * Zhongjie Cai <squall.leonhart.cai@gmail.com> * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | ***************************************************************************/ |
11 | #include "KBlocksGraphics.h" |
12 | #include <KDebug> |
13 | |
14 | #include <QPixmapCache> |
15 | #include <QPainter> |
16 | |
17 | KBlocksGraphics::KBlocksGraphics ( const QString& themeFile ) |
18 | { |
19 | m_theme = new KGameTheme(); |
20 | if (!m_theme->load(themeFile)) { |
21 | kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl; |
22 | m_theme->loadDefault(); |
23 | } |
24 | m_renderer = new QSvgRenderer(m_theme->graphics()); |
25 | readThemeValues(); |
26 | } |
27 | |
28 | KBlocksGraphics::~KBlocksGraphics() |
29 | { |
30 | delete m_theme; |
31 | delete m_renderer; |
32 | } |
33 | |
34 | bool KBlocksGraphics::loadTheme ( const QString& themeFile ) |
35 | { |
36 | if (!m_theme->load(themeFile)) { |
37 | kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl; |
38 | return false; |
39 | } |
40 | if (!m_renderer->load(m_theme->graphics())) { |
41 | kDebug(11000) << "Error loading SVG theme" << m_theme->graphics() << endl; |
42 | return false; |
43 | } |
44 | //clear the cache or pixmaps from the old theme will be returned |
45 | //QPixmapCache::clear(); |
46 | readThemeValues(); |
47 | return true; |
48 | } |
49 | |
50 | |
51 | void KBlocksGraphics::adjustForSize(const QSize& newsize) |
52 | { |
53 | Q_UNUSED(newsize) |
54 | //Reset our values |
55 | readThemeValues(); |
56 | |
57 | return; |
58 | |
59 | /* |
60 | double aspectratio; |
61 | double nw = newsize.width(); |
62 | double nh = newsize.height(); |
63 | |
64 | double origw = m_View_Size_Width; |
65 | double origh = m_View_Size_Height; |
66 | |
67 | if ((origw/origh)>(nw/nh)) { |
68 | //space will be left on height, use width as limit |
69 | aspectratio = nw/origw; |
70 | } else { |
71 | aspectratio = nh/origh; |
72 | } |
73 | //kDebug(11000) << aspectratio; |
74 | m_Block_Size = (int) (aspectratio*(qreal)m_Block_Size); |
75 | m_View_Size_Width = (int) (aspectratio*(double)m_View_Size_Width); |
76 | m_View_Size_Height = (int) (aspectratio*(double)m_View_Size_Height); |
77 | m_PlayArea_OffsetPoint_X = (int) (aspectratio*(qreal)m_PlayArea_OffsetPoint_X); |
78 | m_PlayArea_OffsetPoint_Y = (int) (aspectratio*(qreal)m_PlayArea_OffsetPoint_Y); |
79 | m_PreviewArea_CenterPoint_X = (int) (aspectratio*(qreal)m_PreviewArea_CenterPoint_X); |
80 | m_PreviewArea_CenterPoint_Y = (int) (aspectratio*(qreal)m_PreviewArea_CenterPoint_Y); |
81 | */ |
82 | } |
83 | |
84 | void KBlocksGraphics::readThemeValues() |
85 | { |
86 | //Extract values from SVG elements |
87 | QRectF bounds; |
88 | bounds = m_renderer->boundsOnElement ( "BLOCK_SIZE" ); |
89 | m_Block_Size = bounds.width(); |
90 | bounds = m_renderer->boundsOnElement ( "VIEW" ); |
91 | m_View_Size_Width = bounds.width(); |
92 | m_View_Size_Height = bounds.height(); |
93 | bounds = m_renderer->boundsOnElement ( "PLAY_AREA" ); |
94 | m_PlayArea_OffsetPoint_X = bounds.x(); |
95 | m_PlayArea_OffsetPoint_Y = bounds.y(); |
96 | m_PlayArea_NumberOfBlocks_X = bounds.width()/(double)m_Block_Size; |
97 | m_PlayArea_NumberOfBlocks_Y = bounds.height()/(double)m_Block_Size; |
98 | bounds = m_renderer->boundsOnElement ( "NEXTPIECE_AREA" ); |
99 | m_PreviewArea_CenterPoint_X = bounds.center().x(); |
100 | m_PreviewArea_CenterPoint_Y = bounds.center().y(); |
101 | } |
102 | |
103 | /* |
104 | QPixmap KBlocksGraphics::elementPixmap(int width, int height, const QString & elementid) { |
105 | QPixmap pm; |
106 | if (!QPixmapCache::find(pixmapCacheNameFromElementId(width, height, elementid), pm)) { |
107 | pm = renderElement(width, height, elementid); |
108 | QPixmapCache::insert(pixmapCacheNameFromElementId(width, height, elementid), pm); |
109 | } |
110 | return pm; |
111 | } |
112 | |
113 | QPixmap KBlocksGraphics::renderElement(int width, int height, const QString & elementid) { |
114 | QImage qiRend(QSize(width, height),QImage::Format_ARGB32_Premultiplied); |
115 | qiRend.fill(0); |
116 | |
117 | if (m_renderer->isValid()) { |
118 | QPainter p(&qiRend); |
119 | m_renderer->render(&p, elementid, QRectF(0,0,width,height)); |
120 | } |
121 | return QPixmap::fromImage(qiRend); |
122 | } |
123 | |
124 | QString KBlocksGraphics::pixmapCacheNameFromElementId(int width, int height, const QString & elementid) { |
125 | return elementid + QString("W%1H%2").arg(width).arg(height); |
126 | } |
127 | */ |
128 | |
129 | |