File indexing completed on 2024-04-28 04:41:47

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Emmanuel Lepage Vallee                          *
0003  *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@kde.org>             *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 3 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0017  **************************************************************************/
0018 #include "decorationadapter.h"
0019 
0020 // Qt
0021 #include <QtGui/QPainter>
0022 
0023 class DecorationAdapterPrivate
0024 {
0025 public:
0026     QPixmap m_Pixmap;
0027     QIcon   m_Icon  ;
0028     QString m_ThemeFallback;
0029     QIcon   m_FallbackIcon;
0030 };
0031 
0032 DecorationAdapter::DecorationAdapter(QQuickItem* parent) : QQuickPaintedItem(parent),
0033     d_ptr(new DecorationAdapterPrivate())
0034 {}
0035 
0036 DecorationAdapter::~DecorationAdapter()
0037 {
0038     delete d_ptr;
0039 }
0040 
0041 QPixmap DecorationAdapter::pixmap() const {
0042     return d_ptr->m_Pixmap;
0043 }
0044 
0045 void DecorationAdapter::setPixmap(const QVariant& var)
0046 {
0047     d_ptr->m_Pixmap = qvariant_cast<QPixmap>(var);
0048     d_ptr->m_Icon   = qvariant_cast<QIcon  >(var);
0049     update();
0050     emit changed();
0051 }
0052 
0053 void DecorationAdapter::paint(QPainter *painter)
0054 {
0055     if (!d_ptr->m_Icon.isNull()) {
0056         const QPixmap pxm = d_ptr->m_Icon.pixmap(boundingRect().size().toSize());
0057 
0058         painter->drawPixmap(
0059             boundingRect().toRect(),
0060             pxm,
0061             pxm.rect()
0062         );
0063     }
0064     else if (!d_ptr->m_Pixmap.isNull()) {
0065         painter->drawPixmap(
0066             boundingRect(),
0067             d_ptr->m_Pixmap,
0068             d_ptr->m_Pixmap.rect()
0069         );
0070     }
0071     else if (!d_ptr->m_ThemeFallback.isEmpty()) {
0072         if (d_ptr->m_FallbackIcon.isNull())
0073             d_ptr->m_FallbackIcon = QIcon::fromTheme(d_ptr->m_ThemeFallback);
0074 
0075         const QPixmap pxm = d_ptr->m_FallbackIcon.pixmap(boundingRect().size().toSize());
0076 
0077         painter->drawPixmap(
0078             boundingRect().toRect(),
0079             pxm,
0080             pxm.rect()
0081         );
0082     }
0083 }
0084 
0085 QString DecorationAdapter::themeFallback() const
0086 {
0087     return d_ptr->m_ThemeFallback;
0088 }
0089 
0090 void DecorationAdapter::setThemeFallback(const QString& s)
0091 {
0092     d_ptr->m_ThemeFallback = s;
0093     d_ptr->m_FallbackIcon  = {};
0094     update();
0095     emit changed();
0096 }
0097 
0098 bool DecorationAdapter::hasPixmap() const
0099 {
0100     qDebug() << d_ptr->m_FallbackIcon.isNull() << d_ptr->m_Pixmap.isNull() << d_ptr->m_Icon.isNull();
0101     return !(d_ptr->m_FallbackIcon.isNull() && d_ptr->m_Pixmap.isNull() && d_ptr->m_Icon.isNull());
0102 }