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.
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 m_lua_loader
.runFile(m_file
.toAscii().constData());
41 if(m_lua_loader
.error())
42 ERROR("Script load error: " << std::endl
<< m_lua_loader
.errorString());
44 settings
.onChange(this, SLOT(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_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_pixmaps_cache
.clear();
62 void Theme::refSize(int size
) {
63 m_cache
[size
].m_ref_count
++;
66 void Theme::unrefSize(int size
) {
67 Cache::iterator it
= m_cache
.find(size
);
68 if(it
== m_cache
.end()) {
69 ERROR("Size " << size
<< " not referenced.");
73 if(!--it
->second
.m_ref_count
)
77 PixmapOrMap
Theme::getPixmapMap(const QString
& key
, int size
) {
78 if(m_lua_loader
.error())
81 Cache::iterator it
= m_cache
.find(size
);
82 if(it
== m_cache
.end()) {
83 ERROR("Size " << size
<< " not referenced.");
87 SizeCache::PixmapsCache::iterator pix
= it
->second
.m_pixmaps_cache
.find(key
);
88 if(pix
!= it
->second
.m_pixmaps_cache
.end())
91 PixmapOrMap retv
= to_pixmap_map(m_lua_loader
.getImageMap(key
, size
));
92 if(m_lua_loader
.error()) {
93 ERROR("Script run error: " << std::endl
<< m_lua_loader
.errorString());
94 m_lua_loader
.clearError();
97 it
->second
.m_pixmaps_cache
[key
] = retv
;
101 QPixmap
Theme::getPixmap(const QString
& key
, int size
) {
102 PixmapOrMap p
= getPixmapMap(key
, size
);
103 if(QPixmap
*px
= boost::get
<QPixmap
>(&p
))
108 Glyph
Theme::getGlyph(const QString
& key
, int size
) {
109 if(m_lua_loader
.error())
112 Cache::iterator it
= m_cache
.find(size
);
113 if(it
== m_cache
.end()) {
114 ERROR("Size " << size
<< " not referenced.");
118 SizeCache::GlyphsCache::iterator pix
= it
->second
.m_glyphs_cache
.find(key
);
119 if(pix
!= it
->second
.m_glyphs_cache
.end())
122 Glyph retv
= m_lua_loader
.getGlyph(key
);
123 retv
.m_font
.setPointSize(size
+retv
.m_delta
);
125 if(m_lua_loader
.error()) {
126 ERROR("Script run error: " << std::endl
<< m_lua_loader
.errorString());
127 m_lua_loader
.clearError();
130 it
->second
.m_glyphs_cache
[key
] = retv
;
136 } //end namespace Loader