File indexing completed on 2024-05-12 16:39:55

0001 /* This file is part of the KDE project
0002    Copyright (C) 2014 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 "KexiCloseButton.h"
0021 #include "utils.h"
0022 
0023 #include <KStandardGuiItem>
0024 
0025 #include <QIcon>
0026 #include <QApplication>
0027 #include <QStyle>
0028 #include <QStyleOptionButton>
0029 #include <QPainter>
0030 #include <QPaintEvent>
0031 
0032 class Q_DECL_HIDDEN KexiCloseButton::Private
0033 {
0034 public:
0035     Private()
0036     {
0037     }
0038     bool dummy = true;
0039 };
0040 
0041 KexiCloseButton::KexiCloseButton(QWidget* parent)
0042  : QToolButton(parent), d(new Private)
0043 {
0044     init();
0045 }
0046 
0047 KexiCloseButton::~KexiCloseButton()
0048 {
0049     delete d;
0050 }
0051 
0052 void KexiCloseButton::init()
0053 {
0054     setToolTip(KStandardGuiItem::close().plainText());
0055     setAutoRaise(true);
0056     setText(QString());
0057     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0058     setFocusPolicy(Qt::NoFocus);
0059     setMarginEnabled(true);
0060 }
0061 
0062 void KexiCloseButton::setMarginEnabled(bool set)
0063 {
0064     QStyleOptionButton option;
0065     option.initFrom(this);
0066     int m = set ? style()->pixelMetric(QStyle::PM_ButtonMargin, &option, this) : 0;
0067     const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, &option, this);
0068     setFixedSize(QSize(iconSize, iconSize) + QSize(m*2, m*2));
0069     update();
0070 }
0071 
0072 void KexiCloseButton::paintEvent(QPaintEvent *e)
0073 {
0074     Q_UNUSED(e);
0075     if (style()->objectName() != "breeze" && QApplication::style()->objectName() != "breeze") {
0076         // Draw frames. Breeze's close button has no frames.
0077         QToolButton::paintEvent(e);
0078     }
0079     QStyleOptionButton option;
0080     option.initFrom(this);
0081     QIcon icon(style()->standardIcon(QStyle::SP_TitleBarCloseButton, &option, this));
0082     QPainter p(this);
0083     const int metric = style()->pixelMetric(QStyle::PM_SmallIconSize, &option, this);
0084     QSize iconSize(metric, metric);
0085     const QSize margin = (size() - iconSize) / 2;
0086     QRect iconRect(QPoint(margin.width(), margin.height()), iconSize);
0087     const bool enabled(option.state & QStyle::State_Enabled);
0088     QIcon::Mode mode;
0089     if (option.state & QStyle::State_MouseOver) {
0090         mode = QIcon::Active;
0091     }
0092     else if (enabled) {
0093         mode = QIcon::Normal;
0094     }
0095     else {
0096         mode = QIcon::Disabled;
0097     }
0098     const QIcon::State iconState((isDown() & Qt::LeftButton) ? QIcon::On : QIcon::Off);
0099     const QPixmap pixmap = icon.pixmap(iconSize, mode, iconState);
0100     style()->drawItemPixmap(&p, iconRect, Qt::AlignCenter, pixmap);
0101 }
0102