Themes are now searched by desktop files.
[tagua/yd.git] / src / loader / theme.cpp
blobe0e244f147a6a157159f39e8636ade0adf065fbf
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 "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 QString& lua_file)
36 : m_file(lua_file)
37 , m_context()
38 , m_lua_loader(&m_context) {
40 std::cout << "******** LOADING " << m_file << " ***********" << std::endl;
41 m_lua_loader.runFile(qPrintable(m_file));
42 if(m_lua_loader.error())
43 ERROR("Script load error: " << std::endl << m_lua_loader.errorString());
45 settings.onChange(this, "onSettingsChanged");
46 onSettingsChanged();
49 Theme::~Theme() {
50 if(!m_cache.empty())
51 ERROR("Sizes still referenced.");
54 void Theme::onSettingsChanged() {
55 SettingMap<QString> s_lua = settings.group("lua-settings").map<QString>("entry", "file-name");
56 Settings entry = s_lua.insert(m_file);
57 OptList ol = m_lua_loader.getValue<OptList>("options", 0, NULL, true);
58 if(m_lua_loader.error()) {
59 m_lua_loader.clearError();
60 return;
63 if(options_list_load_from_settings(ol, entry.group("options")))
64 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)
65 it->second.m_pixmaps_cache.clear();
68 void Theme::refSize(int size) {
69 m_cache[size].m_ref_count++;
72 void Theme::unrefSize(int size) {
73 Cache::iterator it = m_cache.find(size);
74 if(it == m_cache.end()) {
75 ERROR("Size " << size << " not referenced.");
76 return;
79 if(!--it->second.m_ref_count)
80 m_cache.erase(size);
83 template<typename T>
84 T Theme::getValue(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
85 if(m_lua_loader.error())
86 return T();
88 Cache::iterator it = m_cache.find(size);
89 if(it == m_cache.end()) {
90 ERROR("Size " << size << " not referenced.");
91 return T();
94 T retv = m_lua_loader.getValue<T>(key, size, args);
96 if(m_lua_loader.error()) {
97 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
98 m_lua_loader.clearError();
101 return retv;
104 template<>
105 PixmapOrMap Theme::getValue<PixmapOrMap>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
106 if(m_lua_loader.error())
107 return PixmapOrMap();
109 Cache::iterator it = m_cache.find(size);
110 if(it == m_cache.end()) {
111 ERROR("Size " << size << " not referenced.");
112 return PixmapOrMap();
115 if(!args) {
116 SizeCache::PixmapsCache::iterator pix = it->second.m_pixmaps_cache.find(key);
117 if(pix != it->second.m_pixmaps_cache.end())
118 return pix->second;
121 PixmapOrMap retv = to_pixmap_map(m_lua_loader.getValue< ::LuaApi::ImageOrMap>(key, size, args));
122 if(m_lua_loader.error()) {
123 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
124 m_lua_loader.clearError();
127 if(!args)
128 it->second.m_pixmaps_cache[key] = retv;
129 return retv;
132 template<>
133 QPixmap Theme::getValue<QPixmap>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
134 PixmapOrMap p = getValue<PixmapOrMap>(key, size, args);
135 if(QPixmap *px = boost::get<QPixmap>(&p))
136 return *px;
137 return QPixmap();
140 template<>
141 Glyph Theme::getValue<Glyph>(const QString& key, int size, const ::LuaApi::LuaValueMap* args) {
142 if(m_lua_loader.error())
143 return Glyph();
145 Cache::iterator it = m_cache.find(size);
146 if(it == m_cache.end()) {
147 ERROR("Size " << size << " not referenced.");
148 return Glyph();
151 if(!args) {
152 SizeCache::GlyphsCache::iterator pix = it->second.m_glyphs_cache.find(key);
153 if(pix != it->second.m_glyphs_cache.end())
154 return pix->second;
157 Glyph retv = m_lua_loader.getValue<Glyph>(key, size, args);
159 if(m_lua_loader.error()) {
160 ERROR("Script run error: " << std::endl << m_lua_loader.errorString());
161 m_lua_loader.clearError();
164 retv.m_font.setPointSize(size+retv.delta());
165 if(!args)
166 it->second.m_glyphs_cache[key] = retv;
167 return retv;
170 template double Theme::getValue<double>(const QString&, int, const ::LuaApi::LuaValueMap* args);
171 template QPointF Theme::getValue<QPointF>(const QString&, int, const ::LuaApi::LuaValueMap* args);
172 template QRectF Theme::getValue<QRectF>(const QString&, int, const ::LuaApi::LuaValueMap* args);
173 template QBrush Theme::getValue<QBrush>(const QString&, int, const ::LuaApi::LuaValueMap* args);
174 template QColor Theme::getValue<QColor>(const QString&, int, const ::LuaApi::LuaValueMap* args);
175 template QFont Theme::getValue<QFont>(const QString&, int, const ::LuaApi::LuaValueMap* args);
176 template ::LuaApi::LuaValueMap Theme::getValue< ::LuaApi::LuaValueMap>(const QString&, int, const ::LuaApi::LuaValueMap* args);
178 } //end namespace Loader