Better comments and docs for resource manager classes.
[tagua/yd.git] / src / loader / theme.cpp
blobc011485f9c32bd7e108c39403a8b20636db72d8e
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
12 #include <iostream>
13 #include <QHash>
14 #include "common.h"
15 #include "global.h"
16 #include "loader/theme.h"
18 namespace Loader {
20 PixmapOrMap Theme::to_pixmap_map(const ::LuaApi::ImageOrMap& m) {
21 if(const QImage *i = boost::get<QImage>(&m)) {
22 return PixmapOrMap(QPixmap::fromImage(*i));
24 else if(const ::LuaApi::ImageMap *i = boost::get< ::LuaApi::ImageMap>(&m)) {
25 PixmapMap p;
26 for(::LuaApi::ImageMap::const_iterator it = i->begin(); it != i->end(); ++it) {
27 p[it->first] = QPixmap::fromImage(it->second);
29 return PixmapOrMap(p);
31 else
32 return PixmapOrMap();
35 Theme::Theme(const QString& lua_file)
36 : m_file(lua_file)
37 , m_context()
38 , m_lua_loader(&m_context) {
40 m_lua_loader.runFile(m_file.toAscii().constData());
41 if(m_lua_loader.error())
42 ERROR("Script load error: " << std::endl << m_lua_loader.errorString());
44 settings.onChange(this, SLOT(onSettingsChanged()));
45 onSettingsChanged();
48 Theme::~Theme() {
49 if(!m_cache.empty())
50 ERROR("Sizes still referenced.");
53 void Theme::onSettingsChanged() {
54 SettingMap<QString> s_lua = settings.group("lua-settings").map<QString>("entry", "file-name");
55 Settings entry = s_lua.insert(m_file);
56 OptList ol = m_lua_loader.getOptList("options");
57 if(options_list_load_from_settings(ol, entry.group("options")))
58 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)
59 it->second.m_pixmaps_cache.clear();
62 void Theme::refSize(int size) {
63 m_cache[size].m_ref_count++;
66 void Theme::unrefSize(int size) {
67 Cache::iterator it = m_cache.find(size);
68 if(it == m_cache.end()) {
69 ERROR("Size " << size << " not referenced.");
70 return;
73 if(!--it->second.m_ref_count)
74 m_cache.erase(size);
77 PixmapOrMap Theme::getPixmapMap(const QString& key, int size) {
78 if(m_lua_loader.error())
79 return PixmapOrMap();
81 Cache::iterator it = m_cache.find(size);
82 if(it == m_cache.end()) {
83 ERROR("Size " << size << " not referenced.");
84 return PixmapOrMap();
87 SizeCache::PixmapsCache::iterator pix = it->second.m_pixmaps_cache.find(key);
88 if(pix != it->second.m_pixmaps_cache.end())
89 return pix->second;
91 PixmapOrMap retv = to_pixmap_map(m_lua_loader.getImageMap(key, size));
92 if(m_lua_loader.error()) {
93 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
94 m_lua_loader.clearError();
97 it->second.m_pixmaps_cache[key] = retv;
98 return retv;
101 QPixmap Theme::getPixmap(const QString& key, int size) {
102 PixmapOrMap p = getPixmapMap(key, size);
103 if(QPixmap *px = boost::get<QPixmap>(&p))
104 return *px;
105 return QPixmap();
108 Glyph Theme::getGlyph(const QString& key, int size) {
109 if(m_lua_loader.error())
110 return Glyph();
112 Cache::iterator it = m_cache.find(size);
113 if(it == m_cache.end()) {
114 ERROR("Size " << size << " not referenced.");
115 return Glyph();
118 SizeCache::GlyphsCache::iterator pix = it->second.m_glyphs_cache.find(key);
119 if(pix != it->second.m_glyphs_cache.end())
120 return pix->second;
122 Glyph retv = m_lua_loader.getGlyph(key);
123 retv.m_font.setPointSize(size+retv.m_delta);
125 if(m_lua_loader.error()) {
126 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
127 m_lua_loader.clearError();
130 it->second.m_glyphs_cache[key] = retv;
131 return retv;
134 #include "theme.moc"
136 } //end namespace Loader