File indexing completed on 2024-05-19 04:29:52

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2023 Halla Rempt <halla@valdyas.org>
0003  * SPDX-FileCopyrightText: 2023 Alvin Wong <alvin@alvinhc.com>
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "KisClickableLabel.h"
0007 
0008 #include <QResizeEvent>
0009 #include <QtMath>
0010 #include <QBoxLayout>
0011 #include <kis_icon_utils.h>
0012 
0013 KisClickableLabel::KisClickableLabel(QWidget* parent)
0014     : QLabel(parent)
0015 {
0016     m_closeButton = new QPushButton(this);
0017     m_closeButton->setGeometry(0,0,16,16);
0018     m_closeButton->setFlat(true);
0019     m_closeButton->setIcon(KisIconUtils::loadIcon("dark_close-tab"));
0020     connect(m_closeButton, &QPushButton::clicked, this, [&](){
0021         emit dismissed();
0022     });
0023 
0024     setDismissable(true);
0025 }
0026 
0027 KisClickableLabel::~KisClickableLabel() {}
0028 
0029 bool KisClickableLabel::hasHeightForWidth() const
0030 {
0031     return true;
0032 }
0033 
0034 int KisClickableLabel::heightForWidth(int w) const
0035 {
0036     if (m_pixmap.isNull()) {
0037         return height();
0038     }
0039     return qCeil(static_cast<qreal>(w) * m_pixmap.height() / m_pixmap.width());
0040 }
0041 
0042 QSize KisClickableLabel::minimumSizeHint() const
0043 {
0044     return {};
0045 }
0046 
0047 QSize KisClickableLabel::sizeHint() const
0048 {   
0049     return {};
0050 }
0051 
0052 void KisClickableLabel::setUnscaledPixmap(QPixmap pixmap)
0053 {
0054     m_pixmap = std::move(pixmap);
0055     setMaximumSize(m_pixmap.size());
0056     updatePixmap();
0057 }
0058 
0059 void KisClickableLabel::updatePixmap()
0060 {
0061     if (!m_pixmap.isNull()) {
0062         // setPixmap(m_pixmap.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
0063         setPixmap(m_pixmap.scaledToWidth(width(), Qt::SmoothTransformation));
0064     }
0065 }
0066 
0067 void KisClickableLabel::setDismissable(bool value)
0068 {
0069     m_dismissable = value;
0070 
0071     m_closeButton->setVisible(m_dismissable);
0072 }
0073 
0074 bool KisClickableLabel::isDismissable()
0075 {
0076     return m_dismissable;
0077 }
0078 
0079 void KisClickableLabel::mousePressEvent(QMouseEvent *event)
0080 {
0081     Q_UNUSED(event);
0082     emit clicked();
0083 }
0084 
0085 void KisClickableLabel::resizeEvent(QResizeEvent */*event*/)
0086 {
0087     updatePixmap();
0088 }