File indexing completed on 2024-05-05 04:47:19
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org> * 0003 * Copyright (c) 2012 Lachlan Dufton <dufton@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #include "AnimatedBarWidget.h" 0019 0020 #include <QIcon> 0021 0022 #include <QHBoxLayout> 0023 #include <QMargins> 0024 #include <QPainter> 0025 #include <QStyleOption> 0026 0027 AnimatedBarWidget::AnimatedBarWidget( const QIcon &icon, const QString &text, const QString &animatedIconName, QWidget *parent ) 0028 : QAbstractButton( parent ) 0029 { 0030 setIconSize( QSize( 22, 22 ) ); 0031 setIcon( icon ); 0032 setText( text ); 0033 m_animating = false; 0034 m_animatedWidget = new AnimatedWidget( animatedIconName, this ); 0035 m_animatedWidget->setFixedSize( 22, 22 ); 0036 m_animatedWidget->hide(); 0037 m_animatedWidget->setAutoFillBackground( false ); 0038 0039 setFocusPolicy( Qt::NoFocus ); 0040 m_hoverHint = false; 0041 0042 // Need to change the size policy to allow for wrapping 0043 QSizePolicy poli( QSizePolicy::Preferred, QSizePolicy::Preferred ); 0044 poli.setHeightForWidth( true ); 0045 setSizePolicy( poli ); 0046 } 0047 0048 AnimatedBarWidget::~AnimatedBarWidget() 0049 {} 0050 0051 void 0052 AnimatedBarWidget::animate() 0053 { 0054 m_animating = true; 0055 m_animatedWidget->show(); 0056 m_animatedWidget->start(); 0057 update(); 0058 } 0059 0060 void 0061 AnimatedBarWidget::stop() 0062 { 0063 m_animating = false; 0064 m_animatedWidget->stop(); 0065 m_animatedWidget->hide(); 0066 update(); 0067 } 0068 0069 void 0070 AnimatedBarWidget::fold() 0071 { 0072 hide(); 0073 } 0074 0075 //protected: 0076 0077 0078 0079 void 0080 AnimatedBarWidget::setHoverHintEnabled( bool enable ) 0081 { 0082 m_hoverHint = enable; 0083 update(); 0084 } 0085 0086 bool 0087 AnimatedBarWidget::isHoverHintEnabled() const 0088 { 0089 return m_hoverHint; 0090 } 0091 0092 void 0093 AnimatedBarWidget::enterEvent( QEvent* event ) 0094 { 0095 QWidget::enterEvent( event ); 0096 setHoverHintEnabled( true ); 0097 update(); 0098 } 0099 0100 void 0101 AnimatedBarWidget::leaveEvent( QEvent* event ) 0102 { 0103 QWidget::leaveEvent( event ); 0104 setHoverHintEnabled( false ); 0105 update(); 0106 } 0107 0108 void 0109 AnimatedBarWidget::paintEvent( QPaintEvent* event ) 0110 { 0111 Q_UNUSED(event); 0112 0113 QPainter painter(this); 0114 0115 const int buttonHeight = height(); 0116 int buttonWidth = width(); 0117 0118 drawHoverBackground(&painter); 0119 0120 const QMargins margins = contentsMargins(); 0121 const int padding = 2; 0122 const int iconWidth = iconSize().width(); 0123 const int iconHeight = iconSize().height(); 0124 const int iconTop = ( (buttonHeight - margins.top() - margins.bottom()) - iconHeight ) / 2; 0125 0126 if( !m_animating ) 0127 { 0128 const QRect iconRect( margins.left() + padding, iconTop, iconWidth, iconHeight ); 0129 painter.drawPixmap( iconRect, icon().pixmap( iconSize() ) ); 0130 } 0131 else 0132 m_animatedWidget->move( margins.left() + padding, iconTop ); 0133 0134 const QRect textRect( margins.left() + (padding * 3) + iconWidth, margins.top(), 0135 buttonWidth - (margins.left() + padding * 3 + iconWidth) - padding, buttonHeight); 0136 QFontMetrics fm( font() ); 0137 painter.drawText( textRect, Qt::AlignVCenter | Qt::TextWordWrap, text() ); 0138 } 0139 0140 0141 void 0142 AnimatedBarWidget::drawHoverBackground(QPainter* painter) 0143 { 0144 const bool isHovered = isHoverHintEnabled(); 0145 if( isHovered ) 0146 { 0147 QStyleOptionViewItem option; 0148 option.initFrom(this); 0149 option.state = QStyle::State_Enabled | QStyle::State_Selected; 0150 option.viewItemPosition = QStyleOptionViewItem::OnlyOne; 0151 style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, this ); 0152 } 0153 else 0154 { 0155 QStyleOptionViewItem option; 0156 option.initFrom(this); 0157 option.state = QStyle::State_Enabled | QStyle::State_MouseOver; 0158 option.viewItemPosition = QStyleOptionViewItem::OnlyOne; 0159 style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, this ); 0160 } 0161 } 0162 0163 QColor 0164 AnimatedBarWidget::foregroundColor() const 0165 { 0166 const bool isHighlighted = isHoverHintEnabled(); 0167 0168 QColor foregroundColor = palette().color( foregroundRole() ); 0169 if( !isHighlighted ) 0170 foregroundColor.setAlpha( 60 ); 0171 0172 return foregroundColor; 0173 } 0174 0175 0176 QSize 0177 AnimatedBarWidget::sizeHint() const 0178 { 0179 QSize size = QAbstractButton::sizeHint(); 0180 size.setHeight( iconSize().height() + 8 ); 0181 return size; 0182 } 0183 0184 /** 0185 * Find the height required to fit the widget with wrapped text 0186 * and the icon, plus padding 0187 */ 0188 int 0189 AnimatedBarWidget::heightForWidth(int w) const 0190 { 0191 const int hPadding = 2; 0192 const int vPadding = 4; 0193 // Padding to the left and right of both the icon and text 0194 const QRect bound( 0, 0, (w - hPadding * 4 - iconSize().width()), 0 ); 0195 QFontMetrics fm( font() ); 0196 int fontHeight = fm.boundingRect( bound, Qt::TextWordWrap, text() ).height(); 0197 if( fontHeight < iconSize().height() ) 0198 return iconSize().height() + 2 * vPadding; 0199 0200 return fontHeight + 2 * vPadding; 0201 }