File indexing completed on 2025-02-23 04:34:24

0001 /**
0002  * \file picturelabel.cpp
0003  * Label for picture preview.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 04 Jan 2009
0008  *
0009  * Copyright (C) 2009-2024  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 "picturelabel.h"
0028 #include <QLabel>
0029 #include <QVBoxLayout>
0030 #include <QHash>
0031 #include <QByteArray>
0032 #include <QPixmap>
0033 #include <QCoreApplication>
0034 
0035 namespace {
0036 
0037 class PictureLabelIntern : public QLabel {
0038 public:
0039   explicit PictureLabelIntern(QWidget* parent = nullptr);
0040   ~PictureLabelIntern() override = default;
0041   int heightForWidth(int w) const override;
0042 
0043 private:
0044   Q_DISABLE_COPY(PictureLabelIntern)
0045 };
0046 
0047 PictureLabelIntern::PictureLabelIntern(QWidget* parent) : QLabel(parent)
0048 {
0049   setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0050   setWordWrap(true);
0051 }
0052 
0053 int PictureLabelIntern::heightForWidth(int w) const
0054 {
0055   return w;
0056 }
0057 
0058 }
0059 
0060 /**
0061  * Constructor.
0062  *
0063  * @param parent parent widget
0064  */
0065 PictureLabel::PictureLabel(QWidget* parent)
0066   : QWidget(parent), m_pixmapHash(0)
0067 {
0068   setObjectName(QLatin1String("PictureLabel"));
0069   auto layout = new QVBoxLayout(this);
0070   layout->setContentsMargins(0, 0, 0, 0);
0071   m_pictureLabel = new PictureLabelIntern;
0072   layout->addWidget(m_pictureLabel);
0073   m_sizeLabel = new QLabel;
0074   m_sizeLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0075   layout->addWidget(m_sizeLabel);
0076   clearPicture();
0077 }
0078 
0079 /**
0080  * Destructor.
0081  */
0082 PictureLabel::~PictureLabel()
0083 {
0084   // not inline or default to silence weak-vtables warning
0085 }
0086 
0087 /**
0088  * Clear picture.
0089  */
0090 void PictureLabel::clearPicture()
0091 {
0092   const char* const msg = QT_TRANSLATE_NOOP("@default", "Drag album\nartwork\nhere");
0093   m_pictureLabel->setText(QCoreApplication::translate("@default", msg));
0094   m_sizeLabel->clear();
0095 }
0096 
0097 /**
0098  * Set picture data.
0099  *
0100  * @param data picture data, empty if no picture is available
0101  */
0102 void PictureLabel::setData(const QByteArray& data)
0103 {
0104   if (!data.isEmpty()) {
0105     uint hash = qHash(data);
0106 #if QT_VERSION >= 0x050f00
0107     if (!m_pictureLabel->pixmap(Qt::ReturnByValue).isNull() && hash == m_pixmapHash)
0108 #else
0109     if (m_pictureLabel->pixmap() && hash == m_pixmapHash)
0110 #endif
0111       return; // keep existing pixmap
0112 
0113     // creating new pixmap
0114     if (QPixmap pm; pm.loadFromData(data)) {
0115       int dimension = m_pictureLabel->width();
0116       if (QPixmap scaledPm = pm.scaled(dimension, dimension, Qt::KeepAspectRatio);
0117           !scaledPm.isNull()) {
0118         m_pixmapHash = hash;
0119         m_pictureLabel->setContentsMargins(0, 0, 0, 0);
0120         m_pictureLabel->setPixmap(scaledPm);
0121         m_sizeLabel->setText(QString::number(pm.width()) + QLatin1Char('x') +
0122                              QString::number(pm.height()));
0123         return;
0124       }
0125     }
0126   }
0127 
0128   clearPicture();
0129 }