File indexing completed on 2025-02-23 04:34:22
0001 /** 0002 * \file pixmapprovider.cpp 0003 * Image provider to get pixmaps by ID. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 21 Jun 2014 0008 * 0009 * Copyright (C) 2014-2018 Urs Fleisch 0010 * 0011 * This file is part of Kid3. 0012 * 0013 * Kid3 is free software; you can redistribute it and/or modify 0014 * it under the terms of the GNU General Public License as published by 0015 * the Free Software Foundation; either version 2 of the License, or 0016 * (at your option) any later version. 0017 * 0018 * Kid3 is distributed in the hope that it will be useful, 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 * GNU General Public License for more details. 0022 * 0023 * You should have received a copy of the GNU General Public License 0024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0025 */ 0026 0027 #include "pixmapprovider.h" 0028 #include <QHash> 0029 #include <QVariant> 0030 #include "coretaggedfileiconprovider.h" 0031 0032 /** 0033 * Constructor. 0034 * @param iconProvider icon provider to use 0035 */ 0036 PixmapProvider::PixmapProvider(CoreTaggedFileIconProvider* iconProvider) 0037 : m_fileIconProvider(iconProvider), m_pixmapHash(0) 0038 { 0039 } 0040 0041 /** 0042 * Request a pixmap. 0043 * @param id ID of pixmap to get, "image://kid3/fileicon/..." or 0044 * "image://kid3/data..." 0045 * @param size the original size of the image is returned here 0046 * @param requestedSize the size requested via the Image.sourceSize property 0047 * @return pixmap for ID. 0048 */ 0049 QPixmap PixmapProvider::getPixmap(const QString& id, QSize* size, 0050 const QSize& requestedSize) 0051 { 0052 if (QByteArray imageId = id.toLatin1(); imageId.startsWith("fileicon/")) { 0053 imageId = imageId.mid(9); 0054 if (imageId.isEmpty() || imageId == "undefined") { 0055 imageId = "null"; 0056 } 0057 // Avoid creation of images with default size just because the 0058 // first request has an invalid size. 0059 if (!requestedSize.isValid() && imageId == "null") { 0060 QPixmap pixmap(1, 1); 0061 pixmap.fill(Qt::transparent); 0062 return pixmap; 0063 } 0064 m_fileIconProvider->setRequestedSize(requestedSize); 0065 return m_fileIconProvider->pixmapForIconId(imageId).value<QPixmap>(); 0066 } else if (imageId.startsWith("data")) { 0067 if (QByteArray data = getImageData(); !data.isEmpty()) { 0068 if (uint hash = qHash(data); 0069 m_dataPixmap.isNull() || hash != m_pixmapHash) { 0070 if (m_dataPixmap.loadFromData(data)) { 0071 if (size) { 0072 *size = m_dataPixmap.size(); 0073 } 0074 if (requestedSize.isValid()) { 0075 m_dataPixmap = m_dataPixmap.scaled(requestedSize, 0076 Qt::KeepAspectRatio); 0077 } 0078 if (!m_dataPixmap.isNull()) { 0079 m_pixmapHash = hash; 0080 } 0081 } 0082 } 0083 if (!m_dataPixmap.isNull()) { 0084 return m_dataPixmap; 0085 } 0086 } 0087 static QPixmap emptyPixmap; 0088 if (emptyPixmap.isNull()) { 0089 emptyPixmap = QPixmap(1, 1); 0090 emptyPixmap.fill(Qt::transparent); 0091 } 0092 return emptyPixmap; 0093 } 0094 return QPixmap(); 0095 }