Warning, file /office/calligra/libs/widgets/KoDockWidgetTitleBarButton.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (c) 2007 Marijn Kruisselbrink <mkruisselbrink@kde.org>
0003    Copyright (C) 2007 Thomas Zander <zander@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library 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 GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoDockWidgetTitleBarButton.h"
0022 
0023 #include <WidgetsDebug.h>
0024 
0025 #include <QAbstractButton>
0026 #include <QAction>
0027 #include <QLabel>
0028 #include <QLayout>
0029 #include <QStyle>
0030 #include <QStylePainter>
0031 #include <QStyleOptionFrame>
0032 
0033 class Q_DECL_HIDDEN KoDockWidgetTitleBarButton::Private
0034 {
0035 public:
0036     Private() : styleSize(0, 0), iconSize(0) {}
0037     QSize styleSize;
0038     int iconSize;
0039 };
0040 
0041 KoDockWidgetTitleBarButton::KoDockWidgetTitleBarButton(QWidget *parent)
0042         : QAbstractButton(parent), d(new Private())
0043 {
0044     setFocusPolicy(Qt::NoFocus);
0045 }
0046 
0047 KoDockWidgetTitleBarButton::~KoDockWidgetTitleBarButton()
0048 {
0049     delete d;
0050 }
0051 
0052 QSize KoDockWidgetTitleBarButton::sizeHint() const
0053 {
0054     ensurePolished();
0055 
0056     const int margin = style()->pixelMetric(QStyle::PM_DockWidgetTitleBarButtonMargin, 0, this);
0057     if (icon().isNull()) {
0058         return QSize(18, 18); // TODO: was QSize(margin, margin), hardcoded values seem bad
0059     }
0060 
0061     int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
0062     if (iconSize != d->iconSize) {
0063         const_cast<KoDockWidgetTitleBarButton*>(this)->d->iconSize = iconSize;
0064         const QPixmap pm = icon().pixmap(iconSize);
0065         const_cast<KoDockWidgetTitleBarButton*>(this)->d->styleSize = QSize(pm.width() + margin, pm.height() + margin);
0066     }
0067     return d->styleSize;
0068 }
0069 
0070 QSize KoDockWidgetTitleBarButton::minimumSizeHint() const
0071 {
0072     return sizeHint();
0073 }
0074 
0075 // redraw the button when the mouse enters or leaves it
0076 void KoDockWidgetTitleBarButton::enterEvent(QEvent *event)
0077 {
0078     if (isEnabled())
0079         update();
0080     QAbstractButton::enterEvent(event);
0081 }
0082 
0083 void KoDockWidgetTitleBarButton::leaveEvent(QEvent *event)
0084 {
0085     if (isEnabled())
0086         update();
0087     QAbstractButton::leaveEvent(event);
0088 }
0089 
0090 void KoDockWidgetTitleBarButton::paintEvent(QPaintEvent *)
0091 {
0092     QPainter p(this);
0093 
0094     QStyleOptionToolButton opt;
0095     opt.initFrom(this);
0096     opt.state |= QStyle::State_AutoRaise;
0097 
0098     if (isEnabled() && underMouse() && !isChecked() && !isDown())
0099         opt.state |= QStyle::State_Raised;
0100     if (isChecked())
0101         opt.state |= QStyle::State_On;
0102     if (isDown())
0103         opt.state |= QStyle::State_Sunken;
0104     style()->drawPrimitive(QStyle::PE_PanelButtonTool, &opt, &p, this);
0105 
0106     opt.icon = icon();
0107     opt.subControls = 0;
0108     opt.activeSubControls = 0;
0109     opt.features = QStyleOptionToolButton::None;
0110     opt.arrowType = Qt::NoArrow;
0111     int size = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
0112     opt.iconSize = QSize(size, size);
0113     style()->drawComplexControl(QStyle::CC_ToolButton, &opt, &p, this);
0114 }