File indexing completed on 2022-10-04 14:26:22

0001 /*
0002  * Copyright 1999 by Martin R. Jones <mjones@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 #include "amorpixmapmanager.h"
0019 
0020 #include <QPixmap>
0021 #include <QBitmap>
0022 
0023 AmorPixmapManager *AmorPixmapManager::mManager = 0;
0024 
0025 
0026 AmorPixmapManager::AmorPixmapManager()
0027   : mPixmapDir(QLatin1String( "." ))
0028 {
0029 }
0030 
0031 
0032 AmorPixmapManager::~AmorPixmapManager()
0033 {
0034     qDeleteAll( mPixmaps );
0035 }
0036 
0037 
0038 void AmorPixmapManager::setPixmapDir(const QString &dir)
0039 {
0040     mPixmapDir = dir;
0041 }
0042 
0043 
0044 void AmorPixmapManager::reset()
0045 {
0046     mPixmapDir = QLatin1Char( '.' );
0047     qDeleteAll( mPixmaps );
0048     mPixmaps.clear();
0049 }
0050 
0051 
0052 const QPixmap* AmorPixmapManager::load(const QString & img)
0053 {
0054     QHash<QString, QPixmap*>::const_iterator it = mPixmaps.constFind( img );
0055     QPixmap *pixmap = it != mPixmaps.constEnd() ? *it : 0;
0056 
0057     if( !pixmap ) {
0058         // pixmap has not yet been loaded.
0059         QString path = mPixmapDir + QLatin1String( "/" ) + img;
0060         pixmap = new QPixmap( path );
0061 
0062         if( !pixmap->isNull() ) {
0063             pixmap->setMask(pixmap->createHeuristicMask(true));
0064             mPixmaps[img] = pixmap;
0065         }
0066         else {
0067             delete pixmap;
0068             pixmap = 0;
0069         }
0070     }
0071 
0072     return pixmap;
0073 }
0074 
0075 
0076 const QPixmap* AmorPixmapManager::pixmap(const QString & img) const
0077 {
0078     return mPixmaps.contains( img ) ? mPixmaps[ img ] : 0;
0079 }
0080 
0081 
0082 AmorPixmapManager* AmorPixmapManager::manager()
0083 {
0084     if( !mManager ) {
0085         mManager = new AmorPixmapManager;
0086     }
0087 
0088     return mManager;
0089 }
0090 
0091 
0092 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0093 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: