Added gitignore
[tagua/yd.git] / src / loader / theme.cpp
blob1aa2c34759824578719304c5928c5c35b2dd0cf0
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 std::cout << "SCRIPT LOAD ERROR:" << std::endl << m_lua_loader.errorString() << std::endl;
43 settings.onChange(this, SLOT(onSettingsChanged()));
44 onSettingsChanged();
47 Theme::~Theme() {
48 if(!m_cache.empty()) {
49 std::cout << " --> Error in Theme::~Theme, sizes still referenced" << std::endl;
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_cache.clear();
63 void Theme::refSize(int size) {
64 m_cache[size].m_ref_count++;
67 void Theme::unrefSize(int size) {
68 Cache::iterator it = m_cache.find(size);
69 if(it == m_cache.end()) {
70 std::cout << " --> Error in Theme::unrefSize, size not referenced " << size << std::endl;
71 return;
74 if(!--it->second.m_ref_count)
75 m_cache.erase(size);
78 PixmapOrMap Theme::getPixmapMap(const QString& key, int size) {
79 if(m_lua_loader.error())
80 return PixmapOrMap();
82 Cache::iterator it = m_cache.find(size);
83 if(it == m_cache.end()) {
84 std::cout << " --> Error in Theme::getPixmap, size not referenced " << size << std::endl;
85 return PixmapOrMap();
88 SizeCache::Cache::iterator pix = it->second.m_cache.find(key);
89 if(pix != it->second.m_cache.end())
90 return pix->second;
92 PixmapOrMap retv = to_pixmap_map(m_lua_loader.getImageMap(key, size));
93 if(m_lua_loader.error()) {
94 std::cout << "SCRIPT RUN ERROR:" << std::endl << m_lua_loader.errorString() << std::endl;
95 m_lua_loader.clearError();
98 it->second.m_cache[key] = retv;
99 return retv;
102 QPixmap Theme::getPixmap(const QString& key, int size) {
103 PixmapOrMap p = getPixmapMap(key, size);
104 if(QPixmap *px = boost::get<QPixmap>(&p))
105 return *px;
106 return QPixmap();
109 Glyph Theme::getGlyph(const QString& key, int size) {
110 if(m_lua_loader.error())
111 return Glyph();
113 Cache::iterator it = m_cache.find(size);
114 if(it == m_cache.end()) {
115 std::cout << " --> Error in Theme::getGlyph, size not referenced " << size << std::endl;
116 return Glyph();
119 SizeCache::Cache2::iterator pix = it->second.m_cache2.find(key);
120 if(pix != it->second.m_cache2.end())
121 return pix->second;
123 Glyph retv = m_lua_loader.getGlyph(key);
124 retv.m_font.setPointSize(size+retv.m_delta);
126 if(m_lua_loader.error()) {
127 std::cout << "SCRIPT RUN ERROR:" << std::endl << m_lua_loader.errorString() << std::endl;
128 m_lua_loader.clearError();
131 it->second.m_cache2[key] = retv;
132 return retv;
135 #include "theme.moc"
137 } //end namespace Loader