File indexing completed on 2024-05-12 16:39:56
0001 /* This file is part of the KDE project 0002 Copyright (C) 2012 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KexiLinkButton.h" 0021 #include "utils.h" 0022 0023 #include <KGuiItem> 0024 0025 #include <QCursor> 0026 #include <QStyle> 0027 #include <QStyleOptionButton> 0028 #include <QEvent> 0029 0030 class Q_DECL_HIDDEN KexiLinkButton::Private 0031 { 0032 public: 0033 Private() 0034 : usesForegroundColor(false) 0035 { 0036 } 0037 bool usesForegroundColor; 0038 //! Used to remember the orig icon so setUsesForegroundColor(false) is possible 0039 QIcon origIcon; 0040 }; 0041 0042 KexiLinkButton::KexiLinkButton(QWidget* parent) 0043 : QPushButton(parent), d(new Private) 0044 { 0045 init(); 0046 } 0047 0048 KexiLinkButton::KexiLinkButton(const QIcon &icon, QWidget* parent) 0049 : QPushButton(icon, QString(), parent), d(new Private) 0050 { 0051 init(); 0052 } 0053 0054 KexiLinkButton::KexiLinkButton(const QPixmap &pixmap, QWidget* parent) 0055 : QPushButton(pixmap, QString(), parent), d(new Private) 0056 { 0057 init(); 0058 } 0059 0060 KexiLinkButton::KexiLinkButton(const KGuiItem &item, QWidget *parent) 0061 : QPushButton(item.icon(), item.text(), parent), d(new Private) 0062 { 0063 init(); 0064 } 0065 0066 KexiLinkButton::~KexiLinkButton() 0067 { 0068 delete d; 0069 } 0070 0071 void KexiLinkButton::init() 0072 { 0073 setFlat(true); 0074 setText(QString()); 0075 setCursor(QCursor(Qt::PointingHandCursor)); 0076 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); 0077 setForegroundRole(QPalette::Text); 0078 QStyleOptionButton option; 0079 option.initFrom(this); 0080 int m = style()->pixelMetric(QStyle::PM_ButtonMargin, &option, this); 0081 setFixedSize(iconSize() + QSize(m*2, m*2)); 0082 d->origIcon = icon(); 0083 } 0084 0085 void KexiLinkButton::setUsesForegroundColor(bool set) 0086 { 0087 if (d->usesForegroundColor == set) 0088 return; 0089 d->usesForegroundColor = set; 0090 setIcon(d->origIcon); 0091 } 0092 0093 bool KexiLinkButton::usesForegroundColor() const 0094 { 0095 return d->usesForegroundColor; 0096 } 0097 0098 void KexiLinkButton::changeEvent(QEvent* event) 0099 { 0100 switch (event->type()) { 0101 case QEvent::EnabledChange: 0102 case QEvent::PaletteChange: 0103 updateIcon(icon()); 0104 break; 0105 default:; 0106 } 0107 QPushButton::changeEvent(event); 0108 } 0109 0110 void KexiLinkButton::updateIcon(const QIcon &icon) 0111 { 0112 if (!d->usesForegroundColor) 0113 return; 0114 QColor c(palette().color(foregroundRole())); 0115 QPixmap pixmap(icon.pixmap(iconSize())); 0116 KexiUtils::replaceColors(&pixmap, c); 0117 QPushButton::setIcon(pixmap); 0118 } 0119 0120 void KexiLinkButton::setIcon(const QIcon &icon) 0121 { 0122 d->origIcon = icon; 0123 if (d->usesForegroundColor) { 0124 updateIcon(d->origIcon); 0125 } 0126 else { 0127 QPushButton::setIcon(d->origIcon); 0128 } 0129 }