Removed unimplemented GUI actions.
[tagua/yd.git] / src / loader / theme.cpp
blob022a0642ec02e3bfd587635204cd6b56c734d772
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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 "mastersettings.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 ThemeInfo& theme)
36 : m_theme(theme)
37 , m_context()
38 , m_lua_loader(&m_context, theme) {
39 //std::cout << "loading theme " << theme.file_name << std::endl;
40 m_lua_loader.runFile(m_theme.file_name);
41 if(m_lua_loader.error())
42 ERROR("Script load error: " << std::endl << m_lua_loader.errorString());
44 settings().onChange(this, "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_theme.file_name);
56 OptList ol = m_lua_loader.getValue<OptList>("options", 0, NULL, true);
57 if(m_lua_loader.error()) {
58 m_lua_loader.clearError();
59 return;
62 if(options_list_load_from_settings(ol, entry.group("options")))
63 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)
64 it->second.m_pixmaps_cache.clear();
67 void Theme::refSize(int size) {
68 m_cache[size].m_ref_count++;
71 void Theme::unrefSize(int size) {
72 Cache::iterator it = m_cache.find(size);
73 if(it == m_cache.end()) {
74 ERROR("Size " << size << " not referenced.");
75 return;
78 if(!--it->second.m_ref_count)
79 m_cache.erase(size);
82 template<typename T>
83 T Theme::getValue(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
84 if(m_lua_loader.error())
85 return T();
87 Cache::iterator it = m_cache.find(size);
88 if(it == m_cache.end()) {
89 ERROR("Size " << size << " not referenced.");
90 return T();
93 T retv = m_lua_loader.getValue<T>(key, size, args);
95 if(m_lua_loader.error()) {
96 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
97 m_lua_loader.clearError();
100 return retv;
103 template<>
104 PixmapOrMap Theme::getValue<PixmapOrMap>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
105 if(m_lua_loader.error())
106 return PixmapOrMap();
108 Cache::iterator it = m_cache.find(size);
109 if(it == m_cache.end()) {
110 ERROR("Size " << size << " not referenced.");
111 return PixmapOrMap();
114 if(!args) {
115 SizeCache::PixmapsCache::iterator pix = it->second.m_pixmaps_cache.find(key);
116 if(pix != it->second.m_pixmaps_cache.end())
117 return pix->second;
120 PixmapOrMap retv = to_pixmap_map(m_lua_loader.getValue< ::LuaApi::ImageOrMap>(key, size, args));
121 if(m_lua_loader.error()) {
122 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
123 m_lua_loader.clearError();
126 if(!args)
127 it->second.m_pixmaps_cache[key] = retv;
128 return retv;
131 template<>
132 QPixmap Theme::getValue<QPixmap>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
133 PixmapOrMap p = getValue<PixmapOrMap>(key, size, args);
134 if(QPixmap *px = boost::get<QPixmap>(&p))
135 return *px;
136 return QPixmap();
139 template<>
140 Glyph Theme::getValue<Glyph>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
141 if(m_lua_loader.error())
142 return Glyph();
144 Cache::iterator it = m_cache.find(size);
145 if(it == m_cache.end()) {
146 ERROR("Size " << size << " not referenced.");
147 return Glyph();
150 if(!args) {
151 SizeCache::GlyphsCache::iterator pix = it->second.m_glyphs_cache.find(key);
152 if(pix != it->second.m_glyphs_cache.end())
153 return pix->second;
156 Glyph retv = m_lua_loader.getValue<Glyph>(key, size, args);
158 if(m_lua_loader.error()) {
159 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
160 m_lua_loader.clearError();
163 retv.m_font.setPointSize(size+retv.delta());
164 if(!args)
165 it->second.m_glyphs_cache[key] = retv;
166 return retv;
169 template double Theme::getValue<double>(const QString&, int, const ::LuaApi::LuaValueMap* args);
170 template QPointF Theme::getValue<QPointF>(const QString&, int, const ::LuaApi::LuaValueMap* args);
171 template QRectF Theme::getValue<QRectF>(const QString&, int, const ::LuaApi::LuaValueMap* args);
172 template QBrush Theme::getValue<QBrush>(const QString&, int, const ::LuaApi::LuaValueMap* args);
173 template QColor Theme::getValue<QColor>(const QString&, int, const ::LuaApi::LuaValueMap* args);
174 template QFont Theme::getValue<QFont>(const QString&, int, const ::LuaApi::LuaValueMap* args);
175 template ::LuaApi::LuaValueMap Theme::getValue< ::LuaApi::LuaValueMap>(const QString&, int, const ::LuaApi::LuaValueMap* args);
177 } //end namespace Loader