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.
15 #include "mastersettings.h"
16 #include "loader/theme.h"
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
)) {
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
);
35 Theme::Theme(const ThemeInfo
& theme
)
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");
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();
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.");
78 if(!--it
->second
.m_ref_count
)
83 T
Theme::getValue(const QString
& key
, int size
, const ::LuaApi::LuaValueMap
* args
) {
84 if(m_lua_loader
.error())
87 Cache::iterator it
= m_cache
.find(size
);
88 if(it
== m_cache
.end()) {
89 ERROR("Size " << size
<< " not referenced.");
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();
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();
115 SizeCache::PixmapsCache::iterator pix
= it
->second
.m_pixmaps_cache
.find(key
);
116 if(pix
!= it
->second
.m_pixmaps_cache
.end())
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();
127 it
->second
.m_pixmaps_cache
[key
] = retv
;
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
))
140 Glyph
Theme::getValue
<Glyph
>(const QString
& key
, int size
, const ::LuaApi::LuaValueMap
* args
) {
141 if(m_lua_loader
.error())
144 Cache::iterator it
= m_cache
.find(size
);
145 if(it
== m_cache
.end()) {
146 ERROR("Size " << size
<< " not referenced.");
151 SizeCache::GlyphsCache::iterator pix
= it
->second
.m_glyphs_cache
.find(key
);
152 if(pix
!= it
->second
.m_glyphs_cache
.end())
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());
165 it
->second
.m_glyphs_cache
[key
] = 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