Variants are not singletons anymore.
[tagua/yd.git] / src / pixmaploader.cpp
blobabc6bbd64ba61c73158dd7135ab6528cbc3baf97
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 */
11 #include <map>
12 #include <iostream>
13 #include "common.h"
14 #include "loader/theme.h"
15 #include "pixmaploader.h"
16 #include "themeinfo.h"
18 class PixmapLoader::ThemeLoader : public Loader::Theme {
19 public:
20 int m_ref_count;
22 ThemeLoader(const ThemeInfo& t)
23 : Loader::Theme(t)
24 , m_ref_count(0) {
28 PixmapLoader::PixmapLoader()
29 : m_loader(NULL)
30 , m_size(0)
34 PixmapLoader::~PixmapLoader() {
35 flush();
38 PixmapLoader::ThemeLoadersCache& PixmapLoader::loaders() {
39 static ThemeLoadersCache cache;
40 return cache;
43 void PixmapLoader::flush() {
44 if (m_loader) {
45 /* unref the size */
46 if(m_size)
47 m_loader->unrefSize(m_size);
48 m_loader->unrefSize(0);
50 /* unref the loader, and possibly destroy it */
51 if(!--m_loader->m_ref_count) {
52 delete m_loader;
53 loaders().remove(m_theme);
55 m_loader = NULL;
59 void PixmapLoader::setTheme(const ThemeInfo& theme) {
60 if (theme == m_theme)
61 return;
63 flush();
64 m_theme = theme;
67 void PixmapLoader::setSize(int s) {
68 if(s == m_size)
69 return;
71 if(m_loader) {
72 if(s)
73 m_loader->refSize(s);
74 if(m_size)
75 m_loader->unrefSize(m_size);
77 m_size = s;
80 void PixmapLoader::initialize() {
81 if (m_loader)
82 return;
84 /* try to get a loader */
85 m_loader = loaders().value(m_theme, 0);
86 if (!m_loader) {
87 // no loader, yet
88 // create it
89 m_loader = new ThemeLoader(m_theme);
90 loaders().insert(m_theme, m_loader);
93 Q_ASSERT(m_loader);
95 m_loader->m_ref_count++;
96 if (m_size)
97 m_loader->refSize(m_size);
99 m_loader->refSize(0);
102 QPixmap PixmapLoader::operator()(const QString& id) {
103 return getValue<QPixmap>(id);
106 template<typename T>
107 T PixmapLoader::getValue(const QString& id, const ::LuaApi::LuaValueMap* args) {
108 if (!m_size || !m_theme)
109 return T();
111 if (!m_loader)
112 initialize();
114 return m_loader->getValue<T>(id, m_size, args);
117 template QPixmap PixmapLoader::getValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*);
118 template Loader::PixmapOrMap PixmapLoader::getValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*);
119 template Loader::Glyph PixmapLoader::getValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*);
120 template double PixmapLoader::getValue<double>(const QString&, const ::LuaApi::LuaValueMap*);
121 template QPointF PixmapLoader::getValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*);
122 template QRectF PixmapLoader::getValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*);
123 template QBrush PixmapLoader::getValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*);
124 template QColor PixmapLoader::getValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*);
125 template QFont PixmapLoader::getValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*);
126 template ::LuaApi::LuaValueMap PixmapLoader::getValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*);
129 template<typename T>
130 T PixmapLoader::getStaticValue(const QString& id, const ::LuaApi::LuaValueMap* args) {
131 if (!m_theme)
132 return T();
134 if (!m_loader)
135 initialize();
137 return m_loader->getValue<T>(id, 0, args);
140 template QPixmap PixmapLoader::getStaticValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*);
141 template Loader::PixmapOrMap PixmapLoader::getStaticValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*);
142 template Loader::Glyph PixmapLoader::getStaticValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*);
143 template double PixmapLoader::getStaticValue<double>(const QString&, const ::LuaApi::LuaValueMap*);
144 template QPointF PixmapLoader::getStaticValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*);
145 template QRectF PixmapLoader::getStaticValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*);
146 template QBrush PixmapLoader::getStaticValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*);
147 template QColor PixmapLoader::getStaticValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*);
148 template QFont PixmapLoader::getStaticValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*);
149 template ::LuaApi::LuaValueMap PixmapLoader::getStaticValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*);