File indexing completed on 2025-01-26 05:24:12

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "iconlabel.hpp"
0010 
0011 // Qt
0012 #include <QEvent>
0013 #include <QStyle>
0014 
0015 namespace Kasten {
0016 
0017 IconLabel::IconLabel(QWidget* parent)
0018     : QLabel(parent)
0019 {
0020     setAlignment(Qt::AlignCenter);
0021     updateSize();
0022 }
0023 
0024 void IconLabel::setIcon(const QIcon& icon)
0025 {
0026     mIcon = icon;
0027     updatePixmap();
0028 }
0029 
0030 void IconLabel::changeEvent(QEvent* event)
0031 {
0032     QLabel::changeEvent(event);
0033 
0034     if (event->type() == QEvent::PaletteChange || event->type() == QEvent::StyleChange) {
0035         if (!mIcon.isNull()) {
0036             updatePixmap();
0037         }
0038     }
0039     if (event->type() == QEvent::StyleChange) {
0040         updateSize();
0041     }
0042 }
0043 
0044 QSize IconLabel::iconSize() const
0045 {
0046     const int iconSizeExtent = style()->pixelMetric(QStyle::PM_ButtonIconSize, nullptr, this);
0047     return QSize(iconSizeExtent, iconSizeExtent);
0048 }
0049 
0050 void IconLabel::updateSize()
0051 {
0052     setFixedSize(iconSize());
0053 }
0054 
0055 void IconLabel::updatePixmap()
0056 {
0057     const QPixmap pixmap = mIcon.pixmap(iconSize());
0058     setPixmap(pixmap);
0059 }
0060 
0061 }
0062 
0063 #include "moc_iconlabel.cpp"