1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
(c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include <map>
#include <iostream>
#include "common.h"
#include "loader/theme.h"
#include "pixmaploader.h"
#include "themeinfo.h"
class PixmapLoader::ThemeLoader : public Loader::Theme {
public:
int m_ref_count;
ThemeLoader(const ThemeInfo& t)
: Loader::Theme(t)
, m_ref_count(0) {
}
};
PixmapLoader::PixmapLoader()
: m_loader(NULL)
, m_size(0)
{
}
PixmapLoader::~PixmapLoader() {
flush();
}
PixmapLoader::ThemeLoadersCache& PixmapLoader::loaders() {
static ThemeLoadersCache cache;
return cache;
}
void PixmapLoader::flush() {
if (m_loader) {
/* unref the size */
if(m_size)
m_loader->unrefSize(m_size);
m_loader->unrefSize(0);
/* unref the loader, and possibly destroy it */
if(!--m_loader->m_ref_count) {
delete m_loader;
loaders().remove(m_theme);
}
m_loader = NULL;
}
}
void PixmapLoader::setTheme(const ThemeInfo& theme) {
if (theme == m_theme)
return;
flush();
m_theme = theme;
}
void PixmapLoader::setSize(int s) {
if(s == m_size)
return;
if(m_loader) {
if(s)
m_loader->refSize(s);
if(m_size)
m_loader->unrefSize(m_size);
}
m_size = s;
}
void PixmapLoader::initialize() {
if (m_loader)
return;
/* try to get a loader */
m_loader = loaders().value(m_theme, 0);
if (!m_loader) {
// no loader, yet
// create it
m_loader = new ThemeLoader(m_theme);
loaders().insert(m_theme, m_loader);
}
Q_ASSERT(m_loader);
m_loader->m_ref_count++;
if (m_size)
m_loader->refSize(m_size);
m_loader->refSize(0);
}
QPixmap PixmapLoader::getPixmap(const QString& id) {
return getValue<QPixmap>(id);
}
QPixmap PixmapLoader::piecePixmap(const QString& id, bool flipped) {
::LuaApi::LuaValueMap args;
if (flipped)
args["flipped"] = 0.0;
return getValue<QPixmap>(id, &args);
// return getValue<QPixmap>(id);
}
template<typename T>
T PixmapLoader::getValue(const QString& id, const ::LuaApi::LuaValueMap* args, bool allow_nil) {
if (!m_size || !m_theme)
return T();
if (!m_loader)
initialize();
return m_loader->getValue<T>(id, m_size, args, allow_nil);
}
template QPixmap PixmapLoader::getValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template Loader::PixmapOrMap PixmapLoader::getValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template Loader::Glyph PixmapLoader::getValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template double PixmapLoader::getValue<double>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QPointF PixmapLoader::getValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QRectF PixmapLoader::getValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QBrush PixmapLoader::getValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QColor PixmapLoader::getValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QFont PixmapLoader::getValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template ::LuaApi::LuaValueMap PixmapLoader::getValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template<typename T>
T PixmapLoader::getStaticValue(const QString& id, const ::LuaApi::LuaValueMap* args, bool allow_nil) {
if (!m_theme)
return T();
if (!m_loader)
initialize();
return m_loader->getValue<T>(id, 0, args, allow_nil);
}
template QPixmap PixmapLoader::getStaticValue<QPixmap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template Loader::PixmapOrMap PixmapLoader::getStaticValue<Loader::PixmapOrMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template Loader::Glyph PixmapLoader::getStaticValue<Loader::Glyph>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template double PixmapLoader::getStaticValue<double>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QPointF PixmapLoader::getStaticValue<QPointF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QRectF PixmapLoader::getStaticValue<QRectF>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QBrush PixmapLoader::getStaticValue<QBrush>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QColor PixmapLoader::getStaticValue<QColor>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template QFont PixmapLoader::getStaticValue<QFont>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
template ::LuaApi::LuaValueMap PixmapLoader::getStaticValue< ::LuaApi::LuaValueMap>(const QString&, const ::LuaApi::LuaValueMap*, bool allow_nil);
|