File indexing completed on 2024-05-05 04:48:42
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #include "PlaylistBreadcrumbItemSortButton.h" 0018 0019 #include <QIcon> 0020 0021 #include <QMargins> 0022 #include <QPainter> 0023 #include <QStyle> 0024 #include <QStyleOption> 0025 0026 namespace Playlist 0027 { 0028 0029 BreadcrumbItemSortButton::BreadcrumbItemSortButton( QWidget *parent ) 0030 : BreadcrumbItemButton( parent ) 0031 , m_order( Qt::AscendingOrder ) 0032 , m_arrowPressed( false ) 0033 , m_arrowHovered( false ) 0034 , m_arrowWidth( 11 ) 0035 , m_arrowHeight( 13 ) 0036 { 0037 init(); 0038 } 0039 0040 BreadcrumbItemSortButton::BreadcrumbItemSortButton(const QIcon &icon, const QString &text, QWidget *parent ) 0041 : BreadcrumbItemButton( icon, text, parent ) 0042 , m_order( Qt::AscendingOrder ) 0043 , m_arrowPressed( false ) 0044 , m_arrowHovered( false ) 0045 , m_arrowWidth( 11 ) 0046 , m_arrowHeight( 13 ) 0047 { 0048 init(); 0049 } 0050 0051 BreadcrumbItemSortButton::~BreadcrumbItemSortButton() 0052 {} 0053 0054 void 0055 BreadcrumbItemSortButton::init() 0056 { 0057 setMouseTracking( true ); 0058 Q_EMIT arrowToggled( m_order ); 0059 repaint(); 0060 } 0061 0062 void 0063 BreadcrumbItemSortButton::paintEvent( QPaintEvent *event ) 0064 { 0065 Q_UNUSED( event ) 0066 QPainter painter(this); 0067 0068 const int buttonHeight = height(); 0069 const int preferredWidth = qMax( minimumWidth(), sizeHint().width() ); 0070 const int buttonWidth = qMin( preferredWidth, width() ); 0071 0072 QMargins margins = contentsMargins(); 0073 const int padding = 2; 0074 0075 const int arrowLeft = buttonWidth - m_arrowWidth - padding; 0076 const int arrowTop = ( ( buttonHeight - margins.top() - margins.bottom()) - m_arrowHeight )/2; 0077 m_arrowRect = QRect( arrowLeft, arrowTop, m_arrowWidth, m_arrowHeight ); 0078 0079 drawHoverBackground( &painter ); 0080 0081 const QColor fgColor = foregroundColor(); 0082 QStyleOption option; 0083 option.initFrom(this); 0084 option.rect = m_arrowRect; 0085 option.palette = palette(); 0086 option.palette.setColor(QPalette::Text, fgColor); 0087 option.palette.setColor(QPalette::WindowText, fgColor); 0088 option.palette.setColor(QPalette::ButtonText, fgColor); 0089 0090 if( m_order == Qt::DescendingOrder ) 0091 style()->drawPrimitive( QStyle::PE_IndicatorArrowDown, &option, &painter, this ); 0092 else 0093 style()->drawPrimitive( QStyle::PE_IndicatorArrowUp, &option, &painter, this ); 0094 0095 QRect newPaintRect( 0, 0, buttonWidth - m_arrowWidth - padding, buttonHeight ); 0096 QPaintEvent newEvent( newPaintRect ); 0097 BreadcrumbItemButton::paintEvent( &newEvent ); 0098 } 0099 0100 void 0101 BreadcrumbItemSortButton::drawHoverBackground( QPainter *painter ) 0102 { 0103 const bool isHovered = isDisplayHintEnabled( HoverHint ); 0104 if( isHovered ) 0105 { 0106 QStyleOptionViewItem option; 0107 option.initFrom(this); 0108 option.state = QStyle::State_Enabled | QStyle::State_MouseOver; 0109 option.viewItemPosition = QStyleOptionViewItem::OnlyOne; 0110 0111 if( m_arrowHovered ) 0112 { 0113 option.rect = m_arrowRect; 0114 } 0115 0116 style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, this ); 0117 } 0118 } 0119 0120 void 0121 BreadcrumbItemSortButton::mouseMoveEvent( QMouseEvent *e ) 0122 { 0123 bool oldArrowHovered = m_arrowHovered; 0124 m_arrowHovered = m_arrowRect.contains( e->pos() ); 0125 if( oldArrowHovered != m_arrowHovered ) 0126 repaint(); 0127 BreadcrumbItemButton::mouseMoveEvent( e ); 0128 } 0129 0130 void 0131 BreadcrumbItemSortButton::mousePressEvent( QMouseEvent *e ) 0132 { 0133 m_pressedPos = e->pos(); 0134 if( m_arrowRect.contains( m_pressedPos ) ) 0135 m_arrowPressed = true; 0136 else 0137 { 0138 m_arrowPressed = false; 0139 BreadcrumbItemButton::mousePressEvent( e ); 0140 } 0141 } 0142 0143 void 0144 BreadcrumbItemSortButton::mouseReleaseEvent( QMouseEvent *e ) 0145 { 0146 if( m_arrowPressed && e->pos() == m_pressedPos ) 0147 invertOrder(); 0148 BreadcrumbItemButton::mouseReleaseEvent( e ); 0149 } 0150 0151 void 0152 BreadcrumbItemSortButton::invertOrder() 0153 { 0154 if( m_order == Qt::DescendingOrder ) 0155 m_order = Qt::AscendingOrder; 0156 else //ascending 0157 m_order = Qt::DescendingOrder; 0158 Q_EMIT arrowToggled( m_order ); 0159 repaint(); 0160 } 0161 0162 QSize 0163 BreadcrumbItemSortButton::sizeHint() const 0164 { 0165 QSize size = BreadcrumbItemButton::sizeHint(); 0166 size.setWidth( size.width() + m_arrowWidth ); 0167 return size; 0168 } 0169 0170 Qt::SortOrder 0171 BreadcrumbItemSortButton::orderState() const 0172 { 0173 return m_order; 0174 } 0175 0176 }