Warning, file /office/calligra/libs/widgets/KoContextBarButton.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // vim: set tabstop=4 shiftwidth=4 noexpandtab: 0002 /* This file is part of the KDE project 0003 Copyright 2011 Aurélien Gâteau <agateau@kde.org> 0004 Copyright 2011 Paul Mendez <paulestebanms@gmail.com> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include "KoContextBarButton.h" 0023 0024 // Qt 0025 #include <QIcon> 0026 #include <QStyleOptionToolButton> 0027 #include <QStylePainter> 0028 #include <QPainterPath> 0029 #include <QApplication> 0030 0031 /** How lighter is the border of context bar buttons */ 0032 const int CONTEXTBAR_BORDER_LIGHTNESS = 140; 0033 0034 /** How darker is the background of context bar buttons */ 0035 const int CONTEXTBAR_BACKGROUND_DARKNESS = 80; 0036 0037 /** How lighter are context bar buttons when under mouse */ 0038 const int CONTEXTBAR_MOUSEOVER_LIGHTNESS = 120; 0039 0040 /** Radius of ContextBarButtons */ 0041 const int CONTEXTBAR_RADIUS = 50; 0042 0043 KoContextBarButton::KoContextBarButton(const QString &iconName, QWidget* parent) 0044 : QToolButton(parent) 0045 , m_isHovered(false) 0046 , m_fadingValue(0) 0047 , m_fadingTimeLine(nullptr) 0048 { 0049 const int size = QApplication::style()->pixelMetric(QStyle::PM_ButtonIconSize); 0050 setIconSize(QSize(size, size)); 0051 setAutoRaise(true); 0052 setIcon(QIcon::fromTheme(iconName)); 0053 } 0054 0055 void KoContextBarButton::paintEvent(QPaintEvent*) 0056 { 0057 QStylePainter painter(this); 0058 painter.setRenderHint(QPainter::Antialiasing); 0059 QStyleOptionToolButton opt; 0060 initStyleOption(&opt); 0061 0062 const QColor bgColor = palette().color(QPalette::Highlight); 0063 QColor color = bgColor.dark(CONTEXTBAR_BACKGROUND_DARKNESS); 0064 QColor borderColor = bgColor.light(CONTEXTBAR_BORDER_LIGHTNESS); 0065 0066 if (opt.state & QStyle::State_MouseOver && opt.state & QStyle::State_Enabled) { 0067 color = color.light(CONTEXTBAR_MOUSEOVER_LIGHTNESS); 0068 borderColor = borderColor.lighter(CONTEXTBAR_MOUSEOVER_LIGHTNESS); 0069 } 0070 0071 const QRectF rectF = QRectF(opt.rect).adjusted(0.5, 0.5, -0.5, -0.5); 0072 QPainterPath path; 0073 path.addRoundRect(rectF, CONTEXTBAR_RADIUS, CONTEXTBAR_RADIUS); 0074 0075 if (m_fadingValue < 255) { 0076 color.setAlpha(m_fadingValue); 0077 } 0078 0079 // Background 0080 painter.fillPath(path, color); 0081 0082 if (opt.state & QStyle::State_Raised && opt.state & QStyle::State_Enabled) { 0083 // Bottom shadow 0084 QLinearGradient gradient(rectF.bottomLeft(), rectF.bottomLeft() - QPoint(0, 5)); 0085 gradient.setColorAt(0, QColor::fromHsvF(0, 0, 0, .3)); 0086 gradient.setColorAt(1, Qt::transparent); 0087 painter.fillPath(path, gradient); 0088 0089 // Left shadow 0090 gradient.setFinalStop(rectF.bottomLeft() + QPoint(3, 0)); 0091 painter.fillPath(path, gradient); 0092 } 0093 else { 0094 // Top shadow 0095 QLinearGradient gradient(rectF.topLeft(), rectF.topLeft() + QPoint(0, 5)); 0096 gradient.setColorAt(0, QColor::fromHsvF(0, 0, 0, .3)); 0097 gradient.setColorAt(1, Qt::transparent); 0098 painter.fillPath(path, gradient); 0099 0100 // Left shadow 0101 gradient.setFinalStop(rectF.topLeft() + QPoint(5, 0)); 0102 painter.fillPath(path, gradient); 0103 } 0104 0105 // Border 0106 painter.setPen(QPen(borderColor, 0)); 0107 painter.drawPath(path); 0108 0109 // Content 0110 painter.drawControl(QStyle::CE_ToolButtonLabel, opt); 0111 } 0112 0113 void KoContextBarButton::startFading() 0114 { 0115 Q_ASSERT(!m_fadingTimeLine); 0116 0117 const int duration = 300; 0118 0119 m_fadingTimeLine = new QTimeLine(duration, this); 0120 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)), 0121 this, SLOT(setFadingValue(int))); 0122 m_fadingTimeLine->setFrameRange(0, 255); 0123 m_fadingTimeLine->start(); 0124 m_fadingValue = 0; 0125 } 0126 0127 void KoContextBarButton::stopFading() 0128 { 0129 if (m_fadingTimeLine) { 0130 m_fadingTimeLine->stop(); 0131 delete m_fadingTimeLine; 0132 m_fadingTimeLine = nullptr; 0133 } 0134 m_fadingValue = 0; 0135 } 0136 0137 void KoContextBarButton::enterEvent(QEvent *event) 0138 { 0139 QToolButton::enterEvent(event); 0140 0141 // if the mouse cursor is above the selection toggle, display 0142 // it immediately without fading timer 0143 m_isHovered = true; 0144 if (m_fadingTimeLine) { 0145 m_fadingTimeLine->stop(); 0146 } 0147 m_fadingValue = 255; 0148 update(); 0149 } 0150 0151 void KoContextBarButton::leaveEvent(QEvent *event) 0152 { 0153 QToolButton::leaveEvent(event); 0154 0155 m_isHovered = false; 0156 update(); 0157 } 0158 0159 void KoContextBarButton::setFadingValue(int value) 0160 { 0161 m_fadingValue = value; 0162 if (m_fadingValue >= 255) { 0163 Q_ASSERT(m_fadingTimeLine); 0164 m_fadingTimeLine->stop(); 0165 } 0166 update(); 0167 } 0168 0169 void KoContextBarButton::showEvent(QShowEvent *event) 0170 { 0171 stopFading(); 0172 startFading(); 0173 QToolButton::showEvent(event); 0174 } 0175 0176 void KoContextBarButton::hideEvent(QHideEvent *event) 0177 { 0178 stopFading(); 0179 QToolButton::hideEvent(event); 0180 }