Rough PGN save support.
[tagua/yd.git] / src / pixmaploader.cpp
blob59a49d3dbe99b2c4be8acc2359ccfbbcc525cea3
1 /*
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.
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::getPixmap(const QString& id) {
103 return getValue<QPixmap>(id);
106 QPixmap PixmapLoader::piecePixmap(const QString& id, bool flipped) {
107 ::LuaApi::LuaValueMap args;
108 if (flipped)
109 args["flipped"] = 0.0;
111 return getValue<QPixmap>(id, &args);
112 // return getValue<QPixmap>(id);
115 template<typename T>
116 T PixmapLoader::getValue(const QString& id, const ::LuaApi::LuaValueMap* args, bool allow_nil) {
117 if (!m_size || !m_theme)
118 return T();
120 if (!m_loader)
121 initialize();
123 return m_loader->getValue<T>(id, m_size, args, allow_nil);
126 template QPixmap PixmapLoader::getValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
127 template Loader::PixmapOrMap PixmapLoader::getValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
128 template Loader::Glyph PixmapLoader::getValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
129 template double PixmapLoader::getValue<double>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
130 template QPointF PixmapLoader::getValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
131 template QRectF PixmapLoader::getValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
132 template QBrush PixmapLoader::getValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
133 template QColor PixmapLoader::getValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
134 template QFont PixmapLoader::getValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
135 template ::LuaApi::LuaValueMap PixmapLoader::getValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
138 template<typename T>
139 T PixmapLoader::getStaticValue(const QString& id, const ::LuaApi::LuaValueMap* args, bool allow_nil) {
140 if (!m_theme)
141 return T();
143 if (!m_loader)
144 initialize();
146 return m_loader->getValue<T>(id, 0, args, allow_nil);
149 template QPixmap PixmapLoader::getStaticValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
150 template Loader::PixmapOrMap PixmapLoader::getStaticValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
151 template Loader::Glyph PixmapLoader::getStaticValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
152 template double PixmapLoader::getStaticValue<double>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
153 template QPointF PixmapLoader::getStaticValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
154 template QRectF PixmapLoader::getStaticValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
155 template QBrush PixmapLoader::getStaticValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
156 template QColor PixmapLoader::getStaticValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
157 template QFont PixmapLoader::getStaticValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
158 template ::LuaApi::LuaValueMap PixmapLoader::getStaticValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);