![]() |
Mixxx
|
00001 /*************************************************************************** 00002 wpixmapstore.cpp - description 00003 ------------------- 00004 begin : Mon Jul 28 2003 00005 copyright : (C) 2003 by Tue & Ken Haste Andersen 00006 email : haste@diku.dk 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "wpixmapstore.h" 00019 //Added by qt3to4: 00020 #include <QPixmap> 00021 #include <QtDebug> 00022 00023 QHash<QString, PixmapInfoType*> WPixmapStore::dictionary; 00024 00025 QSharedPointer<ImgSource> WPixmapStore::loader = QSharedPointer<ImgSource>(); 00026 00027 QPixmap * WPixmapStore::getPixmap(const QString &fileName) 00028 { 00029 // Search for pixmap in list 00030 PixmapInfoType* info = NULL; 00031 00032 QHash<QString, PixmapInfoType*>::iterator it = dictionary.find(fileName); 00033 if (it != dictionary.end()) { 00034 info = it.value(); 00035 info->instCount++; 00036 //qDebug() << "WPixmapStore returning cached pixmap for:" << fileName; 00037 return info->pixmap; 00038 } 00039 00040 // Pixmap wasn't found, construct it 00041 //qDebug() << "WPixmapStore Loading pixmap from file" << fileName; 00042 00043 QPixmap* loadedPixmap = NULL; 00044 if (loader) { 00045 QImage* img = loader->getImage(fileName); 00046 00047 if (img != NULL && !img->isNull()) { 00048 // ack, hacky; there must be a better way (we're using pixmap 00049 // pointers, but perhaps qt4 expects that you'll just copy?) --kousu 00050 // 2009/03 00051 loadedPixmap = new QPixmap(QPixmap::fromImage(*img)); 00052 } 00053 // No longer need the original QImage (I hope...) - adam_d 00054 delete img; 00055 } else { 00056 loadedPixmap = new QPixmap(fileName); 00057 } 00058 00059 if (loadedPixmap == NULL || loadedPixmap->isNull()) { 00060 qDebug() << "WPixmapStore couldn't load:" << fileName << (loadedPixmap == NULL); 00061 delete loadedPixmap; 00062 return NULL; 00063 } 00064 00065 info = new PixmapInfoType; 00066 info->pixmap = loadedPixmap; 00067 info->instCount = 1; 00068 dictionary.insert(fileName, info); 00069 return info->pixmap; 00070 } 00071 00072 QPixmap * WPixmapStore::getPixmapNoCache(const QString& fileName) { 00073 if (loader) { 00074 QImage * img = loader->getImage(fileName); 00075 QPixmap r = QPixmap::fromImage(*img); 00076 delete img; 00077 return new QPixmap(r); //ack, hacky; there must be a better way (we're using pixmap pointers, but perhaps qt4 expects that you'll just copy?) --kousu 2009/03 00078 } else { 00079 return new QPixmap(fileName); 00080 } 00081 } 00082 00083 void WPixmapStore::deletePixmap(QPixmap * p) 00084 { 00085 // Search for pixmap in list 00086 PixmapInfoType *info = NULL; 00087 QMutableHashIterator<QString, PixmapInfoType*> it(dictionary); 00088 00089 while (it.hasNext()) 00090 { 00091 info = it.next().value(); 00092 if (p == info->pixmap) 00093 { 00094 info->instCount--; 00095 if (info->instCount<1) 00096 { 00097 it.remove(); 00098 delete info->pixmap; 00099 delete info; 00100 } 00101 00102 break; 00103 } 00104 } 00105 } 00106 00107 void WPixmapStore::emptyStore() { 00108 00109 } 00110 00111 void WPixmapStore::setLoader(QSharedPointer<ImgSource> ld) { 00112 loader = ld; 00113 }