Better comments and docs for resource manager classes.
[tagua/yd.git] / src / pixmaploader.cpp
blob33dcc86830c4a7c97d54b2bbbd98928cf59d532b
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 "loader/theme.h"
13 #include "pixmaploader.h"
15 class PixmapLoader::ThemeLoader : public Loader::Theme {
16 public:
17 int m_ref_count;
19 ThemeLoader(const QString& s)
20 : Loader::Theme(s)
21 , m_ref_count(0) {
26 PixmapLoader::ThemeLoadersCache PixmapLoader::s_loaders;
29 PixmapLoader::PixmapLoader()
30 : m_loader(NULL)
31 , m_size(0)
35 PixmapLoader::~PixmapLoader() {
36 flush();
39 void PixmapLoader::flush() {
40 if(m_loader) {
41 /* unref the size */
42 if(m_size)
43 m_loader->unrefSize(m_size);
45 /* unref the loader, and possibly destroy it */
46 if(!--m_loader->m_ref_count) {
47 delete m_loader;
48 s_loaders.erase(m_base);
50 m_loader = NULL;
54 void PixmapLoader::setBasePath(const QString& base) {
55 //QString base = PixmapLoader::ThemeLoader::resolveBasePath(__base);
57 if(base == m_base)
58 return;
60 flush();
61 m_base = base;
64 void PixmapLoader::setSize(int s) {
65 if(m_loader) {
66 if(s)
67 m_loader->refSize(s);
68 if(m_size)
69 m_loader->unrefSize(m_size);
71 m_size = s;
74 void PixmapLoader::initialize() {
75 if(m_loader)
76 return;
78 /* try to get a loader */
79 if(s_loaders.count(m_base))
80 m_loader = s_loaders[m_base];
81 else {
82 m_loader = new ThemeLoader(m_base);
83 s_loaders[m_base] = m_loader;
86 m_loader->m_ref_count++;
87 m_loader->refSize(m_size);
90 QPixmap PixmapLoader::operator()(const QString& id) {
91 if(!m_size || m_base.isEmpty())
92 return QPixmap();
94 if(!m_loader)
95 initialize();
97 return m_loader->getPixmap(id, m_size);
100 Loader::PixmapOrMap PixmapLoader::getPixmapMap(const QString& id) {
101 if(!m_size || m_base.isEmpty())
102 return Loader::PixmapOrMap();
104 if(!m_loader)
105 initialize();
107 return m_loader->getPixmapMap(id, m_size);
110 Loader::Glyph PixmapLoader::getGlyph(const QString& id) {
111 if(!m_size || m_base.isEmpty())
112 return Loader::Glyph();
114 if(!m_loader)
115 initialize();
117 return m_loader->getGlyph(id, m_size);