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.
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 QString
& lua_file
)
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");
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();
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.");
79 if(!--it
->second
.m_ref_count
)
84 T
Theme::getValue(const QString
& key
, int size
, const ::LuaApi::LuaValueMap
* args
) {
85 if(m_lua_loader
.error())
88 Cache::iterator it
= m_cache
.find(size
);
89 if(it
== m_cache
.end()) {
90 ERROR("Size " << size
<< " not referenced.");
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();
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();
116 SizeCache::PixmapsCache::iterator pix
= it
->second
.m_pixmaps_cache
.find(key
);
117 if(pix
!= it
->second
.m_pixmaps_cache
.end())
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();
128 it
->second
.m_pixmaps_cache
[key
] = retv
;
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
))
141 Glyph
Theme::getValue
<Glyph
>(const QString
& key
, int size
, const ::LuaApi::LuaValueMap
* args
) {
142 if(m_lua_loader
.error())
145 Cache::iterator it
= m_cache
.find(size
);
146 if(it
== m_cache
.end()) {
147 ERROR("Size " << size
<< " not referenced.");
152 SizeCache::GlyphsCache::iterator pix
= it
->second
.m_glyphs_cache
.find(key
);
153 if(pix
!= it
->second
.m_glyphs_cache
.end())
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());
166 it
->second
.m_glyphs_cache
[key
] = 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