File indexing completed on 2024-10-13 04:22:10
0001 /* 0002 SPDX-FileCopyrightText: 2005-2008 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-FileCopyrightText: 2009 Michal Malek <michalm@jabster.pl> 0004 SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "k3bflatbutton.h" 0009 #include "k3bthememanager.h" 0010 #include "k3bapplication.h" 0011 0012 #include <KIconLoader> 0013 0014 #include <QEvent> 0015 #include <QFontMetrics> 0016 #include <QMouseEvent> 0017 #include <QPainter> 0018 #include <QAction> 0019 #include <QFrame> 0020 #include <QToolTip> 0021 0022 0023 namespace { 0024 0025 const int FRAME_WIDTH = 1; 0026 const int FRAME_HEIGHT = 1; 0027 const int ICON_LABEL_SPACE = 5; 0028 const int MARGIN_SIZE = 5; 0029 0030 } // namespace 0031 0032 0033 K3b::FlatButton::FlatButton( QWidget* parent) 0034 : QAbstractButton( parent ) 0035 { 0036 init(); 0037 } 0038 0039 0040 K3b::FlatButton::FlatButton( const QString& text, QWidget* parent ) 0041 : QAbstractButton( parent ) 0042 { 0043 setText( text ); 0044 init(); 0045 } 0046 0047 0048 K3b::FlatButton::FlatButton( QAction* action, QWidget* parent ) 0049 : QAbstractButton( parent ) 0050 { 0051 setText( action->text() ); 0052 setToolTip( action->toolTip() ); 0053 setIcon( action->icon() ); 0054 init(); 0055 0056 connect( this, SIGNAL(clicked(bool)), action, SLOT(trigger()) ); 0057 } 0058 0059 0060 K3b::FlatButton::~FlatButton() 0061 { 0062 } 0063 0064 0065 void K3b::FlatButton::init() 0066 { 0067 setContentsMargins( MARGIN_SIZE + FRAME_WIDTH, MARGIN_SIZE + FRAME_HEIGHT, 0068 MARGIN_SIZE + FRAME_WIDTH, MARGIN_SIZE + FRAME_HEIGHT ); 0069 setIconSize( QSize( KIconLoader::SizeMedium, KIconLoader::SizeMedium ) ); 0070 setHover(false); 0071 0072 connect( k3bappcore->themeManager(), SIGNAL(themeChanged()), this, SLOT(slotThemeChanged()) ); 0073 slotThemeChanged(); 0074 } 0075 0076 0077 bool K3b::FlatButton::event( QEvent *event ) 0078 { 0079 if( event->type() == QEvent::StyleChange ) { 0080 slotThemeChanged(); 0081 } 0082 return QAbstractButton::event(event); 0083 } 0084 0085 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0086 void K3b::FlatButton::enterEvent( QEvent* ) 0087 #else 0088 void K3b::FlatButton::enterEvent( QEnterEvent* ) 0089 #endif 0090 { 0091 setHover(true); 0092 } 0093 0094 0095 void K3b::FlatButton::leaveEvent( QEvent* ) 0096 { 0097 setHover(false); 0098 } 0099 0100 0101 void K3b::FlatButton::setHover( bool b ) 0102 { 0103 m_hover = b; 0104 update(); 0105 } 0106 0107 0108 QSize K3b::FlatButton::sizeHint() const 0109 { 0110 // height: pixmap + spacing + font height + frame width 0111 // width: max( pixmap, text) + frame width 0112 return QSize( qMax( iconSize().width(), fontMetrics().horizontalAdvance( text() ) ) + ( MARGIN_SIZE + FRAME_WIDTH )*2, 0113 iconSize().height() + fontMetrics().height() + ICON_LABEL_SPACE + ( MARGIN_SIZE + FRAME_HEIGHT )*2 ); 0114 } 0115 0116 0117 void K3b::FlatButton::paintEvent( QPaintEvent* event ) 0118 { 0119 QPainter p( this ); 0120 p.setPen( m_hover ? m_backColor : m_foreColor ); 0121 p.fillRect( event->rect(), m_hover ? m_foreColor : m_backColor ); 0122 p.drawRect( event->rect().x(), event->rect().y(), event->rect().width()-1, event->rect().height()-1 ); 0123 0124 QRect rect = contentsRect(); 0125 0126 if( !icon().isNull() ) { 0127 int pixX = rect.left() + qMax( 0, (rect.width() - iconSize().width()) / 2 ); 0128 int pixY = rect.top(); 0129 p.drawPixmap( pixX, pixY, icon().pixmap( iconSize() ) ); 0130 p.drawText( rect, Qt::AlignBottom | Qt::AlignHCenter | Qt::TextHideMnemonic, text() ); 0131 } 0132 else { 0133 p.drawText( rect, Qt::AlignCenter | Qt::TextHideMnemonic, text() ); 0134 } 0135 } 0136 0137 0138 void K3b::FlatButton::setColors( const QColor& fore, const QColor& back ) 0139 { 0140 m_foreColor = fore; 0141 m_backColor = back; 0142 0143 setHover( m_hover ); 0144 } 0145 0146 0147 void K3b::FlatButton::slotThemeChanged() 0148 { 0149 if( K3b::Theme* theme = k3bappcore->themeManager()->currentTheme() ) { 0150 setColors( theme->foregroundColor(), theme->backgroundColor() ); 0151 } 0152 } 0153 0154 #include "moc_k3bflatbutton.cpp"