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 std::cout
<< "SCRIPT LOAD ERROR:" << std::endl
<< m_lua_loader
.errorString() << std::endl
;
43 settings
.onChange(this, SLOT(onSettingsChanged()));
48 if(!m_cache
.empty()) {
49 std::cout
<< " --> Error in Theme::~Theme, sizes still referenced" << std::endl
;
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_cache
.clear();
63 void Theme::refSize(int size
) {
64 m_cache
[size
].m_ref_count
++;
67 void Theme::unrefSize(int size
) {
68 Cache::iterator it
= m_cache
.find(size
);
69 if(it
== m_cache
.end()) {
70 std::cout
<< " --> Error in Theme::unrefSize, size not referenced " << size
<< std::endl
;
74 if(!--it
->second
.m_ref_count
)
78 PixmapOrMap
Theme::getPixmapMap(const QString
& key
, int size
) {
79 if(m_lua_loader
.error())
82 Cache::iterator it
= m_cache
.find(size
);
83 if(it
== m_cache
.end()) {
84 std::cout
<< " --> Error in Theme::getPixmap, size not referenced " << size
<< std::endl
;
88 SizeCache::Cache::iterator pix
= it
->second
.m_cache
.find(key
);
89 if(pix
!= it
->second
.m_cache
.end())
92 PixmapOrMap retv
= to_pixmap_map(m_lua_loader
.getImageMap(key
, size
));
93 if(m_lua_loader
.error()) {
94 std::cout
<< "SCRIPT RUN ERROR:" << std::endl
<< m_lua_loader
.errorString() << std::endl
;
95 m_lua_loader
.clearError();
98 it
->second
.m_cache
[key
] = retv
;
102 QPixmap
Theme::getPixmap(const QString
& key
, int size
) {
103 PixmapOrMap p
= getPixmapMap(key
, size
);
104 if(QPixmap
*px
= boost::get
<QPixmap
>(&p
))
109 Glyph
Theme::getGlyph(const QString
& key
, int size
) {
110 if(m_lua_loader
.error())
113 Cache::iterator it
= m_cache
.find(size
);
114 if(it
== m_cache
.end()) {
115 std::cout
<< " --> Error in Theme::getGlyph, size not referenced " << size
<< std::endl
;
119 SizeCache::Cache2::iterator pix
= it
->second
.m_cache2
.find(key
);
120 if(pix
!= it
->second
.m_cache2
.end())
123 Glyph retv
= m_lua_loader
.getGlyph(key
);
124 retv
.m_font
.setPointSize(size
+retv
.m_delta
);
126 if(m_lua_loader
.error()) {
127 std::cout
<< "SCRIPT RUN ERROR:" << std::endl
<< m_lua_loader
.errorString() << std::endl
;
128 m_lua_loader
.clearError();
131 it
->second
.m_cache2
[key
] = retv
;
137 } //end namespace Loader